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

tasks (look for the TODO tasks in the Student class) (1) Finish the 3 constructo

ID: 3883221 • Letter: T

Question

tasks (look for the TODO tasks in the Student class)
(1) Finish the 3 constructors.
(2) Finish the accessor and mutator methods for Student's instance variables.
(3) Write the code to calculate and return the gpa for the student.

Ex run

Welcome to the Student GPA Tester program! Enter the name of the student:

Rachel

Enter the major of the student:

Electrical

Enter the marks for subject1:

90

Enter the marks for subject2:

88

Enter the marks for subject3:

95

Student name: Rachel, Majoring in: Electrical has grades: [90.0,88.0,95.0] with a GPA of 91.0

Student name: Alex, Majoring in: Computer Science has grades: [90.0,85.0,89.0] with a GPA of 88.0

Student name: Yash, Majoring in: Business has grades: [80.0,85.0,90.0] with a GPA of 85.0

CODE:

import java.util.Scanner;

class Student
{
//instance variables
private String name;
private String major;
double marks1;
double marks2;
double marks3;
private double gpa;
  
/**
* Constructors for objects of class Student
*/   
public Student(){
//TODO
}

// Constructor with two arguments
public Student(String name, String major)
{
//TODO
}

// Constructor with 5 arguments
public Student(String name, String major, double marks1, double marks2, double marks3)
{
//TODO
}

//Accessor method ("setter") for the private name variable
public void setName(String name){
this.name = name;
}
  
//Accessor method ("setter") for the private major variable
public void setMajor(String major){
//TODO
}
  
//Accessor method ("setter") for the private marks1 variable
public void setMarks1(double marks1){
//TODO
}

//Accessor method ("setter") for the private marks2 variable
public void setMarks2(double marks2){
//TODO
}

//Accessor method ("setter") for the private marks3 variable
public void setMarks3(double marks3){
//TODO
}

//Accessor method ("getter") for the private name variable
public String getName(){
return name;
}

//Accessor method ("getter") for the private major variable
public String getMajor(){
//TODO
}

//Accessor method ("getter") for the private marks1 variable
public double getMarks1(){
//TODO
}
  
//Accessor method ("getter") for the private marks2 variable
public double getMarks2(){
//TODO
}

//Accessor method ("getter") for the private marks3 variable
public double getMarks3(){
//TODO
}

// Compute GPA. Add the marks of 3 subjects and divide it by 3.0
public double computeGPA(){
//TODO
}
}

public class StudentGPA_Tester{

public static void main(String[] args) {
  
System.out.println("Welcome to the Student GPA Tester program!");
  
// Prompt user to input student's name, major and marks for 3 subjects
Scanner scan= new Scanner(System.in);
System.out.println("Enter the name of the student:");
String name= scan.nextLine();
System.out.println("Enter the major of the student:");
String major= scan.nextLine();
System.out.println("Enter the marks for subject1:");
double marks1= scan.nextInt();
System.out.println("Enter the marks for subject2:");
double marks2= scan.nextInt();
System.out.println("Enter the marks for subject3:");
double marks3= scan.nextInt();
  
// Create a student object by calling the constructor with 5 arguments
Student s1= new Student(name, major, marks1, marks2, marks3);
printStudent(s1); //helper method to print the student info in a nice minor

  
// Create a student object by calling the default constructor with 0 arguments
Student s2= new Student();
// Using the "setter" method, set name as Alex, major as Computer Science, and marks for 3 subjects as 80, 85 and 89
s2.setName("Alex");
s2.setMajor("Computer Science");
s2.setMarks1(90.0);
s2.setMarks2(85.0);
s2.setMarks3(89.0);
printStudent(s2); //helper method to print the student info in a nice minor

  
// Create a student object by calling the constructor with 2 arguments
Student s3= new Student("Yash", "Business");
// Using the "setter" method, set marks for 3 subjects as 80, 85 and 90
s3.setMarks1(80.0);
s3.setMarks2(85.0);
s3.setMarks3(90.0);
printStudent(s3); //helper method to print the student info in a nice minor


  
}
//Helper method that takes any student object and prints the information in a nice minor.
public static void printStudent(Student s){
System.out.println("Student name: " + s.getName()+", Majoring in: " + s.getMajor()
+ " has grades: ["+s.getMarks1()+","+s.getMarks2()+","+s.getMarks3()+"] with a GPA of "+ s.computeGPA() );
}
}

Explanation / Answer

Hi,

Below is the Java Code-

import java.util.Scanner;
class Student
{
//instance variables
private String name;
private String major;
double marks1;
double marks2;
double marks3;
private double gpa;
  
/**
* Constructors for objects of class Student
*/   
public Student(){
  
}

// Constructor with two arguments
public Student(String name, String major)
{
this.name=name;
this.major=major;
}

// Constructor with 5 arguments
public Student(String name, String major, double marks1, double marks2, double marks3)
{
this.name=name;
this.major=major;
this.marks1=marks1;
this.marks2=marks2;
this.marks3=marks3;
}
//Accessor method ("setter") for the private name variable
public void setName(String name){
this.name = name;
}
  
//Accessor method ("setter") for the private major variable
public void setMajor(String major){
this.major=major;
}
  
//Accessor method ("setter") for the private marks1 variable
public void setMarks1(double marks1){
this.marks1=marks1;
}

//Accessor method ("setter") for the private marks2 variable
public void setMarks2(double marks2){
this.marks2=marks2;
}

//Accessor method ("setter") for the private marks3 variable
public void setMarks3(double marks3){
this.marks3=marks3;
}

//Accessor method ("getter") for the private name variable
public String getName(){
return name;
}

//Accessor method ("getter") for the private major variable
public String getMajor(){
return major;
}

//Accessor method ("getter") for the private marks1 variable
public double getMarks1(){
return marks1;
}
  
//Accessor method ("getter") for the private marks2 variable
public double getMarks2(){
return marks2;
}

//Accessor method ("getter") for the private marks3 variable
public double getMarks3(){
return marks3;
}

// Compute GPA. Add the marks of 3 subjects and divide it by 3.0
public double computeGPA(){
return (marks1+marks2+marks3)/3;
}
}
public class StudentGPA_Tester{

public static void main(String[] args) {
  
System.out.println("Welcome to the Student GPA Tester program!");
  
// Prompt user to input student's name, major and marks for 3 subjects
Scanner scan= new Scanner(System.in);
System.out.println("Enter the name of the student:");
String name= scan.nextLine();
System.out.println("Enter the major of the student:");
String major= scan.nextLine();
System.out.println("Enter the marks for subject1:");
double marks1= scan.nextInt();
System.out.println("Enter the marks for subject2:");
double marks2= scan.nextInt();
System.out.println("Enter the marks for subject3:");
double marks3= scan.nextInt();
  
// Create a student object by calling the constructor with 5 arguments
Student s1= new Student(name, major, marks1, marks2, marks3);
printStudent(s1); //helper method to print the student info in a nice minor

  
// Create a student object by calling the default constructor with 0 arguments
Student s2= new Student();
// Using the "setter" method, set name as Alex, major as Computer Science, and marks for 3 subjects as 80, 85 and 89
s2.setName("Alex");
s2.setMajor("Computer Science");
s2.setMarks1(90.0);
s2.setMarks2(85.0);
s2.setMarks3(89.0);
printStudent(s2); //helper method to print the student info in a nice minor

  
// Create a student object by calling the constructor with 2 arguments
Student s3= new Student("Yash", "Business");
// Using the "setter" method, set marks for 3 subjects as 80, 85 and 90
s3.setMarks1(80.0);
s3.setMarks2(85.0);
s3.setMarks3(90.0);
printStudent(s3); //helper method to print the student info in a nice minor


  
}
//Helper method that takes any student object and prints the information in a nice minor.
public static void printStudent(Student s){
System.out.println("Student name: " + s.getName()+", Majoring in: " + s.getMajor()
+ " has grades: ["+s.getMarks1()+","+s.getMarks2()+","+s.getMarks3()+"] with a GPA of "+ s.computeGPA() );
}
}

Thanks and Regards,

Vinay Prakash Singh