Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

. Tracking Grades A teacher wants a program to keep track of grades for students

ID: 3725597 • Letter: #

Question

. Tracking Grades A teacher wants a program to keep track of grades for students and decides to create a student class for his program as follows . Each student will be described by three pieces of data: his/her name, his/her score on test # 1, and hisher score on test#2. There will be one constructor, which will have one argument-the name ofthe student There will be three methods: getName, which will return the student's name inputGrades, which will prompt for and read in the student's test grades; and getAverage, which will compute and return the student's average · File Student.java contains an incomplete definition for the Student class. Save it to your directory and complete the class definition as follows a. Declare the instance data (name, score for testl, and score for test2) b. Create a Scanner object for reading in the scores. c. Add the missing method headers d. Add the missing method bodies. File Grades java contains a shell program that declares two Student objects. Save it to your directory and use the inputGrades method to read in each student s test scores, then use the getAverage method to find their average. Print the average with the student's name, eg. The average for Joe is 87. You can use the get Name method to print the student's name. 3. Add statements to your Grades program that print the values of your Student objects directly, e.g: System.out printin("Student 1: + studentl) This should compile, but notice what it does when you run it nothing very useful! When an object is printed, Java looks for a roString method for that object. This method must have no

Explanation / Answer

1.

import java.util.*;

class Student
{
private String studentName;
private int score1;
private int score2;

public static Scanner input = new Scanner(System.in);

public Student(String studentName)
{
this.studentName = studentName;

}

public void inputGrades()
{

System.out.println("Enter "+studentName +"'s score for test1 : ");
score1 = input.nextInt();
System.out.println("Enter "+studentName +"'s score for test2 : ");
score2 = input.nextInt();

}
public double getAverage()
{
return (score1+score2)/2.0;
}
public String getName()
{
  return studentName;
}
public String toString()
{
  return "Name "+ studentName +" Test1 : "+score1 +" Test2 : "+score2;
}
}
class Grades
{
public static void main (String[] args)
{
Student student1 = new Student("Marry");
Student student2 = new Student("Mike");

student1.inputGrades();

System.out.println("The average for "+student1.getName()+" is "+student1.getAverage());

System.out.println();

student2.inputGrades();

System.out.println("The average for "+student2.getName()+" is "+student2.getAverage());

System.out.println(" Student 1 "+student1);
System.out.println(" Student 2 "+student2);

}
}

Output:

Enter Marry's score for test1 :80
Enter Marry's score for test2 :100
The average for Marry is 90.0

Enter Mike's score for test1 :90
Enter Mike's score for test2 :85
The average for Mike is 87.5

Student 1 Name Marry Test1 : 80 Test2 : 100

Student 2 Name Mike Test1 : 90 Test2 : 85

2.

class Bulb
{
private String status ;

public Bulb()//default constructor
{
  status = "off";
}
public Bulb(String status) // argument constructor
{
  this.status = status;
}
public boolean isOn() // check status
{
  if (status == "on")
  return true;
  else
  return false;
}
public void setStatus(String status)//set status
{
  this.status = status;
}

}
class TestBulbs
{
public static void main (String[] args)
{
Bulb b1 = new Bulb();

System.out.println("IS b1 on? "+b1.isOn());

Bulb b2 = new Bulb();

System.out.println("IS b2 on? "+b2.isOn());

Bulb b3 = new Bulb();

System.out.println("IS b3 on? "+b3.isOn());

b1.setStatus("on");
b2.setStatus("on");

System.out.println("Bulb on? "+b1.isOn());
   System.out.println("Bulb on? "+b2.isOn());
   System.out.println("Bulb on? "+b3.isOn());


}
}

Output:

IS b1 on? false
IS b2 on? false
IS b3 on? false
Bulb on? true
Bulb on? true
Bulb on? false