
Java compiler: https://www.onlinegdb.com/
Bài 1:
Để quản lý các hộ dân cư trong một khu phố, người ta cần các thông tin sau: Số thành viên trong gia đình, Số nhà, thông tin mỗi cá nhân trong gia đinh. Với mỗi cá nhân, người ta quản lý các thông tin sau: Họ tên, Tuổi, Nghề nghiệp, Số chứng minh nhân dân (duy nhất cho mỗi người).
import java.util.Scanner;
class Person {
private String fullName;
private int age;
private String occupation;
private String idNumber;
public Person(String fullName, int age, String occupation, String idNumber) {
this.fullName = fullName;
this.age = age;
this.occupation = occupation;
this.idNumber = idNumber;
}
public void displayInfo() {
System.out.println("- Full Name: " + fullName);
System.out.println("- Age: " + age);
System.out.println("- Occupation: " + occupation);
System.out.println("- ID Number: " + idNumber);
}
}
class House {
private int numberOfHouse;
private int numberOfResidentsPerHouse;
private String address;
private Person[] persons;
public House(int numberOfHouse, int numberOfResidentsPerHouse, String address) {
this.numberOfHouse = numberOfHouse;
this.numberOfResidentsPerHouse = numberOfResidentsPerHouse;
this.address = address;
persons = new Person[numberOfResidentsPerHouse];
}
public void enterResidentsPerHouseInfo(int numberOfResidentsPerHouse) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < numberOfResidentsPerHouse; i++) {
System.out.println("Enter information for person " + (i + 1));
System.out.print("- Full Name: ");
String fullName = sc.nextLine();
System.out.print("- Age: ");
int age = sc.nextInt();
sc.nextLine();
System.out.print("- Occupation: ");
String occupation = sc.nextLine();
System.out.print("- ID Number: ");
String idNumber = sc.nextLine();
persons[i] = new Person(fullName, age, occupation, idNumber);
}
}
public void displayHouseInfo() {
System.out.println("Information for household " + (numberOfHouse + 1));
System.out.println("+ Address: " + address);
System.out.println("+ Number of residents in house: " + numberOfResidentsPerHouse);
for (int i = 0; i < numberOfResidentsPerHouse; i++) {
System.out.println("Person " + (i + 1) + ":");
persons[i].displayInfo();
}
}
}
class Neighborhood {
private int numberOfHouses;
private House[] houses;
public Neighborhood(int numberOfHouses) {
this.numberOfHouses = numberOfHouses;
houses = new House[numberOfHouses];
}
public void enterHouseInfo(int numberOfHouses) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < numberOfHouses; i++) {
System.out.println("Enter information for household " + (i + 1));
System.out.print("Enter the number of residents in house "+ (i + 1) +": ");
int numberOfResidentsPerHouse = sc.nextInt();
sc.nextLine();
System.out.print("Enter the address: ");
String address = sc.nextLine();
houses[i] = new House(i, numberOfResidentsPerHouse, address);
houses[i].enterResidentsPerHouseInfo(numberOfResidentsPerHouse);
}
}
public void displayNeighborhoodInfo() {
for (int i = 0; i < numberOfHouses; i++) {
houses[i].displayHouseInfo();
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of houses in the neighborhood: ");
int numberOfHouses = sc.nextInt();
System.out.println("-------------------------------------------");
Neighborhood neighborhood = new Neighborhood(numberOfHouses);
neighborhood.enterHouseInfo(numberOfHouses);
System.out.println("--------------------------------------------");
System.out.println("| List of information for the neighborhood |");
System.out.println("--------------------------------------------");
neighborhood.displayNeighborhoodInfo();
}
}Bài 2:
Để quản lý hồ sơ học sinh của trường THPT nhà trường cần các thông tin sau: Lớp, và các thông tin về cá nhân của mỗi học sinh. Mỗi học sinh có các thông tin sau: Họ tên, tuổi, quê quán. Cài đặt chương trình có các chức năng sau:
import java.util.ArrayList;
import java.util.Scanner;
class Student {
private String fullName;
private int age;
private String hometown;
public Student(String fullName, int age, String hometown) {
this.fullName = fullName;
this.age = age;
this.hometown = hometown;
}
public int getAge() {
return age;
}
public String getHometown() {
return hometown;
}
public String toString() {
return "- Full Name: " + fullName + ", Age: " + age + ", Hometown: " + hometown;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> studentList = new ArrayList<Student>();
System.out.print("Enter the number of students: ");
int n = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < n; i++) {
System.out.println("Enter information for student " + (i + 1) + ":");
System.out.print("- Full Name: ");
String fullName = scanner.nextLine();
System.out.print("- Age: ");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("- Hometown: ");
String hometown = scanner.nextLine();
Student student = new Student(fullName, age, hometown);
studentList.add(student);
}
System.out.println("-----------------------------------------");
System.out.println("| List of students who are 20 years old |");
System.out.println("-----------------------------------------");
for (Student student : studentList) {
if (student.getAge() == 20) {
System.out.println(student);
}
}
int numberOfStudents23YearsOldHanoi = 0;
for (Student student : studentList) {
if (student.getAge() == 23 && student.getHometown().equalsIgnoreCase("Hanoi")) {
numberOfStudents23YearsOldHanoi++;
}
}
System.out.println("\n--------------------------------------------------------------");
System.out.println("Number of students who are 23 years old and come from Hanoi: " + numberOfStudents23YearsOldHanoi);
}
}Share this article
Share: