Write a Java class Student to meet the following specificati

Write a Java class Student to meet the following specification. - The class should be able to support a 5 digit student ID, student name, marks for 3 subjects. You should have methods to set and get each of the attributes, and calculate the average for the student. Write a tester program to test your class. You should create 2 or 3 students and write code to test the class.

Solution

StudentTestDrive.java

import java.util.Scanner;


public class StudentTestDrive {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter student1 5 digit id: \");
       int id1 = scan.nextInt();
       System.out.println(\"Enter student1 name: \");
       String name1 = scan.next();
       System.out.println(\"Enter Student1 3 marks: \");
       int marks1[] = new int[3];
       for(int i=0; i<marks1.length; i++){
           marks1[i] = scan.nextInt();
       }
       System.out.println(\"Enter student2 5 digit id: \");
       int id2 = scan.nextInt();
       System.out.println(\"Enter student2 name: \");
       String name2 = scan.next();
       System.out.println(\"Enter Student2 3 marks: \");
       int marks2[] = new int[3];
       for(int i=0; i<marks1.length; i++){
           marks2[i] = scan.nextInt();
       }
       Student s1= new Student();
       s1.setId(id1);
       s1.setName(name1);
       s1.setMarks(marks1);
      
       Student s2= new Student();
       s2.setId(id2);
       s2.setName(name2);
       s2.setMarks(marks2);
      
       System.out.println(s1.toString());
       System.out.println(s2.toString());
   }
  
}

Student.java

import java.util.Arrays;


public class Student {
   private int id;
   private String name;
   private int marks[];
   public Student(){
      
   }
   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int[] getMarks() {
       return marks;
   }
   public void setMarks(int[] marks) {
       this.marks = marks;
   }
   public double average(){
       int sum = marks[0] + marks[1] + marks[2];
       return sum/(double)3;
   }
   public String toString(){
       return \"Name: \"+name+\" ID: \"+id+\" Amrks: \"+Arrays.toString(marks)+\" Average: \"+average();
   }
}

Output:

Enter student1 5 digit id:
11111
Enter student1 name:
Suresh
Enter Student1 3 marks:
50 60 70
Enter student2 5 digit id:
22222
Enter student2 name:
Sekhar
Enter Student2 3 marks:
70 80 90
Name: Suresh ID: 11111 Amrks: [50, 60, 70] Average: 60.0
Name: Sekhar ID: 22222 Amrks: [70, 80, 90] Average: 80.0

Write a Java class Student to meet the following specification. - The class should be able to support a 5 digit student ID, student name, marks for 3 subjects.
Write a Java class Student to meet the following specification. - The class should be able to support a 5 digit student ID, student name, marks for 3 subjects.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site