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

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

View File

@ -0,0 +1,39 @@
import java.util.*;
class ArrayOverflowException extends Exception
{
int a;
ArrayOverflowException(int b)
{
a=b;
}
public String toString()
{
return "ArrayOverflowException:"+a+" is out of bound";
}
}
public class Array_Exception {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int arr[]=new int[10];
for(int k=0;k<arr.length;k++)
arr[k]=k;
System.out.println("Enter the index:");
int i;
try{
i=in.nextInt();
if(i<arr.length)
{
System.out.println("Value="+arr[i]);
}
else
{
throw new ArrayOverflowException(i);
}
}
catch(ArrayOverflowException e)
{
System.out.println(e.toString());
}
}
}

View File

@ -0,0 +1,23 @@
import java.util.Scanner;
class EvenNumberException extends Exception{
EvenNumberException(String message){
super(message);
}
}
public class EvenNumberHandling {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
try{
int input = sc.nextInt();
if(input%2==0){
throw new EvenNumberException("You had to enter an odd number");
}
System.out.println("Odd number was entered");
}catch(EvenNumberException e){
System.out.println(e.getMessage());
}
}
}

View File

@ -0,0 +1,26 @@
import java.util.*;
class karlihandle extends Exception
{
float input;
public String toString()
{
return "Error:Haan,karliya Handle";
}
}
class Handled{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter 1 to cause an error for handling");
try
{
if(in.nextInt()==1)
{
throw new karlihandle();
}
}
catch(karlihandle e)
{
System.out.println(e.toString());
}
}
}

View File

@ -0,0 +1,51 @@
import java.util.Scanner;
class Students {
String name;
int marks[];
Students(String name, int marks[]) {
this.name = name;
this.marks = marks;
}
}
class arkOutOfBoundsHandling extends Exception {
arkOutOfBoundsHandling(String message) {
super(message);
}
}
public class MarkOutOfBoundsHandling {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter student name: ");
String studentName = sc.nextLine();
System.out.print("Enter the number of subjects: ");
int numSubjects = sc.nextInt();
int[] studentMarks = new int[numSubjects];
System.out.println("Enter marks for each subject:");
try {
for (int i = 0; i < numSubjects; i++) {
System.out.print("Subject " + (i + 1) + ": ");
studentMarks[i] = sc.nextInt();
if (studentMarks[i] < 0 || studentMarks[i] > 100) {
throw new arkOutOfBoundsHandling("Marks should be between 0 and 100");
}
}
Students student = new Students(studentName, studentMarks);
System.out.println("Students details: " + student.name);
System.out.print("Marks: ");
for (int mark : student.marks) {
System.out.print(mark + " ");
}
} catch (arkOutOfBoundsHandling e) {
System.out.println("Error: " + e.getMessage());
}
}
}

View File

@ -0,0 +1,21 @@
import java.util.Scanner;
public class negativearr {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter the number of elements");
n = sc.nextInt();
try{
int array[] = new int[n];
System.out.println("Enter the elments");
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
}
}catch(NegativeArraySizeException e){
System.out.println("Error:"+e.toString());
}
}
}