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.