Create a class named Student that has the following member v

Create a class named Student that has the following member variables: first name (string), last name(string), student identification number(int), an integer array where the grades of all classes are stored, grade point average (gpa) (float). Write appropriate constructor, mutator and accessor functions for the class along with the following A function that inputs all values from the user (except for gpa) A function that outputs the names and gpa of students

Solution

/* Java Programme for Grades Calculation */

import java.util.Scanner;
public class Student
{
//Private varibles declaration
private String firstName;
private String lastName;
private int sid;
private int[] grades= new int[10];
int[] tempgrades= new int[10];
int noc;

private float gpa=0;

//Constructor method
Scanner in=new Scanner(System.in);       //scanner class object for reading input
public static void main(String[] args)
{
Student st=new Student();           //creating object and calling default constructor

st.readInput();
st.calgpa();
}
//Mutator for firstName
public void setfirstName(String firstName)
{
this.firstName = firstName;
}
//Mutator for lastName
public void setlastName(String lastName)
{
this.lastName = lastName;
}
//Mutator for sid
public void setsid(int sid)
{
this.sid = sid;
}
//Mutator for grades
public void setgrades(int[] grades)
{
this.grades = grades;
}

void readInput()               //Function to read input data
{
       System.out.println(\"Enter First Name\ \");   //Prompt for name
       String temp=in.nextLine();           //reading name
       setfirstName(temp);               //calling mutator for firstName

       System.out.println(\"Enter Last Name\ \");   //Prompt for last name  
       String temp1=in.nextLine();           //read last anme
       setlastName(temp1);           //calling mutator for lastName

       System.out.println(\"Enter Student ID\ \");   //prompt for student id
       int temp2=in.nextInt();               //read student id
       setsid(temp2);                   //calling mutator for sid

       System.out.println(\"Enter Number of classes ID\ \"); //Prompt for Number of classes
       noc=in.nextInt();               //read Number of classes

       System.out.println(\"Enter Grades of classes ID\ \"); //Prompt for Grades of classes          
       for(int i=0;i<noc;i++)               //loop for reading all grades for classes
       {
       tempgrades[i]=in.nextInt();           //storing grades in tempgrades array
       }
       setgrades(tempgrades);               //calling nutator for grades
}

   void calgpa()               //Function to read input data
       {
       float sum=0;
           for(int i=0;i<noc;i++)           //loop for sum of all grades
           {
               sum=sum+grades[i];
           }
           gpa=sum/noc;                   //calculate gpa
           System.out.println(\"First Name : \"+firstName+\"\\tLast Name : \"+lastName+\"\\tStudent Id : \"+sid+ \" --> Gpa : \" + gpa+\"\ \");   //Dispaly output

       }

}

Output :

javac Student.java //Compilation of java programme

java Student //running java programme

Sample output :
Enter First Name

Ashok
Enter Last Name

Kumar
Enter Student ID

101
Enter Number of classes ID

4
Enter Grades of classes ID

10
9
8
7
First Name : Ashok Last Name : Kumar Student Id : 101 --> Gpa : 8.5

 Create a class named Student that has the following member variables: first name (string), last name(string), student identification number(int), an integer ar
 Create a class named Student that has the following member variables: first name (string), last name(string), student identification number(int), an integer ar

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site