0% found this document useful (0 votes)
1 views

Set9

The document contains Java code for two main functionalities: counting the number of zero bits in the binary representation of an integer and managing employee details through inheritance. The first part prompts the user for an integer, converts it to binary, and counts the zero bits. The second part defines an Employee class and its subclasses for full-time and part-time employees, allowing for input and display of employee details including salary and pay calculations.

Uploaded by

kareena.it222110
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Set9

The document contains Java code for two main functionalities: counting the number of zero bits in the binary representation of an integer and managing employee details through inheritance. The first part prompts the user for an integer, converts it to binary, and counts the zero bits. The second part defines an Employee class and its subclasses for full-time and part-time employees, allowing for input and display of employee details including salary and pay calculations.

Uploaded by

kareena.it222110
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Set9

import java.util.Scanner;

public class BinaryZeroBitsCounter {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input integer from user

System.out.print("Enter an integer: ");

int number = scanner.nextInt();

// Convert to binary representation

String binaryRepresentation = Integer.toBinaryString(number);

System.out.println("Binary representation of " + number + " is: " + binaryRepresentation);

// Count the number of zero bits

int zeroBitsCount = 0;

for (char bit : binaryRepresentation.toCharArray()) {

if (bit == '0') {

zeroBitsCount++;

System.out.println("Number of zero bits: " + zeroBitsCount);

scanner.close();

Output

Enter an integer: 25

Binary representation of 25 is: 11001


Number of zero bits: 2

Q2
import java.util.Scanner;

// Base class: Employee

class Employee {

String name;

String address;

int age;

String gender;

// Constructor for Employee class

public Employee(String name, String address, int age, String gender) {

this.name = name;

this.address = address;

this.age = age;

this.gender = gender;

// Display method for Employee details

public void display() {

System.out.println("Name: " + name);

System.out.println("Address: " + address);

System.out.println("Age: " + age);

System.out.println("Gender: " + gender);

// FullTimeEmployee class inheriting Employee

class FullTimeEmployee extends Employee {


double salary;

String designation;

// Constructor for FullTimeEmployee

public FullTimeEmployee(String name, String address, int age, String gender, double salary, String
designation) {

super(name, address, age, gender); // Call superclass constructor

this.salary = salary;

this.designation = designation;

// Overridden display method

@Override

public void display() {

super.display(); // Call superclass display method

System.out.println("Salary: " + salary);

System.out.println("Designation: " + designation);

// PartTimeEmployee class inheriting Employee

class PartTimeEmployee extends Employee {

int workingHours;

double ratePerHour;

// Constructor for PartTimeEmployee

public PartTimeEmployee(String name, String address, int age, String gender, int workingHours,
double ratePerHour) {

super(name, address, age, gender); // Call superclass constructor

this.workingHours = workingHours;

this.ratePerHour = ratePerHour;

}
// Method to calculate the pay

public double calculatePay() {

return workingHours * ratePerHour; // Pay = workingHours * ratePerHour

// Overridden display method

@Override

public void display() {

super.display(); // Call superclass display method

System.out.println("Working Hours: " + workingHours);

System.out.println("Rate Per Hour: " + ratePerHour);

System.out.println("Total Pay: " + calculatePay());

// Main class

public class EmployeeManagement {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input for FullTimeEmployee

System.out.println("Enter details for Full-Time Employee:");

System.out.print("Name: ");

String ftName = scanner.nextLine();

System.out.print("Address: ");

String ftAddress = scanner.nextLine();

System.out.print("Age: ");

int ftAge = scanner.nextInt();

scanner.nextLine(); // Consume newline

System.out.print("Gender: ");
String ftGender = scanner.nextLine();

System.out.print("Salary: ");

double ftSalary = scanner.nextDouble();

scanner.nextLine(); // Consume newline

System.out.print("Designation: ");

String ftDesignation = scanner.nextLine();

// Create FullTimeEmployee object

FullTimeEmployee fullTimeEmployee = new FullTimeEmployee(ftName, ftAddress, ftAge,


ftGender, ftSalary, ftDesignation);

// Input for PartTimeEmployee

System.out.println("\nEnter details for Part-Time Employee:");

System.out.print("Name: ");

String ptName = scanner.nextLine();

System.out.print("Address: ");

String ptAddress = scanner.nextLine();

System.out.print("Age: ");

int ptAge = scanner.nextInt();

scanner.nextLine(); // Consume newline

System.out.print("Gender: ");

String ptGender = scanner.nextLine();

System.out.print("Working Hours: ");

int ptWorkingHours = scanner.nextInt();

System.out.print("Rate Per Hour: ");

double ptRatePerHour = scanner.nextDouble();

// Create PartTimeEmployee object

PartTimeEmployee partTimeEmployee = new PartTimeEmployee(ptName, ptAddress, ptAge,


ptGender, ptWorkingHours, ptRatePerHour);

// Display details
System.out.println("\n--- Full-Time Employee Details ---");

fullTimeEmployee.display();

System.out.println("\n--- Part-Time Employee Details ---");

partTimeEmployee.display();

scanner.close();

Output

Enter details for Full-Time Employee:

Name: John Doe

Address: 123 Main St

Age: 35

Gender: Male

Salary: 60000

Designation: Manager

Enter details for Part-Time Employee:

Name: Jane Smith

Address: 456 Elm St

Age: 28

Gender: Female

Working Hours: 20

Rate Per Hour: 15

--- Full-Time Employee Details ---

Name: John Doe

Address: 123 Main St

Age: 35

Gender: Male
Salary: 60000.0

Designation: Manager

--- Part-Time Employee Details ---

Name: Jane Smith

Address: 456 Elm St

Age: 28

Gender: Female

Working Hours: 20

Rate Per Hour: 15.0

Total Pay: 300.0

You might also like