9.Person
Tue May 28 2024 15:52:58 GMT+0000 (Coordinated Universal Time)
Saved by @adsj
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
class Person {
private String name;
private int age;
private double income;
public Person(String name, int age, double income) {
this.name = name;
this.age = age;
this.income = income;
}
public int getAge() {
return age;
}
public double getIncome() {
return income;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + ", income=" + income + '}';
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Set<Person> setA = new HashSet<>();
Set<Person> setB = new HashSet<>();
Set<Person> setC = new HashSet<>();
// Reading set A of persons from the user
System.out.println("Enter the number of persons in set A:");
int n = scanner.nextInt();
scanner.nextLine(); // Consume newline
for (int i = 0; i < n; i++) {
System.out.println("Enter details for person " + (i + 1) + " (name, age, income):");
String[] details = scanner.nextLine().split("\\s+");
String name = details[0];
int age = Integer.parseInt(details[1]);
double income = Double.parseDouble(details[2]);
setA.add(new Person(name, age, income));
}
// Computing set B of persons whose age >= 60
for (Person person : setA) {
if (person.getAge() >= 60) {
setB.add(person);
}
}
// Computing set C of persons whose income < 10000
for (Person person : setA) {
if (person.getIncome() < 10000) {
setC.add(person);
}
}
// Computing set B union C
Set<Person> setBC = new HashSet<>(setB);
setBC.addAll(setC);
// Printing the computed sets
System.out.println("\nSet B (Persons aged 60 or above):");
printSet(setB);
System.out.println("\nSet C (Persons with income < 10000):");
printSet(setC);
System.out.println("\nSet B union C:");
printSet(setBC);
}
private static void printSet(Set<Person> set) {
for (Person person : set) {
System.out.println(person);
}
}
}



Comments