Exercise 1: Write a Point class that has xpos and ypos as its fields, get and se
ID: 3873584 • 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
import java.util.Scanner;
public class PointTester {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Integer coordinates x and y values ");
int intx=sc.nextInt();
int inty=sc.nextInt();
System.out.println("Enter Double coordinates x and y values ");
double doublex=sc.nextDouble();
double doubley=sc.nextDouble();
System.out.println("Enter String coordinates x and y values ");
String stringx=sc.next();
String stringy=sc.next();
Point<Integer> point1=new Point<Integer>(intx,inty);
Point<Double> point2=new Point<Double>(doublex,doubley);
Point<String> point3=new Point<String>(stringx,stringy);
System.out.println(point1);
System.out.println(point2);
System.out.println(point3);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.