Question

This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named...

This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named Budgeter.java. To use a Scanner for console input, you must import java.util.*; in your code. This program prompts a person for income and expense amounts, then calculates their net monthly income. Below are two example logs of execution from the program. This program’s behavior is dependent on the user input (user input is bold and underlined below to make it stand out and differentiate it from the program’s output). Your output should match our examples exactly when given the same input. (Be mindful of spacing, such as after input prompts and between output sections.) Look at the other example logs on the course web site to get more examples of the program's behavior. The program begins with an introductory message that briefly explains the program. The program then prompts the user for the number of income categories, then 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.) The program will then read in a number of expense categories and an amount for each category, similar to how income was read. The program should then print out the total amount of income and expenses for the month, as well as the average amount of each per day. You may assume a month has exactly 31 days, though your program should be able to be easily modified to change this assumption (see below). After printing the total income and expenses, the program should print out whether the user spent or earned more money for the given month and by how much. If income 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. The user’s net income is the result of subtracting their expenses from their income. The four categories are defined as follows: More than +$250: "big saver" More than -$250 but not more than $0: "spender" More than $0 but not more than +$250: "saver" -$250 or less: "big spender" After printing the user’s category, print a custom message of your choice about their spending habits. This message should be different for each range shown above and should be at least 1 line of any non-offensive text you like. This program processes user input using a Scanner. All monetary inputs will be real numbers; all other input will be integers. You may assume the user always enters valid input. When prompted for a value, the user will enter a value of the correct type. The user will enter a number of income and expense categories ≥ 1, and will only ever enter 1 or 2 when asked how to enter expenses. The monetary amount for each category of income and expense will be a non-negative real number (including real numbers such as 3.14 or 89.705).

This program asks for your monthly income and

expenses, then tells you your net monthly income.

How many categories of income? 3

Next income amount? $1000

Next income amount? $250.25

Next income amount? $175.50

Enter 1) monthly or 2) daily expenses? 1

How many categories of expense? 4

Next expense amount? $850

Next expense amount? $49.95

Next expense amount? $75

Next 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.

another example:

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? $200.25

Enter 1) monthly or 2) daily expenses? 2

How many categories of expense? 1

Next expense amount? $45.33

Total income = $1000.25 ($32.27/day)

Total expenses = $1405.23 ($45.33/day)

You spent $404.98 more than you earned this month. You're a big spender.

You should define a class constant for the number of days in a month. You should refer to this constant throughout your program appropriately so that the value can be changed and your program will continue to function correctly. Development Strategy and Hints: • Tackle parts of the program one at a time, rather than writing the entire program at once. Write a bit of code, get it to compile, and test what you have so far. If you try to write large amounts of code without attempting to compile it, you may encounter a seemingly overwhelming list of compiler errors and/or bugs. • To compute total income and expenses, use a cumulative algorithm as described in lecture and in textbook section 4.2. • Many students get "cannot find symbol" compiler errors. Common mistakes include forgetting to pass / return a needed value, forgetting to store a returned value into a variable, neglecting to pass a necessary parameter, and referring to a variable by the wrong name. • All monetary output values are printed with 2 digits after the decimal point. If the second digit after the decimal point (the “hundredths” digit) would be a zero, it is acceptable either for your program can include this in the output or not. You should achieve this using either a custom rounding method (as shown in lecture) or with System.out.printf. If you choose to use the System.out.printf method, you can base your code off of the following code which prints the variable x rounded to the nearest hundredth: double x = 1.2345; System.out.printf("x is around %.2f in size.\n", x); // 1.23 • Even though your program is rounding numbers to two decimal places when they are displayed, it should not round the numbers that are used to compute results or in determining in which range the user belongs. • If you are getting averages of 0 regardless of what data the user types, you may have a problem with integer division. See Chapter 2 about types int and double, type-casting, and how to avoid integer division problems. If you have a value of type double but need to convert it into an int, use a type-cast such as the following: double d = 5.678; int i = (int) d; // 5 Style Guidelines: For this assignment, you are limited to Java features from Chapters 1-4 of the textbook. A major part of this assignment is demonstrating that you understand how to use parameters and return values. Use static methods, parameters, and returns to structure your program well and to eliminate redundancy. You should declare only a single Scanner in your program and pass it as a parameter to any methods that require it. (You should NOT declare your Scanner as a constant.) For full credit, your program must have and use at least 4 non-trivial methods other than main. (For reference, our solution is 86 lines long (58 “substantive”), with 7 methods other than main, though you do not need to match these numbers.) Similar to previous assignments, you should not have println statements in your main method. Also, main should be a concise summary of the overall program and should make calls to several other methods that implement the majority of the program's behavior. Your methods will need to make appropriate use of parameters and return values. Each method should perform a coherent task and should not do too large a share of the overall work. Avoid lengthy “chaining” of method calls, where each method calls the next, no values are returned, and control does not come back to main. See textbook Chapter 4's case study for a discussion of well-designed versus poorly-designed methods. When handling numeric data, you are expected to choose appropriately between types int and double. You will lose points if you use type double for variables in cases where type int would be more appropriate. Some of your code will use conditional execution with if and if/else statements. You are expected to use these statements properly. Review book sections 4.1-4.3 about nested if/else statements and factoring them. Give meaningful names to methods, variables, and parameters. Use proper indentation and whitespace. Follow Java's naming standards as specified in Chapter 1. Limit line lengths to 100 chars. Localize variables when possible; (i.e. declare them in the smallest scope needed). Include meaningful header comments at the top of your program and at the start of each method.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code


import java.util.Scanner;


public class Budgeter
{
public static void main(String[] args)
{
double totalIncome,totalExpense;
Scanner sc=new Scanner(System.in);
startingMessage();
totalIncome=getTotalIncome(sc);
totalExpense=getTotalExpense(sc);
printInfo(totalIncome,totalExpense);
}

public static void startingMessage() {
System.out.println("This program asks for your monthly income and\n" +
"expenses, then tells you your net monthly income.");
}

private static double getTotalIncome(Scanner sc)
{
int categories;
double sum=0;
System.out.print("How many categories of income? ");
categories=sc.nextInt();
for(int i=0;i<categories;i++)
{
System.out.print("Next income amount? $");
sum+=sc.nextDouble();
}
return sum;
}

public static double getTotalExpense(Scanner sc)
{
int choice;
int categories;
double sum=0;
System.out.print("Enter 1) monthly or 2) daily expenses? ");
choice=sc.nextInt();
if(choice==1)
{
System.out.print("How many categories of expense? ");
categories=sc.nextInt();
for(int i=0;i<categories;i++)
{
System.out.print("Next expense amount? $");
sum+=sc.nextDouble();
}
}
if(choice==2)
{
System.out.print("How many categories of expense? ");
categories=sc.nextInt();
for(int i=0;i<categories;i++)
{
System.out.print("Next expense amount? $");
sum+=sc.nextDouble();
}
sum*=31;
}
return sum;
}

private static void printInfo(double totalIncome, double totalExpense)
{
double diff=totalIncome-totalExpense;
System.out.printf("Total income = $%.2f ($%.2f/day)%n",totalIncome,(totalIncome/31));
System.out.printf("Total expenses = $%.2f ($%.2f/day)%n",totalExpense,(totalExpense/31));
if(diff>0)
{
System.out.printf("You earned $%.2f more than you spent this month.%n",diff);
if(diff>250)
System.out.println("You're a big saver.");
else
System.out.println("You're a saver.");
}
else
{
System.out.printf("You spent $%.2f more than you earned this month.%n",(diff*-1));
if(diff>=-250 && diff<0)
System.out.println("You're a spender");
else
System.out.println("You're a big spender");
}
}


}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named...

    This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named Budgeter.java. To use a Scanner for console input, you must import java.util.*; in your code. This program prompts a person for income and expense amounts, then calculates their net monthly income. Below are two example logs of execution from the program. This program’s behavior is dependent on the user input (user input is bold and underlined below to make it stand out and differentiate...

  • This same solution except that you need to use the Dialog boxes option instead of Scanner...

    This same solution except that you need to use the Dialog boxes option instead of Scanner class option Planting Grapevines A vineyard owner is planting several new rows of grapevines, and needs to know how many grapevines to plant in each row. She has determined that after measuring the length of a future row, she can use the following formula to calculate the number of vines that will fit in the row, along with the trellis end-post assemblies that will...

  • JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole...

    JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole I would like it to print the first 3 payments and the last 3 payments if n is larger than 6 payments. Please help! Thank you!! //start of program import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.lang.Math; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * Write a description of class MortgageCalculator here. * * @author () * @version () */ public class LoanAmortization {    /* *...

  • This assignment will continue our hardware store system. You will turn in a java file named...

    This assignment will continue our hardware store system. You will turn in a java file named "MyMethods.java". It will contain one class named "MyMethods". That class will contain three methods. These methods must be exactly as specified (including method names): getAnInt will return a value of type int, and take as an argument a string to prompt the user. The actual prompt presented to the user should include not only the string argument, but also the information that the user...

  • You will turn in a java file named "MyMethods.java". It will contain one class named "MyMethods". That class will contain three methods. These methods must be in JOptionPane and must c...

    You will turn in a java file named "MyMethods.java". It will contain one class named "MyMethods". That class will contain three methods. These methods must be in JOptionPane and must contain: getAnInt will return a value of type int, and take as an argument a string to prompt the user. The actual prompt presented to the user should include not only the string argument, but also the information that the user may press Cancel or enter an empty string to...

  • *MYST BE I NJAVA* *PLEASE INCORPORATE ALL OF STRING METHODS AND SCANNER METHOD LISTED IN THE...

    *MYST BE I NJAVA* *PLEASE INCORPORATE ALL OF STRING METHODS AND SCANNER METHOD LISTED IN THE DIRECTIONS BELOW* Write a Java class that takes a full name (first and last) as inputted by the user, and outputs the initials. Call the class Initials. The first and last names should be entered on the same input line i.e. there should be only one input to your program. For example, if the name is Jane Doe, the initials outputted will be J...

  • The main method of your calculator program has started to get a little messy. In this...

    The main method of your calculator program has started to get a little messy. In this assignment, you will clean it up some by moving some of your code into new methods. Methods allow you to organize your code, avoid repetition, and make aspects of your code easier to modify. While the calculator program is very simple, this assignment attempts to show you how larger, real world programs are structured. As a new programmer in a job, you will likely...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • Java This assignment will give you practice with line based file processing and scanner methods. Modify...

    Java This assignment will give you practice with line based file processing and scanner methods. Modify the Hours program we did in class. You are going to write a program that allows the user to search for a person by ID. Your program is required to exactly reproduce the format and behavior of the log of execution as follows: Enter an ID: 456 Brad worked 36.8 hours (7.36 hours/day) Do you want to search again? y Enter an ID: 293...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT