9 th condition qs using list
Fri Jun 07 2024 13:54:41 GMT+0000 (Coordinated Universal Time)
Saved by @dbms
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
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 String getName() {
return name;
}
public int getAge() {
return age;
}
public double getIncome() {
return income;
}
}
public class PersonExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Person> setA = new ArrayList<>();
// Read the number of persons
System.out.print("Enter the number of persons: ");
int n = scanner.nextInt();
scanner.nextLine(); // consume newline character
// Read details for each person
for (int i = 0; i < n; i++) {
System.out.println("Enter details for person " + (i + 1) + ":");
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Age: ");
int age = scanner.nextInt();
System.out.print("Income: ");
double income = scanner.nextDouble();
scanner.nextLine(); // consume newline character
// Create a new Person object and add it to the list
Person person = new Person(name, age, income);
setA.add(person);
}
// Initialize sets B, C, and B and C
List<Person> setB = new ArrayList<>();
List<Person> setC = new ArrayList<>();
List<Person> setBC = new ArrayList<>();
// Compute the sets
for (Person person : setA) {
if (person.getAge() > 60) {
setB.add(person);
}
if (person.getIncome() < 10000) {
setC.add(person);
}
if (person.getAge() > 60 && person.getIncome() < 10000) {
setBC.add(person);
}
}
// Print the results
System.out.println("\nSet A:");
for (Person person : setA) {
System.out.println("Name: " + person.getName() + ", Age: " + person.getAge() + ", Income: " + person.getIncome());
}
System.out.println("\nSet B (age > 60):");
for (Person person : setB) {
System.out.println("Name: " + person.getName() + ", Age: " + person.getAge() + ", Income: " + person.getIncome());
}
System.out.println("\nSet C (income < 10000):");
for (Person person : setC) {
System.out.println("Name: " + person.getName() + ", Age: " + person.getAge() + ", Income: " + person.getIncome());
}
System.out.println("\nSet B and C (age > 60 and income < 10000):");
for (Person person : setBC) {
System.out.println("Name: " + person.getName() + ", Age: " + person.getAge() + ", Income: " + person.getIncome());
}
}
}



Comments