0% found this document useful (0 votes)
29 views10 pages

Week 9 Assignment Problems

The document contains multiple programming problems related to Java concepts such as object-oriented programming, including class creation, method overriding, and cloning. It covers topics like the use of toString() and getClass(), equals() vs ==, hashCode() and equals() contract, deep vs shallow cloning, and inner classes. Each topic includes a problem statement, hints, a sample program, and expected output to demonstrate the concepts effectively.

Uploaded by

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

Week 9 Assignment Problems

The document contains multiple programming problems related to Java concepts such as object-oriented programming, including class creation, method overriding, and cloning. It covers topics like the use of toString() and getClass(), equals() vs ==, hashCode() and equals() contract, deep vs shallow cloning, and inner classes. Each topic includes a problem statement, hints, a sample program, and expected output to demonstrate the concepts effectively.

Uploaded by

bezawadakumar555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Topic 1: Using toString() and getClass()

Problem Statement 1:
Create a class Employee with fields id, name, and salary. Override the toString()
method to print employee details in a readable format. In the main method, create multiple
Employee objects and print their class name using getClass().getName().

Hints:
● Override toString() to provide a meaningful string
representation. ● Use getClass() to obtain runtime class
information.
● Display both the object details and its class name.
Program
class Employee {
int id;
String name;
double salary;

Employee(int id, String name, double salary) {


this.id = id;
this.name = name;
this.salary = salary;
}

@Override
public String toString() {
return "Employee[id=" + id + ", name=" + name + ", salary="
+ salary + "]";
}
}

public class EmployeeDemo {


public static void main(String[] args) {
Employee e1 = new Employee(101, "Asish", 50000);
Employee e2 = new Employee(102, "Shreyas", 55000);

System.out.println(e1); // calls toString()


System.out.println("Class of e1: " +
e1.getClass().getName());
System.out.println(e2);
System.out.println("Class of e2: " +
e2.getClass().getName());
}
}

Output
D:\STEP COURSE JAVA DSA\week-9\ASSIGNMENT PROBLEM>java
EmployeeDemo.java
Employee[id=101, name=Asish, salary=50000.0]
Class of e1: Employee
Employee[id=102, name=Shreyas, salary=55000.0]
Class of e2: Employee

Topic 2: equals() vs ==

Problem Statement 2:
Create a class Product with productId and productName fields. Compare two
Product objects using both == and .equals() to demonstrate the difference between
reference and content comparison. Override the equals() method to compare objects by
productId.

Hints:
● == checks reference equality, .equals() checks logical equality.
● Override equals() properly using the @Override annotation.
● Print results of both comparisons for clarity.
Program
class Product {
int productId;
String productName;

Product(int productId, String productName) {


this.productId = productId;
this.productName = productName;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return
false;
Product other = (Product) obj;
return this.productId == other.productId;
}
}

public class ProductDemo {


public static void main(String[] args) {
Product p1 = new Product(1, "Laptop");
Product p2 = new Product(1, "Laptop");
Product p3 = p1;

System.out.println("p1 == p2: " + (p1 == p2)); // false,


different references
System.out.println("p1.equals(p2): " + p1.equals(p2)); //
true, same productId
System.out.println("p1 == p3: " + (p1 == p3)); // true, same
reference
}
}

Output
D:\STEP COURSE JAVA DSA\week-9\ASSIGNMENT PROBLEM>java
ProductDemo.java
p1 == p2: false
p1.equals(p2): true
p1 == p3: true

Topic 3: hashCode() and equals() Contract

Problem Statement 3:
Create a Student class with rollNo and name fields. Override both equals()
and hashCode() so that two students with the same roll number are considered
equal. Demonstrate how these methods affect object storage in a HashSet.

Hints:
● Use Objects.hash() to generate hash codes.
● Ensure equals() and hashCode() produce consistent results.
● Add duplicate objects to a HashSet and observe the output.
Program
import java.util.*;

class Student {
int rollNo;
String name;

Student(int rollNo, String name) {


this.rollNo = rollNo;
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return rollNo == student.rollNo;
}

@Override
public int hashCode() {
return Objects.hash(rollNo);
}

@Override
public String toString() {
return "Student[rollNo=" + rollNo + ", name=" + name + "]";
}
}

public class StudentDemo {


public static void main(String[] args) {
HashSet<Student> set = new HashSet<>();
Student s1 = new Student(101, "Asish");
Student s2 = new Student(101, "Shreyas");
Student s3 = new Student(102, "John");

set.add(s1);
set.add(s2); // duplicate rollNo, will not be added
set.add(s3);

System.out.println("Students in HashSet:");
for (Student s : set) {
System.out.println(s);
}
}
}
Output
D:\STEP COURSE JAVA DSA\week-9\ASSIGNMENT PROBLEM>java
StudentDemo.java
Students in HashSet:
Student[rollNo=101, name=Asish]
Student[rollNo=102, name=John]

Topic 4: Deep vs Shallow Cloning of Objects

Problem Statement 4:
Create a class Library containing a list of Book objects. Implement cloning such that
shallow cloning only copies object references while deep cloning copies the entire list with
individual book data. Modify one book in the cloned object and observe its effect on the
original. Hints:
● Use Cloneable interface and override clone().
● For deep cloning, clone each Book object inside the list manually.
● Use loops or streams to copy nested objects.
Program
import java.util.*;

class Book implements Cloneable {


String title;

Book(String title) {
this.title = title;
}

@Override
protected Book clone() throws CloneNotSupportedException {
return (Book) super.clone();
}

@Override
public String toString() {
return title;
}
}

class Library implements Cloneable {


List<Book> books;

Library() {
books = new ArrayList<>();
}

void addBook(Book b) {
books.add(b);
}

@Override
protected Library clone() throws CloneNotSupportedException {
Library cloned = (Library) super.clone(); // shallow copy
cloned.books = new ArrayList<>();
for (Book b : this.books) {
cloned.books.add(b.clone()); // deep copy
}
return cloned;
}

@Override
public String toString() {
return books.toString();
}
}

public class LibraryDemo {


public static void main(String[] args) throws
CloneNotSupportedException {
Library lib1 = new Library();
lib1.addBook(new Book("Java"));
lib1.addBook(new Book("Python"));

Library lib2 = lib1.clone(); // deep clone


lib2.books.get(0).title = "C++";

System.out.println("Original Library: " + lib1);


System.out.println("Cloned Library: " + lib2);
}
}

Output
D:\STEP COURSE JAVA DSA\week-9\ASSIGNMENT PROBLEM>java
LibraryDemo.java
Original Library: [Java, Python]
Cloned Library: [C++, Python]
Topic 5: Member and Static Inner Classes

Problem Statement 5:
Create an University class with a non-static inner class Department and a static nested
class ExamCell. The Department class should access outer class data, while the
ExamCell performs general exam operations. Demonstrate access of both inner types from
the main method.
Hints:
● Use Outer.Inner syntax to create a member inner class object.
● Access outer class fields directly from member inner class.
● Use class name to access static nested class methods.
Program
class University {
String uniName = "SRM";

class Department { // non-static inner class


String deptName;

Department(String deptName) {
this.deptName = deptName;
}

void show() {
System.out.println("Department: " + deptName + ",
University: " + uniName);
}
}

static class ExamCell { // static nested class


static void conductExam() {
System.out.println("Exams are conducted.");
}
}
}

public class UniversityDemo {


public static void main(String[] args) {
University uni = new University();

University.Department dept = uni.new Department("CSE");


dept.show();

University.ExamCell.conductExam();
}
}

Output
D:\STEP COURSE JAVA DSA\week-9\ASSIGNMENT PROBLEM>java
UniversityDemo.JAVA
Department: CSE, University: SRM
Exams are conducted

Topic 6: Local and Anonymous Inner Classes

Problem Statement 6:
Create a Payment class with a method processTransaction(). Inside it, define a local
inner class Validator that checks if payment amount is valid. Also, create an
anonymous inner class implementing an interface Discount to apply discount
dynamically. Hints:
● Define local inner class inside a method body.
● Use anonymous inner class for one-time interface implementation.
● Call methods of both classes inside processTransaction().

Program
interface Discount {
double apply(double amount);
}

class Payment {
void processTransaction(double amount) {
class Validator { // local inner class
boolean isValid(double amt) {
return amt > 0;
}
}

Validator v = new Validator();


if (!v.isValid(amount)) {
System.out.println("Invalid payment amount.");
return;
}
// Anonymous inner class
Discount discount = new Discount() {
public double apply(double amt) {
return amt * 0.9; // 10% discount
}
};

double finalAmount = discount.apply(amount);


System.out.println("Original: " + amount + ", After
Discount: " + finalAmount);
}
}

public class PaymentDemo {


public static void main(String[] args) {
Payment p = new Payment();
p.processTransaction(1000);
}
}

Output
D:\STEP COURSE JAVA DSA\week-9\ASSIGNMENT PROBLEM>java
PaymentDemo.java
Original: 1000.0, After Discount: 900.0
3

You might also like