AP Computer Science I

advertisement
AP Computer Science II
Java Keyword Lab Assignment # 27
The "Geometry" Program
80 & 100 Point Versions
Assignment Purpose:
This assignment has nothing to do with Geometry concepts. The purpose of this
assignment is to demonstrate the ability to redefine the existing methods toString
and equals, as well as implement the compareTo method.
Java has methods toString and equals first declared in the Object class. All other classes are
derived from the Object class, even if you do not observe the extends Object statement. Method
compareTo is a method of the Comparable interface.
You need to write classes Triangle and Rectangle, which will redefine toString and equals. You
also need to implement the Comparable class and define method compareTo.
How each method needs to be defined is specified below:
toString
Method toString returns a string that displays the length of the sides of the object, separated
by commas and shown inside square brackets. For instance, a triangle with sides 20, 17 and 14
will return the following string:
[20,17,14]
equals
Method equals returns true if the corresponding sides of both objects have equal length and
false otherwise.
compareTo
Method compareTo appears similar to equals, but makes a comparison based on the sum
of the sides of each object. An integer value is returned based on the difference between
object1's perimeter and object2's perimeter. The difference is computed by using this-rhs.
Exposure Java Chapter XXVII Lab Assignments Page 1 08-29-03
// Lab27st.java
// This is the student version of the Lab 27 assignment.
import java.io.*;
public class Lab27st
{
public static void main (String args[]) throws IOException
{
System.out.println("\nLAB 27 80 POINT VERSION\n");
Triangle t1 = getTriangle();
Triangle t2 = getTriangle();
System.out.println();
System.out.println("Triangle 1 sides: " + t1);
System.out.println("Triangle 2 sides: " + t2);
System.out.println("Equality test: " + t1.equals(t2));
System.out.println("Compare To test: " + t1.compareTo(t2));
System.out.println();
}
public static Triangle getTriangle() throws IOException
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
}
}
80 Point Version
The 80-point version only uses the Triangle class. The Triangle class needs to include the following
class members:
private int ab;
private int bc;
private int ca;
// 1st side entered
// 2nd side entered
// 3rd side entered
private int getPerimeter()
// returns the sum of the length of the sides
{
}
public Triangle(int s1, int s2, int s3)
// constructor, which initializes the object's private data fields
{
}
public String toString()
// returns a string in the format [ab,bc,ca], where ab, bc and ca are their int values
{
}
public boolean equals(Triangle rhs)
// returns true if both objects have equal perimeters and false otherwise
{
}
public int compareTo(Object rhs)
// return the difference between this and rhs perimeter values
{
}
Exposure Java Chapter XXVII Lab Assignments Page 2 08-29-03
80 Point Execution
Lab23 80 Point Version
Three Required Separate Outputs
LAB 27 80 POINT VERSION
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
20
17
15
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
20
17
15
Triangle 1 sides:
Triangle 2 sides:
Equality test:
CompareTo test:
[20,17,15]
[20,17,15]
true
0
LAB 27 80 POINT VERSION
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
20
17
15
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
40
34
30
Triangle 1 sides:
Triangle 2 sides:
Equality test:
CompareTo test:
[20,17,15]
[40,34,30]
false
-5
LAB 27 80 POINT VERSION
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
40
34
30
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
20
17
15
Triangle 1 sides:
Triangle 2 sides:
Equality test:
CompareTo test:
[40,34,30]
[20,17,15]
false
52
Exposure Java Chapter XXVII Lab Assignments Page 3 08-29-03
100 Point Version
The 100-point version requires completion of both the Triangle class and the Rectangle classes.
The methods of the Rectangle class are identical in name and purpose as the methods of the
Triangle class. Additionally, the main method needs to be expanded to test the second class.
100 Point Execution
Lab23 100 Point Version
Three Required Separate Outputs
LAB 27 100 POINT VERSION
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
20
17
15
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
20
17
15
Enter
Enter
Enter
Enter
side
side
side
side
1
2
3
4
===>>
===>>
===>>
===>>
30
15
30
15
Enter
Enter
Enter
Enter
side
side
side
side
1
2
3
4
===>>
===>>
===>>
===>>
30
15
30
15
Triangle 1 sides:
Triangle 2 sides:
Equality test:
Compare To test:
[20,17,15]
[20,17,15]
true
0
Rectangle 1 sides:
Rectangle 2 sides:
Equality test:
CompareTo test:
[30,15,30,15]
[30,15,30,15]
true
0
LAB 27 100 POINT VERSION
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
20
17
15
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
40
34
30
Enter
Enter
Enter
Enter
===>>
===>>
===>>
===>>
30
15
30
15
side
side
side
side
1
2
3
4
Exposure Java Chapter XXVII Lab Assignments Page 4 08-29-03
Enter
Enter
Enter
Enter
side
side
side
side
1
2
3
4
===>>
===>>
===>>
===>>
Triangle 1 sides:
Triangle 2 sides:
Equality test:
Compare To test:
60
30
60
30
[20,17,15]
[40,34,30]
false
-52
Rectangle 1 sides:
Rectangle 2 sides:
Equality test:
CompareTo test:
[30,15,30,15]
[60,30,60,30]
false
-90
LAB 27 100 POINT VERSION
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
40
34
30
Enter side 1
Enter side 2
Enter side 3
===>>
===>>
===>>
20
17
15
Enter
Enter
Enter
Enter
side
side
side
side
1
2
3
4
===>>
===>>
===>>
===>>
60
30
60
30
Enter
Enter
Enter
Enter
side
side
side
side
1
2
3
4
===>>
===>>
===>>
===>>
30
15
30
15
Triangle 1 sides:
Triangle 2 sides:
Equality test:
Compare To test:
Rectangle 1 sides:
Rectangle 2 sides:
Equality test:
CompareTo test:
[40,34,30]
[20,17,15]
false
52
[60,30,60,30]
[30,15,30,15]
false
90
Important Note
Your program does not need to check for illogical input. In other words, there are many values that
would not result in an actual triangle. The same is true for a rectangle.
Exposure Java Chapter XXVII Lab Assignments Page 5 08-29-03
Exposure Java Chapter XXVII Lab Assignments Page 6 08-29-03
Download