Assignment #5 - Chapter 6 - Classes

Due: Thursday, June 19

From chapter 6, do programming exercise 6.1 (page 260), with the modified instructions below.


Filename should be

Note that this filename starts with a capital letter. Please make your filename matches mine exactly.

Modifications to 6.1 Instructions

  1. Instead of the private instance variable color, create a private data variable called pattern, of type char. Do not make this a static variable.
  2. In the constructor with 3 parameters, change the last parameter to a char parameter, and use this to allow the pattern variable to be specified on the object's creation. The default pattern should be '#'.
  3. Instead of the methods getColor and setColor, create methods called getPattern and setPattern. The getPattern method should return the pattern character stored in an object (accessor method), and the setPattern method should take in a char parameter, which will specify a new pattern. (These are also not static methods).
  4. Add to your class a method called findPerimeter, which computes and returns the perimeter of the rectangle. This method does not need any parameters.
  5. Add to your class a method called draw (no parameters, returns no value). The draw method should print the rectangle to the screen using the pattern character. For example, if you have a rectangle with a height of 4 and a width of 10, and the pattern character is set to #, then the draw method would print the following:
      ##########
      ##########
      ##########
      ##########
    
    Since the width and height properties are stored as double variables, the draw method will need to estimate the drawing by rounding the height and width to the nearest integer. The rounding should not change the stored values of height and width -- only use the rounding to determine the drawing. Example: If the rectangle is 3.5 in height and 6.2 in width, then the draw method should print out a 4 x 6 rectangle. (You may use the Math class).
  6. In the client program (the main method) that you write, do the following so that it matches the output in the sample run shown below:
  7. Your private data should match the names given in the problem, but you many change the variable names of the parameters in your methods if you like

Sample Output

** First rectangle:

#

Area = 1.0
Perimeter = 4.0

** Second rectangle:

$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$

Area = 160.0
Perimeter = 56.0

Setting new rectangle properties

** First rectangle:

Width = 10.5
Height = 6.49
Pattern = !

!!!!!!!!!!!
!!!!!!!!!!!
!!!!!!!!!!!
!!!!!!!!!!!
!!!!!!!!!!!
!!!!!!!!!!!

Area = 68.145
Perimeter = 33.980000000000004

** Second rectangle:

Width = 13.8
Height = 5.2
Pattern = @

@@@@@@@@@@@@@@
@@@@@@@@@@@@@@
@@@@@@@@@@@@@@
@@@@@@@@@@@@@@
@@@@@@@@@@@@@@

Area = 71.76
Perimeter = 38.0

Compiling

Remember that the compile command is "javac", at the unix command prompt. Compile your code on program.cs.fsu.edu, and then run your programs with the "java" command.

Submitting:

Submit programs through the submission web page.