com sci intro course question code: https://docs.google.com/document/d/1ZSH_3Mhb
ID: 3678230 • Letter: C
Question
com sci intro course question
code: https://docs.google.com/document/d/1ZSH_3MhbOT0WRypmZKD7f49C8GVpBelNqQGJaBlshPg/edit?usp=sharing
Create two classes Node and PointList which will implement a list of (x,y points using a linked list. l. A Node should have three private instance variables: two double values x and y, and a link to the next Node in the list. Provide a constructor which will initialize all three of these instance variables. Provide a Node getLink() method which will allow access to the link to the next Node in the list. You do not need any other getset methods for the Bronze exercise. 2. Provide a string toString method in the Node class which will return a String showing the x and y values in the format "(0.343,0.982) Use the built-in method String format ("%5.3f", datavalue) to ensure that both the x and y values display exactly 3 decimal places. 3. A PointList should have a single private instance variable containing a reference to the first Node in the list (or null if there are none). Provide a constructor which will create an empty list. Provide a void add(double x, double y) method which will add the point (x,y) to this list of points, at the beginning. Provide the usual String tostring() method which will return a String showing all of the points in the list, in the following format: (0.435,0.234) (0.123,0.876) (0.321 0.789) J". 4. Run the supplied TestLab8Bronze. java program to test your classes. This program will generate and print a list of 5 random pointsExplanation / Answer
//Node class
class Node{
private double x;
private double y;
private Node link;
public Node(double x, double y, Node link) {
this.x = x;
this.y = y;
this.link = link;
}
public Node getLink(){
return link;
}
@Override
public String toString() {
return "("+String.format("%5.3f", x)+","+String.format("%5.3f", y)+")";
}
}
//PointList class
public class PointList {
private Node head;
public PointList() {
head = null;
}
public void add(double x, double y){
if(head == null){// if list is empty
head = new Node(x, y, null);
}else{// else add newNode at begining
Node newNode = new Node(x,y,head);
head = newNode;// make newNode ad head
}
}
@Override
public String toString() {
Node temp = head;
String result = "[ ";
while(temp !=null){ // traversing through list
result = result + temp.toString()+" ";
temp = temp.getLink();
}
result = result+"]";
return result;
}
}
//Test class
public class TestLab8Bronze {
public static void main(String[] args) {
// creating PointList Object
PointList p = new PointList();
// adding 5 points
p.add(2.3, 1);
p.add(3.9, 5);
p.add(4.3, 6.7);
p.add(1.1, 0.5);
p.add(5.432, 2.34);
//printing
System.out.println(p.toString());
}
}
/*
output:
[ (5.432,2.340) (1.100,0.500) (4.300,6.700) (3.900,5.000) (2.300,1.000) ]
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.