1.
Write Java methods to compute the future investment value at different
given interest rate for a specified number of years in 2 different bank.
2. Suppose a class 'A' has a static method to print "Parent". Its
subclass 'B' also has a static method with the same name to
print "Child". Now call this method by the objects of the two
classes. Also, call this method by an object of the parent class
refering to the child class i.e. A obj = new B()
3. Create an overloaded complex class to implement complex addition,
subtraction, multiplication, division, finding argument of the complex
number, initialization-using constructor, complex assignments etc.
4.
Write a superclass called Shape (as shown in the class diagram), which
contains:
Two instance variables color (String) and filled (boolean).
Two constructors: a no-arg (no-argument) constructor that initializes
the color to "green" and filled to true, and a constructor that
initializes the color and filled to the given values.
Getter and setter for all the instance variables. By convention, the
getter for a boolean variable xxx is called isXXX() (instead
of getXxx() for all the other types).
A toString() method that returns "A Shape with color of xxx and
filled/Not filled".
Write a test program to test all the methods defined in Shape.
Write two subclasses of Shape called Circle and Rectangle, as shown in the
class diagram.
The Circle class contains:
An instance variable radius (double).
Three constructors as shown. The no-arg constructor initializes the
radius to 1.0.
Getter and setter for the instance variable radius.
Methods getArea() and getPerimeter().
Override the toString() method inherited, to return "A Circle with
radius=xxx, which is a subclass of yyy", where yyy is the output of
the toString() method from the superclass.
The Rectangle class contains:
Two instance variables width (double) and length (double).
Three constructors as shown. The no-arg constructor initializes
the width and length to 1.0.
Getter and setter for all the instance variables.
Methods getArea() and getPerimeter().
Override the toString() method inherited, to return "A Rectangle with
width=xxx and length=zzz, which is a subclass of yyy", where yyy is
the output of the toString() method from the superclass.
Write a class called Square, as a subclass of Rectangle. Convince yourself
that Square can be modeled as a subclass of Rectangle. Square has no instance
variable, but inherits the instance variables width and length from its
superclass Rectangle.
Provide the appropriate constructors (as shown in the class diagram).
Hint:
public Square(double side) {
super(side, side); // Call superclass Rectangle(double, double)
}
Override the toString() method to return "A Square with side=xxx,
which is a subclass of yyy", where yyy is the output of
the toString() method from the superclass.
Do you need to override the getArea() and getPerimeter()? Try them
out.
Override the setLength() and setWidth() to change both
the width and length, so as to maintain the square geometry.
5. File [Link] contains a class that holds information about an athlete:
name, team, and uniform number. File [Link]
contains a skeletal program that uses the Player class to read in information
about two baseball players and determine whether or not they are the same
player.
a. Fill in the missing code in ComparePlayers so that it reads in two players
and prints "Same player" if they are the same, "Different players" if they are
different. Use the equals method, which Player inherits from the Object class,
to determine whether two players are the same. Are the results what you
expect?
b. The problem above is that as defined in the Object class, equals does an
address comparison. It says that two objects are the same if they live at the
same memory location, that is, if the variables that hold references to them
are aliases. The two Player objects in this program are not aliases, so even if
they contain exactly the same information they will be "not equal." To make
equals compare the actual information in the object, you can override it with
a definition specific to the class. It might make sense to say that two players
are "equal" (the same player) if they are on the same team and have the
same uniform number.
Use this strategy to define an equals method for the Player class. Your
method should take a Player object and return true if it is equal to the current
object, false otherwise.
Test your ComparePlayers program using your modified Player class. It
should give the results you would expect.
Hint:
// **********************************************************
// [Link]
//
// Defines a Player class that holds information about an athlete.
// **********************************************************
import [Link];
public class Player
{
private String name;
private String team;
private int jerseyNumber;
//-----------------------------------------------------------
// Prompts for and reads in the player's name, team, and
// jersey number.
//-----------------------------------------------------------
public void readPlayer()
{
Scanner scan = new Scanner([Link]);
[Link]("Name: ");
name = [Link]();
[Link]("Team: ");
team = [Link]();
[Link]("Jersey number: ");
jerseyNumber = [Link]();
}
}
// **************************************************************
// ComparePlayers
//
// Reads in two Player objects and tells whether they represent
// the same player.
// **************************************************************
import [Link];
public class ComparePlayers
{
public static void main(String[] args)
{
Player player1 = new Player();
Player player2 = new Player();
Scanner scan = new Scanner();
//Prompt for and read in information for player 1
//Prompt for and read in information for player 2
//Compare player1 to player 2 and print a message saying
//whether they are equal
}
}