0% found this document useful (0 votes)
2 views

Java assignment

The document contains two Java assignments focused on user-defined exceptions. The first assignment validates an employee's name, ensuring it contains only characters, while the second assignment checks a patient's oxygen level and HRCT report to determine if they need hospitalization due to potential COVID-19 symptoms. Both assignments demonstrate exception handling in Java through custom exception classes.

Uploaded by

231401088
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java assignment

The document contains two Java assignments focused on user-defined exceptions. The first assignment validates an employee's name, ensuring it contains only characters, while the second assignment checks a patient's oxygen level and HRCT report to determine if they need hospitalization due to potential COVID-19 symptoms. Both assignments demonstrate exception handling in Java through custom exception classes.

Uploaded by

231401088
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java assignment

Sabarish Sai S 231401088

1. Write a java program to accept Employee name


from the user and check whether it is valid or not.
If it is not valid then throw user defined Exception
"Name is Invalid" otherwise display it. (Name
should contain only characters)
import java.util.Scanner;

public class EmployeeNameValidation {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter employee name: ");
String name = scanner.nextLine();

try {
validateName(name);
System.out.println("Name is valid: " + name);
} catch (InvalidNameException e) {
System.out.println(e.getMessage());
}
}
private static void validateName(String name) throws
InvalidNameException {
if (!name.matches("[a-zA-Z]+")) {
throw new InvalidNameException("Name is Invalid");
}
}

static class InvalidNameException extends Exception {


public InvalidNameException(String message) {
super(message);
}
}
}

2. Define a class patient (patient_name, patient_age,


patient_oxy_level,patient_HRCT_report). Create
an object of patient. Handle appropriate exception
while patient oxygen level less than 95% and HRCT
scan report greater than 10, then throw user
defined Exception "Patient is Covid Positive(+) and
Need to Hospitalized" otherwise display its
information.
import java.util.Scanner;
public class Patient {

private String patientName;


private int patientAge;
private int patientOxygenLevel;
private String patientHRCTReport;

public Patient(String patientName, int patientAge, int


patientOxygenLevel, String patientHRCTReport) {
this.patientName = patientName;
this.patientAge = patientAge;
this.patientOxygenLevel = patientOxygenLevel;
this.patientHRCTReport = patientHRCTReport;
}

public void displayPatientInfo() throws


CovidPositiveException {
if (patientOxygenLevel < 95 &&
Integer.parseInt(patientHRCTReport) > 10) {
throw new CovidPositiveException("Patient is Covid
Positive(+) and Need to Hospitalized");
} else {
System.out.println("Patient Information:");
System.out.println("Name: " + patientName);
System.out.println("Age: " + patientAge);
System.out.println("Oxygen Level: " +
patientOxygenLevel);
System.out.println("HRCT Report: " +
patientHRCTReport);
}
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter patient name: ");


String name = scanner.nextLine();

System.out.print("Enter patient age: ");


int age = scanner.nextInt();

System.out.print("Enter patient oxygen level: ");


int oxygenLevel = scanner.nextInt();

System.out.print("Enter patient HRCT report: ");


String hrctReport = scanner.next();

Patient patient = new Patient(name, age, oxygenLevel,


hrctReport);

try {
patient.displayPatientInfo();
} catch (CovidPositiveException e) {
System.out.println(e.getMessage());
}
}

static class CovidPositiveException extends Exception {


public CovidPositiveException(String message) {
super(message);
}
}
}

You might also like