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

Define a Student class with instance variables name, id, midterm1, midterm2 and

ID: 3675857 • Letter: D

Question

Define a Student class with instance variables name, id, midterm1, midterm2 and final.

Name is a string whereas others are all inte

gers`. Also, add a static variable nextId, which is

an integer and statically initialized to 1. Have some overloaded constructors. In each of

them, the id should be assigned to the next available id given by nextId. The default

constructor should set the n

ame of the student object to “StudentX” where X is the next id.

Add a

calculateGrade()

method

which returns a string for the letter grade of the

student, like “A”, “B”, “C”, “D” or “F”, based on the overall score. Overall score should be calculated as (30%

of midterm1 + 30% of midterm2 + 40% of final). The letter grade should be calculated the same way as in the homework 1 exercises.

Your test class, to be named as

TestStudent

s

, should create 25 student objects with

default constructor and invoke the setter

methods for midterm1, midterm2 and final with

random numbers ranging from 50 to 100 inclusive. After that, it should print the student

information

. Student information

should include name, midterm1, midterm2 , final and the letter grade given by the calculateGrade()

method.

Explanation / Answer


/**The java class Student that represents a student object.
* The class has constructor to set default vales.
* The class also have methods to set mid1, mid2 and final score.
* The class has method calculateGrade that returns the grade
* of the student*/
//Student.java
public class Student
{
  
   //private variables of class
   private String name;
   private int id;
   private double midterm1;
   private double midterm2;
   private double finalScore;
  
  
   //static variable nextid
   private static int nextid=1;
  
  
   //Set default vlaues
   public Student()
   {              
       name="Student"+nextid;
       id=nextid;
       nextid++;  
       midterm1=0;
       midterm2=0;
       finalScore=0;
   }
  
  
   //Returns name
   public String getName()
   {
       return name;
   }
  
   //Set mid1 score
   public void setMid1(double midterm1)
   {
       this.midterm1=midterm1;
   }
  
   //Set mid2 score
   public void setMid2(double midterm2)
   {
       this.midterm2=midterm2;
   }
  
   //Set final score
   public void setFinalScore(double finalScore)
   {
       this.finalScore=finalScore;
   }
  
   //Returns mid1 score
   public double getMid1()
   {
       return midterm1;
   }
  
   //Returns mid2 score
   public double getMid2()
   {
       return midterm2;
   }
  
   //Returns final score
   public double getFinalScore()
   {
       return finalScore;
   }
  
   //Returns grade letter of student
   public char calculateGrade()
   {
       char grade=' ';
       //calculate total score
       double tScore=getMid1()*0.3+getMid2()*0.3
               +getFinalScore()*0.4;
      
       if(tScore>90)
           grade='A';
       else if(tScore>=80 && tScore<90)
           grade='B';
       else if(tScore>=70 && tScore<80)
           grade='C';
       else if(tScore>=60 && tScore<70)
           grade='D';
       else
           grade='F';
      
       //return grade letter
       return grade;
   }
  
}

---------------------------------   ---------------------------------   --------------------------------- ---------------------------------

/**The java program TestStudent that creates 25 student
* objects and generate random value for mid1 , mid2 and
* final score and print the grade letters to the console.*/
//TestStudent.java
import java.util.Random;
public class TestStudent
{
   public static void main(String[] args)
   {
      
       //Set SIZE=25
       final int SIZE=25;
       //Create an array of 25 students
       Student[] students=new Student[SIZE];
      
       //Call createStudents
       createStudents(students);
       //Call printStudentsgrades
       printStudentsgrades(students);
      
      
   }

   /**The method printStudentsgrades that takes students
   * array as input variable and prnt the name, mid1, mid2 , final score
   * and grade letter of 25 students*/
   private static void printStudentsgrades(Student[] students) {
       System.out.printf("%-20s%-10s%-10s%-10s%-5s ",
               "Name","Mid1","Mid2","FinalScore","Grade");
              
       for (int i = 0; i < students.length; i++)
       {
           System.out.printf("%-20s%-10.2f%-10.2f%-10.2f%-5c ",
                   students[i].getName(),
                   students[i].getMid1(),
                   students[i].getMid2(),
                   students[i].getFinalScore(),
                   students[i].calculateGrade());
       }
   }

   /**The method createStudents that takes students array as input argument
   * and create an object of student and set mid1, mid2 and final score
   * */
   private static void createStudents(Student[] students)
   {
       Random rand=new Random();
      
       for (int i = 0; i < students.length; i++)
       {
           students[i]=new Student();
           students[i].setMid1(rand.nextInt((100 - 50) + 1) + 50);
           students[i].setMid2(rand.nextInt((100 - 50) + 1) + 50);
           students[i].setFinalScore(rand.nextInt((100 - 50) + 1) + 50);
       }
      
   }
}

---------------------------------   ---------------------------------   --------------------------------- ---------------------------------

Sample output:

Name                Mid1      Mid2      FinalScoreGrade
Student1            54.00     87.00     56.00     D  
Student2            84.00     99.00     57.00     C  
Student3            65.00     85.00     60.00     D  
Student4            98.00     97.00     64.00     B  
Student5            76.00     53.00     62.00     D  
Student6            53.00     68.00     75.00     D  
Student7            56.00     94.00     60.00     D  
Student8            60.00     70.00     89.00     C  
Student9            91.00     64.00     75.00     C  
Student10           100.00    73.00     81.00     B  
Student11           70.00     84.00     89.00     B  
Student12           55.00     54.00     69.00     D  
Student13           100.00    79.00     92.00     A  
Student14           95.00     62.00     66.00     C  
Student15           79.00     72.00     85.00     C  
Student16           73.00     91.00     59.00     C  
Student17           76.00     59.00     51.00     D  
Student18           56.00     79.00     92.00     C  
Student19           92.00     51.00     70.00     C  
Student20           82.00     81.00     74.00     C  
Student21           95.00     62.00     83.00     B  
Student22           57.00     66.00     90.00     C  
Student23           64.00     55.00     54.00     F  
Student24           79.00     79.00     83.00     B  
Student25           92.00     77.00     62.00     C  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote