Tuesday, 2 October 2012

Java Practical - 22




Program Definition 22 ::


The abstract class Robot has concrete subclasses named RobotA, RobotB and RobotC.  Class RobotA1 extends RobotA.  Classes RobotB1 and RobotB2 extend RobotB.  Class RobotC1 extends RobotC.  The Locomotion interface declares three methods named forward(), reverse() and stop().  It is implemented by classes RobotB and RobotC.  The Sound interface declares one method named beep().  It is implemented by classes RobotA1, RobotB1 and RobotC.  Define all of these classes and implement the interfaces as specified.  Create one instance of each class. Then invoke the beep() method of all objects that are of type Sound. Also invoke the stop() method of all objects that are of types Locomotion.
 



abstract class Robot
{
}

class RobotA extends Robot
{
}

class RobotB extends Robot implements Locomotion
{
public void forward()
{
System.out.println(" RobotB >> forward ");
}
public void reverse()
{
System.out.println(" RobotB >> reverse ");
}
public void stop()
{
System.out.println(" RobotB >> stop ");
}
}

class RobotC extends Robot implements Locomotion, Sound
{
public void forward()
{
System.out.println(" RobotC >> forward ");
}
public void reverse()
{
System.out.println(" RobotC >> reverese ");
}
public void stop()
{
System.out.println(" RobotC >> stop ");
}
public void beep()
{
System.out.println(" RobotC >> beep ");
}
}

class RobotA1 extends RobotA implements Sound
{
public void beep()
{
System.out.println(" RobotA1 >> beep ");
}

}

class RobotB1 extends RobotB implements Sound
{
public void beep()
{
System.out.println(" RobotB1 >> beep ");
}

}

class RobotB2 extends RobotB
{}

class RobotC1 extends RobotC
{}

interface Locomotion
{
public void forward();
public void reverse();
public void stop();
}

interface Sound
{
public void beep();
}

class Prog22
{
public static void main( String args[] )
{
Robot[] r = new Robot[7];

r[ 0 ] = new RobotA();
r[ 1 ] = new RobotB();
r[ 2 ] = new RobotC();
r[ 3 ] = new RobotA1();
r[ 4 ] = new RobotB1();
r[ 5 ] = new RobotB2();
r[ 6 ] = new RobotC1();

//beep from Sound implemented classes
for( Robot ri : r )
{
if ( ri instanceof Sound )
{
Sound s = (Sound) ri;
s.beep();
}
if ( ri instanceof Locomotion )
{
Locomotion l = (Locomotion) ri ;
l.stop();
}
}
}
}

/*

D:\MCA\JAVA\Practical Assigment-2>javac Prog22.java

D:\MCA\JAVA\Practical Assigment-2>java Prog22
 RobotB >> stop
 RobotC >> beep
 RobotC >> stop
 RobotA1 >> beep
 RobotB1 >> beep
 RobotB >> stop
 RobotB >> stop
 RobotC >> beep
 RobotC >> stop
*/


Kindly Bookmark and Share it:

0 comments :

Post a Comment

Any Query ? any suggestion ? comment here

 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT