Java lab
Exercises that focus on teaching interfaces and inheritance through different thematic examples:
---
Java Lab Exercise 1: Animals in the Jungle
Objective: Understand interfaces and inheritance using a jungle ecosystem example.
### Instructions:
1. **Create an Interface `Animal`
This interface should declare a method `makeSound()`.
2. Create Classes `Tiger`, `Monkey`, and `Snake`
These classes should implement the `Animal` interface.
Each class should override the `makeSound()` method to print a unique sound (e.g., Tiger roars).
3. Create a Class `Jungle`
This class should have a method `hearSoundsInJungle(Animal animal)` that invokes the `makeSound()` method of the given animal.
Sample Code:
interface Animal {
void makeSound();
}
class Tiger implements Animal {
public void makeSound() {
System.out.println("Roar");
}
}
Let's expand this code into a fully functional Java program that illustrates polymorphism.
I'll add comments to explain what is happening at each step.
```java
// Define the interface Animal with an abstract method makeSound()
interface Animal {
void makeSound(String whatistheSound);
}
// Tiger class implements the Animal interface
class Tiger implements Animal {
// Implementation of makeSound() for Tiger
public void makeSound(String sound) {
System.out.println("Tiger says Growl!");
}
}
// Monkey class also implements the Animal interface
class Monkey implements Animal {
// Implementation of makeSound() for Monkey
public void makeSound(String sound) {
System.out.println("Monkey says Cheep Cheep!");
}
}
// Main class to demonstrate polymorphism
public class Main {
public static void main(String[] args) {
// Polymorphism in action
// Here, Animal is a reference type, and it can refer to any object that implements the Animal interface
Animal myAnimal;
// myAnimal refers to a Tiger object
myAnimal = new Tiger();
myAnimal.makeSound("Growl");
// myAnimal now refers to a Monkey object
myAnimal = new Monkey();
myAnimal.makeSound("Cheep Cheep");
// This demonstrates polymorphism where a single reference (myAnimal) can take many forms (Tiger, Monkey, etc.)
}
}
```
### Explanation
- **Interface `Animal`**: This defines a contract (an abstract method `makeSound()`) that all classes implementing the interface must fulfill.
- **Classes `Tiger` and `Monkey`**: Both classes implement the `Animal` interface, providing their own version of `makeSound()`.
- **Polymorphism in Main Class**: In the `main` method, we see polymorphism in action. We declare a reference of type `Animal` and use it to refer to objects of classes `Tiger` and `Monkey`. This is the essence of polymorphism: the ability to treat objects of different classes in the same way through a common interface.
The main advantage here is flexibility. We can add more classes that implement `Animal` without changing the main class. This makes our code easily extensible and manageable, a key feature in object-oriented programming.
// Implement Monkey and Snake classes similarly
public class Jungle {
void hearSoundsInJungle(Animal animal) {
animal.makeSound();
}
public static void main(String[] args) {
Jungle jungle = new Jungle();
jungle.hearSoundsInJungle(new Tiger());
// Test with Monkey and Snake objects
}
}
```
---
## Java Lab Exercise 2: Flower Families
### Objective:
Learn about interfaces and inheritance through the classification of flowers.
### Instructions:
1. **Create an Interface `Flower`**: This interface should declare methods `bloom()` and `fragrance()`.
2. **Create Classes `Rose`, `Lily`, and `Daisy`**: These classes should implement the `Flower` interface. Each class should override the methods to provide specific behaviors (e.g., Rose blooms with red petals).
3. **Create a Class `Garden`**: This class should have a method `displayFlowers(Flower flower)` that invokes the `bloom()` and `fragrance()` methods of the given flower.
### Sample Code:
```java
interface Flower {
void bloom();
void fragrance();
}
class Rose implements Flower {
public void bloom() {
System.out.println("Rose blooms with red petals");
}
public void fragrance() {
System.out.println("Rose has a sweet fragrance");
}
}
// Implement Lily and Daisy classes similarly
public class Garden {
void displayFlowers(Flower flower) {
flower.bloom();
flower.fragrance();
}
public static void main(String[] args) {
Garden garden = new Garden();
garden.displayFlowers(new Rose());
// Test with Lily and Daisy objects
}
}
```
---
## Java Lab Exercise 3: Space Astronomy Schema
### Objective:
Explore interfaces and inheritance using a space astronomy context.
### Instructions:
1. **Create an Interface `CelestialBody`**: This interface should declare methods `rotate()` and `revolve()`.
2. **Create Classes `Planet`, `Star`, and `Asteroid`**: These classes should implement the `CelestialBody` interface. Each class should override the methods to reflect their specific behaviors in space.
3. **Create a Class `Observatory`**: This class should have a method `studyCelestialBody(CelestialBody body)` that invokes the `rotate()` and `revolve()` methods of the given celestial body.
### Sample Code:
```java
interface CelestialBody {
void rotate();
void revolve();
}
class Planet implements CelestialBody {
public void rotate() {
System.out.println("Planet rotates on its axis");
}
public void revolve() {
System.out.println("Planet revolves around a star");
}
}
// Implement Star and Asteroid classes similarly
public class Observatory {
void studyCelestialBody(CelestialBody body) {
body.rotate();
body.revolve();
}
public static void main(String[] args) {
Observatory observatory = new Observatory();
observatory.studyCelestialBody(new Planet());
// Test with Star and Asteroid objects
}
}
```
---
These exercises provide a hands-on approach to understanding the concepts of interfaces and inheritance in Java, using interesting and relatable examples.