Except where otherwise noted, the contents of this document are Copyright 2019 Stuart Reges and Marty Stepp.
Modified by T. W. Bennet
Goals for this problem set:
public class ClassName { // fields fieldType fieldName; // methods public returnType methodName() { statements; } }A couple things look different than programs for past homeworks:
main
method. It won't be run like a client program.static
keyword in the header.
Suppose a method in the BankAccount
class is defined as:
public double computeInterest(int rate)
If the client code has declared a BankAccount
variable named acct
, which of the following would be a valid call to the above method?
PointClient
program is supposed to construct two Point
objects, translate each, and then print their coordinates.
Finish the program so that it runs properly.
(You don't need to modify Point.java
.)
Point
and PointMain
Point.java
and add several methods.
The PointMain
program calls these methods; you can use it to test your code. Initially, these
tests are commented out. As you add each method to Point
, uncomment the test in
PointMain.java
.
Point
Comparable.
Point.java
to make the Point
class implement the
Comparable
interface. A Point
should be comparable to
another Point
.CompareTo
method to satify the Comparable
interface.
If the x values of the two points differ, the order of the points is the order of the x values.
If the x values match, the order is that of the y values. Of course, if the x values and
y values each match, the points are equalPointMain
that tests comparable. Compile it and run it.
and fix any errors.quadrant
Add the following method to the Point
class:
public int quadrant()
Returns which quadrant of the x/y plane this Point
object falls in.
Quadrant 1 contains all points whose x and y values are both positive.
Quadrant 2 contains all points with negative x but positive y.
Quadrant 3 contains all points with negative x and y values.
Quadrant 4 contains all points with positive x but negative y.
If the point lies directly on the x and/or y axis, return 0.
(Test your code by running the PointMain
program.)
flip
Add the following method to the Point
class:
public void flip()
Negates and swaps the x/y coordinates of the Point
object.
For example, if an object pt
initially represents the point (5, -3), after a call of pt.flip();
, the object should represent (3, -5).
If the same object initially represents the point (4, 17), after a call to pt.flip();
, the object should represent (-17, -4).
Note carefully: the method both exchanges the x and y values, and changes the sign of each one.
Test your code by uncommenting the code in PointMain
for testing flip, and running the program again.
Suppose you are given a class named Rectangle
with the following contents:
// A Rectangle stores an (x, y) coordinate of its top/left corner, a width and height. public class Rectangle { private int x; private int y; private int width; private int height; // constructs a new Rectangle with the given x,y, width, and height public Rectangle(int x, int y, int w, int h) // returns the fields' values public int getX() public int getY() public int getWidth() public int getHeight() // returns a string such as {(5,12), 4x8} public String toString() ... }
{(6,4), 5x8}
Rectangle.java
, and copy the code from the previous
slide into it.
Rectangle
: getUpLeft, getUpRight, getLowLeft, getUpRight.
Each returns a Point representing the location of the appropriate corner.
contains
that will be placed inside the Rectangle
class.
The method accepts integers for x and y as parameters and turns true
if
and only if that x,y coordinate lies within this rectangle.
The edges are included; for example, a rectangle with x=2, y=5, width=8, height=10 will return true
for any point from (2, 5) through (10, 15) inclusive.
contains
, which takes a Point object as its argument and returns true if and only if
the Point lies inside the rectangle in the same sense. Your second method should just call the first.
TestRectangle.java
to test your new methodsoverlap
that will be placed inside the Rectangle
class.
The method accepts another Rectangle
, and returns true of the two overlap, including sharing an edge or
a corner.
TestRectangle.java
to test your new method.