Untitled
unknown
plain_text
2 years ago
5.0 kB
4
Indexable
public class Main {
public static void main(String[] args) {
String text = "abCdefgAbc";
// a) Length of the string
int length = text.length();
System.out.println("Length of the string: " + length);
// b) Number of 'a's in the string
int countOfA = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == 'a' || text.charAt(i) == 'A') {
countOfA++;
}
}
System.out.println("Number of 'a's in the string: " + countOfA);
// c) Convert all lowercase to uppercase and vice versa
String uppercaseText = text.toUpperCase();
String lowercaseText = text.toLowerCase();
System.out.println("Uppercase text: " + uppercaseText);
System.out.println("Lowercase text: " + lowercaseText);
// d) Find if the substring pattern "abc" is present; if so, display the first position of occurrence
int indexOfAbc = text.indexOf("abc");
if (indexOfAbc != -1) {
System.out.println("Substring 'abc' found at position: " + indexOfAbc);
} else {
System.out.println("Substring 'abc' not found.");
}
}
}
import java.util.Date;
class Inpatient {
private String name;
private int age;
private static int hospitalNumberCounter = 1000;
private int hospitalNumber;
private Date dateOfAdmission;
private double roomRent;
// Default constructor
public Inpatient() {
this("", 0, 0, null, 0.0);
}
// Copy constructor
public Inpatient(Inpatient other) {
this(other.name, other.age, other.hospitalNumber, other.dateOfAdmission, other.roomRent);
}
// Parameterized constructor
public Inpatient(String name, int age, Date dateOfAdmission, double roomRent) {
this(name, age, ++hospitalNumberCounter, dateOfAdmission, roomRent);
}
// Full constructor
public Inpatient(String name, int age, int hospitalNumber, Date dateOfAdmission, double roomRent) {
this.name = name;
this.age = age;
this.hospitalNumber = hospitalNumber;
this.dateOfAdmission = dateOfAdmission;
this.roomRent = roomRent;
}
// Input method
public void inputRecord(String name, int age, Date dateOfAdmission, double roomRent) {
this.name = name;
this.age = age;
this.hospitalNumber = ++hospitalNumberCounter;
this.dateOfAdmission = dateOfAdmission;
this.roomRent = roomRent;
}
// Display method
public void displayRecord() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Hospital Number: " + hospitalNumber);
System.out.println("Date of Admission: " + dateOfAdmission);
System.out.println("Room Rent: " + roomRent);
}
// Check if date of admission is today's date
public boolean isAdmittedToday(Date today) {
return dateOfAdmission.equals(today);
}
// Method to check if two patients have the same information except for hospital number
public boolean hasSameInfoExceptHospitalNumber(Inpatient other) {
return name.equals(other.name) && age == other.age && dateOfAdmission.equals(other.dateOfAdmission) && roomRent == other.roomRent;
}
// Getter for hospitalNumber (for comparison purposes)
public int getHospitalNumber() {
return hospitalNumber;
}
}
public class Main {
public static void main(String[] args) {
// Create an array of inpatient records
Inpatient[] patients = new Inpatient[3];
// Assign data to each of the fields through assignment statement
patients[0] = new Inpatient("John Doe", 35, new Date(), 100.0);
patients[1] = new Inpatient("Jane Smith", 45, new Date(), 120.0);
patients[2] = new Inpatient("Alice Johnson", 55, new Date(), 150.0);
// Display all records
for (Inpatient patient : patients) {
patient.displayRecord();
System.out.println();
}
// Check if two patients have the same information except for hospital number
for (int i = 0; i < patients.length - 1; i++) {
for (int j = i + 1; j < patients.length; j++) {
if (patients[i].hasSameInfoExceptHospitalNumber(patients[j])) {
System.out.println("Patients " + patients[i].getHospitalNumber() + " and " + patients[j].getHospitalNumber() + " have the same information except for hospital number.");
}
}
}
// Determine the number of patients admitted today
Date today = new Date();
int admittedToday = 0;
for (Inpatient patient : patients) {
if (patient.isAdmittedToday(today)) {
admittedToday++;
}
}
System.out.println("Number of patients admitted today: " + admittedToday);
}
}
Editor is loading...
Leave a Comment