Laboratory No. 4 - Faculty of Computer Science

advertisement
CSCI 1101 – Winter 2012
Laboratory No. 4
Answers
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.
public class Shape
{
private double area;
//default constructor
public Shape()
{
}
//method to set the area
public void setArea(double a)
{
area = a;
}
//method to get the area
public double getArea()
{
return area;
}
}
public class Circle2 extends Shape
{
private double radius;
public Circle2()
{
}
public void setRadius(double r)
{
radius = r;
}
public double getRadius()
{
return radius;
}
public String toString()
{
1
return ("Radius: " + radius + "\nArea: " + getArea());
}
}
import java.util.Scanner;
public class CircleDemo
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the radius: ");
double r = keyboard.nextDouble();
Circle c = new Circle(r);
System.out.println(c);
}
}
Exercise 2: The following figure shows a UML diagram in which the class Student is inherited from the
class Person.
Person
- name: String
+ Person()
+ setName(String newName):
void
+ getName(): String
+ toString(): String
+ hasSameName(Person
anotherPerson)): boolean
Student
- studentNumber: int
+ Student()
+ reset(String newName, int
newNumber): void
+ getStudentNumber(): int
+ setStudentNumber(int n):
void
+ toString(): String
+ equals(Student
anotherStuent): boolean
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.
2
Can you modify the above toString method so that it uses the toString method from the Person class? Hint:
keyword super.
public class Person
{
private String name;
public Person()
{
}
public void setName(String newName)
{
name = newName;
}
public String getName()
{
return name;
}
public String toString()
{
return ("Name: " + name);
}
public boolean hasSameName(Person anotherPerson)
{
if (this.name.equals(anotherPerson.getName()))
return true;
else
return false;
}
}
public class Student extends Person
{
private int studentNumber;
public Student()
{
}
public void reset(String newName, int newNumber)
{
setName(newName);
studentNumber = newNumber;
}
public int getStudentNumber()
{
return studentNumber;
}
public void setStudentNumber(int n)
{
studentNumber = n;
3
}
public String toString()
{
return ("Name: " + getName() + "\nStudent Number: " + studentNumber);
}
public boolean equals(Student anotherStudent)
{
if ((this.studentNumber == anotherStudent.getStudentNumber())
&&(this.hasSameName(anotherStudent)))
return true;
else
return false;
}
}
public class StudentDemo
{
public static void main(String[] args)
{
Student joe1 = new Student();
joe1.reset("Joe", 123);
Student joe2 = new Student();
joe2.reset("Joe", 345);
System.out.println(joe1);
System.out.println(joe2);
if (joe1.hasSameName(joe2))
System.out.println("Same name");
if (joe1.equals(joe2))
System.out.println("Wow!");
else
System.out.println("Different IDs");
}
}
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”.
public class SchoolKid
{
private String name;
private int age;
private String teach;
private String greeting;
4
public SchoolKid(String n, int a, String t, String g)
{
name = n;
age = a;
teach = t;
greeting = g;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getTeach()
{
return teach;
}
public String getGreeting()
{
return greeting;
}
public String toString()
{
return ("Name: " + name + "\nAge: " + age + "\nTeacher: " + teach + "\n" + greeting);
}
}
public class ExaggeratingKid extends SchoolKid
{
public ExaggeratingKid(String n, int a, String t, String g)
{
super(n, a, t, g);
}
public int getAge()
{
return (super.getAge()+2);
}
public String getGreeting()
{
return (super.getGreeting() + "\nI am the best!");
}
public String toString()
{
5
return ("Name: " + getName() + "\nAge: " + getAge() + "\nTeacher: " + getTeach() + "\n"
+ getGreeting());
}
}
public class KidDemo
{
public static void main(String[] args)
{
SchoolKid joe = new SchoolKid("Joe", 10, "Srini", "Hello!");
System.out.println(joe);
ExaggeratingKid axl = new ExaggeratingKid("Axl", 10, "Srini", "Hello!");
System.out.println(axl);
}
}
Exercise 4: Implement the following UML diagram
Book
- numPages: int
+ Book(int n)
+ getPages(): int
Dictionary
- numDefinitions: int
+ Dictionary(int p, int d)
+ setDefinitions(int n):
void
+ getDefintions(): int
+computeRatio(): int
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.
6
public class Book
{
private int numPages;
public Book(int n)
{
numPages = n;
}
public int getPages()
{
return numPages;
}
}
public class Dictionary extends Book
{
private int numDefinitions;
public Dictionary(int p, int d)
{
super(p);
numDefinitions = d;
}
public void setDefinitions(int d)
{
numDefinitions = d;
}
public int getDefinitions()
{
return numDefinitions;
}
public int computeRatio()
{
return numDefinitions/getPages();
}
}
public class DictionaryDemo
{
public static void main(String[] args)
{
Dictionary webster = new Dictionary(1500, 52500);
System.out.println("Number of pages: " + webster.getPages());
System.out.println("Number of definitions: " + webster.getDefinitions());
System.out.println("Definitions per page: " + webster.computeRatio());
}
}
7
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
8



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.
public class LineShape extends ShapeBasics
{
public LineShape()
{
}
public void setLine(int o)
{
setOffset(o);
}
public void drawVertical(int length)
{
for(int count=0;count<length; count++)
{
skipSpaces(getOffset());
System.out.println('*');
}
}
public void drawHorizontal(int length)
{
skipSpaces(getOffset());
for(int count=0;count<length; count++)
System.out.print('*');
}
}
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.
public class RectangleShape extends LineShape
{
private int height;
private int width;
9
public RectangleShape(int off, int h, int w)
{
setOffset(off);
height = h;
width = w;
}
public void drawHere()
{
drawHorizontal(width);
System.out.println();
drawSides();
drawHorizontal(width);
}
public void drawSides()
{
for(int count=0;count<height;count++)
{
skipSpaces(getOffset());
System.out.print('*');
skipSpaces(width-2);
System.out.print('*');
System.out.println();
}
}
}
public class ShapeBasicsDemo
{
public static void main(String[] args)
{
//ShapeBasics x = new ShapeBasics(10);
//ShapeBasics.skipLines(5);
//x.drawHere();
//LineShape l = new LineShape();
//l.setLine(5,7);
//l.drawVertical();
//l.drawHorizontal();
RectangleShape r = new RectangleShape(5, 6, 7);
r.drawHere();
}
}
10
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.
11
Download