In: Computer Science
Could you implement a gpa calculator so that it asks for a letter grade for each of four classes, calculates the gpa, and prints the GPA out along with the student id, major, and name.
/*****************************Student.java*************************/
public class Student {
// private data members
private String id;
private String name;
private String major;
private double gpa;
// default constructor which set the data member to
defualt value
public Student() {
this.id = "";
this.name = "";
this.major = "";
this.gpa = 0;
}
// parameterized constructor
public Student(String id, String name, String major,
double gpa) {
this.id = id;
this.name = name;
this.major = major;
this.gpa = gpa;
}
// getter and setter
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
// print student data
public void printStudent() {
System.out.println("Student ID:
" + id);
System.out.println("Student name: "
+ name);
System.out.println("Student major:
" + major);
System.out.println("Student GPA: "
+ gpa);
}
}
/**************************StudentApplication.java*******************/
public class StudentApplication {
public static void main(String[] args) {
//create object by default
constructor
Student s1 = new Student();
//create object by overloaded
constructor
Student s2 = new Student("S12345",
"Virat Kohli", "Computer Science", 4.5);
//set the information of object
1
s1.setName("John Doe");
s1.setId("S123456");
s1.setMajor("Electronics");
s1.setGpa(4.0);
s1.printStudent();
System.out.println();
s2.printStudent();
}
}
/*
* The java test program that create an instance of Student class
and calls the method readClassGrades that prompts
* user to enter 4 credit hours and grade value and then print the
student details with the gpa value
* */
//StudentApplication.java
public class StudentApplication
{
public static void main(String[] args)
{
//create object by overloaded
constructor
Student s1 = new Student("S12345",
"Virat Kohli", "Computer Science");
//Call readClassGrades that
prompts user to enter the grade and credit hours
//from user
s1.readClassGrades();
s1.printStudent();
}
}
----------------------------------------------------------------------------------------------------------------
//Student.java
import java.util.Scanner;
public class Student
{
// private data members
private String id;
private String name;
private String major;
private double gpa;
// default constructor which set the data member to
defualt value
public Student()
{
this.id = "";
this.name = "";
this.major = "";
this.gpa = 0;
}
// parameterized constructor
public Student(String id, String name, String
major)
{
this.id = id;
this.name = name;
this.major = major;
this.gpa = 0;
}
/*Read scores */
public void readClassGrades()
{
int course1CreditHours;
char course1Grade;
int course2CreditHours;
char course2Grade;
int course3CreditHours;
char course3Grade;
int course4CreditHours;
char course4Grade;
//create a Scanner class
object
Scanner console=new
Scanner(System.in);
System.out.printf("Enter credit
hours course1:");
course1CreditHours=Integer.parseInt(console.nextLine());
System.out.printf("Enter course1
Grade :");
course1Grade=console.nextLine().charAt(0);
System.out.printf("Enter credit
hours course2:");
course2CreditHours=Integer.parseInt(console.nextLine());
System.out.printf("Enter course2
Grade :");
course2Grade=console.nextLine().charAt(0);
System.out.printf("Enter credit
hours course3:");
course3CreditHours=Integer.parseInt(console.nextLine());
System.out.printf("Enter course3
Grade :");
course3Grade=console.nextLine().charAt(0);
System.out.printf("Enter credit
hours course4:");
course4CreditHours=Integer.parseInt(console.nextLine());
System.out.printf("Enter course4
Grade :");
course4Grade=console.nextLine().charAt(0);
//calculate the total grade
points
int
totalGradePoints=course1CreditHours*getGradeValue(course1Grade)+
course2CreditHours*getGradeValue(course2Grade)+
course3CreditHours*getGradeValue(course3Grade)+
course4CreditHours*getGradeValue(course4Grade);
//calculate total credit
hours
int
totalCreditHours=course1CreditHours+course2CreditHours+
course3CreditHours+course4CreditHours;
//set gpa value by dividing the
total grade points by total credit hours
gpa=(float)totalGradePoints/totalCreditHours;
}
public int getGradeValue(char grade)
{
if(grade=='A')
return 4;
else if(grade=='B')
return 3;
else if(grade=='C')
return 2;
else if(grade=='D')
return
1;
return 0;
}
// getter and setter
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getMajor()
{
return major;
}
public void setMajor(String major)
{
this.major = major;
}
public double getGpa()
{
return gpa;
}
// print student data
public void printStudent()
{
System.out.println("Student ID: " +
id);
System.out.println("Student name: "
+ name);
System.out.println("Student major:
" + major);
System.out.println("Student GPA: "
+ gpa);
}
}
-----------------------------------------------------------------------------------------------------------------------------
Sample Output:
Enter credit hours course1:5
Enter course1 Grade :A
Enter credit hours course2:1
Enter course2 Grade :B
Enter credit hours course3:5
Enter course3 Grade :C
Enter credit hours course4:5
Enter course4 Grade :F
Student ID: S12345
Student name: Virat Kohli
Student major: Computer Science
Student GPA: 2.0625