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 [Link];
public class EmployeeNameValidation {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter employee name: ");
String name = [Link]();
try {
validateName(name);
[Link]("Name is valid: " + name);
} catch (InvalidNameException e) {
[Link]([Link]());
}
}
private static void validateName(String name) throws
InvalidNameException {
if () {
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 [Link];
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) {
[Link] = patientName;
[Link] = patientAge;
[Link] = patientOxygenLevel;
[Link] = patientHRCTReport;
}
public void displayPatientInfo() throws
CovidPositiveException {
if (patientOxygenLevel < 95 &&
[Link](patientHRCTReport) > 10) {
throw new CovidPositiveException("Patient is Covid
Positive(+) and Need to Hospitalized");
} else {
[Link]("Patient Information:");
[Link]("Name: " + patientName);
[Link]("Age: " + patientAge);
[Link]("Oxygen Level: " +
patientOxygenLevel);
[Link]("HRCT Report: " +
patientHRCTReport);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter patient name: ");
String name = [Link]();
[Link]("Enter patient age: ");
int age = [Link]();
[Link]("Enter patient oxygen level: ");
int oxygenLevel = [Link]();
[Link]("Enter patient HRCT report: ");
String hrctReport = [Link]();
Patient patient = new Patient(name, age, oxygenLevel,
hrctReport);
try {
[Link]();
} catch (CovidPositiveException e) {
[Link]([Link]());
}
}
static class CovidPositiveException extends Exception {
public CovidPositiveException(String message) {
super(message);
}
}
}