For this programming assignment, you will copy a program called ExpensePerPerson.java, furnished by the instructor.
Your task for this program is to try to compile the program, read all of the error messages, and try to find and fix the errors.
After finding the errors, you need to also add the necessary code to calculate average expense per person in a family.
The formula to use is the total expense of family divided by the number of persons in the family.
// ExpensePerPerson.java
// Trine University
// Fall 2017
// CS 1113
// Program 1
//(Type your name here.)
//(Type your section here.)
// This class takes the input of total expense of a family, and the number of persons in the family
// and then calculate average expense per person of the family.
// Allows us to read in information from the keyboard
import java.util.Scanner;
public class ExpensePerPerson
{
public static void main(String[] args)
{
// Declare all objects
Scanner scan = new Scanner(System.in); // The object that reads in input
// Declare all primitive variables
double expenses, // Total expense of the family
persons; // Total family members
// Prompt user for input
System.out.print(“Please enter the total expenses”);
System.out.println(” and the number of persons in the family.”);
// Read in the user input.
expenses = scan.nextDouble();
persons = scan.nextDouble();
//Compute all results
// [Add code here to compute the value of the variable averageExpense.]
//Print all results.
System.out.println(“The average expense per person in the family is : ” + averageExpense);
} // End of main
} // End of class ExpensePerPerson
1. If you wish to copy the program above from the webpage, you can copy it.
2. The file must be named ExpensePerPerson.java, where ExpensePerPerson is the
name of the class in the Java program. Notice that the name of your file is
the same as the name of the class in that file. Throughout the semester, this
will always be the case. Be sure to name your file exactly as it is shown here
(including capitalization). You will lose points
if you name the file incorrectly.
3. You can try to compile the Java program in the file using :
javac ExpensePerPerson.java
This semester we will always compile files with the command:
javac x.java
where you replace x.java with the name of the Java source code file you created
(In program 1, replace x by ExpensePerPerson).
4. The program shown above contains three errors. When you compiled the
program, you should have gotten error messages that tell you where the errors
are. Using the message, correct the errors, save the changed
program.(If you got more than one error message, this indicates
you made errors while typing the program; fix those errors, too.)
Examples of the errors in this program are as follows:
a. A missing semicolon. The following example contains a missing
semicolon at the end of the line:
System.out.println(“hello”)
This could produce an error as follows:
ExpensePerPerson.java:100: ‘;’ expected
b. Missing quotes. For example,
System.out.println(“hello);
This could produce an error as follows along with many other errors:
ExpensePerPerson.java:5: unclosed string literal
c. A declared but undefined variable. For example, the variable below is
used in an assignment statement but it does not have a value.
int a, b;
b = a + 1;
This could produce an error as follows:
ExpensePerPerson.java:7: variable a might not have been initialized
5. Compile the program again.
The program should now compile with no errors.
6. Add the necessary code to calculate the value of averageExpense,
the average expense per person in the family, in the designated area.
7. Recompile the program.
8. Run the Java program by issuing command:
java x
where x is the name of the class in the Java program. The program will
prompt you (that is, ask you) to enter the expense of the family, and
number of people in the family.
Type total expense of the family and hit the “enter” key. Then enter the
number of people in the familyand hit the “enter” key.
The program will print the results of your arithmetic operations on
those numbers.
(In Java)