1 / 11

Lab 2

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.

camdyn
Download Presentation

Lab 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 ( intmaxNumber ) • 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 () • booleanisEmpty ( ) • booleanisFull ( ) • booleangotoBeginning ( ) • booleangotoEnd ( ) • booleangotoNext ( ) • booleangotoPrior ( ) • 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. // 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

  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 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

  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

More Related