CSCI 1101 – Winter 2012
Laboratory No. 4
Name:
Student ID:
Date of Laboratory:
Time of Laboratory:
Names of TAs:
Signature:
This lab focuses on developing object-oriented programs with inheritance.
Your task is write and compile the class file followed by the tester program. Include all the source codes for each program. Please hand in your lab report to the TA before you leave.
Exercise 1: Quick revision – Implement the Shape class and the Circle class (derived from the Shape class) that we discussed in the lectures.
The Shape class has area as its instance variable, and methods to set and get the area in addition to a default constructor.
The Circle class has radius as its instance variable, a constructor that accepts the radius and sets the radius as well as the area, a method getRadius and a toString method.
Exercise 2: The following figure shows a UML diagram in which the class Student is inherited from the class Person.
Implement the two classes. Write a demo program that tests all the methods in the Student class as well as the inherited methods.
Note: In the toString method in the Student class, use the method getName() to get the name.
Can you modify the above toString method so that it uses the toString method from the Person class? Hint: keyword super.
Exercise 3:Create a class called SchoolKid that is the base class for children at a school. It should have attributes for the child’s name and age, the name of the child’s teacher, and a greeting. It should have appropriate accessor and mutator methods for each of the attributes.
Derive a class ExaggeratingKid from SchoolKid, as described in the previous exercise. The new class should override the accessor method for the age, reporting actual age plus 2. It also should override the accessor method for the greeting, returning the child’s greeting concatenated with the words “I am the best”.
Exercise 4: Implement the following UML diagram
Note: the constructor in the Dictionary class sets the number of pages and number of definitions. The class computeRatio returns the number of definitions per page.
Test the classes.
The following is a class called ShapeBasics that can be used for drawing simple shapes on the screen using keyboard characters. This class will draw an asterisk on the screen as a test. It is not intended to create a “real” shape, but rather to be used as a base class for other classes of shapes.
public class ShapeBasics
{
//offset indicates how far it is indented from the left
//edge of the screen
private int offset;
public ShapeBasics()
{
offset=0;
}
public ShapeBasics(int theOffset)
{
offset = theOffset;
}
public void setOffset(int newOffset)
{
offset = newOffset;
}
public int getOffset()
{
return offset;
}
//static method skipLines skips the given number of lines
//down from the current one
public static void skipLines(int lineNumber)
{
for(int count=0;count<lineNumber;count++)
System.out.println();
}
//static method skipSpaces that skips the given number of spaces
public static void skipSpaces(int number)
{
for(int count = 0; count<number; count++)
System.out.print(' ');
}
//method drawHere draws the shape beginning at the current line
public void drawHere()
{
skipSpaces(offset);
System.out.println('*');
}
}
a)Draw a class LineShape that extends the ShapeBasics class. This class should have the following:
- A no args constructor
- A method to set the given offset
- A method drawVertical that draws a vertical line of a given length starting at the offset
- A method drawHorizontal that draws a horizontal line of a given length starting at the offset
The class does not have any instance variables.
b)Draw a class RectangleShape that extends the LineShape class. The class should have the following:
- Instance variables height and width
- Constructor that sets the offset, height and width
- Method drawHere that draws the rectangle starting at the given offset and of dimensions width and height
- Method drawSides that draws the two sides of the rectangle. This method is used by the drawHere method.
Test all the three classes.
Bonus Challenge: Define two derived classes from the ShapeBasics class. The two classes should be called RightArrow and LeftArrow and they will draw arrows that point right and left, respectively. For example, the right arrow is shown below:
*
* *
* *
********************* *
* *
* *
*
The size of the arrow is determined by two numbers, one for the length of the “tail” and one for the width of the arrowhead. (the width is the length of the vertical base). The arrow shown here has a length of 21 and a width of 7. The width of the arrowhead cannot be an even number, so your constructors and mutator methods should check to make sure that it is always odd. You can assume that the width of the base of the arrowhead is at least 3. Write a test program for each class.
1