Lab 4: Expressions, Conditionals and Objects

Copyright RIT 2004
$Id: writeup.xml,v 1.7 2009/09/21 13:37:43 vcss231 Exp vcss231 $


Goal

The primary purpose of this lab is to introduce you to working with and solving problems using objects. Along the way you will get the chance to practice conditionals, write some expressions, and use some of the features of the Scanner and Math classes.


Team Setup

You are to work on this lab completely on your own.

Overview

Objectives

Ask Dr. Tiger

Pre - Lab Work

  1. Review your course notes and Chapter 3 of Liang.

  2. Look up the formulas for computing the area and perimeter of a circle, triangle and rectangle.

  3. Review the methods provided by the Math class.

  4. Review the expression exercises found by clicking here (http://www.cs.rit.edu/~vcss231/Exercises/_Expressions) .

In-Lab Activities

Jerry is a contractor who installs house siding. He finds that he consistently overestimates the amount of siding needed for a job and this results in lost revenues. Hal, a local programmer, offered to write a simple program to estimate the amount of siding needed for the job. Jerry doesn't think the program works, although Hal claims that the basics are working. Jerry knows that you are going to school and learning how to program and turns to you for help.

Jerry is turning to you for help now but you know he'll probably need future updates to the code. Therefore, you need to make sure to program for any future issues that may arise. A pragmatic programmer knows what to do in this type of situation:

Activity #1 : Write test cases to test bad code

Hal calculated the number of square feet of siding needed by looking at the area of each side of the house. He assumed that opposite sides were the same size and that all houses only have four sides. He then used this area to calculate the number of ( width * height ) siding boards that would fit within that area. Note that measurements are read in as doubles. In order to do this, Hal created a Rectangle class and used it to build the SidingCalculator. Both the sides of the house and the siding boards themselves are rectangles.

Please download the file for the current lab here (/~vcss231/pub/lab04/lab4.jar) and unpack the files (jar xvf lab4.jar) in your labs directory. Try compiling and running the SidingCalculator yourself, remembering that Jerry believes that it doesn't work correctly.

You need to come up with a plan to figure out what's wrong with the program. You could just "wing it" and try a couple of inputs, but that might not find all the problems with the program. Your goal is develop a set of test cases to expose the defects. Here is a hint: the error is in the SidingCalculator class. You should create a file called test_cases that contains a list of inputs for the SidingCalculator and the expected output values (the number of siding boards needed). The input and output values should be labeled so that anyone reviewing your test cases will be able to understand them.

When you find the problem, fix the code and any style issues that might have contributed to the creation of the problem.

How To Submit

As part of your submission, you need to create a text file called report where you answer the following questions:

After you are convinced that your program works correctly, submit it using the following command:


try grd-231 lab4-1 test_cases SidingCalculator.java report 
        

try will test your code; however, it will not give you feedback on the results of those tests. It is up to you to write correct code and verify that your program works as expected.

Activity #2 : You don't side over windows

Jerry is impressed with your work. He mentions that the current program does not consider that you don't install siding over the windows. He asks you to modify the program to allow the user to input the number of windows in the house as well as the size of a window. To keep things simple, we'll asssume that all windows will be the same size.

After all questions about the length and width of the sides and siding are asked, the following questions should appear:

How many windows does this house have?

What is the height of each window?

What is the width of each window?
      

For the purposes of this program you may assume that the data provided to your program is valid, i.e., you do not need to check the values the user entered to make sure they make sense. All measurements are given in feet and may be integers or real numbers.

These questions will be answered by the person running your program. You will use the inputs to revise the calculations, excluding the window area. If the number of windows entered is 0, you should not prompt for window dimensions and no additional calculations need to be done. (You should use a conditional statement to accomplish this.) Obviously, the number of boards needed for a house with windows will be less than the number of boards needed by the same house without windows.

Create a new subdirectory in which to do this activity. Copy your final version of SidingCalculator.java from the previous activity as well as Rectangle.class into the new directory and do the work for this activity there. In this way, if you need to go back and make corrections for the previous activity, you will not need to start all over!

You must test your updated program to make sure it is producing correct results. To help you with your testing, here are some sample input/output numbers.

House Dimensions Board Size Number of Windows Window Height Window Width Number of Boards Needed
10 x 20, 10 x 15 1 x 5 5 2 2 136.0
15 x 10, 15 x 20 8 x 0.5 3 3 2 220.5
10.5 x 10.7, 6.5 x 10.5 8 x 0.5 2 2.5 2.5 87.175

A sample run of your program should look like the following:

 
Welcome to the Simple Siding Helper. Please 
answer the following questions to help estimate 
the amount of siding you will need to side 
your house. Please note that the output 
is a minimum estimate.

Enter the length of side 1: 10

Enter the width of side 1: 20

Enter the length of side 2: 10

Enter the width of side 2: 15

Enter the length of a single board of siding: 1

Enter the width of a single board of siding: 5

How many windows does this house have? 5

What is the height of each window? 2

What is the width of each window? 2


The number of siding boards you need is
136.0. Remember to add 
10-20% onto this number as it does not allow
for different cuts or mistakes.
	

How To Submit

After you are sure that your program produces the correct results, submit it using the following command:

try grd-231 lab4-2 SidingCalculator.java

Activity #3 : Not all windows are rectangular

Jerry has recently signed a contract with a customer who only has circular windows. He wants you to change the program to deal with either rectangular or circular windows.

The SidingCalculator will still ask for the number of windows to determine if any further calculations are needed. If the house has any windows, the SidingCalculator will ask the following question:

 Would you like to have circular windows (y or n)? 

The input should be read in with the Scanner class methods (see lab 3 if you have any questions) and the y/n entered can be either upper or lower case. If neither y or n are entered, the program will assume rectangular windows and proceed as discussed above. If y is entered, the program will then ask the following:

 What is the radius of each window? 

The program will then use the Circle class to calculate and subtract the area of the circular windows from the siding area.

Create a new subdirectory in which to do this activity. Copy your final version of SidingCalculator.java from the previous activity as well as Circle.class and Rectangle.class into the new directory and do the work for this activity there. In this way, if you need to go back and make corrections for a previous activity, you will not need to start all over!

You must test your program to make sure it is producing the correct results. To help with your testing, the table below provides some house specifications and the resulting number of boards needed to side the house.

House Dimensions Board Size Number of Windows Radius Number of Boards Needed
10 x 10, 10 x 20 1 x 5 4 1.5 114.34513322353837
10 x 20, 10 x 30 8 x 0.5 9 1 242.93141652942296

A sample run of your program should look like:

 
Welcome to the Simple Siding Helper. Please 
answer the following questions to help estimate 
the amount of siding you will need to side 
your house. Please note that the output 
is a minimum estimate.

Enter the length of side 1: 10

Enter the width of side 1: 10

Enter the length of side 2: 10

Enter the width of side 2: 20

Enter the length of a single board of siding: 1

Enter the width of a single board of siding: 5

How many windows does this house have? 4

Would you like to have circular windows (y or n)?y

What is the radius of each window? 1.5


The number of siding boards you need is
114.34513322353837. Remember to add 
10-20% onto this number as it does not allow
for different cuts or mistakes.
	

Check to make sure your program produces the correct results for these values. Don't forget that it should also still work correctly for rectangular windows! You also need to make sure that the format of your program's output EXACTLY matches the specifications given above. In addition, you should test your program with input of your own. You may also want to team up with someone in the lab to make sure that both of your programs produce the same output given identical input.

How To Submit

After you are sure that your program produces the correct results, submit it using the following command:

try grd-231 lab4-3 SidingCalculator.java

Post-Lab Activity : Compute the surface area of a pyramid

Consider the following shape, a pyramid, that consists of 4 identical isosceles triangles and a square base:


pyramid

In this activity you will be writing a Java program that computes the surface area of a pyramid. Your program must:

The height of the pyramid is defined as the distance from the point where the tips of the triangles meet to the center of the square at the bottom. Note that this is not the same as the height of a triangle! The base of the pyramid is the length of the unequal side in the isosceles triangles. Your program should collect the necessary information from the user using the Scanner object, compute the area, and then display the results. As in previous activities of this lab, you must use the shape classes (Rectangle and IsoscelesTriangle) that have been provided. Name your program Pyramid.java.

You might find the Pythagorean theorem useful:

A triangle is a right triangle if and only if the square of the length of the longest side is equal to the sum of the squares of the lengths of the two shorter sides, i.e., c2 = a2 + b2.

You will also need to use some methods from java.lang.Math to complete this task.

The format of the prompts to collect the input from the user is as follows:

Enter pyramid height: 

and

Enter pyramid base: 

Remember to include a space as the last character in each of the prompts and a blank line after each invocation of the read.

The format of the output should be:

The area of a pyramid with height H and base BASE is: 
AREA

where H is the height of the pyramid, BASE is the base of the pyramid, and AREA is your calculated area of the pyramid. Note that the output consists of two lines of output:

The output from a single execution of your program is shown below:

Enter pyramid height: 10

Enter pyramid base: 5

The area of a pyramid with height 10.0 and base 5.0 is:  
128.07764064044153

Note that in this example the user entered 10 as the height and 5 as the base.

You must test your program to make sure it is producing the correct results. To help with your testing, the table below gives the sizes of some pyramids and their corresponding areas.

Height Base Area
10 5 128.0776
15 15 728.1153
10 2 44.1995
25 30 2649.2856

How To Submit

After you are sure that your program computes the correct answer, submit the program using the following command:

try grd-231 lab4-4 Pyramid.java


Grade Computation

Grade Breakdown: