











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Various java code snippets and assignments for implementing different sorts, searches, and linked lists. It includes instructions for accessing the publisher's website for additional resources and hints for specific projects. Students are encouraged to use the provided code as a starting point for their assignments and to adapt it as needed.
Typology: Assignments
1 / 19
This page cannot be seen from the preview
Don't miss anything!
01/08/09 – Welcome to our CSE Program and Problem Solving II class – I’M EXCITED TO BE YOUR HELPER AND PARTNER IN EDUCATION THIS SEMESTER. I HOPE YOU ENJOY THE CLASS AND LEARN LOTS ABOUT PROGRAMMING AND PROBLEM SOLVING. Read the following, currently dated announcements. The others will be important (and will be dated) when we cover the material in class. 01/08/09 – All programming assignments (Projects and Labs) must include the following documentation on the source code (include the tabbing shown): // Program Name classname.java // Course: CSE 1302 M-W // Student Name: John Doe // Assignment Number: Project# or Lab# // Due Date: xx/xx/ // Signature: (for Projects only) ____________________________ // (The signature means that the program is your own work) // Simple program description in 1-5 sentences For Closed Lab Assignments, add the following line to the end of your source program: System.out.println(“Coded by __________ ” ); //enter your name here 01/12/09 – Some of the code and instructions for lab assignments is available at the publisher’s website (www.aw.com/cssupport) along with additional information at the class web site. You do not have to type in the whole program – just add the necessary code. Do not forget to logout! Come to closed lab prepared to finish the assignment in 1 ½ hours. 01/12/09 – Homework#1 was assigned in the first class. Check the syllabus for the assignment. It is due on Wednesday, Jan 28, 2009. You may use JGrasp for this assignment since we have not learned how to use Linux yet. Also, include 2
01/12/09 – Due to the Martin Luther King Holiday, class will not meet on Monday, January 19. Everyone should plan to come to lab on Wednesday, January 21, that week after lecture. 01/13/09 – Information provided by a student for reaching the CS server from home :
01/22/09 – Project#1 has been posted on the class web site. It will be due on February 4, 2009. Remember that, for projects, all classes like the Weather class, the WeatherList class, and the Driver class must have a documentation block preceding the class that explains briefly what the class does. This documentation is in addition to the documentation requirements given above. All projects must be submitted in a folder with the assignment page, UML, the source (properly documented) and the required outputs. 01/22/09 – I can’t emphasize enough that you must read and partially prepare the lab assignments before you come to lab. You only have 110 minutes to complete the assignment. The lab assignments are posted on the class website.
Sunny 21 50. Cloudy 50 60. Raining 60 67. PartlyCloudy 32 57. Sunny 80 71. Sunny 100 75. Raining 170 91. PartlyCloudy 300 70. Cloudy 200 88. Snow 345 29. Snow 346 28. 01/22/09 – Sorts and Searches for Project 1 Sequential Search public static int search (int[] x, int target) { int i; for (i = 0; i < x.length; i++) if (x[i] == target)
Enhanced Bubble Sort (which exits early if array becomes sorted) public static void bubble (int[] x) // ascending order { boolean sorted; int temp; int numpairs = x.length-1; do { sorted = true; for (int i = 0; i < numpairs; ++i) if (x[i] > x[i+1]) { temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; sorted = false; } numpairs--; } while (sorted == false); } Insertion Sort //ascending order public static void insertion (int [] x) { for ( int i = 0; i < x.length; ++i) { int key = x[i]; int position = i; while (position > 0 && x[position-1] > key) { x[position] = x[position-1]; position--; } x[position] = key; } } 01/22/09 – HINT FOR PROJECT 1 Remember to use the count or itemCount of your WeatherList class to control searches, print, and sort methods. Otherwise you will get a null item error. 01/26/09 – Help with coding standards from David Hood, a former lab assistant
2/2/09 – I have decided to make Project 1 due on Feb 9, 2009. If turned in this Wednesday, it will be marked early for 5 extra points.
Remember to use the count or itemCount of your WeatherList class to control searches, print, and sort methods. Otherwise you will get a null item error. 02/4/09 – By the end of the day on Monday, Feb 2, I classified each student as engaged in this course, not engaged, or has not shown up at all (the last two classifications caused you to be contacted by the registrar to see if there was a problem). If you are serious about this course, you should have turned in lab#1, lab#2, and the first homework by last so you remain on the class roster as engaged. If you are having problems completing the first assignments or you received an “unengaged”, please see me ASAP. 02/4/09 – I can’t emphasize enough that you must read and partially prepare the lab assignments before you come to lab. You only have 110 minutes to complete the assignment. The lab assistant and I must leave at the end of the 110 minute session and lock up the lab. 02/4/09 – Project#2 has been posted on the class website. It is due on Mon, Feb 23. Start working on it as soon as possible to help prepare you for Quiz 1, which is Feb
for (i=0;i<numElements;i++) // look for position where value must be added if (toBeAdded < list[i]) break; for (int j=numElements; j>i; j--) // move array elements up, from top down list[j] = list[j-1]; // to that position, i list[i] = toBeAdded; numElements++; // add new element to position i }
but this one is descending sequence: int i=0; while (itemToInsert <= a[i]) i++; for (int j=n; j>i; j--) a[j] = a[j-1]; // move rest of array down one position a[i] = itemToInsert; // insert element in array n++; 03/19/09 – A third example of code to add a value to a sorted array list of integers (ascending sequence) // array empty, add value to first position if (numElements == 0) list[numElements++] = value; // value to be added goes after data in array because it’s larger else if (value > list[numElements-1] list[numElements++] = value; else { // find proper position to insert new value while moving each //array position back one to make room int pos = numElements – 1; while (pos >= 0 && value < list [pos]) { list [pos + 1] = list [pos]; pos --; } list [++pos] = value; numElements++; } 03/19/09 – A fourth example of code to add a value to a sorted list of integers – Lab 4
03/19/09 Source program to transpose a matrix
if (numElements == 0) list[numElements++] = value; // value to be added goes after data in array because it’s larger else if (value > list[numElements-1] list[numElements++] = value; else { // find proper position to insert new value while moving each //array position back one to make room int pos = numElements – 1; while (pos >= 0 && value < list [pos]) { list [pos + 1] = list [pos]; pos --; } list [++pos] = value; numElements++; } 09/19/09 – A fourth example of code to add a value to a sorted list of integers – Lab 4
4/1/09 – We are starting a section on graphics/GUI in class this week so you will be able to begin thinking about a presentation on graphics or GUI during the week of April 20. This presentation will be counted as Project#6. A completed folder is required for Project 6 – it may be turned in on the day of presentation or by April 29, the last day of class. Project 6 is now posted on the class web site. 4/1/09 –Over this weekend, read Chapters 4 and 12 in your text book and then choose one of the graphics problems from the electronic lab book for your presentation or make up a graphics/GUI project of your own. I’m encouraging you to try to design a game program this semester if you design your own. (Chapters refer to Electronic Lab Book) M-W Topic Student Presenter M-W April 20 *A Polygon Person, Chapter 7 Bahati & Kosche April 20 *An Array of Radio Buttons, Chapter 7 Zack Johnson April 20 *Drawing Circles with Mouse Clicks, Chapter 7 Kungu&Ashrafi April 20 *Moving Circles with the Mouse and more, Chapter 7 Brian Hunt April 20 *Moving a Stick Figure, Chapter 7 Williams April 20 Rebound Revisited, Chapter 8 FULLER April 20 Count Down, Chapter 8 Millett April 20 Graphics program from end of Chapter 12 in Java Illuminated text LAWSON & MOSBY April 20 Own Design Chopson & Mason April 20 Own Design KEMELMAKHER April 22 Painting Shapes, Chapter 9 (you must add GUI to this one and explain the polymorphism) April 22 Coloring a Moveable Circle, Chapter 9 Duke & Banscri April 22 Speed Control, Chapter 9 Klimek April 22 Enhancing a Movable Circle, Chapter 10 Gross April 22 A Currency Converter, Chapter 10 Garrigus April 22 Listing Prime Numbers, Chapter 10 Lane April 22 Graphics program from end of Chapter 12 in Java Illuminated text April 22 Own Design Dyett April 22 Own Design Boyd & Shadix
current.next = current.next.next; else System.out.println(“” + item + “ not found in list”); } 11/12/08 – An insertInOrder method for linked lists: (can be used for extra credit) public void insertInOrder (int n) { Node newnode = new Node (n); if (list == null) list = newnode; else { Node temp = list; if (temp.data >= newnode.data) {newnode.next = list; list = newnode; } else { while (temp.next != null && temp.next.data < newnode.data) temp = temp.next; newnode.next = temp.next; temp.next = newnode; } } } 4/14/09 – SCHEDULE FOR REST OF SEMESTER (T-R class in parens) April 15 Binary Trees Closed lab 10 April 20 Presentations Everyone must attend, attendance taken. Closed lab 11 April 22 Second day of Presentations Everyone must attend, attendance taken. Closed lab 11 April 27 Project 5 due Review for Quiz 3 No closed lab April 29 Quiz 3 (no final) No closed lab Project 6 due on day of presentation or today at the latest 4/14/09 – I keep all work for a semester, not counting summer. You may stop by the office during my office hours next semester or in the fall to get your last assignments and test 3. 4/14/09 – Lab 12 is optional. If you choose to do it, it counts as 5 extra points on Project 6. It must be turned in by Wednesday April 29. The lab (J251) will be open after class the week of April 27 to work on this optional lab or any make-up work. The lab assistant (Sean) will be there but I will not be in lab.
4/28/09 - Here is a graphics review and a sample test. You will not be tested on graphics or GUI material. That part is for your information only. Review - CS1302 Quiz III – Lewis and Loftus Text Chapters: Chapters1-12 (Applets & GUI sections), 11, 13, 14 I. GRAPHIC and GUI REVIEW a. Applets and Drawing Shapes 1.) Applets are Java programs that are usually transported across a network and executed using a Web browser. Java applications are stand-alone programs that can be executed using the Java interpreter. 2.) Most shapes can be drawn filled or unfilled. The Java coordinate system places the 0,0 point at the top left-hand corner of the screen. 3.) A bounding rectangle is often used to define the position and size of curved shapes, such as circles and ovals. 4.) An arc is a segment of an oval; the segment begins at a specific start angle and extends for a distance specified by the arc angle. 5.) The color class provides several predetermined colors – see page 95. 6.) The pixels of a black and white picture can be represented using a single bit for each color, with 0 for white and 1 for black. 4.) The pixels of a color picture can be represented using three numbers (called the RGB value) which represent the relative contributions of three primary colors: red, green, and blue. b. Applet Methods and Graphical Objects 1.) Applet methods: paint – automatically invoked by the applet – used in Chapter p. 98 init – executed when applet is loaded, before paint. start – invoked every time the applet becomes active, such as when a browser returns to the page it is on. stop – invoked automatically when the applet becomes inactive. other applet methods – see page 737-738. 2.) Graphical objects, such as the stick figure, can be defined and instantiated just like any other Java object. c. Dialog Boxes 1.) JOptionPane is a Swing class that facilitates the creation of dialog boxes. A dialog box is a small window that appears for the purpose of conveying information (message dialog), confirming an action ( confirm dialof), or accepting input (input dialog). Generally, dialog boxes are used in specific situations for brief user interactions. 2.) A GUI is made up of graphical components, events that represent user actions, and listeners that respond to those events. Inner classes are often used to define listener objects. GUI components can be part of an applet or application program. An application program needs a window, such as a frame, to serve as a container for the GUI elements in the program. You can also organize components in a panel but a panel cannot be displayed on its own – a panel must be added to a frame. An applet is itself a container object so the components can be added directly to the content pane (obtained with the getContentPane() method), the primary container for the applet. d. Check Boxes and Radio Buttons
C. Optimize user abilities by providing shortcuts and other redundant means to accomplish a task D. Be consistent in GUI layout and coloring schemes 2.) The layout manager of a container can be explicitly set and determines how components are visually presented. The full appearance of the GUI is a function of the containment hierarchy and the layout managers of each of the containers. There are 5 different layouts: A. Flow Layout – the width of the container determines how many components fit on a row – is the default B. Border Layout – contains five areas (north, south, west, east, center) – not all need to be used – the areas that contain components will fill in the space. C. Grid Layout – cells are filled in the order components are added to the container. D. Box Layout - a single row or column of components. It uses invisible components to provide space between components (rigid and glue) 3.) Special features of components A. Tool tip – small amount of text that appears when cursor comes to rest on component – provides info about that component – p. 549-556. B. Mnemonic – character that can be used to activate a control the same way the mouse click on the component would. Character is depressed with ALT key - p. 549-556. C. Disable a component – when it is not a viable option for the user at a given time - p. 549-556. 4.) Borders can be applied to Swing components to group them better or enhance the visual effect. There are 7 different borders that can also be used in combinations. 5.) Scroll pane, along with its optional scroll bars, can be used to view an image that is bigger than the window – p. 562. 6.) Split pane – displays two (or more) components separated by a movable divider bar. Pane can be split horizontally or vertically – p. 564. 7.) List – whole list of choices can be viewed at once and one (or several) selected – p. 566. 8.) Combo box – provides a list of options from a drop down menu from which the user can choose one selection – p. 556. 9.) Slider - lets user specify a numeric value within a bounded range. A slider prevents an improper value from being entered and conveys the valid range to the user – page 521-525. 10.) Some events are generated by every Swing component; others are generated only by a few so you can’t add any listener to any component. Each component generates a certain set of events, and only listeners of those types can be added to the component – Key Events – p. 420-425. 11.) Review Project#6 (Discriminent Problem with GUI in Spring, 03) or (GUI/Graphic presentations in Fall, 05) 12.) Review Closed lab#10 Presentations (Applet and GUI labs in lab book) and Project#5 (Summer,03) II. Chapter 11 Files and Exceptions A thrown exception can be handled in one of three ways: it can be ignored, which will cause the program to terminate, it can be handled where it occurs using a try/catch block, or it can be caught and handled higher in the method calling hierarchy. If it propagates out of the main method without being caught, the program terminates. A catch phrase, after a particular try, names the exception encountered and specifies the action to be taken.
A programmer can define his own exception and create that exception with the throw statement. A stream is a sequential series of bytes that is either coming into the computer (input stream) or going out (output). A stream either handles 16 bit character data (text file) or 8 bit bytes (program file). FileReader and BufferedReader classes can be used together to create a convenient text file input stream which reads 1 string at a time (readLine). For numeric data, you would have to have 1 number/line. FileWriter and PrintWriter classes can be used together to create a convenient text file output stream which can use either the print or println method to print a line of text data and, optionally, skip to a new line. The readLine method returns a null reference if an attempt is made to read past the end of the input file. Output files should be explicitly closed. For more review: Do any exercises at the end of the chapter, page791- Do programming projects 46, 49, 50, 51 Go over the three Exception labs completed in class: Exceptions Aren’t Always Errors Placing Exception Handlers Throwing Exceptions III. Chapter 13 Recursion 1.) Recursion is a programming technique in which a method calls itself, solving a smaller version of the problem each time, until the terminating condition is reached. A key to being able to program recursively is being able to think recursively. Takes practice. Mathematical problems and formulas are often expressed recursively. 2.) Any recursive definition has a non-recursive part, called the base case, which permits the recursion to eventually end by returning through the calling hierarchy. Infinite recursion occurs when there is no base case that serves as an end or when the base case is improperly specified. In a recursive program with infinite recursion, the program will often result in an error that indicates that available memory has been exhausted. 3.) Recursion is not necessary. Every recursive algorithm can be written iteratively. However, some problem solutions are much more elegant and straightforward when written recursively. 4.) Recursion has the overhead of multiple method calls. Use iterative solutions when they are simpler or more easily understood and programmed. 5.) Direct recursion is when a method calls itself. Indirect recursion is when a lower level method calls the original method. 6.) Exercises 1-38, pages 969- 7.) Do problems 39-44 at end of chapter on page 977-978. 8.) Review Closed Labs in Chapter 11: Palindromes – recursive String handling (not done fall 05, summer or fall 06) Recursive Sequential and Binary Search – recursive array handling 9.) Review Project#4 Fibonacci Sequence – recursive formula calculation 10.) Review class notes for additional recursive examples IV. Chapter 14 Data Structures 1.) An abstract data type (ADT) is a collection of data and the operations that can be performed on that data. An ADT hides the implementation of a data structure behind a well-defined interface. For example, we talk about an ADT of a stack – a
2.) (5) Code a member method for the class IntList on page 254 of the Lab Book to count the number of nodes in the list that are equal to a target integer value which is passed to the method. Return that count. 3.) (15) Compare and contrast implementing a stack as an array and as a linked list. Discuss how they are different and/or the same conceptually and physically and how the code of the constructor, push, and pop methods will differ. 4.) (15) Compare and contrast implementing a queue as an array and as a linked list. Discuss how they are different and/or the same conceptually and physically and how the code of the constructor, enqueue, and dequeue methods will differ. 5.) (5) Exercise 12.6 on page 655. (Mark the front and last element.) 6.) (5) Exercise 12.8 on page 656. (Mark the top element.) 7.) 10) Define a private binary tree EmploeeNode class that includes a String name, integer idnum, and a float rate_of_pay. (Remember that a binary tree node has 2 pointers. Call them left and right) Also code a parametized constructor for this node class which will allow the user to set up a node with a particular employee name, id, and pay rate. 8.) (5) Write the recursive code for a method that would allow you to search for a particular id in the binary tree with the node defined above. Remember that the logic for the search of a binary tree is: (assume root points to the binary tree) if the tree is empty the target id is not there – return false else if the target id is the root id return true else if the target id is smaller than the root’s id search the left subtree else search the right subtree
IGNORE THE FOLLOWING ANNOUNCEMENTS UNTIL PROPERLY DATED**