Design a class called Student to holds information about a student. The class co
ID: 3638413 • Letter: D
Question
Design a class called Student to holds information about a student. The class contains following instance variables and methods:• A student’s first name and last name.
• A student’s id number. The format should be xxx-xxx where each “x” should be a digit. For example, a valid id number is 123-456. Note that there is a dash symbol in the middle.
• A student’s mid-I exam score and mid-II exam score.
• A student’s average of mid-I and mid-II exam scores.
• A student’s letter grade for the average.
• A method called readStudentName() that reads a student’s first name and last name from a user.
• A method called readStudentId() that reads a student’s id number from a user.
• A method called readStudentScores() that reads a student’s exam scores from a user.
• A method called displayStudentInfo() that displays a student’s first name, last name, id, mid-I score, mid-II score, average, and letter grade. Note that if a student average is between 90 and 100, the grade is A. If the average is greater than or equal to 80 but less than 90, the grade is B. If the average is greater than or equal to 70 but less than 80, the grade is C. If the average is less than 70, the grade is F.
• A method called displayStudentAverage() that displays a student’s mid-I score, mid-II score, and average.
• A method called updateID() that changes the id number of a student that is given as an argument. If the argument doesn’t have a correct format, the method should not update the id number.
• A method called updateScores() that changes the values of mid-I and mid-II exam scores that are given as arguments. If each argument doesn’t have a correct value between 0 and 100, the method should not update the scores.
The following code presents a class named StudentDemo that uses the class Student. If there’s any problem in the demo program, change it to fix the error.
public class StudentDemo {
public static void main(String[] args)
{
Student tom = new Student();
tom.readStudentName();
tom.readStudentId();
tom.readStudentScores();
tom.displayStudentInfo();
tom.displayStudentAverage();
tom.updateID (“111-222”);
tom.updateID (“111444”);
tom.updateScores (100.0, 90.0);
tom.updateScores (99.0, 105.0);
tom.displayStudentInfo();
}
}
This is a sample result of the execution:
Enter first name: Tom
Enter last name: Smith
Enter ID (xxx-xxx): 123456
Incorrect format. Try again: 123-abc
Incorrect format. Try again: 123-456
Enter mid-I exam score: 90.5
Enter mid-II exam score: 89.5
===== Student Information ====
Name: Tom Smith
ID: 123-456
Scores: 90.5 89.5
Average: 90.0 (A)
===== Student Average ====
Scores: 90.5 89.5
Average: 90.0 (A)
===== Student Information ====
Name: Tom Smith
ID: 111-222
Scores: 100.0 90.0
Average: 95.0 (A)
Explanation / Answer
please rate - thanks
sample run
import java.util.Scanner;
class Student
{Scanner in=new Scanner(System.in);
private String first,last,ID;
private double mid1,mid2;
Student()
{mid1=0;
mid2=0;
}
public void updateID(String I)
{if(valid(I))
ID=I;
}
public void updateScores(double m1,double m2)
{if(m1>=0&&m1<=100&&m2>=0&&m2<=100)
{ mid1=m1;
mid2=m2;
}
}
public void readStudentScores()
{double m1,m2;
System.out.print("Enter mid-I exam score: ");
m1=in.nextDouble();
System.out.print("Enter mid-II exam score: ");
m2=in.nextDouble();
updateScores(m1,m2);
}
public void readStudentName()
{System.out.print("Enter first name: ");
first=in.next();
System.out.print("Enter last name: ");
last=in.next();
}
public void readStudentId()
{System.out.print("Enter ID (xxx-xxx): ");
String I;
I=in.next();
while(!valid(I))
{System.out.print("Incorrect format. Try again: ");
I=in.next();
}
ID=I;
}
public boolean valid(String I)
{if(I.length()!=7)
return false;
for(int i=0;i<2;i++)
if(I.charAt(i)<'0'||I.charAt(i)>'9')
return false;
for(int i=4;i<6;i++)
if(I.charAt(i)<'0'||I.charAt(i)>'9')
return false;
if(I.charAt(3)!='-')
return false;
return true;
}
public void displayStudentInfo()
{System.out.println(" ===== Student Information ====");
System.out.println("Name: "+first+" "+last);
System.out.println("ID: "+ID);
printAvg(mid1,mid2);
}
public void printAvg(double m1, double m2)
{double a;
char let;
System.out.printf("Scores: %.1f %.1f ",mid1,mid2);
a=(mid1+mid2)/2.;
switch((int)a/10)
{case 10: case 9: let='A'; break;
case 8: let='B'; break;
case 7: let='C'; break;
default: let='F';break;
}
System.out.printf("Average: %.1f (%c) ",a,let);
}
public void displayStudentAverage()
{System.out.println(" ===== Student Average ====");
printAvg(mid1,mid2);
}
}
--------------------------------------------
public class StudentDemo {
public static void main(String[] args)
{
Student tom = new Student();
tom.readStudentName();
tom.readStudentId();
tom.readStudentScores();
tom.displayStudentInfo();
tom.displayStudentAverage();
tom.updateID ("111-222");
tom.updateID ("111444");
tom.updateScores (100.0, 90.0);
tom.updateScores (99.0, 105.0);
tom.displayStudentInfo();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.