Upload files to "OOP/Java/Lab/Week10"

This commit is contained in:
Aadit Agrawal 2024-10-14 02:40:51 +05:30
parent 22f306573d
commit 0b8cfc509f
5 changed files with 162 additions and 0 deletions

View File

@ -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());
}
}
}
}

View File

@ -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());
}
}
}

View File

@ -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());
}
}
}

View File

@ -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);
}
}

View File

@ -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");
}
}
}