Advertisement
thespeedracer38

Corrected Student

Mar 30th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 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.         // NEW:
  41.         int temp_sub1 = -1;
  42.         do{
  43.             try{
  44.                 temp_sub1 = Integer.parseInt(br.readLine());
  45.                 if(temp_sub1<0 || temp_sub1>100)
  46.                     throw new T(1, temp_sub1);
  47.                 sub1 = temp_sub1;
  48.             }
  49.             catch(T e)
  50.             {
  51.                 System.out.println(e);
  52.             }
  53.         }while(temp_sub1<0 || temp_sub1>100);
  54.         /*
  55.         try{
  56.             int sub2= Integer.parseInt(br.readLine());
  57.             if(sub2<0 || sub2>100)
  58.             throw new T(sub2);
  59.         }
  60.         catch(T e)
  61.         {
  62.             System.out.println("Exception caught with second subject marks");
  63.         }  
  64.         try{
  65.             int sub3= Integer.parseInt(br.readLine());
  66.             if(sub3<0 || sub3>100)
  67.             throw new T(sub3);
  68.         }
  69.         catch(T e)
  70.         {
  71.             System.out.println("Exception caught with third subject marks");
  72.         }
  73.         */
  74.     }
  75.     void display()
  76.     {
  77.         char c= calc();
  78.         System.out.println("Name: "+name+"\nAverage: "+avg+"\nGrade :"+c); 
  79.     }
  80.     public static void main(String args[])throws Exception 
  81.     {
  82.         // OLD:
  83.         //Student obj = new Student("Ann", 55, 66, 77);
  84.         Student obj = new Student();
  85.         obj.input();
  86.         obj.display();
  87.     }
  88. }
  89.  
  90. class T extends Exception{
  91.     int subject_no;
  92.     int marks;
  93.     T(int subject_no, int marks){
  94.         this.subject_no = subject_no;
  95.         this.marks = marks;
  96.     }
  97.     public String toString(){
  98.         return "Exception caught with " + subject_no + " subject marks\n" +
  99.                 marks + " is an invalid marks. Range is 0-100";
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement