Exercise 1: Write a Point class that has xpos and ypos as its fields, get and se
ID: 3873621 • Letter: E
Question
Exercise 1: Write a Point class that has xpos and ypos as its fields, get and set methods, and a toString method. It must be written as a generic class, which means that it should work for any type of object, that is, the xpos and ypos could be either Integer objects, or Double objects, or String objects. A sample test program is given below. Modify the test program to accept input data from the user and try it for different input values. public class PointTester public static void main (String[ args) Point point! new Point(10, 20) ; PointDouble> point2 = new PointDouble> (14.5, 15.6); PointString> point3 = new Point("topleftx", "toplefty") System.out.println (pointl); System.out.println (point2); System.out.println (point3) The output is XPOS: 10 YPOS: 20 XPOS: 14.5 YPOS: 15.6 XPOS: topleftx YPOS: toplefty The following exercises (2 to 5) llustrate the concept of building data structures in "layers". You will design a Generic Stack and Generic Queue data structure using an ArrayList. Although an ArrayList itself is a generic data structure, the idea behind layering is that the arraylist can be changed to another data structure such as a linked list and the applications running the queue will not notice the difference.Explanation / Answer
PointerTester.java
public class PointerTester {
public static void main(String[] args) {
Point<Integer> point1 = new Point<Integer>(10, 20);
Point<Double> point2 = new Point<Double>(14.5, 15.6);
Point<String> point3 = new Point<String>("topleftx", "toplefty");
System.out.println(point1);
System.out.println(point2);
System.out.println(point3);
}
}
Point.java
public class Point<T> {
private T xpos, ypos;
public Point(T xpos, T ypos) {
this.xpos = xpos;
this.ypos = ypos;
}
@Override
public String toString() {
return "XPOS: "+xpos+" YPOS: "+ypos;
}
}
Output:
XPOS: 10 YPOS: 20
XPOS: 14.5 YPOS: 15.6
XPOS: topleftx YPOS: toplefty
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.