Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class Student
- {
- int sub1, sub2, sub3;
- double avg;
- String name;
- // NEW:
- Student()
- {
- this("", 0, 0, 0);
- }
- Student(String name, int a, int b, int c)
- {
- this.name = name;
- sub1=a;
- sub2=b;
- sub3=c;
- avg=0.0;
- }
- char calc()
- {
- avg=(sub1+sub2+sub3)/3;
- if(avg>=90)
- return 'O';
- else if(avg>=80 && avg<90)
- return 'A';
- else if(avg>=70 && avg<80)
- return 'B';
- else if(avg>=50 && avg<70)
- return 'C';
- else
- return 'F';
- }
- void input()throws IOException
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Enter name");
- name=br.readLine();
- System.out.println("Enter marks in three subjects");
- // NEW:
- int temp_sub1 = -1;
- do{
- try{
- temp_sub1 = Integer.parseInt(br.readLine());
- if(temp_sub1<0 || temp_sub1>100)
- throw new T(1, temp_sub1);
- sub1 = temp_sub1;
- }
- catch(T e)
- {
- System.out.println(e);
- }
- }while(temp_sub1<0 || temp_sub1>100);
- /*
- try{
- int sub2= Integer.parseInt(br.readLine());
- if(sub2<0 || sub2>100)
- throw new T(sub2);
- }
- catch(T e)
- {
- System.out.println("Exception caught with second subject marks");
- }
- try{
- int sub3= Integer.parseInt(br.readLine());
- if(sub3<0 || sub3>100)
- throw new T(sub3);
- }
- catch(T e)
- {
- System.out.println("Exception caught with third subject marks");
- }
- */
- }
- void display()
- {
- char c= calc();
- System.out.println("Name: "+name+"\nAverage: "+avg+"\nGrade :"+c);
- }
- public static void main(String args[])throws Exception
- {
- // OLD:
- //Student obj = new Student("Ann", 55, 66, 77);
- Student obj = new Student();
- obj.input();
- obj.display();
- }
- }
- class T extends Exception{
- int subject_no;
- int marks;
- T(int subject_no, int marks){
- this.subject_no = subject_no;
- this.marks = marks;
- }
- public String toString(){
- return "Exception caught with " + subject_no + " subject marks\n" +
- marks + " is an invalid marks. Range is 0-100";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement