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

Question 2. (Student java, tester java) (15 marks) Create a Student dass, which

ID: 3906224 • Letter: Q

Question

Question 2. (Student java, tester java) (15 marks) Create a Student dass, which contains the following. A constant first and last name associated with the student, which is set in the constructor A constant ID (integer) of the Student, which is unique and automatically set and generated by the constructor The GPA (double) of the student, which is initially 10 The class should have appropriate functions to: - Modify the GPA of the student (do not allow values below 0, or above 10) - Get the GPA as a range of percent (refer to bttps://aurentian.calcranscripta Example, a GPA of 6.8 would return "70% to 74% Get the GPA a5 ? GPA - Get the full name associated with the student - Get the ID associated with the student Finally, (in tester java) have a main method which creates at least 5 student objects. Test all the functionality by prompting the user to enter inputs appropriate for testing your methods and constructors. Remember, to do all input and output in your main method. Pass the input to your methods and constructors, and collect the results using assignment statements. You don't need to do any input checking in the main method. I suggest you write and test your methods one at a time. You can get part marks for the methods that work

Explanation / Answer

import java.util.*;

class Student
{
private String firstName,lastName;
private double gpa;
private static int counter = 1000;//static variable
private int id;

public Student(String firstName,String lastName,double gpa)// constructor
{
this.firstName = firstName;
this.lastName = lastName;
if(gpa >=0 && gpa <=10) // validate GPA
this.gpa = gpa;
else
this.gpa = 10;
id = ++counter;// increment static variable and assign to id
}
// get methods
public String getName()
{
return firstName +" "+lastName;
}

public double getGPA()
{
return gpa;
}
public int getID()
{
return id;
}
public void setGPA(double gpa)
{
if(gpa >=0 && gpa <=10)
this.gpa = gpa;
}
}
class Tester
{
public static void main (String[] args)
{
Student[] s = new Student[5];// array of 5 student objects
String firstName,lastName;
double gpa;

Scanner input = new Scanner(System.in);

for(int i=0;i<5;i++)
{
  System.out.println("Enter the first name and last name of student : ");
  firstName = input.next();
  lastName = input.next();

System.out.println("Enter the GPA of student : ");
  gpa = input.nextDouble();
  
  s[i] = new Student(firstName,lastName,gpa);
}

//display students
for(int i=0;i<5;i++)
{
System.out.println("Student ID : "+s[i].getID());
System.out.println("Student Name : "+s[i].getName());
System.out.println("Student GPA : "+s[i].getGPA());

}
}
}

Output:

Enter the first name and last name of student :John Cooper
Enter the GPA of student :5.6
Enter the first name and last name of student :Stewart James
Enter the GPA of student :10.5
Enter the first name and last name of student :Candy Lewis
Enter the GPA of student :6.8
Enter the first name and last name of student :Amy Jones
Enter the GPA of student :7.1
Enter the first name and last name of student :Maggie Johnson
Enter the GPA of student :4.9


Student ID : 1001
Student Name : John Cooper
Student GPA : 5.6
Student ID : 1002
Student Name : Stewart James
Student GPA : 10.0
Student ID : 1003
Student Name : Candy Lewis
Student GPA : 6.8
Student ID : 1004
Student Name : Amy Jones
Student GPA : 7.1
Student ID : 1005
Student Name : Maggie Johnson
Student GPA : 4.9

Do ask if any doubt. Please upvote.

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