Question

In: Computer Science

Could you implement a gpa calculator so that it asks for a letter grade for each...

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();
   }
}

Solutions

Expert Solution

/*
* 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


Related Solutions

To compute a student's Grade Point Average (GPA) for a term, the student's grades for each...
To compute a student's Grade Point Average (GPA) for a term, the student's grades for each course are weighted by the number of credits for the course. Suppose a student had these grades: 3.8 in a 5 credit Math course 1.9 in a 3 credit Music course 2.8 in a 5 credit Chemistry course 3.4 in a 5 credit Journalism course What is the student's GPA for that term? Round to two decimal places. Student's GPA =
Java-- For this homework you are required to implement a change calculator that will provide the...
Java-- For this homework you are required to implement a change calculator that will provide the change in the least amount of bills/coins. The application needs to start by asking the user about the purchase amount and the paid amount and then display the change as follows: Supposing the purchase amount is $4.34 and the paid amount is $20, the change should be: 1 x $10 bill 1 x $5 bill 2 x Quarter coin 1 x Dime coin 1...
Each question will be labeled as a calculator or formula question. For calculator problems , you...
Each question will be labeled as a calculator or formula question. For calculator problems , you are to label and input all the variables of interest: N, I/Y, PV, PMT, FV (and Begin mode if you switch). the unknown variable should be indicated by a ? symbol. Once solved, rewrite the variable identifier with the correct answer. For the formula problems, set up the problem and solve. Be sure to show each step for credit. Answer each question in BB...
You must implement a simple program that asks the user for three values, a, b, and...
You must implement a simple program that asks the user for three values, a, b, and c, which you should store as double values. Using these numbers, you compute the value of the two solutions to the quadratic equation, assuming that the equation is of the form: ax^2 + bx + c = 0 Complete the description of the program given above so that your output looks as close to the following sample output as possible. In this sample, the...
PROJECT DETAILS: You will create a carefully produced, professional cover letter (10% of final grade) and...
PROJECT DETAILS: You will create a carefully produced, professional cover letter (10% of final grade) and resume (15% of final grade) that must be based on one of the two job postings listed. You are a free to choose either Job Posting A or Job Posting B. Using what you learn from in-class lessons and helpful links you find online, you will develop a strong understanding of what is involved in creating as flawless a cover letter/resume package as possible....
Thank you in advance, i'm not so good with this subject so could you include the...
Thank you in advance, i'm not so good with this subject so could you include the steps you did please 1. The gross weekly sales at a certain super market are a Gaussian random with mean $2200 and standard deviation $230. Assume that the sales from week to week are independent. (a) Find the probability that the gross sales over the next two weeks exceed $5000. (b) Find the probability that the gross weekly sales exceed $2000 in at least...
Can you solve this via a HP calculator, if so, how? If not, whats the long...
Can you solve this via a HP calculator, if so, how? If not, whats the long version? In order to save for a new car, you decide to invest into Cuba Bank which pays an annual interest rate of 8% compounding interest semi-annually. You invest $2,000 now and add $500 at the end of every six month period. Including your final deposit, how much total will you have at the end of 6 years?
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings....
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings. Assume that any char array passed into the functions will contain valid, null-terminated data. The functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
51.  Your supervisor asks you if a layer 2 switch could be used in the core of...
51.  Your supervisor asks you if a layer 2 switch could be used in the core of the campus network. Prepare a response to your supervisor. Be sure to justify your recommendation. (10 points) 52.  A 1 Gbps data link is to be set up between building A and building B in a campus network. Does it matter if the link if fiber or microwave or some other media? Explain your answer.
During an interview with an IRS official on Dateline, the interviewer asks, "So how do you...
During an interview with an IRS official on Dateline, the interviewer asks, "So how do you decide which Forms 1040 get audited and which do not?" a. Complete below the hypothetical IRS response to the question posed by Dateline. "The IRS does not reveal the details of its audit selection process, so the interviewee should keep his or her comments within the agency's policies. Tax returns for audit are chiefly selected by use of the (Data identification factor/ discriminant individual...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT