Skip to main content
ICT
Lesson A4 - Object Behavior
 
Main Previous
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

LAB ASSIGNMENT A4.2 page 11 of 11

Rectangle

Background:

  1. It is always important to have a well-designed class before writing down any code. Having a class laid out on paper before writing the code allows programmers to see any design flaws before they have coded those flaws into their classes. Determining which classes are needed, what data those classes hold, and how those classes behave are the main objectives of OOP.

  2. The specifications of a class that models a rectangular shape would be:

Assignment:

  1. Implement a Rectangle class with the following properties.

    1. A Rectangle object is specified in the constructor with the left and right edges of the rectangle at x and x + width. The top and bottom edges are at y and y + height.

    2. A method getPerimeter calculates and returns the perimeter of the Rectangle.

    3. A method getArea calculates and returns the area of the Rectangle.

    4. A method draw displays a new instance of a Rectangle object. Refer to Handout A1.1 DrawingTool Specifications, for details on DrawingTool methods.

  2. Try your rectangle with both the default constructor and with a constructor that can take the x and y coordinates, the length of the rectangle, and the width. Here are some sample constructor calls:

    Rectangle rectA = new Rectangle();
    Rectangle rectB = new Rectangle(0, -80, 400, 160);
    Rectangle rectC = new Rectangle(100, -100, 20, 300);

  3. A circle could be constructed and stored in a very similar manner to a rectangle. Write a class that does the same things as your rectangle class, but modified to apply to a circle. Note: For all values of Pi, you may simply use the value 3.14.

 

 

Main Previous
Contact
 © ICT 2006, All Rights Reserved.