Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util.Scanner; /** * Student.java - Define a student class that stores name, score on * test 1, and score on test 2. Methods prompt
import java.util.Scanner; /** * Student.java - Define a student class that stores name, score on * test 1, and score on test 2. Methods prompt for and read in grades, * compute the average, and return a string containing student info. * * @author Lewis/Loftus and* @version 2/22/2017 */ public class Student { //declare instance data (declare as private!!!!) /** * constructor. * * @param String */ public Student( String studentName ) { // add body of constructor // be sure to set the instance variable for the student name // and initialize the scores to 0; } /** * getAverage - Compute and return the students test average. * * @return ? */ //add header for getAverage { //add body of getAverage } /** * getName - Return the students name. * * @return ? */ //add header for getName { //add body of getName } /** * set Test Score 1. * * @param ? */ // add header for setScore1 { // add body of setScore1 } /** * set Test Score 2. * * @param ? */ // add header for setScore2 { // add body of setScore2 } /** * toString - return the object as a String. Include the student name * and test scores in the form: Name (average score) * * @return ? */ // write the toString method, but only after you have tested this // without the toString method. The method should be public and it // should have no parameters. I'll let you figure out what the return // type is. }
/** * Grades.java - Use the Student class to get test grades for two * students and compute their averages. * * @author Lewis/Loftus andA teacher wants a program to keep track of grades for students and decides to create a java program that includes a Student class set up as follows: Each student is described by three pieces of data: his/her name, his/her score on test #1, and his/her score on test #2. There is a constructor, which has a single argument,the name of the student. This method will assign the incoming argument to the instance variable for student name and will initialize the values for the 2 test scores. Other than the constructor are three methods: 1. String getName() : returns the student's name; 2. double getAverage() : computes and returns the student's average as a double. 3. void setScore1( int score ) : assigns the value of score to the first score 4. void setScore2( int score ) : assigns the value of score to the second score 1. Create a project for this lab within Eclipse. I would suggest calling the project "HW11", although you can name it whatever you want. 2. File Student.java e contains an incomplete definition for the Student class. Save it to your directory and import into Eclipse (either save to the Desktop and drag to the "src" folder or use the "Import" function). Then complete the class definition as follows: 1. Declare the instance data (name, score for test1, and score for test2). 2. Add the missing method headers and stub in the methods. 3. Add the missing method bodies. 3. File Grades.javae contains a shell program that declares two Student objects. Save it to your directory and import into your Eclipse project. 1. You will need to instantiate 2 Student objects, one for Mary and the other for Mike. 2. You will then prompt the user to enter in the two test scores for each student, using the setScore1() and setScore2() methods of the Student objects to associate these with the "Mary" and "Mike" objects. 3. You will then use the getAverage() method to return their averages for both students. You should print the average using the name of the appropriate student, e.g., "The average for Mary is 87.5." You can use the getName() method to print the student's name. 4. Add statements to your Grades program that print the values of your Student variables directly, e.g.: System.out.println("Student 1: " + mary); This should compile, but notice what it does when you run it-nothing very useful! When an object is printed, Java looks for a toString() method for that object. This method must have no parameters and must return a String . If such a method has been defined for this object, it is called and the string it returns is printed. Otherwise the default toString() method, which is inherited from the Object class, is called; it simply returns a unique hexadecimal identifier for the object such as the ones you saw above. Add a toString() method to your Student class that returns a string containing the student's name and test scores, e.g.: Mary (Test1: 85 Test2: 91) Note that the toString() method does not call System.out.println()-it just returns a string. Recompile your student class and the Grades program (you shouldn't have to change the Grades program-you don't have to call toString() explicitly). Now see what happens when you print a student object-much nicer* @version 2/22/2004 */ public class Grades { /** * Main method * * @args command line arguments */ public static void main(String[] args) { // declare students "Mary" (mary) and "Mike" (mike) //------------------------------------------------- int score; Scanner keyboard; Student mary; // do the others after this // instantiate the Student & Scanner objects & initialize variables //----------------------------------------------------------------- mary = new Student( "Mary" ); // do the others after this //input grades for Mary and set the scores in the Student object System.out.print( "Score 1 for " + mary.getName() + ": " ); score = keyboard.nextInt() mary.setScore1( score ); //input grades for Mike and set the scores in the Student object //print average for Mary System.out.println(); //print average for Mike } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started