0% found this document useful (0 votes)
9 views3 pages

Java Full Codes With Output

The document contains two Java code implementations for managing a list of people using ArrayList and HashMap. The ArrayList version includes classes for Person, Student, and Employee, allowing for detailed display of information based on user input. The HashMap version simplifies data retrieval by using a key-value pair approach to access and display person details based on an ID input.

Uploaded by

ashishraj421878
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Java Full Codes With Output

The document contains two Java code implementations for managing a list of people using ArrayList and HashMap. The ArrayList version includes classes for Person, Student, and Employee, allowing for detailed display of information based on user input. The HashMap version simplifies data retrieval by using a key-value pair approach to access and display person details based on an ID input.

Uploaded by

ashishraj421878
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

ArrayList Code

// ArrayList Version
import [Link].*;

class Person {
String name;
int age;

Person(String n, int a) {
name = n;
age = a;
}

void display() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}

class Student extends Person {


int regNo;
String branch, course;
int internal, external, total;
int sem;

Student(String n, int a, int r, String b, String c, int i, int e, int s) {


super(n, a);
regNo = r;
branch = b;
course = c;
internal = i;
external = e;
total = i + e;
sem = s;
}

void display() {
[Link]();
[Link]("Reg No: " + regNo);
[Link]("Branch: " + branch);
[Link]("Course: " + course);
[Link]("Marks: " + internal + " + " + external + " = " + total);
[Link]("Semester: " + sem);
[Link]();
}
}

class Employee extends Person {


int empId;
String company;
double salary;

Employee(String n, int a, int id, String comp, double sal) {


super(n, a);
empId = id;
company = comp;
salary = sal;
}

void display() {
[Link]();
[Link]("Emp ID: " + empId);
[Link]("Company: " + company);
[Link]("Salary: " + salary);
[Link]();
}
}

public class Main {


public static void main(String[] args) {
ArrayList<Person> data = new ArrayList<>();

[Link](new Student("Rahul", 20, 101, "CSE", "Java", 20, 70, 1));


[Link](new Student("Amit", 21, 102, "ECE", "Python", 25, 65, 2));
[Link](new Employee("Ravi", 30, 201, "TCS", 40000));
Scanner sc = new Scanner([Link]);
[Link]("Enter ID: ");
int id = [Link]();

boolean found = false;

for (Person p : data) {


if (p instanceof Student) {
Student s = (Student) p;
if ([Link] == id) {
[Link]();
found = true;
}
} else if (p instanceof Employee) {
Employee e = (Employee) p;
if ([Link] == id) {
[Link]();
found = true;
}
}
}

if (!found) {
[Link]("No record found");
}
}
}

Output

Output Example:

Enter ID: 101


Name: Rahul
Age: 20
Reg No: 101
Branch: CSE
Course: Java
Marks: 20 + 70 = 90
Semester: 1

2. HashMap Code

// HashMap Version
import [Link].*;

class Person {
String name;
int age;

Person(String n, int a) {
name = n;
age = a;
}

void display() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}

class Student extends Person {


int regNo;
String branch;

Student(String n, int a, int r, String b) {


super(n, a);
regNo = r;
branch = b;
}
void display() {
[Link]();
[Link]("Reg No: " + regNo);
[Link]("Branch: " + branch);
[Link]();
}
}

class Employee extends Person {


int empId;
String company;

Employee(String n, int a, int id, String c) {


super(n, a);
empId = id;
company = c;
}

void display() {
[Link]();
[Link]("Employee ID: " + empId);
[Link]("Company: " + company);
[Link]();
}
}

public class Main {


public static void main(String[] args) {
HashMap<Integer, Person> data = new HashMap<>();

[Link](101, new Student("Rahul", 20, 101, "CSE"));


[Link](201, new Employee("Ravi", 30, 201, "TCS"));

Scanner sc = new Scanner([Link]);


[Link]("Enter ID: ");
int id = [Link]();

if ([Link](id)) {
Person p = [Link](id);
[Link]();
} else {
[Link]("No record found");
}
}
}

Output
Output Example:

Enter ID: 201


Name: Ravi
Age: 30
Employee ID: 201
Company: TCS

You might also like