Friday 5 October 2012

GTU List of Java Programs

0 comments


GTU Java program Definitions ::



1. Write a simple java application to print a pyramid with 5 lines. The first line has one character, 2nd line has two characters and so on. The character to be used in the pyramid is taken as a command line argument.

2 Write a class, with main method, which declares floating point variables and observe the output of dividing the floating point values by a 0, also observe the effect of assigning a high integer value (8 digits and above) to a float and casting it back to int and printing.

3. Write a class called Statistics, which has a static method called average, which takes a one-dimensional array for double type, as parameter, and prints the average for the values in the array. Now write a class with the main method, which creates a two-dimensional array for the four weeks of a month, containing minimum temperatures for the days of the week(an array of 4 by 7), and uses the average method of the Statistics class to compute and print the average temperatures for the four weeks.

4. Define a class called Product, each product has a name, a product code and manufacturer name. Define variables, methods and constructors, for the Product class. Write a class called TestProduct, with the main method to test the methods and constructors of the Product class.

5. Define a class called CartesianPoint, which has two instance variables, x and y. Provide the methods getX() and getY() to return the values of the x and y values respectively, a method called move() which would take two integers as parameters and change the values of x and y respectively, a method called display() which would display the current values of x and y. Now overload the method move() to work with single parameter, which would set both x and y to the same values, .
Provide constructors with two parameters and overload to work with one parameter as well. Now define a class called TestCartesianPoint, with the main method to test the various methods in the CartesianPoint class.

6. Define a class called Triangle, which has constructor with three parameters, which are of type Cartesian Point, defined in the exercise 6. Provide methods to find the area and the perimeter of the Triangle, a method display() to display the three Cartesian Points separated by ':' character, a method move() to move the first Cartesian Point to the specified x, y location, the move should take care of relatively moving the other points as well, a method called rotate, which takes two arguments, one is the Cartesian Point and other is the angle in clockwise direction. Overload the move method to work with Cartesian Point as a parameter. Now define a class called TestTriangle to test the various methods defined in the Triangle class.

7. Override the toString, equals and the hash Code methods of the classes Triangle defined in exercises 5 and 6 above, in appropriate manner, and also redefine the display methods to use the toString method.

8. Define an abstract class called Polygon. Provide a constructor which takes an array of Cartesian Point as parameter. Also provide method called perimeter, which calculates and returns the perimeter of the Polygon. Declare abstract method area for this class. Also define a method called move, which takes two parameters x and y to specify the destination for the first point of the Polygon, and overload to make it work for Cartesian Point as a parameter. Now update the classes Triangle and Rectangle in the exercise 8 above, to be a subclass of the Polygon class. Write appropriate class with main method to test the polymorphism in the area method.

9. Write an application that defines a Sphere class with three constructors.  The first form accepts no arguments.  It assumes the sphere is centered at the origin and has a radius of one unit.  The second form accepts one double value that represents the radius of the sphere.  It assumes the sphere is centered at the origin.  The third form accepts four double arguments.  These specify the coordinates of the center and the radius.  Provide two instance methods to this class.  The first named move(), which takes three double parameters that are new values for the co-ordinates of the center.  The second is named scale(), which takes one double parameter that is used to scale the radius.  Demonstrate these methods.

10. Write a program that illustrates method overriding.  Class Bond is extended by ConvertibleBond.  Each of these classes defines a display() method that outputs the string “Bond” or  “ConvertibleBond”, respectively.  Declare an array to hold six Bond objects.  Initialize the elements of the array with a mix of Bond and ConvertibleBond objects.  Execute a program loop to invoke the display() method of each object.

11. The abstract Airplane class has three subclasses named B747, B757 and B767.  Each airplane type can transport a different number of passengers.  Each airplane object has a unique serial number.  Write an application that declares class hierarchy.  Instantiate several types of airplanes and display them.  Override the toString() method of Object to return a string with the type, serial number and capacity.

12. The abstract Monster class has three concrete subclasses named Vampire, Werewolf and Zombie.  Create six different monsters of various types and store them in a one-dimensional array.  Create a loop that displays the type of each monster.

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.

14. The abstract Fruit class has four subclasses named Apple, Banana, Orange and Strawberry.  Write an application that demonstrates how to establish this class hierarchy.  Declare one instance variable of type string that indicates the color of a fruit.  Create and display instances of these object. Override the toString() method of Object to return a string with the name of the fruit and its color.

15. Define one abstract class named Mammal.  Bear, Elephant, Horse and Lion are concrete classes which extends Mammal.  Define interface Vehicle which declares drive() method and this interface is implemented by the Elephant and Horse classes.  Write main() method which creates and initializes an array of four Mammal objects.  Call drive() method for each object which has implemented Vehicle interface.

16. Interface Material defines a set of String constants for various materials.  Abstract class MaterialObject has one instance variable named material of type String.  These records the material used to construct the object.  Classes Ball, Coin and Ring extend MaterialObject .  The constructor initializes the material variable.  Class MaterialObjects instantiates these three classes.  A different material is passed to each constructor.  Display material of each object.

17. Interface A declares a display() method.  Classes C1, C2 and C3 implement that method.  The main() method declares variable a of type A.  Therefore, it can hold a reference to any object of class C1, C2 and C3 and invoking the display() method relative to the interface reference.

18. Interface LuminousObject declares lightOff() and lightOn() methods.  Class SolidObject is extended by Cone and Cube.  Class LuminousCone extends Cone and implements LuminousObject.  Class LuminousCube extends Cube and implements LuminousObject.  Instantiate the LuminousCone and LuminousCube classes.  Use interface reference to refer to those objects.  Invoke the methods of the LuminousObject interface via the interface reference.

19. The abstract class Fish declares one abstract method named display().   It also has two abstract subclasses named FreshWaterFish and SaltWaterFish.  Define three concrete subclasses named Trout, Flounder and Tuna.  Trout extends FreshWaterFish and Flounder and Tuna both extends SaltWaterFish.  Write a main() method which creates an array of type Fish.  Instantiate and assign different types of Fish to the elements of the array.  Display the name of fish which is of type SaltWaterFish using display() method.

20. Declare interfaces I1 and I2.  Interface I3 extends both of these interfaces.  Also declare interface I4.  Class X implements I3.  Class W extends X and implements I4.  Create an object of class W.  Use the instanceof operator to test if that object implements each of the interfaces and is of type X.

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.

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.

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.

24. Define one class E in package epack.  In class E, three variables are defined of access modifiers protected, private & public. Define class F in package fpack which extends E and write display() method which access variables of class E.  Define class G in package gpack which has one method display() in that create one object of class E and display its variables.  Define class ProtectedDemo in package hpack in which write main() method.  Create objects of class F and G and class display method for both these objects.

25. Make the class CartesianPoint, belong to a package called edu. gtu. geometry, the classes Polygon, Triangle and Rectangle belong to the package edu. gtu. geometry. shapes and the classes TestCartesianPoint, TestTriangle belong to the package edu. gtu.test. Use appropriate access specifiers for the classes and the members of the classes defined in the earlier exercises. Now onwards all the classes must be defined in a package.


Java Practical - 25

0 comments




Program Definition 25 ::



Make the class CartesianPoint, belong to a package called edu. gtu. geometry, the classes Polygon, Triangle and Rectangle belong to the package edu. gtu. geometry. shapes and the classes TestTriangle belong to the package edu. gtu.test. Use appropriate access specifiers for the classes and the members of the classes defined in the earlier exercises. Now onwards all the classes must be defined in a package.





// save this code in edu/gtu/geometry folder
package edu.gtu.geometry;
//program 25

public class CartesianPoint
{
public int x, y;
public CartesianPoint(int a)
{
x = y = a;
}
public CartesianPoint(int a , int b)
{
x = a; y = b;
}

public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void move(int a)
{
x = y = a;
}
public void move(int a, int b)
{
x = a; y = b;
}

public void display()
{
System.out.println("("+ x +", " + y + ")");
}
}



// save this code in edu/gtu/geometry/shapes folder
package edu.gtu.geometry.shapes;
import edu.gtu.geometry.*;
public class Triangle
{
CartesianPoint cp[];
public Triangle(CartesianPoint c[])
{
cp = new CartesianPoint[ c.length ];
for(int i=0;i<c.length;i++)
{
cp[i]=c[i];
}
}

public double area()
{
return ((Math.abs(cp[0].getX()*(cp[1].getY()-cp[2].getY()) + cp[1].getX()*(cp[2].getY()-cp[0].getY()) + cp[2].getX()*(cp[0].getY()-cp[1].getY()))/2));
}
public void display()
{
for ( int i = 0 ; i < 3 ; i++ )
{
cp[i].display();
}
}
}


// save this code in edu/gtu/test folder

package edu.gtu.test;
import edu.gtu.geometry.*;
import edu.gtu.geometry.shapes.*;

class TestTriangle
{
public static void main(String args[])
{
CartesianPoint c[] = new CartesianPoint[ 3 ];
c[0] = new CartesianPoint(5,5);
c[1] = new CartesianPoint(10,5);
c[2] = new CartesianPoint(10,10);

Triangle T1 = new Triangle(c);
T1.display();
System.out.println("Area :: " + T1.area() );
}
}

/*
D:\JAVA >javac edu/gtu/test/*.java

D:\JAVA >java edu.gtu.test.TestTriangle
(5, 5)
(10, 5)
(10, 10)
Area :: 12.0
*/

Thursday 4 October 2012

Java Practical - 24

0 comments




Program Definition 24 ::

Define one class E in package epack.  In class E, three variables are defined of access modifiers protected, private & public. Define class F in package fpack which extends E and write display() method which access variables of class E.  Define class G in package gpack which has one method display() in that create one object of class E and display its variables.  Define class ProtectedDemo in package hpack in which write main() method.  Create objects of class F and G and class display method for both these objects.




// save this file in epack folder
package epack;
public class E
{
public int publicVar = 10;
protected int protectedVar = 20;
private int privateVar = 30;

}


// save this file in fpack folder
package fpack;
import epack.*;

public class F extends E
{
public void display()
{
System.out.println("from Class F : package fpack");
//System.out.println("private" + privateVar); // not accessible
System.out.println("public" + publicVar);
System.out.println("protected" + protectedVar);

}
}

// save this file in gpack folder
package gpack;
import epack.*;

public class G
{
E eObj = new E();

public void display()
{
System.out.println("from Class G : package gpack");
//System.out.println("private" + eObj.privateVar); // not accessible
System.out.println("public" + eObj.publicVar);
//System.out.println("protected" + eObj.protectedVar); // not accessible
}
}

// save this file in hpack folder
package hpack;
import gpack.*;
import fpack.*;

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

F fObj = new F();
G gObj = new G();

fObj.display();
gObj.display();
}
}

/*

D:\JAVA>javac epack/*.java

D:\JAVA>javac fpack/*.java

D:\JAVA>javac gpack/*.java

D:\JAVA>javac hpack/*.java

D:\JAVA>java hpack.ProtectedDemo
from Class F : package fpack
public10
protected20
from Class G : package gpack
public10
*/

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