Advertisement
thespeedracer38

Student Improved

Mar 30th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. import java.io.*;
  2. class Student
  3. {
  4.     int sub1, sub2, sub3;
  5.     double avg;
  6.     String name;
  7.     // NEW:
  8.     Student()
  9.     {
  10.         this("", 0, 0, 0);
  11.     }
  12.     Student(String name, int a, int b, int c)
  13.     {
  14.         this.name = name;
  15.         sub1=a;
  16.         sub2=b;
  17.         sub3=c;
  18.         avg=0.0;
  19.     }
  20.     char calc()
  21.     {
  22.         avg=(sub1+sub2+sub3)/3;
  23.         if(avg>=90)
  24.         return 'O';
  25.         else if(avg>=80 && avg<90)
  26.         return 'A';
  27.         else if(avg>=70 && avg<80)
  28.         return 'B';
  29.         else if(avg>=50 && avg<70)
  30.         return 'C';
  31.         else
  32.         return 'F';
  33.     }
  34.     void input()throws IOException
  35.     {
  36.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  37.         System.out.println("Enter name");
  38.         name=br.readLine();
  39.         System.out.println("Enter marks in three subjects");
  40.         boolean correct_marks_entered = true;
  41.         // NEW:
  42.         do{
  43.             try{
  44.                 sub1 = Integer.parseInt(br.readLine());
  45.                 sub2= Integer.parseInt(br.readLine());
  46.                 sub3= Integer.parseInt(br.readLine());
  47.                 if(sub1<0 || sub1>100)
  48.                 {
  49.                     correct_marks_entered = false;
  50.                     throw new T(1, sub1);
  51.                 }    
  52.                 else if(sub2<0 || sub2>100)
  53.                 {
  54.                     correct_marks_entered = false;
  55.                     throw new T(2, sub2);
  56.                 }
  57.                 else if(sub3<0 || sub3>100)
  58.                 {
  59.                     correct_marks_entered = false;
  60.                     throw new T(3, sub3);
  61.                 }
  62.                 correct_marks_entered = true;
  63.             }
  64.             catch(T e)
  65.             {
  66.                 System.out.println(e);
  67.             }
  68.         }while(!correct_marks_entered);
  69.     }
  70.     void display()
  71.     {
  72.         char c= calc();
  73.         System.out.println("Name: "+name+"\nAverage: "+avg+"\nGrade :"+c); 
  74.     }
  75.     public static void main(String args[])throws Exception 
  76.     {
  77.         // OLD:
  78.         //Student obj = new Student("Ann", 55, 66, 77);
  79.         Student obj = new Student();
  80.         obj.input();
  81.         obj.display();
  82.     }
  83. }
  84.  
  85. class T extends Exception{
  86.     int subject_no;
  87.     int marks;
  88.     T(int subject_no, int marks){
  89.         this.subject_no = subject_no;
  90.         this.marks = marks;
  91.     }
  92.     public String toString(){
  93.         return "Exception caught with " + subject_no + " subject marks\n" +
  94.                 marks + " is an invalid marks. Range is 0-100";
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement