Pledged lab quiz
The only "good programming practice" that is required for
this lab is to include your name and e-mail ID in the header
comments. You are welcome to include more, but make sure you
get the programs working first, as the quiz is timed.
The activities of this week are not to be
discussed outside of lab. The quiz is open textbook but nothing
else. You are to
submit one program, Star.jara, which contains the Star
class. A template file is being provided. There are
to be 9 methods in Star.java, 4 of which we provide. The Star class
represents a astronomical star, to be used in a computer program.
Instance variables
The sample Star.java file contains four double instance variables
to represent the brightness and location of the star. The brightness
of the star will be a positive number (this is not the same as
astronomical magnitude). The other three instance variables together
represent the (x,
y, z) location of the star. Our template file
Star.java provides the following definitions for
them.
private double brightness;
private double xLocation;
private double yLocation;
private double zLocation;
Methods
You are to implement five methods in the Star class.
- public Star()
The default constructor, it should set the brightness to 1, and the
location to (0,0,0)
- public Star (double b, double x, double y, double z)
The specific constructor, it should set the brightness to b,
and the location to (x, y, z)
- public void setLocation (int which, double toWhat)
One of the two mutators (we implement the other mutator). Parameter which specifies
which location part of the ordered triple to set, and parameter
toWhat
specifics the value to which it is set. If parameter which is 1,
then the x location of the Star is set to the value of toWhat.
A which value of 2 sets the y location, and a which
value of 3 sets the z location. If which is less
than 1, or greater than 3, then no modification should be made to the Star's
location (i.e., the request is ignored). For example, if star is a Star object, then
star.setLocation(1,2) will set the x value of the location
to 2.
- public double getLocation (int which)
One of the two accessors (we implement the other accessor). This returns one of the location
parts: if which is 1, it returns the x location, if which is
2, it returns the y location, and if if which is 3, it returns the
z location.
If which is less than 1 or greater than 3, then this should
return 0.
- public double distance()
This method will return the distance of the star from the origin
(0,0,0). The distance of location (x,y,z)
from the origin is the square root of (x*x + y*y + z*z). In case
you do not recall, you can get the square root of an expression e
in Java with
Math.pow(e, 0.5).
The template Star.java file provides the
following additional three methods -- DO NOT CHANGE THEM.
- public void setBrightness (double b)
One of the two mutators, it sets the brightness to the parameter b
public void setBrightness (double b) {
brightness = b;
}
- public double getBrightness ()
One of the two accessors, it returns the brightness of the Star
public double getBrightness () {
return brightness;
}
- public String toString()
This method must be copied EXACTLY as shown below.
public String toString() {
return "Star (" + brightness + ", @
("
+ xLocation + ", "
+ yLocation +
", "
+ zLocation + "))";
}
If you want to test your program you are welcomed to copy and paste into
it your class. Because we will have our own testing program, it will not
affect your grade whether it is included or not.
public static void main (String[] args) {
Star s1 = new Star();
System.out.println (s1);
Star s2 = new Star (3, 1, 2, 3);
System.out.println (s2);
s1.setBrightness (5);
s1.setLocation (0, 10); // This should do
nothing!
s1.setLocation (1, 11);
s1.setLocation (2, 12);
s1.setLocation (3, 13);
s1.setLocation (4, 14); // This should do
nothing!
System.out.println (s1);
double x = s2.getLocation(1);
double y = s2.getLocation(2);
double z = s2.getLocation(3);
System.out.println ("s2's location is (" + x + ", " + y + ",
" + z + ")");
System.out.println ( s1.distance() );
System.out.println ( s2.distance() );
}
Sample Output
The main() method above will produce the following output.
Star (1.0, @ (0.0, 0.0, 0.0))
Star (3.0, @ (1.0, 2.0, 3.0))
Star (5.0, @ (11.0, 12.0, 13.0))
s2's location is (1.0, 2.0, 3.0)
20.83266665599966
3.7416573867739413
Grading
Make sure each of your methods are named EXACTLY as specified. Also,
DO NOT CHANGE the other methods as they are
used by the grading system. If you do not follow these directions, the grading system will give you a zero for pretty
much all of the lab quiz.
Advice
- Remember neither the lab assistants nor other students may
help you design, write, or debug the programs. If you are
having a problem getting JCreator to work right (which does not
mean a compilation error), or an error with the submissions,
then you may ask a TA.
- To maximize your points, make sure your programs compile
successfully.
- If towards the end things are not going as well as you
would want (i.e., the program does not compile), it might be
best to try commenting out errant statements (i.e., put a
// in front of them)
Submission
When you are finished,
submit your Star.java file.