Write an application that combines several classes and interfaces. An abstract class Robot has concrete subclasses named RobotA, RobotB, RobotC. Class RobotA1 extends RobotA. Classes RobotB1 and RobotB2 extends RobotB. Class RobotC1 extends RobotC. The locomotion interface declares three methods name forward() and stop(). It is implemented by classes RobotB, RobotC. The sound interface declareone method named beep(). It is implemented by classes RobotA1,RobotB1 and RobotC1. Define all classes and implement the interface as specified. Create one instance of each class. Then invoke the stop() method of all object that are type Locomotion. Place this code in a package named robots.
source file name: RobotDemo.java
// interface Locomotion has forward ( ) . stop ( ) and reverse ( ) abstract methods
interface Locomotion
{
public void forward ();
public void stop ();
public void reverse () ;
}
// interface Sound have only beep() abstract method
interface Sound
{
public void beep ();
}
abstract class Robot // abstract class
{
abstract public void robot ();
}
// sub classing of Robot class and implementation Locomotion
class RobotB extends Robot implements Locomotion
{
public void robot ()
{
System.out.println ("robot B");
}
public void forward ()
{
System.out.println ("robot B : forward ()");
}
public void stop ()
{
System.out.println ("robot B : stop()");
}
public void reverse ()
{
System.out.println ("robot B : reverse()");
}
}
// subclassing of Robot class and implementation of both interfaces Locomotion and Sound
class RobotC extends Robot implements Locomotion, Sound
{
public void robot()
{
System.out.println ("robot C");
}
public void forward()
{
System.out.println ("robot C : forward()");
}
public void stop()
{
System.out.println ("robot C : stop()");
}
public void reverse()
{
System.out.println ("robot C : reverse()");
}
public void beep()
{
System.out.println ("robot C beep()");
}
}
// subclassing of Robot class
class RobotA extends Robot
{
public void robot()
{
System.out.println ("robot A");
}
}
// subclassing of RobotA class and implementation Sound
class RobotA1 extends RobotA implements Sound
{
public void robot()
{
System.out.println ("robot A1");
}
public void beep()
{
System.out.println ("robot Al beep()");
}
}
// subclassing of RobotB class and implementation Sound
class RobotB1 extends RobotB implements Sound
{
public void robot()
{
System.out.println ("robot B1");
}
public void beep ()
{
System.out.println ("robot B1 beep()");
}
}
//subclassing of RobotB class
class RobotB2 extends RobotB
{
public void robot()
{
System.out.println ("robot B2");
}
}
// subclassing of RobotC class
class RobotC1 extends Robot
{
public void robot()
{
System.out.println( "robot C1 " ) ;
}
}
class RobotDemo
{
public static void main ( String ad[] )
{
RobotA1 a = new RobotA1() ;
a.robot() ;
RobotB1 b1 = new RobotB1() ;
b1.robot() ;
RobotB2 b2 = new RobotB2() ;
b2.robot() ;
RobotC1 c = new RobotC1() ;
c.robot() ;
a.beep();
b1.beep();
b1.forward() ;
b1.reverse() ;
b1.stop() ;
}
}
0 Comments
Post a Comment