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

JAVA Lab Sample Questions Ans

Uploaded by

anujana415
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JAVA Lab Sample Questions Ans

Uploaded by

anujana415
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JAVA Lab Sample Questions

1) create an interface Movable with methods moveForward() and moveBackward().


Create another interface Steerable that extends Movable and adds methods
turnLeft() and turnRight(). Implement Steerable in a Robot class. Create an instance
of Robot and call all the methods.

Ans.:

// Movable.java Interface
public interface Movable {
void moveForward();
void moveBackward();
}

// Steerable.java Interface
public interface Steerable extends Movable {
void turnLeft();
void turnRight();
}

// Robot.java Class
public class Robot implements Steerable {
@Override
public void moveForward() {
System.out.println("Robot is moving forward.");
}

@Override
public void moveBackward() {
System.out.println("Robot is moving backward.");
}

@Override
public void turnLeft() {
System.out.println("Robot is turning left.");
}

@Override
public void turnRight() {
System.out.println("Robot is turning right.");
}
}

// Main.java Class
public class Main {
public static void main(String[] args) {
// Create Robot instance
Robot robot = new Robot();

// Call all methods


robot.moveForward();
robot.moveBackward();
robot.turnLeft();
robot.turnRight();
}
}

2. Create an abstract class named Transport with an abstract method start(). Create
an interface named Fuelable with methods refuel() and checkFuelLevel().
Implement these in a class named Car. The Car class should provide its own
implementation of start(), refuel(), and checkFuelLevel(). Create an instance of
Car and call the methods to demonstrate their functionality.

// Transport.java
public abstract class Transport {
public abstract void start();
}

// Fuelable.java
public interface Fuelable {
void refuel();
void checkFuelLevel();
}

// Car.java
public class Car extends Transport implements Fuelable {
@Override
public void start() {
System.out.println("Car is starting.");
}

@Override
public void refuel() {
System.out.println("Refueling the car.");
}

@Override
public void checkFuelLevel() {
System.out.println("Checking the car's fuel level.");
}
}
// Main.java
public class Main {
public static void main(String[] args) {
// Create Car instance
Car car = new Car();

// Call methods
car.start();
car.refuel();
car.checkFuelLevel();
}
}

3. Create an abstract class Fruit with an abstract method taste(). Create two
subclasses: Apple and Orange. Each subclass should implement the taste() method to
describe the taste of that fruit (e.g., "Sweet" for Apple, "Tangy" for Orange). Create
instances of Apple and Orange, invoke the taste() method on each object, and print the
taste.

// Fruit.java
public abstract class Fruit {
public abstract void taste();
}

// Apple.java
public class Apple extends Fruit {
@Override
public void taste() {
System.out.println("Sweet");
}
}

// Orange.java
public class Orange extends Fruit {
@Override
public void taste() {
System.out.println("Tangy");
}
}

// Main.java
public class Main {
public static void main(String[] args) {
// Create Apple instance
Fruit apple = new Apple();
apple.taste(); // Output: Sweet

// Create Orange instance


Fruit orange = new Orange();
orange.taste(); // Output: Tangy
}
}

You might also like