Return to Jeff's Main Page

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
My Intro to Object-Oriented Programming  
 
 
 
Class Notes

Monday September 18 , 2000
 
Class began with a discussion of the Rectangle Class.  

The rectangle constructor is as follows:

  • new Rectangle (< double >,  < double >) -- > < Rectangle >,  creates a rectangle using the first double as the length and the second double as the width
The rectangle operators include:
  • < Rectangle > .length () -- > < double >,  computes the length of the rectangle
  • < Rectangle > .width () -- > < double >,  computes the width of the rectangle
  • < Rectangle > .area () -- > < double >,  computes the area of the rectangle
  • < Rectangle > .perimeter () -- > < double >,  computes the perimeter of the rectangle
  • < Rectangle > .diagonal () -- > < double >,  computes the diagonal of the rectangle
We were also introduced to a command for the rectangle class:    < Rectangle > .display ()  

Having defined the operators for the rectangle class,  CG presented a problem utilizing the rectangle class.   Given:   a city block of a certain length and width,  partitioned into 16 rectangular lots.   We then used the rectangle class to write Java code to model the city block and one lot,  display the diagonal distance across the block,  and determine the percent of the block taken up by one lot.  

After completed the previous problem,  CG presented a much more interesting problem:   What is the surface area of the cylinder of height 28.5 and diameter 11.6?
After noting that the surface area of the cylinder consists of the suface area of the top + surface area of bottom + surface area of ``side'',  we used problem decomposition to relate our knowns to our unknowns.   Essentially,  the problem consisted of utilizing two circles (the ends) and a rectangle (the middle portion cut down the middle and unrolled).   Here is the code:

What is the surface area of the cylinder of height 28.5 and diameter 11.6? 

class SurfaceAreaApp 

import blue.shapes.   *; 

/ /Record the given information. 

double cylinderHeight = 28.5;
double cylinderDiameter = 11.6; 

/ /Create the top. 

double topRadius = (cylinderDiameter / 2.0);
Circle top = new Circle (topRadius); 

/ /Create the bottom. 

double bottomRadius = topRadius;
Circle bottom = new Circle (bottomRadius); 

/ /Create the side. 

double sideHeight = cylinderHeight;
double sideLength = top.perimeter ();
Rectangle side = new Rectangle (sideHeight,  sideLength); 

/ /Compute the surface area. 

double sa = top.area () + bottom.area () + side.area (); 

/ /Display result. 

IO.println (``The surface area =  ``+ sa + ``square units.''); 

 

Here are some notes on string manipulation:

  • < string > + < string > -- > < string >
  • < string > + < double > -- > < string >.   The double is converted into a string.