CS 1063 Lab 5:
Designing and Implementing a Class
The Square Class
Objectives
- Construct and use Objects
- Design the public interface of a class
- Determine the instance variables necessary for a class
- Implement the shell of a class using stubs with comments
- Implement constructors
- Implement simple methods
- Access instance fields and local variables
Hand-in Requirements
All projects and laboratories will be submitted electronically
through WebCT. Zip up your entire project folder to submit as
the source. (Right click on the project folder and follow
the SentTo link.) The project folder should include the
following:
- Public Interface as a word or plaintext document
- Square.java
- SquareTest.java
Details
Implement a class Square that has methods double
getSide(), String toString(), double
calculateArea(), and
double calculatePerimeter(). In the constructor, supply the
side of the square. To test the Square class
instantiate two squares with different length sides and print the
side, area and perimeter. Be sure you test ALL of your
methods.
Follow the steps below.
-
Design the public interface for the Square Class.
- How will you need to initialize a new square? This will be
your constructor.
- What behavior will the Square class have? This
will be your methods.
- What type of information will the Square class need to
store to do its job. This will be your private instance
variables.
Setup:
- Project/Folder name: Square
- New class with main method: SquareTest
public class SquareTest {
public static void main(String[] args) {
}
}
- New class: Square
public class Square {
}
- Save your classes in the square folder.
- Compile your classes and run the SquareTest class.
- Save your project.
Implement the class
-
Type in your square.java:
your instance variables and stubs for the constructor and methods.
Compile your
project to test for any bugs.
- Fill in your constructor and methods. Run your
project often to check for bugs.
- Test all of the methods in your class. Make sure
the behavior is correct by looking at your output!