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");
- boolean correct_marks_entered = true;
- // NEW:
- do{
- try{
- sub1 = Integer.parseInt(br.readLine());
- sub2= Integer.parseInt(br.readLine());
- sub3= Integer.parseInt(br.readLine());
- if(sub1<0 || sub1>100)
- {
- correct_marks_entered = false;
- throw new T(1, sub1);
- }
- else if(sub2<0 || sub2>100)
- {
- correct_marks_entered = false;
- throw new T(2, sub2);
- }
- else if(sub3<0 || sub3>100)
- {
- correct_marks_entered = false;
- throw new T(3, sub3);
- }
- correct_marks_entered = true;
- }
- catch(T e)
- {
- System.out.println(e);
- }
- }while(!correct_marks_entered);
- }
- 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