Vous êtes sur la page 1sur 7

WORKSHEET A29.

1
LinkedList

1. The following program builds on Worksheet A15.1, ​ArrayList​ that uses an ArrayList to store and
perform geometric calculations on a set of coordinate points – using the Point2D.Double class. In
this problem, we will use a LinkedList instead of an ArrayList to store the points. This program
uses L​ istNode.java​ and S​ inglyLinkedList.java​ ​from the lesson, as well as the class below.

import java.awt.geom.*; // for Point2D.Double

class​ MySinglyLinkedList extends SinglyLinkedList{


// assumes the first word in the first line from SinglyLinkedList is changed from //
private to protected.
// Note that this method overloads printList() from SinglyLinkedList
public void​ printList(){
ListNode temp = getFirstNode();// start from the first node
while​ (temp != ​null​){
System.out.println("(" + ((Point2D.Double)temp.getValue()).getX() +
"," + ((Point2D.Double)temp.getValue()).getY() + ")");
temp = temp.getNext();// go to next node
}
}

public double​ calculateArea(){


// assumes points received from user form a rectangle
ListNode temp = getFirstNode();// start from the first node
Point2D.Double ptD = (Point2D.Double)temp.getValue();
temp = temp.getNext();// go to next node
Point2D.Double ptC = (Point2D.Double)temp.getValue();
temp = temp.getNext();// go to next node
Point2D.Double ptB = (Point2D.Double)temp.getValue();
temp = temp.getNext();// go to next node
Point2D.Double ptA = (Point2D.Double)temp.getValue();
double base = ptA.distance(ptB);
double height = ptA.distance(ptD);
return base * height;
}
}

The following classes are used with the classes described above. The output from this code is
identical to that of Worksheet A15.1 -- except for one difference. Read through the code and
determine how the output differs. Use the same input from the keyboard that was used in Worksheet
A15.1: (1.5, 2.1), (9.7, 2.1), (9.7, 7.3), (1.5, 7.3) and don’t forget to enter the points in consecutive
order!

W.A.30.1(Page 1)
import java.util.Scanner;
import java.awt.geom.*; // for Point2D.Double
import java.util.*; // for ArrayList

class​ LinkedRectangle {
MySinglyLinkedList myList;

public​ LinkedRectangle(){
myList = ​new​ MySinglyLinkedList();
}

public void createList(){


System.out.println("We need four vertices for our rectangle.");
System.out.println("Please provide them in consecutive order.");
//Scanner console = ​new​ Scanner(System.in);
Scanner console = ​new​ Scanner("1.5\n2.1\n9.7\n2.1\n9.7\n7.3\n1.5\n7.3\n");
for​ (​char​ ch = 'A'; ch <= 'D'; ch++){
System.out.print("Give me the x coordinate for point " + ch + ": ");
double​ x = console.nextDouble();
System.out.print("Give me the y coordinate for point " + ch + ": ");
double​ y = console.nextDouble();
Point2D.Double myPoint = new Point2D.Double(x,y);
myList.addFirst(myPoint);
}
}

public void​ printPoints(){


System.out.println("The following are the points entered:");
myList.printList();
}

public void​ calcRectArea(){


double​ area = myList.calculateArea();
System.out.println("Area of rectangle is " + area);
}

​ tatic void main​(String[] args){


public​ s
LinkedRectangle app = new LinkedRectangle();
app.createList();
app.printPoints();
app.calcRectArea();
}

Output

import java.awt.geom.Point2D; // for Point2D.Double

class MySinglyLinkedList extends SinglyLinkedList{


W.A.30.1(Page 2)
// assumes the first word in the first line from SinglyLinkedList is changed from // private to
protected.
// Note that this method overloads printList() from SinglyLinkedList
public void printList(){
ListNode temp = getFirstNode();// start from the first node
while (temp != null){
System.out.println("(" + ((Point2D.Double)temp.getValue()).getX() +
"," + ((Point2D.Double)temp.getValue()).getY() + ")");
temp = temp.getNext();// go to next node
}
}

public double calculateArea(){


// assumes points received from user form a rectangle
ListNode temp = getFirstNode();// start from the first node
Point2D.Double ptD = (Point2D.Double)temp.getValue();
temp = temp.getNext();// go to next node
Point2D.Double ptC = (Point2D.Double)temp.getValue();
temp = temp.getNext();// go to next node
Point2D.Double ptB = (Point2D.Double)temp.getValue();
temp = temp.getNext();// go to next node
Point2D.Double ptA = (Point2D.Double)temp.getValue();
double base = ptA.distance(ptB);
double height = ptA.distance(ptD);
return base * height;

}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.Scanner;
import java.awt.geom.*; // for Point2D.Double
import java.util.*; // for ArrayList

class LinkedRectangle {
MySinglyLinkedList myList;

public LinkedRectangle(){
myList = new MySinglyLinkedList();
}

public void createList(){


System.out.println("We need four vertices for our rectangle.");
System.out.println("Please provide them in consecutive order.");
//Scanner console = new Scanner(System.in);
Scanner console = new Scanner("1.5\n2.1\n9.7\n2.1\n9.7\n7.3\n1.5\n7.3\n");

W.A.30.1(Page 3)
for (char ch = 'A'; ch <= 'D'; ch++){
System.out.print("Give me the x coordinate for point " + ch + ": ");
double x = console.nextDouble();
System.out.print("Give me the y coordinate for point " + ch + ": ");
double y = console.nextDouble();
Point2D.Double myPoint = new Point2D.Double(x,y);
myList.addFirst(myPoint);
}
}

public void printPoints(){


System.out.println("The following are the points entered:");
myList.printList();
}

public void calcRectArea(){


double area = myList.calculateArea();
System.out.println("Area of rectangle is " + area);
}

public static void main(String[] args){


LinkedRectangle app = new LinkedRectangle();
app.createList();
app.printPoints();
app.calcRectArea();
}

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.NoSuchElementException;

/**
* Implementation of lists, using singly linked elements.
*
* @author G. Peck
* @created April 27, 2002
*/
public class SinglyLinkedList{
private ListNode first; // first element

/**
* Constructor for the SinglyLinkedList object
* Generates an empty list.
*/

W.A.30.1(Page 4)
public SinglyLinkedList(){
first = null;
}

/**
* Returns the first element in this list.
*
* @return the first element in the linked list.
*/
public Object getFirst(){
if (first == null){
throw new NoSuchElementException();
}
else
return first.getValue();
}

/**
* Inserts the given element at the beginning of this list.
*
* @param value the element to be inserted at the beginning of this list.
*/
public void addFirst(Object value){
// note the order that things happen:
// head is parameter, then assigned
first = new ListNode(value, first);
}

/**
* Print the contents of the entire linked list
*/
public void printList(){
ListNode temp = first;// start from the first node
while (temp != null){
System.out.print(temp.getValue() + " ");
temp = temp.getNext();// go to next node
}
}

/**
* Returns a string representation of this list. The string
* representation consists of the list's elements in order,
* enclosed in square brackets ("[]"). Adjacent elements are
* separated by the characters ", " (comma and space).
*

W.A.30.1(Page 5)
* @return string representation of this list
*/
public String toString(){
String s = "[";

ListNode temp = first; // start from the first node


while (temp != null){
s += temp.getValue(); // append the data
temp = temp.getNext(); // go to next node
if (temp != null)
s += ", ";
}
s += "]";
return s;
}
public ListNode getFirstNode(){
return first;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Implementation of a node of a singly linked list.
*
* Adapted from the College Board's AP Computer Science AB:
* Implementation Classes and Interfaces.
*/
public class ListNode{
private Object value;
private ListNode next;

/**
* Constructs a new element with object initValue,
* followed by next element
*
* @param initValue New element object
* @param initNext Reference to next element
*/
public ListNode(Object initValue, ListNode initNext){
value = initValue;
next = initNext;
}

/**
* Constructs a new tail of a list with object initValue
*

W.A.30.1(Page 6)
* @param initValue New element object
*/
public ListNode(Object initValue){
this(initValue, null);
}

/**
* Sets the value attribute of the ListNode object
*
* @param theNewValue value attribute of the ListNode object
*/
public void setValue(Object theNewValue){
value = theNewValue;
}

/**
* Sets reference to new next value
*
* @param theNewNext The new next value
*/
public void setNext(ListNode theNewNext){
next = theNewNext;
}

/**
* Returns value associated with this element
*
* @return The value associated with this element
*/
public Object getValue(){
return value;
}

/**
* Returns reference to next value in list
*
* @return The next value in the list
*/
public ListNode getNext(){
return next;
}
}

W.A.30.1(Page 7)

Vous aimerez peut-être aussi