Week 14 Lab A Linked List of Integers Maximum Points = 10

Similar documents
Chapter 13: Collections Lab Exercises

System.out.print("Enter sales for salesperson " + i + ": "); sales[i] = scan.nextint();

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15)

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

CCHS Math Recursion Worksheets M Heinen CS-A 12/5/2013. Recursion Worksheets Plus Page 1 of 6

Chapter 10 Linked Lists A

Java Coding 3. Over & over again!

King Saud University College of Computer and Information Systems Department of Computer Science CSC 113: Java Programming-II, Spring 2016

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Fundamentals of Programming Data Types & Methods

ECM1406. Answer Sheet Lists

CSC 1051 Data Structures and Algorithms I

This exam is open book. Each question is worth 3 points.

Page 1 / 3. Page 2 / 18. Page 3 / 8. Page 4 / 21. Page 5 / 15. Page 6 / 20. Page 7 / 15. Total / 100. Pledge:

CSC 1051 Data Structures and Algorithms I

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

COMP 202 Java in one week

AP Computer Science. TextLab05 Java Assignment. Assignment Purpose: Do not copy this file, which is provided. TextLab05 Student Version

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Introduction to Computer Science Unit 2. Exercises

H212 Introduction to Software Systems Honors

CSC 1051 Data Structures and Algorithms I

SCOPE. public class ABC { public static void main( String args [ ] ) { Not accessible outside the block. Local to this block; accessible within it

COMP 202. Java in one week

Midterm Examination (MTA)

CAT.woa/wa/assignments/eclipse

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

CS 101 Exam 2 Spring Id Name

CSCI 111 Midterm Spring 2014

Selection Statements and operators

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

MIDTERM REVIEW. midterminformation.htm

CS141 Programming Assignment #6

Practice Midterm 1 Answer Key

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

Assignment-1 Final Code. Student.java

Oct Decision Structures cont d

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

Arrays. Eng. Mohammed Abdualal

Course Project Hospital Information Management

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

CPS109 Lab 7. Source: Big Java, Chapter 7 Preparation: read Chapter 7 and the lecture notes for this week.

Software and Programming 1

SAMPLE EXAM Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Introduction to Linked Data Structures

CS 101 Spring 2007 Midterm 2 Name: ID:

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

Array. Array Declaration:

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Selection Statements and operators

CS212 Midterm. 1. Read the following code fragments and answer the questions.

Object Oriented Programming. Java-Lecture 1

1 Short Answer (5 Points Each)

Assignment 8B SOLUTIONS

St. Edmund Preparatory High School Brooklyn, NY

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

Assignment 19 Deadline: Nov pm COSC211 CRN15376 Session 15 (Nov. 7)

Bjarne Stroustrup. creator of C++

Question 1 [20 points]

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS1083 Week 2: Arrays, ArrayList

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

AP Computer Science Unit 1. Writing Programs Using BlueJ

1.00 Introduction to Computers and Engineering Problem Solving. Final Examination - May 19, 2004

Notes on access restrictions

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

public class Q1 { public int x; public static void main(string[] args) { Q1 a = new Q1(17); Q1 b = new Q1(39); public Q1(int x) { this.

COMP-202A, Fall 2007, All Sections Assignment 1

COE 212 Engineering Programming. Welcome to the Final Exam Thursday December 15, 2016

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

AP COMPUTER SCIENCE A

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CS Computers & Programming I Review_01 Dr. H. Assadipour

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

10/1/2018 Programming Data Structures. Quiz Exception Handling

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

CS 302 Week 9. Jim Williams

AP CS Unit 3: Control Structures Notes

CMPT 125: Lecture 4 Conditionals and Loops

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

Chapter 10 Recursion

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

CS111: PROGRAMMING LANGUAGE II

CIS 110 Introduction to Computer Programming Summer 2017 Final. Recitation # (e.g., 201):

ITERATION WEEK 4: EXMAPLES IN CLASS

Suppose that we have linked list of integers where each node is represented as: // An item in the list.

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

CSIS-120 Final Exam Fall 2015 Name:

Chapter 3. Selections

Problem Grade Total

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Transcription:

Week 14 Lab A Linked List of Integers Maximum Points = 10 File IntList.java contains definitions for a linked list of integers. The class contains an inner class IntNode that holds information for a single node in the list (a node has a value and a reference to the next node) and the following IntList methods: public IntList() constructor; creates an empty list of integers public void addtofront(int val) takes an integer and puts it on the front of the list public void addtoend(int val) takes an integer and puts it on the end of the list public void removefirst() removes the first value from the list public void print() prints the elements in the list from first to last File IntListTest.java contains a driver that allows you to experiment with these methods. Save both of these files to your directory, compile and run IntListTest, and play around with it to see how it works. Then add the following methods to the IntList class. [use the print method as a model for 1,2, &4; use the addtoend method as a model for #3.] For each, add an option to the driver to test it. 1. public int length() returns the number of elements in the list 2. public String tostring() returns a String containing the print value of the list. IF YOU HAVE TIME: 3. public void removelast() removes the last element of the list. If the list is empty, does nothing. 4. public void replace(int oldval, int newval) replaces all occurrences of oldval in the list with newval. Note that you can still use the old nodes; just replace the values stored in those nodes. **************************************************************** FILE: IntList.java Purpose: Defines a class that represents a list of integers **************************************************************** public class IntList private IntNode front; first node in list Constructor. Initially list is empty. public IntList() front = null; Adds given integer to front of list. public void addtofront(int val) front = new IntNode(val,front); -------------------------------------- Adds given integer to end of list. -------------------------------------- public void addtoend(int val) IntNode newnode = new IntNode(val,null);

if list is empty, this will be the only node in it if (front == null) front = newnode; else make temp point to last thing in list IntNode temp = front; while (temp.next!= null) temp = temp.next; link new node into list temp.next = newnode; -- Removes the first node from the list. If the list is empty, does nothing. -- public void removefirst() if (front!= null) front = front.next; ------- Prints the list elements from first to last. ------- public void print() System.out.println("---------------------"); System.out.print("List elements: "); IntNode temp = front; while (temp!= null) System.out.print(temp.val + " "); temp = temp.next; System.out.println("\n---------------------\n"); *************************************************************** An inner class that represents a node in the integer list. The public variables are accessed by the IntList class. *************************************************************** private class IntNode public int val; value stored in node public IntNode next; link to next node in list -------------------------- Constructor; sets up the node given a value and IntNode reference -------------------------- public IntNode(int val, IntNode next) this.val = val; this.next = next;

************************************************************* IntListTest.java Driver to test IntList methods. ************************************************************* import java.util.scanner; public class IntListTest private static Scanner scan; private static IntList list = new IntList(); ----------------------- Creates a list, then repeatedly prints the menu and does what the user asks until they quit. ----------------------- public static void main(string[] args) scan = new Scanner(System.in); printmenu(); int choice = scan.nextint(); while (choice!= 0) dispatch(choice); printmenu(); choice = scan.nextint(); Does what the menu item calls for. public static void dispatch(int choice) int newval; switch(choice) case 0: System.out.println("Bye!"); case 1: add to front System.out.println("Enter integer to add to front"); newval = scan.nextint(); list.addtofront(newval); case 2: add to end System.out.println("Enter integer to add to end"); newval = scan.nextint(); list.addtoend(newval); case 3: remove first element list.removefirst(); case 4: print

list.print(); default: System.out.println("Sorry, invalid choice"); Prints the user's choices public static void printmenu() System.out.println("\n Menu "); System.out.println(" ===="); System.out.println("0: Quit"); System.out.println("1: Add an integer to the front of the list"); System.out.println("2: Add an integer to the end of the list"); System.out.println("3: Remove an integer from the front of the list"); System.out.println("4: Print the list"); System.out.print("\nEnter your choice: "); (Due before end of the day on Friday, November 19, 2010) Submit your.java files containing your program to the dropbox in WebCT. Grades are determined using the following scale: Runs correctly.. : /3 Correct output.. : /2 Design of output.. : /1 Design of logic : /2 Standards.: /1 Documentation....: /1