c2 Slot Answers
c2 Slot Answers
// Output result
if (eligible) {
System.out.println("Eligible for marriage");
} else {
System.out.println("Not eligible for marriage");
}
scanner.close();
}
}
2. Create a Counter class that keeps track of the number of times an object is created.
Use a static variable to store the count of created objects. Implement a constructor that
increments the static variable each time a new object is created. Write a static method
getObjectCount() that returns the total count of created objects.
Sample Testcase: If you create three objects in your code, the output should be: Total
objects created: 3.
class Counter {
// Static variable to keep track of the number of objects created
private static int count = 0;
class Citizen {
protected String aadhaarNumber;
protected String address;
4. write a java program Create a class Appliance with the following method: turnOn()
(prints "Appliance is turned on"). Now, create two subclasses: • Washing Machine
(inherits from Appliance) with a turnOn() method that prints "Washing Machine is
now washing clothes". • Refrigerator (inherits from Appliance) with a turnOn()
method that prints "Refrigerator is cooling food". Implement a Java program by
overriding the turnOn() method in both WashingMachine and Refrigerator. Create a
method testAppliance(Appliance appliance) appliance) that that calls
appliance.turnOn() and shows the respective outputs when passing a Washing
Machine or Refrigerator object.
Sample Testcase:
Input: Creating a testAppliance object for Washing Machine, and Refrigerator in your
code. Output: Machine is now washing clothes Refrigerator is now cooling
class Appliance {
// Method to turn on the appliance
public void turnOn() {
System.out.println("Appliance is turned on");
}
}
// Testing appliances
testAppliance(wm);
testAppliance(fridge);
}
}
5. In a class Employee, create three attributes: name (public), salary (private), and
department (protected). Declare methods to get and set salary (private) and
department (protected) values. Implement a method display Details() to print all the
employee details. Create a subclass Manager that inherits from Employee. Try to
access all attributes, which can be accessed directly from the Manager using the get()
and set() methods.
Expected Output:
Name: Mr. Ramu
Salary: 50000
Department: SCOPE
class Employee {
public String name;
private double salary;
protected String department;
// Constructor
public Employee(String name, double salary, String department) {
this.name = name;
this.salary = salary;
this.department = department;
}
// Display details
mgr.accessDetails();
}
}