|
Assignment 16
Task
Repeat A09, only this time make the sphere its own class and create instances of it.
Concepts: Constructors, instance methods, encapsulation
Textbook: 4.3 - 4.5
Steps
You'll need to write two classes/files for this assignment. Normally, you'd create Sphere and then some other class--SphereUI or SphereTester , perhaps--that creates Sphere instances. However, since you're going to turn in your Sphere class to Tamarin, you'll need to give it the unfriendly name of UsernameA16.java .
File #1: UsernameA16.java
This class models a sphere. It should have at least one private instance variable for the radius; you may have more, if you wish. It should then have at least the following public constructor and methods:
- public UsernameA16(double radius)
- public double getRadius()
- public void setRadius(double r)
- public double getDiameter()
- public double getCircumference()
- public double getSurfaceArea()
- public double getVolume()
Remember that your methods must match these signatures exactly. Note how, unlike in A09, the methods no longer take parameters. This is because every sphere already knows its own radius, so you don't need to pass this information to the method anymore.
Your UsernameA16.java should only model a sphere and not do any direct communcation with the end user. So don't do any printing or user input in this class.
Error-checking of passed values is optional. For instance, you could prevent negative values for the radius by setting it to 0 or taking its absolute value instead; but this is not required--you can allow another programmer to make a sphere with a negative radius if they really want to.
File #2: SphereTester.java
Then write a separate class with a main method that serves as a user interface to testing your sphere class. You can prompt the user to enter a radius and create a sphere from it, producing the same output as for A09. You don't have to get the information from the user, however; you could just hard-code a few tests (as I did in PetStore.java, below).
Make sure you test each method of your sphere and confirm that it works correctly. This means you will need to change at least one sphere's radius and print out its details again in order to test setRadius() .
You will not submit this file; it's just to help you ensure that your sphere is correct.
Sample Output
Your output will depend on how you wrote your SphereTester class. It could be the same as for A09, but doesn't have to be.
What to Submit
Upload your UsernameA16.java file to Tamarin.
Grading [4 points]
- 1 - Compiles
- Your program compiles successfully (no errors)
- 0.5 - Encapsulated instance variables
- All your instance variables are private.
- 0.5 - Constructor
- Has a public constructor that takes a double.
- 0.5 - Accessor (get) and mutator (set) methods for radius
- 1.5 - Accessors for diameter, circumference, surface area, and volume.
- All methods must exactly match the method signatures given above.
FAQs
- Blobby demo code?
- Here: PetBlob.java and PetStore.java.
Try running the PetStore (the only class with a main method) and then tracing it, to be sure you understand how the output was generated.
After encapsulation: SafePetBlob.java and SafePetStore.java
Note that these PetBlob examples are actually more complicated than your Sphere class will be.
And for the curious: The Blob was originally filmed in 1958 (see the theatrical trailer on YouTube). However, it may have been the grislier 1988 remake that I saw on video (see the theatrical trailer on YouTube). I'm not sure; like I said, it's been years.
- Should the Sphere class have a
toString() method?
- You can add one if you want, but it is not required.
- What's the point of doing this?
- The goal is to learn how to define your own objects. You've been using them, but you need to learn how the other side works.
The main advantage of objects is that they are bundles of data. (Admittedly, Student and SafePetBlob are better examples of this, as a Sphere only needs a single value in it. Still, using a Sphere object rather than just a double variable emphasizes what that number represents--a Sphere's radius.)
But besides data/state, it's also convenient to have behaviors/methods directly associated with objects. (Again, the value of this may be clearer with SafePetBlob than Sphere .)
Finally, it's useful to have a separate class that is modular and secure that you could reuse by just dropping into another program. For example, I could have a program that uses the Sphere class to model balloons:
Sphere balloon = new Sphere(10.0);
System.out.println("I have a balloon " + balloon.getDiameter() + " cm in diameter.");
System.out.println("It has a volume of " + balloon.getVolume() + " cubic cm");
System.out.print("If I deflate it until it is only half as large ");
balloon.setRadius(balloon.getRadius() / 2);
System.out.println("(" + balloon.getDiameter() + " cm in diameter),");
System.out.println("it will only have a volume of " + balloon.getVolume() +
" cubic cm");
If later I need to model planets--for example, to calculate the combined volume and surface area of all the planets in the solar system--I could reuse Sphere again.
|