Wednesday, 3 October 2012

Java Practical - 23

0 comments



Program Definition 23 ::


The abstract class Tent has concrete subclasses named TentA, TentB, TentC and TentD. The Waterproof interface defines no constants and declares no methods. Implement it in any two of above mentioned four classes. Define all of these classes, and implement the interfaces as specified. Create one instance of each class. Then display all waterproof tents. Place this code in a package named tents.
 

//create tents folder and then write below code



package tents;
abstract class Tent
{
}
class TentA extends Tent implements WaterProof
{}

class TentB extends Tent implements WaterProof
{}

class TentC extends Tent
{}

class TentD extends Tent
{}

interface WaterProof
{}

class Prog23
{
public static void main( String args[] )
{
Tent t[] = new Tent[ 4 ];
t[ 0 ] = new TentA();
t[ 1 ] = new TentB();
t[ 2 ] = new TentC();
t[ 3 ] = new TentD();

for ( Tent ti : t )
{
if ( ti instanceof WaterProof )
{
Class cls = ti.getClass();
System.out.println(cls.getName());
}
}
}
}

/*

D:\JAVA>javac tents/*.java

D:\JAVA>java tents.Prog23
tents.TentA
tents.TentB

*/

Tuesday, 2 October 2012

Java Practical - 22

0 comments



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
*/

Monday, 1 October 2012

Java Practical - 21

0 comments



Program Definition 21 ::


The abstract class Animal has abstract subclasses named Bird and Reptile.  Classes Dove, Eagle, Hawk, Penguin and Seagull extend Bird.  Classes Rattlesnake and Turtle extend Reptile.  The ColdBlooded interface defines no constants and declares no methods.  It is implemented by Reptile.  The OceanDwelling interface also defines no constants and declares no methods.  It is implemented by the Penguin, Seagull and Turtle classes.  Define all of these classes and implement the interface as specified.  Create one instance of each class.  Then display all ColdBooded animals and all OceanDwelling animals.
 



abstract class Animal
{
}

interface ColdBlooded
{
}
interface OceanDwelling
{
}

abstract class Bird extends Animal
{
}

abstract class Reptile extends Animal implements ColdBlooded
{
}

class Dove extends Bird
{}
class Eagle extends Bird
{}
class Hawk extends Bird
{}
class Penguin extends Bird implements OceanDwelling
{}
class Seagull extends Bird implements OceanDwelling
{}


class RattleSnake extends Reptile
{}
class Turtle extends Reptile implements OceanDwelling
{}


class Prog21
{
public static void main( String args[] )
{

Animal[] animal = new Animal[7];

animal[ 0 ] = new Dove();
animal[ 1 ] = new Eagle();
animal[ 2 ] = new Hawk();
animal[ 3 ] = new Penguin();
animal[ 4 ] = new Seagull();
animal[ 5 ] = new RattleSnake();
animal[ 6 ] = new Turtle();

for ( Animal ai : animal )
{
if ( ai instanceof ColdBlooded )
System.out.println(ai.getClass() + " is ColdBlooded" );
if ( ai instanceof OceanDwelling )
System.out.println(ai.getClass() + " is OceanDwelling" );
}
}
}

/*
D:\MCA\JAVA >javac Prog21.java

D:\MCA\JAVA >java Prog21
class Penguin is OceanDwelling
class Seagull is OceanDwelling
class RattleSnake is ColdBlooded
class Turtle is ColdBlooded
class Turtle is OceanDwelling
*/
 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT