From 0b8cfc509fe565ddbca0d92242745b4c2dcbe809 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Mon, 14 Oct 2024 02:40:51 +0530 Subject: [PATCH] Upload files to "OOP/Java/Lab/Week10" --- OOP/Java/Lab/Week10/Sum.java | 46 ++++++++++++++++++ OOP/Java/Lab/Week10/Valid_matrix.java | 29 ++++++++++++ .../Lab/Week10/numberformatexception.java | 17 +++++++ OOP/Java/Lab/Week10/root.java | 23 +++++++++ OOP/Java/Lab/Week10/student.java | 47 +++++++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 OOP/Java/Lab/Week10/Sum.java create mode 100644 OOP/Java/Lab/Week10/Valid_matrix.java create mode 100644 OOP/Java/Lab/Week10/numberformatexception.java create mode 100644 OOP/Java/Lab/Week10/root.java create mode 100644 OOP/Java/Lab/Week10/student.java diff --git a/OOP/Java/Lab/Week10/Sum.java b/OOP/Java/Lab/Week10/Sum.java new file mode 100644 index 0000000..4943b49 --- /dev/null +++ b/OOP/Java/Lab/Week10/Sum.java @@ -0,0 +1,46 @@ +import java.util.*; +class floatnum extends Exception +{ + float input; + floatnum(float num) + { + input=num; + } + public String toString() + { + return "Error:"+input+" is a Floating point number"; + } +} +class Input_Exception +{ + public static void main(String[] args) { + int n,sum=0; + float input=0; + Scanner in=new Scanner(System.in); + System.out.println("Enter the numbers one at a time,enter -1 to calculate the sum"); + while(true) + { + try + { + input=in.nextFloat(); + if(input == -1) + { + System.out.println("Sum="+sum); + break; + } + else if(input%1!=0) + { + throw new floatnum(input); + } + else + { + sum=sum+(int)(input); + } + } + catch(floatnum e) + { + System.out.println(e.toString()); + } + } + } +} \ No newline at end of file diff --git a/OOP/Java/Lab/Week10/Valid_matrix.java b/OOP/Java/Lab/Week10/Valid_matrix.java new file mode 100644 index 0000000..9766198 --- /dev/null +++ b/OOP/Java/Lab/Week10/Valid_matrix.java @@ -0,0 +1,29 @@ +import java.util.*; +class InvalidMatrixException extends Exception +{ + public String toString() + { + return "InvalidMatrixException:this is an Invalid Matrix"; + } +} +public class Valid_matrix { + public static void main(String[] args) { + int row,col; + Scanner in=new Scanner(System.in); + System.out.println("Enter the number of rows and columns"); + row=in.nextInt(); + col=in.nextInt(); + try + { + if(row!=col) + { + throw new InvalidMatrixException(); + } + System.out.println("This is a valid matrix"); + } + catch(InvalidMatrixException e) + { + System.out.println(e.toString()); + } + } +} diff --git a/OOP/Java/Lab/Week10/numberformatexception.java b/OOP/Java/Lab/Week10/numberformatexception.java new file mode 100644 index 0000000..602b9e3 --- /dev/null +++ b/OOP/Java/Lab/Week10/numberformatexception.java @@ -0,0 +1,17 @@ +import java.util.*; +public class numberformatexception { + public static void main(String[] args) { + int a; + Scanner in=new Scanner(System.in); + System.out.print("Input:"); + try{ + a=Integer.parseInt(in.nextLine()); + System.out.println("Number entered is "+a); + } + catch(NumberFormatException e)//As NumberFormatException is typecasting error + { + System.out.println("Error:"+e.toString()); + } + } + +} diff --git a/OOP/Java/Lab/Week10/root.java b/OOP/Java/Lab/Week10/root.java new file mode 100644 index 0000000..6e624c1 --- /dev/null +++ b/OOP/Java/Lab/Week10/root.java @@ -0,0 +1,23 @@ +import java.util.*; +public class root { + public static void main(String[] args) { + Scanner in=new Scanner(System.in); + System.out.println("Enter the number"); + double num=in.nextDouble(); + try + { + if(num<0) + { + throw new Exception(); + } + System.out.print("Sqaure root="); + } + catch(Exception e) + { + System.out.print("Negative number was entered\nsqaure root=i"); + num=-num; + } + double root=Math.sqrt(num); + System.out.print(root); + } +} \ No newline at end of file diff --git a/OOP/Java/Lab/Week10/student.java b/OOP/Java/Lab/Week10/student.java new file mode 100644 index 0000000..6e7085b --- /dev/null +++ b/OOP/Java/Lab/Week10/student.java @@ -0,0 +1,47 @@ +import java.util.*; +class studrecord{ + String Name; + int rollno; + int marks[]=new int[3]; + int percent=0; + studrecord(String name,int roll, int marks[]) + { + Name=name; + rollno=roll; + this.marks=marks; + percent=(marks[0]+marks[1]+marks[2])/3; + } + char getgrade() + { + if(percent>90) + return 'A'; + else if(percent>80) + return 'B'; + else if(percent>70) + return 'C'; + return 'F'; + } +} +public class student { + public static void main(String[] args) { + Scanner in=new Scanner(System.in); + try{ + System.out.println("Enter the name,roll number and the marks in 3 subjects simutaneously"); + String name=in.nextLine(); + int roll=Integer.parseInt(in.nextLine()); + int marks[]=new int[3]; + for(int i=0;i<3;i++) + { + int a=Integer.parseInt(in.nextLine()); + marks[i]=a; + } + studrecord stud=new studrecord(name, roll, marks); + System.out.println("Succesfully Entered"); + System.out.println("Percentage="+stud.percent+"%\nGrade:"+stud.getgrade()); + } + catch(NumberFormatException e) + { + System.out.println("Error:NumberFormatException Occured"); + } + } +}