Download presentation
Presentation is loading. Please wait.
Published byAmice Beasley Modified over 9 years ago
1
Lab 2 Point List
2
OVERVIEW In this laboratory, you explore lists in which each element is a two-dimensional point or (x,y) pair. We refer to this type of list as a point list.
3
OVERVIEW
4
Elements Each element in a point list is of type Point (a built-in Java class) and contains a pair of integers that represent the points x- and y- coordinates
5
Structure The points form a linear structure in which points follow one after the other, from the beginning of the list to its end. You travel through the list using operations that change the position of the cursor.
6
Constructor PointList ( ) – Precondition: None. – Postcondition: Default Constructor. Calls setup, which creates an empty list. Allocates enough memory for a list containing DEF_MAX_LIST_SIZE ( a constant value) points. PointList ( int maxNumber ) – Precondition: maxNumber > 0. – Postcondition: Constructor. Creates an empty list. Allocates enough memory for a list containing maxNumberpoints.
7
Methods void append ( Point newPoint ) void clear () boolean isEmpty ( ) boolean isFull ( ) boolean gotoBeginning ( ) boolean gotoEnd ( ) boolean gotoNext ( ) boolean gotoPrior ( ) Point getCursor ( ) void showStructure ( )
8
class Point { // Data members // Point coordinates (can be accessed directly) public int x, y; // Constructors public Point ( ) // Default Constructor { x = 0; y = 0; } public Point ( int x0, int y0 ) // Constructor { x = x0; y = y0; } } // built-in class Point
9
class PointList { // Default maximum list size a constant public static final int DEF_MAX_LIST_SIZE = 10; // Data members private int size, // Actual number of points in the list cursor; // Cursor index private Point ptlist[]; // Array containing the points // Constructors and helper method setup public PointList ( ) // Constructor: default size{ } public PointList ( int maxNumber ) // Constructor: specific size{ } // Class methods private void setup(int size) // Called by constructors only{ } // List manipulation operations/methods public void append ( Point newPoint ) // Append point to list{ } public void clear ( ) // Clear list{ } // List status operations/methods public boolean isEmpty ( ) // Is list empty?{ } public boolean isFull ( ) // Is list full?{ } Complete necessary code // List iteration operations public boolean gotoBeginning ( ) // Go to beginning{ } public boolean gotoEnd ( ) // Go to end{ } public boolean gotoNext ( ) // Go to next point{ } public boolean gotoPrior ( ) // Go to prior point{ } public Point getCursor ( ) // Return point{ } // Output the list structureÑused in testing/debugging public void showStructure ( ){ } } // class PointList
10
Implementation of the sample Coffee ADT for testing the Logbook ADT import java.io.*; class SampPtList{ public static void main ( String args[] ) throws IOException{ // Set of vertices for a polygon PointList polygon = new PointList( ); Point vertex; // Vertex InputStreamReader reader = new InputStreamReader(System.in); // Initialize the tokenizer StreamTokenizer tokens = new StreamTokenizer(reader); // Read in the polygon's vertices. System.out.print("Enter the polygon's vertices (end with eof) : "); // Keep reading as long as text (the word eof) has not been entered while ( tokens.nextToken( ) != tokens.TT_WORD ) { vertex = new Point( ); // Create new Point vertex.x = (int)tokens.nval; // Assign x value of the point tokens.nextToken( ); vertex.y = (int)tokens.nval; // Assign y value of the point polygon.append(vertex); // Add to PointList's array of Points } // Output the vertices one per line. if ( polygon.gotoBeginning( ) ) // Go to beginning of list do { vertex = polygon.getCursor( ); System.out.println("(" + vertex.x + "," + vertex.y + ")"); } while ( polygon.gotoNext( ) ); // Go to next point (if any) } } // class SampPtList Test Pointlist
11
You should.. Send me two java files – PointList class – SampPtList class It should be complete and working well Submit by email before your lab time
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.