[go: up one dir, main page]

0% found this document useful (0 votes)
11 views7 pages

c2 Slot Answers

The document contains multiple Java programming tasks, including implementing marriage eligibility checks based on gender, age, and region, creating a Counter class to track object creation, developing a class hierarchy with Citizen and GovernmentEmployee, creating an Appliance class with subclasses for Washing Machine and Refrigerator, and defining an Employee class with attributes and methods for managing employee details. Each task includes sample code and expected outputs. The document emphasizes object-oriented programming concepts such as inheritance, encapsulation, and method overriding.

Uploaded by

harshanadapa07
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)
11 views7 pages

c2 Slot Answers

The document contains multiple Java programming tasks, including implementing marriage eligibility checks based on gender, age, and region, creating a Counter class to track object creation, developing a class hierarchy with Citizen and GovernmentEmployee, creating an Appliance class with subclasses for Washing Machine and Refrigerator, and defining an Employee class with attributes and methods for managing employee details. Each task includes sample code and expected outputs. The document emphasizes object-oriented programming concepts such as inheritance, encapsulation, and method overriding.

Uploaded by

harshanadapa07
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/ 7

C2 SLOT ANSWERS

1. In India, marriage eligibility is sometimes determined based on cultural norms in


addition to legal age. Implement a system that checks the marriage eligibility based on
the following
conditions:
Male (21 years and above) → Eligible for marriage
Female (18 years and above) Eligible for marriage Below legal age → Not eligible for
marriage Cultural exceptions (e.g., tribal areas, arranged marriage customs) →
Specific regions may allow marriage at a younger age
Implement Java Program with the eligibility check considering the legal age and
allow exceptions based on user input about their region (e.g., "Tribal" or "Urban").
Testcase:
Input:
Enter gender (Male/Female): Male
Enter age: 22
Enter region (Urban/Tribal): Urban
Output: Eligible for marriage
import java.util.Scanner;
public class MarriageEligibility {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input gender
System.out.print("Enter gender (Male/Female): ");
String gender = sca5nner.next();
// Input age
System.out.print("Enter age: ");
int age = scanner.nextInt();
// Input region
System.out.print("Enter region (Urban/Tribal): ");
String region = scanner.next();
// Check eligibility
boolean eligible = false;
if ((gender.equalsIgnoreCase("Male") && age >= 21) ||
(gender.equalsIgnoreCase("Female") && age >= 18)) {
eligible = true;
} else if (region.equalsIgnoreCase("Tribal") && age >= 16) {
// Cultural exception for Tribal regions (allowing younger age)
eligible = true;
}

// 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;

// Constructor that increments the count each time an object is created


public Counter() {
count++;
}

// Static method to return the total number of created objects


public static int getObjectCount() {
return count;
}
public static void main(String[] args) {
// Creating multiple objects
Counter obj1 = new Counter();
Counter obj2 = new Counter();
Counter obj3 = new Counter();

// Displaying the total count of objects created


System.out.println("Total objects created: " + Counter.getObjectCount());
}
}
3. Develop a class hierarchy with Citizen as the parent class and GovernmentEmployee
as the child class. The Citizen class has the attributes aadhaarNumber (String) and
address (String). The GovernmentEmployee class inherits from Citizen and adds the
attribute department (String) and designation (String). Use the super keyword to call
the constructor of the parent class Citizen from the GovernmentEmployee constructor.
Implement a method display EmployeeDetails() that prints the Aadhaar number,
address, department, and designation. Include the inheritance and constructor chaining
by creating a GovernmentEmployee object and displaying the details.
Expected Output:
Aadhaar Number: 1122-3344-5566
Address: Amaravati
Department: Health Department
Designation: Medical Officer

class Citizen {
protected String aadhaarNumber;
protected String address;

// Constructor for Citizen class


public Citizen(String aadhaarNumber, String address) {
this.aadhaarNumber = aadhaarNumber;
this.address = address;
}
}

class GovernmentEmployee extends Citizen {


private String department;
private String designation;

// Constructor for GovernmentEmployee class using super to call parent constructor


public GovernmentEmployee(String aadhaarNumber, String address, String
department, String designation) {
super(aadhaarNumber, address);
this.department = department;
this.designation = designation;
}

// Method to display employee details


public void displayEmployeeDetails() {
System.out.println("Aadhaar Number: " + aadhaarNumber);
System.out.println("Address: " + address);
System.out.println("Department: " + department);
System.out.println("Designation: " + designation);
}

public static void main(String[] args) {


// Creating an instance of GovernmentEmployee
GovernmentEmployee emp = new GovernmentEmployee("1122-3344-5566",
"Amaravati", "Health Department", "Medical Officer");

// Displaying employee details


emp.displayEmployeeDetails();
}
}

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");
}
}

class WashingMachine extends Appliance {


// Overriding turnOn method
@Override
public void turnOn() {
System.out.println("Washing Machine is now washing clothes");
}
}

class Refrigerator extends Appliance {


// Overriding turnOn method
@Override
public void turnOn() {
System.out.println("Refrigerator is cooling food");
}
}

public class ApplianceTest {


// Method to test appliance functionality
public static void testAppliance(Appliance appliance) {
appliance.turnOn();
}

public static void main(String[] args) {


// Creating objects
WashingMachine wm = new WashingMachine();
Refrigerator fridge = new Refrigerator();

// 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;
}

// Getter and Setter for salary (private)


public double getSalary() {
return salary;
}

public void setSalary(double salary) {


this.salary = salary;
}

// Getter and Setter for department (protected)


protected String getDepartment() {
return department;
}

protected void setDepartment(String department) {


this.department = department;
}
// Method to display employee details
public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
System.out.println("Department: " + department);
}
}

class Manager extends Employee {

// Constructor for Manager class


public Manager(String name, double salary, String department) {
super(name, salary, department);
}

public void accessDetails() {


// Accessing public, private (via getter), and protected attributes
System.out.println("Name: " + name); // Public
System.out.println("Salary: " + getSalary()); // Private (via getter)
System.out.println("Department: " + getDepartment()); // Protected (via getter)
}

public static void main(String[] args) {


// Creating a Manager object
Manager mgr = new Manager("Mr. Ramu", 50000, "SCOPE");

// Display details
mgr.accessDetails();
}
}

You might also like