Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class assignment3 {
  4.     // you need static as it is a nested class
  5.     public static class student {
  6.  
  7.         private int id;
  8.         private double[] grades = new double[3];
  9.         private double totalGrade;
  10.  
  11.         public student() {
  12.             Scanner sc = new Scanner(System.in);
  13.             System.out.println("please enter student id and grades");
  14.             this.id = sc.nextInt();
  15.             this.grades[0] = sc.nextDouble();
  16.             this.grades[1] = sc.nextDouble();
  17.             this.grades[2] = sc.nextDouble();
  18.             this.totalGrade = this.grades[0] + this.grades[1] + this.grades[2];
  19.         }
  20.  
  21.         public double getTotalGrade() {
  22.             return this.totalGrade;
  23.         }
  24.  
  25.         public double[] getGrades() {
  26.             return this.grades;
  27.         }
  28.  
  29.         public int getId() {
  30.             return this.id;
  31.         }
  32.  
  33.         public double getAvg() {
  34.             return this.totalGrade / 3;
  35.         }
  36.  
  37.     }
  38.  
  39.     public static void main(String args[]) {
  40.  
  41.         student st1 = new student();
  42.         student st2 = new student();
  43.         student st3 = new student();
  44.  
  45.         System.out.println("Total for student 1 is " + st1.getTotalGrade());
  46.         System.out.println("Total for student 2 is " + st2.getTotalGrade());
  47.         System.out.println("Total for student 3 is " + st3.getTotalGrade());
  48.  
  49.         System.out.println("Average of grades for student 1 is " + st1.getAvg());
  50.         System.out.println("Average of grades for student 2 is " + st2.getAvg());
  51.         System.out.println("Average of grades for student 3 is " + st3.getAvg());
  52.  
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement