0% found this document useful (0 votes)
20 views4 pages

Lab2 Shubham

Uploaded by

ajay9861gupta
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)
20 views4 pages

Lab2 Shubham

Uploaded by

ajay9861gupta
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/ 4

Lab2

1. Create a superclass Person with attributes name and age, and a method display().
Create a subclass Student that adds an attribute studentID. Write a program to
create a Student object and display all its attributes.

2. Create a superclass Calculator with a method add(int a, int b). Create a subclass
AdvancedCalculator that overloads the add method to handle three integers.
Program:-
package lab2;

package lab2;

class Calculator {
public int add(int a, int b) {
return a + b;
}
}

class AdvancedCalculator extends Calculator {


public int add(int a, int b, int c) {
return a + b + c;
}
}

public class QU2 {


public static void main(String[] args) {
AdvancedCalculator calc = new AdvancedCalculator();
System.out.println(calc.add(13, 234)); // Calls the method from Calculator
System.out.println(calc.add(134, 242, 334)); // Calls the overloaded method from AdvancedCalculator
}
}
Ouput:-

3. Create a superclass Vehicle with a method move(). Create subclasses Car and
Bike and call the move() method on each.

Program:-
package lab2;

class Vehicle {
public void move() {
System.out.println("The vehicle is moving");
}
}

class Car extends Vehicle {


@Override
public void move() {
System.out.println("The car is moving");
}
}

class Bike extends Vehicle {


@Override
public void move() {
System.out.println("The bike is moving");
}
}

public class QU3 {


public static void main(String[] args) {
Vehicle myCar = new Car();
Vehicle myBike = new Bike();
myCar.move();
myBike.move();
}
}

Ouput:-

4. Create an class Employee with an abstract method calculatePay(). Create


subclasses Salaried Employee and HourlyEmployee that implement the
calculatePay() method. Write a program to create objects of both subclasses and
call the calculate Pay() method.

Program:-
Ouput:-

5. Create an class Document with an method void open(). Implement subclasses


WordDocument, PDFDocument, and SpreadsheetDocument that extend Document
and provide implementations for open().Write a main class to demonstrate opening
different types of documents. (implement complile time- polymorphism).

Program:-
package lab2;
class Document {
public void open() {
System.out.println("Opening a generic document");
}
}

class WordDocument extends Document {


@Override
public void open() {
System.out.println("Opening a Word document");
}
}

class PDFDocument extends Document {


@Override
public void open() {
System.out.println("Opening a PDF document");
}
}

class SpreadsheetDocument extends Document {


@Override
public void open() {
System.out.println("Opening a Spreadsheet document");
}
}

public class QU5 {


public static void main(String[] args) {
Document wordDoc = new WordDocument();
Document pdfDoc = new PDFDocument();
Document spreadsheetDoc = new SpreadsheetDocument();

wordDoc.open();
pdfDoc.open();
spreadsheetDoc.open();
}
}
Ouput:-

6.Create a class Calculator with overloaded methods add() that take different
numbers and types of parameters: int add(int a, int b), double add(double a, double
b), int add(int a, int b, int c) Write a main class to demonstrate the usage
of these methods

Program:-
package lab2;

class Calculator2 {
public int add(int a, int b) {
return a + b;
}

public double add(double a, double b) {


return a + b;
}

public int add(int a, int b, int c) {


return a + b + c;
}
}

public class QU6 {


public static void main(String[] args) {
Calculator2 calc = new Calculator2();

System.out.println(calc.add(32, 75));
System.out.println(calc.add(117.5, 20));
System.out.println(calc.add(130, 220, 3120));
}
}
Ouput:-

You might also like