This programming assignment explores several concepts discussedin class, notably
ID: 3617240 • Letter: T
Question
This programming assignment explores several concepts discussedin class, notably: the use of arithmetic operators, passingparameters by value, data hiding, and making use of keyboardinput.
For this assignment and for all others, documentation (asdiscussed in class) is required and points will be deducted forinadequate or missing documentation. Unless told otherwise,you may use meaningful variable names of your choice, so long asthey are valid variable names in Java.
Create two classes in the same file. Name one classStudent and the other class Average. The main() method will beplaced in the public class called Average, so remember that yourfile name should be Average.java. Remember, too, that theclass Student cannot also be public. The class Average will"get" four integer grades by keyboard input, store them in anobject of the Student class --- this object represents a person,instantiated, perhaps, as you --- who "has" four int grades. Among other things, Average will use the methods of the Studentclass to average the grades.
And, within the Student class, but outside any method, you willdeclare and initialize the following instance variables (remember,you may use the variable names below or other valid Java variablenames):
private int grade1 = 0;
private int grade2 = 0;
private int grade3 = 0;
private int grade4 = 0;
private int total = 0;
Inside the Student class, you will also create several methodswith the following signatures and functionality:
Method name: setgrade1()
Signature: public void setgrade1(int x)
Functionality:
assigns the value x to the object, using the instance variablethis.grade1
accesses the object using the dot operator and therunningtotal() method, sending the value x as a parameter to thatmethod
Method name: setgrade2()
Signature: public void setgrade2(int x)
Functionality: identical to that of setgrade1() except thatgrade1 becomes grade2
Method name: setgrade3()
Signature: public void setgrade3(int x)
Functionality: identical to that of setgrade1() except thatgrade1 becomes grade3
Method name: setgrade4()
Signature: public void setgrade4(int x)
Functionality: identical to that of setgrade1() except thatgrade1 becomes grade4
Method name: runningtotal()
Signature: private void runningtotal(int x)
Functionality: Accumulates the values sent as x and assigns theresult to the instance variable named total. Prints to screen amessage similar to: "The current running total is " followed on thesame line by the current accumulated total
Method name: getaverage()
Signature: public double getaverage()
Functionality: returns the double result of dividing by 4,the accumulated value of the instance variable named total
Method name showenteredvalues()
Signature: public void showenteredvalues()
Functionality: Prints to screen a message similar to: "Thevalues you entered are: "
Accesses the object using the dot operator and theshowfieldvalues() method
Method name: showfieldvalues()
Signature: private void showfieldvalues()
Functionality: On subsequent lines on the screen, prints outeach value for the instance variables grade1, grade2, grade3,grade4
The main() method will do the following, in this order:
· instantiate aninstance of the Scanner class to permit reading of keyboard inpu;don't forget to import java.util.Scanner
· use the defaultconstructor to create a single instance of the Student class
· declare andinitialize four int variables that you will use to hold four gradesentered by keyboard
· Then, theprogram will execute the following numbered steps, four times:
o Prompt the user by tellinghim/her to "Enter a grade as an integer: "
o Assign the value entered bythe user to the appropriate int variable (use the Scanner classmethod nextInt())
o Print out to screen a messagesaying something like, "The grade you just entered is " followed onthe same line by that grade
o access the instance ofStudent using the dot operator and the method setgradeX() to setthe grade in the object (where X is replaced by 1,2,3, or 4 for theappropriate method name
· access theinstance of Student using the dot operator and the methodshowenteredvalues()
· useSystem.out.print() to print to screen a message similar to: "Theaverage is: " (without a subsequent line feed)
· useSystem.out.println() to print the result of accessing the instanceof Student using the dot operator and the method getaverage()
Program output. If you were to enter the integers 95, 86,93, 88, your output should look something like:
Enter first grade as integer:
95
First grade = 95
running total: 95
Enter second grade as integer:
86
Second grade = 86
running total: 181
Enter third grade as integer:
93
Third grade = 93
running total: 274
Enter fourth grade as integer:
88
Fourth grade = 88
running total: 362
The values you entered are:
95
86
93
88
The average is: 90.5
Explanation / Answer
x.j5ce="Courier New">Same question as here: /answers-mar-10/computer-science/java-programming-marth-please-could-you-do-for-me-this-programming-step-by-step_789012.aspx import java.util.*; class Student { private int grade1, grade2, grade3, grade4, total; public Student() { grade1 = grade2 = grade3 = grade4 = total = 0; } public void setgrade1(int x) { grade1 = x; runningtotal(x); } public void setgrade2(int x) { grade2 = x; runningtotal(x); } public void setgrade3(int x) { grade3 = x; runningtotal(x); } public void setgrade4(int x) { grade4 = x; runningtotal(x); } private void runningtotal(int x) { total += x; System.out.println("running total: " + total); } public double getaverage() { return total/4.0; } public void showenteredvalues() { System.out.println("The values you entered are: "); showfieldvalues(); } private void showfieldvalues() { System.out.println(grade1); System.out.println(grade2); System.out.println(grade3); System.out.println(grade4); } } public class Average { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); Student student = new Student(); int in1 = 0, in2 = 0, in3 = 0, in4 = 0; System.out.println("Enter first grade as integer:"); in1 = keyboard.nextInt(); System.out.println("First grade = "+in1); student.setgrade1(in1); System.out.println("Enter second grade as integer:"); in2 = keyboard.nextInt(); System.out.println("Second grade = "+in2); student.setgrade2(in2); System.out.println("Enter third grade as integer:"); in3 = keyboard.nextInt(); System.out.println("Third grade = "+in3); student.setgrade3(in3); System.out.println("Enter fourth grade as integer:"); in4 = keyboard.nextInt(); System.out.println("Fourth grade = "+in4); student.setgrade4(in4); student.showenteredvalues(); System.out.print(
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.