Skip to content

Commit 782c03a

Browse files
Average marks of 2 students
1 parent 337e95f commit 782c03a

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.Scanner;
2+
3+
class UserDefinedException extends Exception
4+
{
5+
UserDefinedException(String s)
6+
{
7+
super(s);
8+
}
9+
}
10+
class Average_Marks
11+
{
12+
Scanner sc=new Scanner(System.in);
13+
String name;
14+
int marks[], sum;
15+
Average_Marks()throws UserDefinedException,NumberFormatException
16+
{
17+
sum=0;
18+
System.out.println("Enter name of student");
19+
name=sc.nextLine();
20+
marks=new int[3];
21+
System.out.println("Enter marks of "+name);
22+
for (int i = 0; i < 3; i++)
23+
{
24+
marks[i] = Integer.parseInt(sc.next());
25+
26+
if (marks[i] < 0)
27+
throw new UserDefinedException("Marks cannot be Negative");
28+
else if (marks[i] > 100)
29+
throw new UserDefinedException("Marks cannot be more than 100");
30+
31+
sum = sum + marks[i];
32+
}
33+
}
34+
public static void main(String[] args)
35+
{
36+
try
37+
{
38+
Average_Marks stu1=new Average_Marks();
39+
System.out.println("Average marks of "+stu1.name+" = "+stu1.sum/3.0);
40+
Average_Marks stu2=new Average_Marks();
41+
System.out.println("Average marks of "+stu2.name+" = "+stu2.sum/3.0);
42+
}
43+
catch (UserDefinedException|NumberFormatException e)
44+
{
45+
System.out.println(e);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)