0% found this document useful (0 votes)
30 views11 pages

Oop Labs Solutions

The document covers fundamental concepts of object-oriented programming, including defining classes and objects, attributes and behaviors, inheritance, and polymorphism. It includes practical lab exercises for creating classes like Book, Calculator, Dog, Vehicle, and Shape, demonstrating instantiation, method overriding, and polymorphic behavior. Each lab provides code examples and expected outputs to illustrate the concepts effectively.

Uploaded by

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

Oop Labs Solutions

The document covers fundamental concepts of object-oriented programming, including defining classes and objects, attributes and behaviors, inheritance, and polymorphism. It includes practical lab exercises for creating classes like Book, Calculator, Dog, Vehicle, and Shape, demonstrating instantiation, method overriding, and polymorphic behavior. Each lab provides code examples and expected outputs to illustrate the concepts effectively.

Uploaded by

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

Objects and Classes, Inheritance and Polymorphism

2.1. Defining classes and objects


2.2. Attributes (properties) and behaviors (methods)
2.3. Instantiating objects from classes
2.4. Accessing and manipulating object state and behavior
4.2. Method overriding and method overloading
4.3. Abstract classes and interfaces

4.4. Polymorphic behavior and dynamic dispatch

Lab 1

Classes and objects.

1. i) Create a class called Book with the following attributes:

-title (String)

-author (String)

-pages (int)

ii) Add a method displayDetails() that prints out the book’s information.

Iii) Then, create two Book objects and call the method for each.

iv) Add comments to your code.

i) creating a class called Book

public class Book {


// The following are attributes
String title;
String author;
int pages;
ii) Add a method displayDetails() that prints out the book’s information

// Constructor
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
// Method to display book details
public void displayDetails() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Pages: " + pages);
System.out.println("----------------------");
}}

public class Main {


public static void main(String[] args) {
// Create two Book objects
Book book1 = new Book("The Programmerz", "Wayne Ghost",
200);
Book book2 = new Book("OOP", "Makanaka Dube", 158);

// Display details of two books


book1.displayDetails(); // call the method for book1
book2.displayDetails();// call the method for book2
}
}
….…………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

Lab 2

Attributes and Behaviors

Design a class Calculator with:

i) An attribute result initialized to 0


ii) Methods add(int a), subtract(int a), and multiply(int a) that update result
iii) A method getResult() that returns the current result
iv) Write a main method that demonstrates using this calculator.
v) Display the output

public class Calculator {


// i) Attribute initialized to 0
private int result = 0;
// ii) Methods to update result
public void add(int a) {
result += a;
}

public void subtract(int a) {


result -= a;
}

public void multiply(int a) {


result *= a;
}
// iii) Method to return current result using getResult()
public int getResult() {
return result;
}}

//iv) Main method


public class Main {
public static void main(String[] args) {
// Create a Calculator object
Calculator calc = new Calculator();
// Perform operations
calc.add(5); // result = 5
calc.subtract(2); // result = 3
calc.multiply(4); // result = 12

// Display final answer


System.out.println("Final Answer: " + calc.getResult());
}
}

Output

Final Answer: 12

Write
notes ….…………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………

Lab 3

Instantiating Objects

Create a class Dog with attributes name and breed, and a method bark() that prints
"Woof! My name is [name]". Instantiate three dogs with the following names ["
Chicco", "Cocco", "Scooby"] and the following breeds ["Jack Russell",German
Shepherd","Chihuahua"] and call bark() on each. Display the output.

public class Dog {

// Attributes

String name;

String breed;

// Constructor

public Dog(String name, String breed) {

this.name = name;

this.breed = breed;

// Method to bark

public void bark() {

System.out.println("Woof! My name is " + name);

public class Main {


public static void main(String[] args) {

// Instantiate three Dog objects

Dog dog1 = new Dog("Chicco", "Jack Russell");

Dog dog2 = new Dog("Cocco", "German Shepherd");

Dog dog3 = new Dog("Scooby", "Chihuahua");

// Call bark() on each

dog1.bark();

dog2.bark();

dog3.bark();

….………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………
Lab 4

Inheritance

Create a base class Vehicle with attributes make and model, and a method
startEngine() that prints "Engine started".

Then create a subclass Car that adds an attribute numDoors and overrides
startEngine() to print "Car engine started".

Instantiate both a Vehicle and a Car, and call startEngine() on each.

//Vehicle and Car Classes

// Base class

class Vehicle {

String make;

String model;

// Constructor

public Vehicle(String make, String model) {

this.make = make;

this.model = model;

// Method to start engine

public void startEngine() {

System.out.println("Engine started");

}
// Subclass

class Car extends Vehicle {

int numDoors;

// Constructor

public Car(String make, String model, int numDoors) {

super(make, model); // Call Vehicle constructor

this.numDoors = numDoors;

// Overridden method

@Override

public void startEngine() {

System.out.println("Car engine started");

//Main Method to Test Inheritance

public class Main {

public static void main(String[] args) {

// Instantiate Vehicle

Vehicle vehicle = new Vehicle("Toyota", "Aqua");

vehicle.startEngine(); // Output: Engine started


// Instantiate Car

Car car = new Car("Honda", "Fit", 4);

car.startEngine(); // Output: Car engine started

Lab 5

Polymorphism

Create a superclass Shape with a method draw() that prints "Drawing a shape".

Create subclasses Circle, Square, and Triangle that override draw() with their own
messages.

Write a method that takes a Shape object and calls draw()—then pass in different
shapes to demonstrate polymorphism.

// Superclass

class Shape {

public void draw() {

System.out.println("Drawing a shape");

// Subclass: Circle

class Circle extends Shape {

@Override
public void draw() {

System.out.println("Drawing a circle");

// Subclass: Square

class Square extends Shape {

@Override

public void draw() {

System.out.println("Drawing a square");

// Subclass: Triangle

class Triangle extends Shape {

@Override

public void draw() {

System.out.println("Drawing a triangle");

public class Main {

// Method that accepts any Shape and calls draw()

public static void renderShape(Shape shape) {


shape.draw();

public static void main(String[] args) {

// Create different shapes

Shape circle = new Circle();

Shape square = new Square();

Shape triangle = new Triangle();

// Demonstrate polymorphism

renderShape(circle);

renderShape(square);

renderShape(triangle);

You might also like