Sunday 23 September 2012

Java Practical - 13



    Program Definition 13 ::

     The abstract Widget class has four concrete subclasses named Widget A, WidgetB, WidgetC and WidgetD.  Each of these four classes has a different mass in kilograms.  The mass of any WidgetA object is 4 kilograms.  The masses for the WidgetB, WidgetC and WidgetD classes are 1, 5 and 17 kilograms, respectively.  Each widget object has a string that identifies its color.  Create six different Widgets and store them in a one-dimension array.  Display the entries in the array and their total mass.




abstract class Widget
{
int weight;
String color;

Widget( int w , String c )
{
weight = w ; color = c ;
}
abstract public void display();
}

class WidgetA extends Widget
{

WidgetA( String c )
{
super( 4 , c);
}

public void display()
{
System.out.println( "Weight = " + weight + "\n color = " + color );
}
}

class WidgetB extends Widget
{
WidgetB( String c )
{
super( 1 , c );
}
public void display()
{
System.out.println( "Weight = " + weight + "\n color = " + color );
}

}

class WidgetC extends Widget
{
WidgetC( String c )
{
super( 5 , c );
}
public void display()
{
System.out.println( "Weight = " + weight + "\n color = " + color );
}

}

class WidgetD extends Widget
{
WidgetD( String c )
{
super( 17 , c );
}
public void display()
{
System.out.println( "Weight = " + weight + "\n color = " + color );
}

}


class TestWidget
{
public static void main( String args[] )
{
Widget w[] = new Widget[6];

w[ 0 ] = new WidgetA("yellow");
w[ 1 ] = new WidgetA("black");
w[ 2 ] = new WidgetB("violet");
w[ 3 ] = new WidgetC("red");
w[ 4 ] = new WidgetC("white");
w[ 5 ] = new WidgetD("blue");


int totalMass = 0;
for( Widget w1 : w)
{
w1.display();

totalMass = totalMass + w1.weight ;
}

System.out.println("\nTotal Mass is :: " + totalMass);
}
}


/*
D:\JAVA >java TestWidget
Weight = 4
 color = yellow
Weight = 4
 color = black
Weight = 1
 color = violet
Weight = 5
 color = red
Weight = 5
 color = white
Weight = 17
 color = blue
Total Mass is :: 36
*/


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