please help me modify my code
Please help me change the code according to the job requirements (pdf) and feedback (black bold font)
(DR) According to the Code Quality Guide, the program should have a class comment that describes what your program is doing at the beginning.
S
ee the Header and Class Comment section for what to include in these comments!
Reply
8 + import java.util.*;
9 +
10 + public class Budgeter {
11 + public static final int DAYS_IN_MONTH = 31;
12 +
13 + public static void main(String[] args) {
14 + Scanner console = new Scanner(System.in);
15 + intro();
(SD) Right now, main is not a concise summary of your program. Some of the contents in totalPart() should be in main instead. For example, getting the returns of the income total and expense totals is something that would make more sense for main to have, since its an important part of the program.
Reply
16 + totalPart(console);
17 +
18 + }
19 +
20 + // prints out the introduction of the program
21 + public static void intro (){
22 + System.out.println(“This program asks for your monthly income and”);
23 + System.out.println(“expenses, then tells you your net monthly income.”);
24 + System.out.println();
25 + }
26 +
(DR) Remember to comment on the return of any non-void method! This lets the reader understand what information they will receive from the method. See the Method Comments section of the Code Quality Guide for a couple examples of how to do this!
Reply
27 + // Reads in and computes the person’s income.
28 + //
29 + // Scanner console – the Scanner to use for input
30 + public static double totalIncome(Scanner console){
(SD) Notice how the code to compute the income/expense totals is almost exactly the same in your totalIncome() and totalExpense() methods! You could eliminate this redundancy by putting this code in a method with a String parameter for the “income” vs. “expense” cases, and calling it twice. See the Election program from lecture for an example of how to do this (make sure you are looking at the ElectionGood.java tab)!
Reply
31 + System.out.print(“How many categories of income? “);
32 + int numCate = console.nextInt();
33 + double totalInc = 0.00;
34 + for (int i = 1; i <= numCate; i++) {
35 + System.out.print(” Next income amount? $”);
36 + double incAmount = console.nextDouble();
37 + totalInc += incAmount;
(DR) We use indentation to show the reader the structure of our code. The indentation of a program should increase by one tab every time a curly brace “{“ is opened and decrease by one tab every time a curly brace “}” is closed. The Indenter Tool can help with this!
Reply
38 + }
39 + System.out.println();
40 + return totalInc;
41 + }
42 +
43 + // Reads in and computes the person’s expense.
44 + //
45 + // Scanner console – the Scanner to use for input
46 + public static double totalExpense (Scanner console){
47 + System.out.print(“Enter 1) monthly or 2) daily expenses? “);
48 + int numType = console.nextInt();
49 + double sum = 0.00;
50 + double totalExp = 0.00;
51 + System.out.print(“How many categories of expense? “);
52 + int numCate = console.nextInt();
53 + for (int i = 1; i <= numCate; i++) {
54 + System.out.print(” Next expense amount? $”);
55 + double expAmount = console.nextDouble();
56 + sum += expAmount;
57 + }
58 + if(numType == 1){
59 + totalExp = sum;
60 + }else if(numType == 2){
61 + totalExp = sum * DAYS_IN_MONTH;
62 + }
63 + System.out.println();
64 + return totalExp;
65 + }
66 +
67 + // Reads in and computes the person’s total income and expenses
68 + //
69 + // Scanner console – the Scanner to use for input
70 + public static void totalPart(Scanner console){
71 + double incTotal = totalIncome(console);
72 + double expTotal = totalExpense(console);
73 + double dailyInc = incTotal / DAYS_IN_MONTH;
74 + double dailyExp = expTotal / DAYS_IN_MONTH;
75 + System.out.println(“Total income = $” + round2(incTotal) +
76 + ” ($” + round2(dailyInc) + “/day)”);
77 + System.out.println(“Total expenses = $” + round2(expTotal) +
78 + ” ($” + round2(dailyExp) + “/day)”);
79 + System.out.println();
80 + double difference = incTotal – expTotal;
81 + if(difference > 0.00){
S
Shananda Dokka
TA
3 days ago
(UOLF) Notice how lines 83 and 88 are both the same. This means that they should not be in this nested conditional and should be factored before it instead.
Reply
82 + if(difference <= 250.00){
83 + System.out.println(“You earned $” + round2(difference) +
84 + ” more than you spent this month.”);
85 + System.out.println(“You’re a saver.”);
86 + System.out.println(“Well planned!”);
87 + }else{
88 + System.out.println(“You earned $” + round2(difference) +
89 + ” more than you spent this month.”);
90 + System.out.println(“You’re a big saver.”);
91 + System.out.println(“Awesome! You will be rich!”);
92 + }
93 + }else{
94 + if(difference <= –250.00){
(UOLF) Notice how lines 95 and 100 are both the same. This means that they should not be in this nested conditional and should be factored before it instead.
Reply
95 + System.out.println(“You spent $” + Math.abs(round2(difference)) +
96 + ” more than you earned this month.”);
97 + System.out.println(“You’re a big spender.”);
98 + System.out.println(“You need to plan your expenses well.”);
99 + }else{
100 + System.out.println(“You spent $” + Math.abs(round2(difference)) +
101 + ” more than you earned this month.”);
102 + System.out.println(“You’re a spender.”);
103 + System.out.println(“You should cut back a little bit.”);
104 + }
105 + }
106 + }
107 +
108 + // Returns the given number rounded to two decimal places.
109 + //
110 + // double num – the number to round
111 + public static double round2(double num) {
112 + return Math.round(num * 100.0) / 100.0;
113 + }
114 + }
CSE 142: Computer Programming I
Spring 2021
Take-home Assessment 4: Budgeter
due April 27, 2021, 11:59pm
This assignment will assess your mastery of the following objectives:
• Write a functionally correct Java program to produce specified console output.
• Write conditional (if) statements to conditionally execute code.
• Write and call methods that accept parameters and return values to manage information flow.
• Use Scanner to accept and process user input.
• Follow prescribed conventions for spacing, indentation, naming methods, and header comments.
Example Output
This program asks for your monthly income and
expenses, then tells you your net monthly income.
How many
Next
Next
Next
categories of income? 3
income amount? $1000
income amount? $250.25
income amount? $175.50
Enter 1)
How many
Next
Next
Next
Next
monthly or 2) daily expenses? 1
categories of expense? 4
expense amount? $850
expense amount? $49.95
expense amount? $75
expense amount? $120.67
Total income = $1425.75 ($45.99/day)
Total expenses = $1095.62 ($35.34/day)
You earned $330.13 more than you spent this month.
You re a big saver.
>
This program asks for your monthly income and
expenses, then tells you your net monthly income.
How many categories of income? 2
Next income amount? $800
Next income amount? $201.30
Enter 1) monthly or 2) daily expenses? 2
How many categories of expense? 1
Next expense amount? $45.33
Total income = $1001.3 ($32.3/day)
Total expenses = $1405.23 ($45.33/day)
Program Behavior
This program prompts a person for income
and expense amounts, then calculates their
net monthly income. Unlike previous assessments, this program’s behavior is dependent on input from a user (user input
is underlined in the examples to the left
and on the next page). Your output should
match our examples exactly when given the
same input, but if the input changes, the
output will also. Additional execution logs
will be posted on the course website, and
you can use the Mark button in Ed to check
your output for various inputs.
The program begins with an introductory
message that briefly explains the program,
then prompts the user for the number
of income categories and reads in that
many income amounts. Next, the program
asks whether the user would like to enter
monthly or daily expenses. (The user enters
1 for monthly and 2 for daily.) When entering monthly expenses, the amounts input
are the total amount for the month. When
entering daily expenses, the amounts input
are for a single day, and should be multiplied by the number of days in a month
to get the monthly total (see below). The
program will then read in a number of expense categories and an amount for each
category, similar to how income was read.
After reading all the user input, the program should then print out the total
amount of income and expenses for the
month, as well as the average income and
expense per day. You may assume a month has exactly 31 days, though you should use a class constant
so that your program can be easily modified to change this assumption (see below). The program should
print out whether the user spent or earned more money for the given month and by how much. If income
You spent $403.93 more than you earned this month.
You re a big spender.
>
Page 1 of 5
Make sure that
the format and
structure of
your output
exactly match
the given logs.
and expenses were exactly equal, the user is considered to have spent $0 more than they earned (as
opposed to earning $0 more than they spent).
Finally, the program should print out which category the user falls into based on their net income for the
month, where the net income is the result of subtracting the user’s expenses from their income. The
category should be followed by a custom message of your choice about the user’s spending habits. This
message must be different for each of the four ranges shown below, and should consist of one line of any
non-offensive text you choose. The categories are defined below. You may find this graphic helpful in
understanding the boundaries of the various categories.
Net income range
More than +$250
More than $0 but not more than than +$250
More than -$250 but not more than $0
-$250 or less
Each category
must have a
separate, unique
message.
Category
big saver
saver
spender
big spender
All monetary values in output should be rounded to two decimal places using a rounding method (as
shown in lecture). If the second digit after the decimal point (the “hundreths” digit) is a zero, your
program should not print out this zero. (e.g. your program will print $3.5 instead of $3.50) This is
acceptable and expected, and you should NOT attempt to fix this. However, you MUST print out two
digits if the second digit is not a zero.
Development Strategy
In particular,
you MAY NOT
use the printf
method on this
assessment.
As usual, we recommend you approach the program in stages, rather than trying to complete everything
at once. Specifically, we suggest implementing functionality in the following order:
(1) Income: Prompt for income categories and amounts and compute and print the total monthly
income.
(2) Monthly expenses: Prompt for monthly expense categories and amounts (as if the user had
entered 1 when asked for monthly or daily expenses) and compute and print the total monthly
expenses.
(3) Daily expenses: Prompt the user to choose monthly or daily expenses, then compute and print
total expenses based on the choice.
(4) Averages: Compute and print daily average income and expenses.
(5) Net income: Compute and print net income and spender/saver category.
Hints
This assessment is significantly more complex than previous ones. The following suggestions and hints
may help you be more successful:
• Approach the program one part at a time, rather than trying to write most or all of the program at
once. Add additional output to test incomplete portions of your code, and compile and test often.
• You will need to use a cumulative sum to compute the total income and expenses, as described in
lecture and section 4.2 of the textbook.
• Be careful of using integer division when you should be using real number division, and vice versa.
Values of type int can be used when a double is expected, but to use a double where you need
an int you will need to use a cast, such as:
double d = 5.678;
int i = (int)d; // i = 5
Page 2 of 5
Remember to
remove any
debugging
output before
submitting.
• If you are receiving “Cannot find symbol” errors from the compiler, look carefully at your parameters and return values and ensure they are all included and named correctly.
Implementation Guidelines
User Input
Remember that,
in most cases,
you will need to
store the value
returned from
a method in a
variable.
This program requires you to process user input, which you must do using a Scanner. You may assume
that all monetary inputs will be real numbers, and that all other input will be integers. You may also
assume the user always enters valid input. Specifically, you may assume that:
• the user will always enter a value of the correct type
• the user will always enter a number of income and expense categories
1
• the user will only ever enter 1 or 2 when asked whether to enter monthly or daily expenses
• the user will only enter a non-negative amount for each category of income and expense
Class Constant
As described above, your program should assume there are 31 days in a month by default, but this
value should be able to be easily changed. You must introduce a class constant for the number of days
in a month, and use this constant throughout your program. See the course website for example logs
with alternate values for the constant. The Mark button in Ed will test your program with different
constant values. To ensure our testing and grading scripts work correctly, you must name your constant
DAYS_IN_MONTH. In addition, please set the value of the constant to 31 before submitting your work.
Make sure you
declare your
constant exactly
right, using
the keywords
public static
final.
Working with Numbers/Rounding
For all numerical values in your program, you should use the type (int or double) that is more appropriate
for the value. In particular, you should NOT simply use double for all values. In addition, you must not
round values until they are output; all calculations should be performed using the full values.
Permitted Java Features
For this assessment, you are restricted to Java concepts covered in chapters 1 through 4 of the textbook.
In particular, you MUST use parameters and return values. You are NOT permitted to use the printf
method or arrays on this assessment.
Code Quality Guidelines
In addition to producing the desired behavior, your code should be well-written and meet all expectations
described in the grading guidelines and the Code Quality Guide. For this assessment, pay particular
attention to the following elements:
Capturing Structure
You should attempt to eliminate all redundancy in your program, and your main method should be a
concise summary of your program’s structure. You should not produce any output in your main method,
though you may have more code in main than you have on previous assessments. In addition, each
method should perform a single, coherent task. To receive the highest grades, your program must include
at least four (4) non-trivial methods other than main. (For reference, our solution consists of 86 lines of
code, 58 of which are “substantive”, and 7 methods besides main.)
Using Parameters and Returns
Your program should utilize parameters and return values effectively to produce a well-structured program
as described above. Your methods should not accept any unnecessary parameters. For this assessment,
Page 3 of 5
Your program
must include at
least four nontrivial methods,
but you are
NOT expected
to match our
line or method
count.
a parameter is considered unncessary if its value is unused, is always the same as the value as another
parameter, or can be directly computed from the values of other parameters. In particular, your program
should include only a single Scanner which is passed as a parameter to all required methods. You should
NOT declare your Scanner as a constant; you must pass it as a parameter. Your program should also
utilize return values to avoid “chaining” (where each method calls the next without returning to main).
Using Conditionals
Your program will need to utilize conditionals (if statements) at various points to achieve the correct
behavior. You are expected to use the most appropriate form of these statements (if, if-else, and/or
if-else if) for each situation and to factor common code out of each branch.
Code Aesthetics
Your code should be properly indented, make good use of blank lines and other whitespace, and include no lines longer than 100 characters. Your class, methods, variables, and constant should all have
meaningful and descriptive names and follow the standard Java naming conventions. (e.g. ClassName,
methodOrVariableName, CONSTANT_NAME) See the Code Quality Guide for more information.
Commenting
Your code should include a header comment at the start of your program, following the same format
described in previous assessments. Your code should also include a comment at the beginning of each
method that describes that methods behavior. Method comments should also explicitly name and describe all parameters to that method and describe the method’s return value (if it has one). Comments
should be written in your own words (i.e. not copied and pasted from this spec) and should not include
implementation details (such as describing loops or expressions included in the code). See the Code
Quality Guide for examples and more information.
Running and Submitting
You can run your Budgeter program by clicking the “Run” button in Ed. This will compile and execute
your code and show you any errors, or the output of your program if it runs correctly. If you believe your
output is correct, you can submit your work by clicking the “Mark” button in the Ed lesson. You will see
the results of some automated tests along with tentative grades. These grades are not final until you
have received feedback from your TA.
You may submit your work as often as you like until the deadline; we will always grade your most recent
submission. Note the due date and time carefully—work submitted after the due time will not be
accepted.
Getting Help
If you find you are struggling with this assessment, make use of all the course resources that are available
to you, such as:
• Reviewing relevant examples from lessons, section, and lab
• Reading the textbook
• Visiting Support hours
• Posting a question on the message board
Collaboration Policy
Remember that, while you are encouraged to use all resources at your disposal, including your classmates,
all work you submit must be entirely your own. In particular, you should NEVER look at a solution
to this assessment from another source (a classmate, a former student, an online repository, etc.). Please
Page 4 of 5
The case study
in chapter 4 of
the textbook
and the election
program from
lecture both
demonstrate
how to avoid
chaining.
review the full policy in the syllabus for more details and ask the course staff if you are unclear on whether
or not a resource is OK to use.
Reflection
In addition to your code, you must submit answers to short reflection questions. These questions will help
you think about what you learned, what you struggled with, and how you can improve next time. The
questions are given in the file Reflection slide in the Ed lesson; type your responses directly into those
textboxes.
Page 5 of 5
Top-quality papers guaranteed
100% original papers
We sell only unique pieces of writing completed according to your demands.
Confidential service
We use security encryption to keep your personal data protected.
Money-back guarantee
We can give your money back if something goes wrong with your order.
Enjoy the free features we offer to everyone
-
Title page
Get a free title page formatted according to the specifics of your particular style.
-
Custom formatting
Request us to use APA, MLA, Harvard, Chicago, or any other style for your essay.
-
Bibliography page
Don’t pay extra for a list of references that perfectly fits your academic needs.
-
24/7 support assistance
Ask us a question anytime you need to—we don’t charge extra for supporting you!
Calculate how much your essay costs
What we are popular for
- English 101
- History
- Business Studies
- Management
- Literature
- Composition
- Psychology
- Philosophy
- Marketing
- Economics