110 likes | 260 Views
Lab 2. Point List. 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. OVERVIEW. Elements.
E N D
Lab 2 Point List
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.
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
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.
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 ( intmaxNumber ) • Precondition: maxNumber > 0. • Postcondition: Constructor. Creates an empty list. Allocates enough memory for a list containing maxNumberpoints.
Methods • void append ( Point newPoint ) • void clear () • booleanisEmpty ( ) • booleanisFull ( ) • booleangotoBeginning ( ) • booleangotoEnd ( ) • booleangotoNext ( ) • booleangotoPrior ( ) • Point getCursor ( ) • void showStructure ( )
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
// List iteration operations public booleangotoBeginning ( ) // Go to beginning{ } public booleangotoEnd ( ) // Go to end{ } public booleangotoNext ( ) // Go to next point{ } public booleangotoPrior ( ) // Go to prior point{ } public Point getCursor ( ) // Return point{ } // Output the list structureÑused in testing/debugging public void showStructure ( ){ } } // class PointList 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 ( intmaxNumber ) // 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 booleanisEmpty ( ) // Is list empty?{ } public booleanisFull ( ) // Is list full?{ } Complete necessary code
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 InputStreamReaderreader = new InputStreamReader(System.in); // Initialize the tokenizer StreamTokenizertokens = 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
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