Lab 11. A sample of the class is:

Size: px
Start display at page:

Download "Lab 11. A sample of the class is:"

Transcription

1 Lab 11 Lesson 11-2: Exercise 1 Exercise 2 A sample of the class is: public class List // Methods public void store(int item) values[length] = item; length++; public void printlist() // Post: If the list is not empty, the elements are // printed on the screen; otherwise "The list // is empty" is printed on the screen if (length > 0) for (int i = 0; i < length; i++) System.out.println(values[i]); System.out.println("The list is empty"); public int length() // Post: return value is the number of items in the list return length; public boolean isempty() // Post: returns true if list is empty; false otherwise return (length == 0); public boolean isfull() // Post: returns true if there is no more room in the // list; false otherwise return (values.length == length); public List(int maxitems) // Constructor // Post: Empty list is created with maxitems cells values = new int[maxitems]; // Data fields private int length; private int[] values; A sample of the driver is:

2 //********************************************************** // Class MyUseList is a driver for class List. The list // is instantiated, values are read in and printed. //********************************************************** import java.io.*; // Access file classes public class MyUseList // This class is a driver to execute class List public static BufferedReader infile; public static void main(string[] args) throws IOException // Data file for this test is on "int.dat" infile = new BufferedReader(new FileReader("int.dat")); String line; // instantiate a list List list; list = new List(20); // read the file and store the values in the list line = infile.readline(); while (line!= null) if (!list.isfull()) list.store(integer.parseint(line)); System.out.println ("List is full!"); line = infile.readline(); list.printlist(); infile.close(); // Close the input file Lesson 11-2: Exercise 3, 4, and 5: The last four lines are: Note need to add to front of the class import java.io.*; Additional methods for exercises 3, 4 and 5 are: // added for exercises 3, 4, and 5 public PrintWriter outfile; public boolean isthere(int item) // Post: returns true if item is in the list; false otherwise

3 // adapted from page 534 of main text int index = 0; while ((index < length) && (values[index]!= item)) index++; return (index < length); public void removeitem(int item) // Pre: value is in the list // Post: value removed from the list and remaining items shifted up // adapted from page 535 of main text int index = 0; boolean found = false; while ((index < length) &&!found) if (values[index] == item) found = true; index++; if (found) for (int count = index; count < length - 1; count++) values[count] = values[count+1]; length--; public void writelist(string file) throws IOException // Post: values in the list are written to the file outfile = new PrintWriter(new FileWriter(file)); int index; for (index = 0; index < length; index++) outfile.println(values[index]); outfile.close(); Exercise 6: An example of the Driver (extended from Exercise 2) : //********************************************************** // Exercise 5 and 6 // Class MyUseList is a driver for the extended class List. // The list is instantiated, values are read in and printed. //********************************************************** import java.io.*; // Access file classes public class MyUseList2 // This class is a driver to execute class List public static BufferedReader infile;

4 public static void main(string[] args) throws IOException // Data file for this test is on "int.dat" infile = new BufferedReader(new FileReader("int.dat")); String line; // instantiate a list List list; list = new List(20); // read the file and store the values in the list line = infile.readline(); while (line!= null) if (!list.isfull()) list.store(integer.parseint(line)); System.out.println ("List is full!"); line = infile.readline(); // print the List list.printlist(); // Search for values and print result System.out.println("Look for 10 " + list.isthere(10) + " Look for 5" + list.isthere(56)); // delete value 10 and print the list list.removeitem(10); list.printlist(); // added test - write list to file list.writelist("newint.dat"); infile.close(); // Close the input file The output from the program is the file and andint.dat and: Look for 10 true Look for 5false

5 Lesson 11-3 Exercise 1 to 5, and NOTE: I have used the method name store rather than insert. This keeps consistency with naming in Lesson 11-2 and Lesson The constructor is the same as List except called Slist(), the store() needs rebuilding, based on the example on page 548 in the main text. The rest are the same as List. For efficiency reasons, you may consider also changing the isthere() method to use a binary search algorithm, see page 557 of the main text. Store is now: public void store(int item) // Modeled on example on page 548 of main text if (!isfull()) int index = length - 1; //Loop control variable while ((index >= 0) && (item < values[index])) // find insertion point values[index+1] = values[index]; index--; values[index+1] = item; // Insert item length++; // Increment item count Lesson 11-4: Exercise 6 The driver is exactly the same as the driver for Lesson 11-2 exercise 6, except it instantiates a SList object. When you run this please reorder the test data sequence so it is not already in ascending order. This is needed to test the new store method. Exercise 1 and 2: NOTE this lesson is provided for completeness. The contents will not be included in any assessment as there is no working solution provided in either text. NOTE - the class names have been changed to separate the classes from earlier versions. An example of the program is:

6 import java.io.*; public class ListLesson4 extends AbstractList // Methods public void store(int item) values[length] = item; length++; public ListLesson4 (int maxitems) // Constructor uses the AbstractList constructor // Post: Empty list is created with maxitems cells super(maxitems); An example of the AbstractList class is import java.io.*; public abstract class AbstractList // Methods public abstract void store(int item); public void printlist() // Post: If the list is not empty, the elements are // printed on the screen; otherwise "The list // is empty" is printed on the screen if (length > 0) for (int i = 0; i < length; i++) System.out.println(values[i]); System.out.println("The list is empty"); public int length() // Post: return value is the number of items in the list return length; public boolean isempty() // Post: returns true if list is empty; false otherwise return (length == 0); public boolean isfull() // Post: returns true if there is no more room in the // list; false otherwise

7 return (values.length == length); public AbstractList (int maxitems) // Constructor // Post: Empty list is created with maxitems cells values = new int[maxitems]; // Data fields int length; int[] values; // added for exercises 3, 4, and 5 public PrintWriter outfile; public boolean isthere(int item) // Post: returns true if item is in the list; false otherwise // adapted from page 534 of main text int index = 0; while ((index < length) && (values[index]!= item)) index++; return (index < length); public void removeitem(int item) // Pre: value is in the list // Post: value removed from the list and remaining items shifted up // adapted from page 535 of main text int index = 0; boolean found = false; while ((index < length) &&!found) if (values[index] == item) found = true; index++; if (found) for (int count = index; count < length - 1; count++) values[count] = values[count+1]; length--; public void writelist(string file) throws IOException // Post: values in the list are written to the file outfile = new PrintWriter(new FileWriter(file)); int index;

8 for (index = 0; index < length; index++) outfile.println(values[index]); outfile.close(); The output from running the driver is the same. Exercise 3 and 4 Note: in this solution SListLesson4 is derived from ListLesson taking benefit of inheritence. See example on page 547 of the main text. An example of the SListLesson4 class is: import java.io.*; public class SListLesson4 extends ListLesson4 // Methods public void store(int item) // Modelled on example on page 548 of main text if (!isfull()) int index = length - 1; //Loop control variable while ((index >= 0) && (item < values[index])) // find insertion point values[index+1] = values[index]; index--; values[index+1] = item; // Insert item length++; // Increment item count public SListLesson4(int maxitems) // Constructor invokes constructor from List // Post: Empty list is created with maxitems cells super(maxitems); Exercise 5 and 6 The only methods that need to be concrete are the store and constructor which both override those in class ListLesson4. Note; This works because String already has a compareto method. If you used the same abstract class with an object that did not have a compareto you would need to implement the interface Comparable and add a custom compareto. See Section 11.7 in the main text. The new AbstractList is now:

9 import java.io.*; public abstract class AbstractListComparable // Methods public abstract void store(comparable item); public void printlist() // Post: If the list is not empty, the elements are // printed on the screen; otherwise "The list // is empty" is printed on the screen if (length > 0) for (int i = 0; i < length; i++) System.out.println(values[i]); System.out.println("The list is empty"); public int length() // Post: return value is the number of items in the list return length; public boolean isempty() // Post: returns true if list is empty; false otherwise return (length == 0); public boolean isfull() // Post: returns true if there is no more room in the // list; false otherwise return (values.length == length); public AbstractListComparable (int maxitems) // Constructor // Post: Empty list is created with maxitems cells values = new Comparable[maxItems]; // Data fields protected Comparable[] values; protected int length; // added for exercises 3, 4, and 5 public PrintWriter outfile; public boolean isthere(comparable item) // Post: returns true if item is in the list; false otherwise

10 // adapted from page 534 of main text - using generic items int index = 0; while ((index < length) && (values[index].compareto(item)!= 0)) index++; return (index < length); public void removeitem(comparable item) // Pre: value is in the list // Post: value removed from the list and remaining items shifted up // adapted from page 535 of main text int index = 0; boolean found = false; while ((values[index].compareto(item)!= 0) &&!found) if (values[index].compareto(item) == 0) found = true; index++; if (found) for (int count = index; count < length - 1; count++) values[count] = values[count+1]; length--; public void writelist(string file) throws IOException // Post: values in the list are written to the file outfile = new PrintWriter(new FileWriter(file)); int index; for (index = 0; index < length; index++) outfile.println(values[index]); outfile.close(); The new SList with comparable is now: import java.io.*; public class SListLesson4Comparable extends AbstractListComparable // Methods public void store(comparable item)

11 // Modelled on example on page 548 of main text if (!isfull()) int index = length - 1; //Loop control variable String s1 = (String)item; // System.out.println(index + " " + length + s1); if (length!= 0) // not first time through String s2 = (String)values[index]; while ((index >= 0) && (s1.compareto(s2)< 0)) // find insertion point values[index+1] = values[index]; index--; if (index >= 0) // skip if to insert item in front of list // System.out.println(index + (String)values[index]); s2 = (String)values[index]; //end if (index >= 0) // end while ((index >= ). // end if (length!= 0) values[index+1] = item; // Insert item length++; // Increment item count // end public void store public SListLesson4Comparable(int maxitems) // Constructor // Post: Empty list is created with maxitems cells super(maxitems); The new driver is now: //********************************************************** // Lesson 11-4 Exercise 5 and 6 // Class MyUseList is a driver for the extended class List. // The list is instantiated, values are read in and printed. //********************************************************** import java.io.*; // Access file classes public class MyUseSListLesson4Comparable // This class is a driver to execute class List public static BufferedReader infile; public static void main(string[] args) throws IOException // Data file for this test is on "int.dat" infile = new BufferedReader(new FileReader("Strings.dat")); String line;

12 // instantiate a list SListLesson4Comparable list; list = new SListLesson4Comparable(20); // read the file and store the values in the list line = infile.readline(); while (line!= null) if (!list.isfull()) list.store(line); System.out.println ("List is full!"); line = infile.readline(); // print the List list.printlist(); infile.close(); // Close the input file The last two values printed are: white yellow

A sample print out is: is is -11 key entered was: w

A sample print out is: is is -11 key entered was: w Lab 9 Lesson 9-2: Exercise 1, 2 and 3: Note: when you run this you may need to maximize the window. The modified buttonhandler is: private static class ButtonListener implements ActionListener public void

More information

Principles of Computer Science I

Principles of Computer Science I Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120A - Fall 2004 Lecture Unit 7 Review Chapter 4 Boolean data type and operators (&&,,) Selection control flow structure if, if-else, nested

More information

Recitation: Loop Jul 7, 2008

Recitation: Loop Jul 7, 2008 Nested Loop Recitation: Loop Jul 7, 2008 1. What is the output of the following program? Use pen and paper only. The output is: ****** ***** **** *** ** * 2. Test this program in your computer 3. Use "for

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O 1 Sending Output to a (Text) File import java.util.scanner; import java.io.*; public class TextFileOutputDemo1 public static void

More information

IT101. File Input and Output

IT101. File Input and Output IT101 File Input and Output IO Streams A stream is a communication channel that a program has with the outside world. It is used to transfer data items in succession. An Input/Output (I/O) Stream represents

More information

JAVA NOTES DATA STRUCTURES AND ALGORITHMS

JAVA NOTES DATA STRUCTURES AND ALGORITHMS 79 JAVA NOTES DATA STRUCTURES AND ALGORITHMS Terry Marris July 2001 11 QUEUE IMPLEMENTATION - ARRAYS 11.1 LEARNING OUTCOMES By the end of this lesson the student should be able to describe how an array

More information

Here is a hierarchy of classes to deal with Input and Output streams.

Here is a hierarchy of classes to deal with Input and Output streams. PART 25 25. Files and I/O 25.1 Reading and Writing Files A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data

More information

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive Solutions Exercises Unit 19 1 General Algorithm for Recursion

More information

ECM1406. Answer Sheet Lists

ECM1406. Answer Sheet Lists ECM1406 Answer Sheet Lists These questions are based on those found in Chapters 3 and 4 of the core text Data Structures Using Java by Malik and Nair. The source code for the ArrayListClass, UnorderedArrayList,

More information

FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO {

FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO { FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO public static void main(string[] args) throws IOException BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int frames,

More information

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi CSE 143 Overview Topics Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 2/3/2005 (c) 2001-5, University of Washington 12-1 2/3/2005 (c) 2001-5,

More information

Input from Files. Buffered Reader

Input from Files. Buffered Reader Input from Files Buffered Reader Input from files is always text. You can convert it to ints using Integer.parseInt() We use BufferedReaders to minimize the number of reads to the file. The Buffer reads

More information

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Sec. 19.1, Appendix A2 11/2/2003 (c) 2001-3,

More information

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Sec. 19.1, Appendix A2 11/2/2003 (c) 2001-3,

More information

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 4/27/2004 (c) 2001-4, University of

More information

CSCD 326 Data Structures I Stacks

CSCD 326 Data Structures I Stacks CSCD 326 Data Structures I Stacks 1 Stack Interface public interface StackInterface { public boolean isempty(); // Determines whether the stack is empty. // Precondition: None. // Postcondition: Returns

More information

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE CSE 143 Overview Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 10/20/2004 (c) 2001-4, University of

More information

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output. CSE 143 Java Streams Reading: 19.1, Appendix A.2 GREAT IDEAS IN COMPUTER SCIENCE REPRESENTATION VS. RENDERING 4/28/2002 (c) University of Washington 09-1 4/28/2002 (c) University of Washington 09-2 Topics

More information

Full file at Chapter 2 - Inheritance and Exception Handling

Full file at   Chapter 2 - Inheritance and Exception Handling Chapter 2 - Inheritance and Exception Handling TRUE/FALSE 1. The superclass inherits all its properties from the subclass. ANS: F PTS: 1 REF: 76 2. Private members of a superclass can be accessed by a

More information

public static boolean isoutside(int min, int max, int value)

public static boolean isoutside(int min, int max, int value) See the 2 APIs attached at the end of this worksheet. 1. Methods: Javadoc Complete the Javadoc comments for the following two methods from the API: (a) / @param @param @param @return @pre. / public static

More information

4. Finding & Displaying Record of Salesman with minimum net income. 5. Finding & Displaying Record of Salesman with maximum net income.

4. Finding & Displaying Record of Salesman with minimum net income. 5. Finding & Displaying Record of Salesman with maximum net income. Solution of problem#55 of Lab Assignment Problem Statement: Design & Implement a java program that can handle salesmen records of ABC Company. Each salesman has unique 4 digit id #, name, salary, monthly

More information

CS Week 11. Jim Williams, PhD

CS Week 11. Jim Williams, PhD CS 200 - Week 11 Jim Williams, PhD This Week 1. Exam 2 - Thursday 2. Team Lab: Exceptions, Paths, Command Line 3. Review: Muddiest Point 4. Lecture: File Input and Output Objectives 1. Describe a text

More information

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request. University of Toronto CSC148 Introduction to Computer Science Fall 2001 Mid Term Test Section L5101 Duration: 50 minutes Aids allowed: none Make sure that your examination booklet has 8 pages (including

More information

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse Object-Oriented Design Object Oriented Design Goals Robustness Adaptability Flexible code reuse Principles Abstraction Encapsulation Modularity March 2005 Object Oriented Design 1 March 2005 Object Oriented

More information

F I N A L E X A M I N A T I O N

F I N A L E X A M I N A T I O N Faculty Of Computer Studies M257 Putting Java to Work F I N A L E X A M I N A T I O N Number of Exam Pages: (including this cover sheet( Spring 2011 April 4, 2011 ( 5 ) Time Allowed: ( 1.5 ) Hours Student

More information

Assignment 8B SOLUTIONS

Assignment 8B SOLUTIONS CSIS 10A Assignment 8B SOLUTIONS Read: Chapter 8 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment,

More information

Byte and Character Streams. Reading and Writing Console input and output

Byte and Character Streams. Reading and Writing Console input and output Byte and Character Streams Reading and Writing Console input and output 1 I/O basics The io package supports Java s basic I/O (input/output) Java does provide strong, flexible support for I/O as it relates

More information

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.) COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.) Recall: The Queue ADT A data structure in which elements enter at one end and are removed from the opposite end is called a

More information

Object-Oriented Design. March 2005 Object Oriented Design 1

Object-Oriented Design. March 2005 Object Oriented Design 1 Object-Oriented Design March 2005 Object Oriented Design 1 Object Oriented Design Goals Robustness Adaptability Flexible code reuse Principles Abstraction Encapsulation Modularity March 2005 Object Oriented

More information

Java file manipulations

Java file manipulations Java file manipulations 1 Categories of Java errors We learn that there are three categories of Java errors : Syntax error Runtime error Logic error. A Syntax error (compiler error) arises because a rule

More information

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004 1.00/1.001 Introduction to Computers and Engineering Problem Solving Final / December 13, 2004 Name: Email Address: TA: Section: You have 180 minutes to complete this exam. For coding questions, you do

More information

//Initializes the variables that will hold the eventual final new versions of the string String pluralextension; String secondwordextension;

//Initializes the variables that will hold the eventual final new versions of the string String pluralextension; String secondwordextension; /* Jackson Baker Rogers High School Vowels-R-Us Final Project 12/15/14 */ import java.io.*; import java.util.stringtokenizer; public class Vowels //Variables needed for inputting outside file private static

More information

Stack Implementation

Stack Implementation Stack Implementation (In Java Using BlueJ) What is BlueJ? BlueJ is a Java integrated development environment (IDE) which has been designed specifically for learning object oriented programming in Java.

More information

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name CSC 1051 Algorithms and Data Structures I Final Examination May 12, 2017 Name Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces provided.

More information

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: CSC 1051 Algorithms and Data Structures I Final Examination May 2, 2015 Name: Question Value 1 10 Score 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 20 TOTAL 100 Please answer questions in the spaces provided.

More information

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013 CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013 Name: This exam consists of 5 problems on the following 6 pages. You may use your double-sided hand-written 8 ½ x 11 note sheet

More information

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.

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. CS 201, Fall 2013 Oct 2nd Exam 1 Name: Question 1. [5 points] What output is printed by the following program (which begins on the left and continues on the right)? public class Q1 { public int x; public

More information

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20 File IO Computer Science and Engineering College of Engineering The Ohio State University Lecture 20 I/O Package Overview Package java.io Core concept: streams Ordered sequences of data that have a source

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises Advanced Java Concepts Unit 5: Trees. Notes and Exercises A Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will

More information

23 Error Handling What happens when a method is called? 23.1 What is Exception Handling? A.m() B.n() C.p()

23 Error Handling What happens when a method is called? 23.1 What is Exception Handling? A.m() B.n() C.p() 23 Error Handling Exit program (System.exit()) usually a bad idea Output an error message does not help to recover from the error Special error return Constructors do not have a return value What if method

More information

Classes Basic Overview

Classes Basic Overview Final Review!!! Classes and Objects Program Statements (Arithmetic Operations) Program Flow String In-depth java.io (Input/Output) java.util (Utilities) Exceptions Classes Basic Overview A class is a container

More information

CS 200 File Input and Output Jim Williams, PhD

CS 200 File Input and Output Jim Williams, PhD CS 200 File Input and Output Jim Williams, PhD This Week 1. WaTor Change Log 2. Monday Appts - may be interrupted. 3. Optional Lab: Create a Personal Webpage a. demonstrate to TA for same credit as other

More information

javagently jg3e H:\ ProgTwo Programming II Lecture 7 A case study (class containing an array) and model diagrams. 02/02/2003 Dr Andy Brooks 1

javagently jg3e H:\ ProgTwo Programming II Lecture 7 A case study (class containing an array) and model diagrams. 02/02/2003 Dr Andy Brooks 1 H:\ jg3e javagently ProgTwo Lab3 Laboratory Programming II Lecture 7 A case study (class containing an array) and model diagrams. 02/02/2003 Dr Andy Brooks 1 Scope The scope of an identifier is the region

More information

Special error return Constructors do not have a return value What if method uses the full range of the return type?

Special error return Constructors do not have a return value What if method uses the full range of the return type? 23 Error Handling Exit program (System.exit()) usually a bad idea Output an error message does not help to recover from the error Special error return Constructors do not have a return value What if method

More information

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O Week 12 Streams and File I/O Overview of Streams and File I/O Text File I/O 1 I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or a file

More information

Happy Cinco de Mayo!!!!

Happy Cinco de Mayo!!!! CSC 1051 Algorithms and Data Structures I Happy Cinco de Mayo!!!! Final Examination May 5, 2018 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 20 TOTAL 100 Please answer questions

More information

CSC 1051 Algorithms and Data Structures I. Final Examination December 18, Name:

CSC 1051 Algorithms and Data Structures I. Final Examination December 18, Name: CSC 1051 Algorithms and Data Structures I Final Examination December 18, 2015 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces

More information

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary CPSC 441 Tutorial TCP Server Department of Computer Science University of Calgary TCP Socket Client Server Connection Request Server Listening on welcoming socket Client Socket Server Socket Data Simple

More information

Networking Code CSCI 201 Principles of Software Development

Networking Code CSCI 201 Principles of Software Development Networking Code CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu Server Networking Client Networking Program Outline USC CSCI 201L Server Software A server application

More information

Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*;

Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*; Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*; /* */ public

More information

Case Study: Savings Account Interest

Case Study: Savings Account Interest ecture 8 Loops: recap + example. Files: abstracting from a specific devise. Streams and Tokens. Examples. Material from the second half of Holmes Chapter 4. 1 w do you add up a sequence of numbers? = 1;

More information

Here is a hierarchy of classes to deal with Input and Output streams.

Here is a hierarchy of classes to deal with Input and Output streams. PART 15 15. Files and I/O 15.1 Reading and Writing Files A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data

More information

CSCD 326 Data Structures I Queues

CSCD 326 Data Structures I Queues CSCD 326 Data Structures I Queues 1 Linked List Queue Implementation Diagram Front Back A B C D 0 6 public interface QueueInterface { Queue Interface public boolean isempty(); // Determines whether

More information

Chapter 5 Some useful classes

Chapter 5 Some useful classes Chapter 5 Some useful classes Lesson page 5-1. Numerical wrapper classes Activity 5-1-1 Wrapper-class Integer Question 1. False. A new Integer can be created, but its contents cannot be changed. Question

More information

Data Structures G5029

Data Structures G5029 Data Structures G5029 Lecture 2 Kingsley Sage Room 5C16, Pevensey III khs20@sussex.ac.uk University of Sussex 2006 Lecture 2 Stacks The usual analogy is the stack of plates. A way of buffering a stream

More information

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST) CSCI Lab 9 Implementing and Using a Binary Search Tree (BST) Preliminaries In this lab you will implement a binary search tree and use it in the WorkerManager program from Lab 3. Start by copying this

More information

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371 Class IX HY 2013 Revision Guidelines Page 1 Section A (Power Point) Q1.What is PowerPoint? How are PowerPoint files named? Q2. Describe the 4 different ways of creating a presentation? (2 lines each) Q3.

More information

CSPP : Introduction to Object-Oriented Programming

CSPP : Introduction to Object-Oriented Programming CSPP 511-01: Introduction to Object-Oriented Programming Harri Hakula Ryerson 256, tel. 773-702-8584 hhakula@cs.uchicago.edu August 7, 2000 CSPP 511-01: Lecture 15, August 7, 2000 1 Exceptions Files: Text

More information

HST 952. Computing for Biomedical Scientists Lecture 8

HST 952. Computing for Biomedical Scientists Lecture 8 Harvard-MIT Division of Health Sciences and Technology HST.952: Computing for Biomedical Scientists HST 952 Computing for Biomedical Scientists Lecture 8 Outline Vectors Streams, Input, and Output in Java

More information

Stacks Goodrich, Tamassia

Stacks Goodrich, Tamassia Stacks Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations Example:

More information

Happy Cinco de Mayo!!!!

Happy Cinco de Mayo!!!! CSC 1051 Algorithms and Data Structures I Happy Cinco de Mayo!!!! Final Examination May 5, 2018 Name: KEY Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 20 TOTAL 100 Please answer questions

More information

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Islamic University of Gaza Faculty of Engineering Computer Engineering Department Student Mark Islamic University of Gaza Faculty of Engineering Computer Engineering Department Question # 1 / 18 Question # / 1 Total ( 0 ) Student Information ID Name Answer keys Sector A B C D E A B

More information

CN208 Introduction to Computer Programming

CN208 Introduction to Computer Programming CN208 Introduction to Computer Programming Lecture #11 Streams (Continued) Pimarn Apipattanamontre Email: pimarn@pimarn.com 1 The Object Class The Object class is the direct or indirect superclass of every

More information

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Queues. CITS2200 Data Structures and Algorithms. Topic 5 CITS2200 Data Structures and Algorithms Topic 5 Queues Implementations of the Queue ADT Queue specification Queue interface Block (array) representations of queues Recursive (linked) representations of

More information

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss The Collections API Mark Allen Weiss Lecture Objectives To learn how to use the Collections package in Java 1.2. To illustrate features of Java that help (and hurt) the design of the Collections API. Tuesday,

More information

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004 1.00/1.001 Introduction to Computers and Engineering Problem Solving Final / December 13, 2004 Name: Email Address: TA: Solution Section: You have 180 minutes to complete this exam. For coding questions,

More information

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616 OBJECT ORIENTED PROGRAMMING Course 4 Loredana STANCIU loredana.stanciu@upt.ro Room B616 Inheritance A class that is derived from another class is called a subclass (also a derived class, extended class,

More information

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed 1 1 COMP200 INPUT/OUTPUT OOP using Java, based on slides by Shayan Javed Input/Output (IO) 2 3 I/O So far we have looked at modeling classes 4 I/O So far we have looked at modeling classes Not much in

More information

CPSC 319. Week 2 Java Basics. Xiaoyang Liu & Sorting Algorithms

CPSC 319. Week 2 Java Basics. Xiaoyang Liu & Sorting Algorithms CPSC 319 Week 2 Java Basics Xiaoyang Liu xiaoyali@ucalgary.ca & Sorting Algorithms Java Basics Variable Declarations Type Size Range boolean 1 bit true, false char 16 bits Unicode characters byte 8 bits

More information

OBJECT ORIENTED PROGRAMMING. Course 6 Loredana STANCIU Room B616

OBJECT ORIENTED PROGRAMMING. Course 6 Loredana STANCIU Room B616 OBJECT ORIENTED PROGRAMMING Course 6 Loredana STANCIU loredana.stanciu@upt.ro Room B616 Exceptions An event, which occurs during the execution of a program, that disrupts the normal flow of the program's

More information

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide CS1092: Tutorial Sheet: No 3 Exceptions and Files Tutor s Guide Preliminary This tutorial sheet requires that you ve read Chapter 15 on Exceptions (CS1081 lectured material), and followed the recent CS1092

More information

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question) CS/B.TECH/CSE(New)/SEM-5/CS-504D/2013-14 2013 OBJECT ORIENTED PROGRAMMING Time Allotted : 3 Hours Full Marks : 70 The figures in the margin indicate full marks. Candidates are required to give their answers

More information

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course JAC444 - Lecture 4 Segment 1 - Exception 1 Objectives Upon completion of this lecture, you should be able to: Separate Error-Handling Code from Regular Code Use Exceptions to Handle Exceptional Events

More information

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score CSC 1051 Algorithms and Data Structures I Final Examination May 12, 2017 Name: KEY Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces

More information

The Composite State Visitor Pattern

The Composite State Visitor Pattern Design Patterns for Data Structures Chapter 8 he Composite State Visitor Pattern Design Patterns for Data Structures Chapter 8 he Composite State Binary ree with the Visitor Pattern BireeCSV Design Patterns

More information

Simple Java Reference

Simple Java Reference Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...

More information

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012 CMPSCI 187: Programming With Data Structures Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012 Implementing Stacks With Arrays The Idea of the Implementation Data Fields

More information

Principles, Models, and Applications for Distributed Systems M

Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M Exercitation 3 Connected Java Sockets Jacopo De Benedetto Distributed architecture

More information

CS 340 Homework Alternative 6. Due 11:59 PM Wednesday December 12

CS 340 Homework Alternative 6. Due 11:59 PM Wednesday December 12 CS 340 Homework Alternative 6 Due 11:59 PM Wednesday December 12 Alternative Homework 6 Implement the Dijkstra class and the BinaryHeap class shown on the following slide. The Dijkstra class already includes

More information

Computer Sciences 302 Exam 2 Information & Sample Exam

Computer Sciences 302 Exam 2 Information & Sample Exam Computer Sciences 302 Exam 2 Information & Sample Exam Below you ll find information about the second midterm exam and sample exam questions. This sample is intended to be similar in length and difficulty

More information

Principles, Models, and Applications for Distributed Systems M

Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M Lab assignment 4 (worked-out) Connection-oriented Java Sockets Luca Foschini Winter

More information

Chapter 12: Inheritance

Chapter 12: Inheritance Chapter 12: Inheritance Objectives Students should Understand the concept and role of inheritance. Be able to design appropriate class inheritance hierarchies. Be able to make use of inheritance to create

More information

Streams and File I/O

Streams and File I/O Chapter 9 Streams and File I/O Overview of Streams and File I/O Text File I/O Binary File I/O File Objects and File Names Chapter 9 Java: an Introduction to Computer Science & Programming - Walter Savitch

More information

i219 Software Design Methodology 8. Dynamic modeling 1 Kazuhiro Ogata (JAIST) Outline of lecture

i219 Software Design Methodology 8. Dynamic modeling 1 Kazuhiro Ogata (JAIST) Outline of lecture i219 Software Design Methodology 8. Dynamic modeling 1 Kazuhiro Ogata (JAIST) Outline of lecture 2 Use case Use case diagram State (machine) diagram Sequence diagram Class diagram of vending machine Vending

More information

CSE 143 Lecture 20. Circle

CSE 143 Lecture 20. Circle CSE 143 Lecture 20 Abstract classes Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; public double area() { return Math.PI * radius * radius; public

More information

MIDTERM REVIEW. midterminformation.htm

MIDTERM REVIEW.   midterminformation.htm MIDTERM REVIEW http://pages.cpsc.ucalgary.ca/~tamj/233/exams/ midterminformation.htm 1 REMINDER Midterm Time: 7:00pm - 8:15pm on Friday, Mar 1, 2013 Location: ST 148 Cover everything up to the last lecture

More information

1.00 Lecture 30. Sending information to a Java program

1.00 Lecture 30. Sending information to a Java program 1.00 Lecture 30 Input/Output Introduction to Streams Reading for next time: Big Java 15.5-15.7 Sending information to a Java program So far: use a GUI limited to specific interaction with user sometimes

More information

CS 251 Intermediate Programming Java I/O Streams

CS 251 Intermediate Programming Java I/O Streams CS 251 Intermediate Programming Java I/O Streams Brooke Chenoweth University of New Mexico Spring 2018 Basic Input/Output I/O Streams mostly in java.io package File I/O mostly in java.nio.file package

More information

COE 212 Engineering Programming. Welcome to Exam II Friday November 27, 2015

COE 212 Engineering Programming. Welcome to Exam II Friday November 27, 2015 1 COE 212 Engineering Programming Welcome to Exam II Friday November 27, 2015 Instructors: Dr. Salim Haddad Dr. Bachir Habib Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam

More information

Week 13 Lab - Exploring Connections & Remote Execution

Week 13 Lab - Exploring Connections & Remote Execution Week 13 Lab - Exploring Connections & Remote Execution COSC244 & TELE202 1 Assessment This lab is worth 0.5%. The marks are awarded for completing the programming exercise and answering the questions.

More information

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Reading from URL. Intent - open URL  get an input stream on the connection, and read from the input stream. Simple Networking Loading applets from the network. Applets are referenced in a HTML file. Java programs can use URLs to connect to and retrieve information over the network. Uniform Resource Locator (URL)

More information

File Practice Problems

File Practice Problems 1) Write a method reads in from two files and writes them, alternating lines, to an output file. The three file names should be passed into the method as Strings, and then method should NOT thrown any

More information

Sorting and Searching

Sorting and Searching CHAPTER 13 Sorting and Searching The exercises in this chapter are a framework for comparing algorithms empirically. This approach provides students with a means for understanding the finer details of

More information

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005 1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Exam / December 21, 2005 Name: Email Address: TA: Section: a You have 180 minutes to complete this exam. For coding questions,

More information

Collections and Iterators. Collections

Collections and Iterators. Collections Collections and Iterators Based on the notes from David Fernandez-Baca and Steve Kautz Based on The Java Tutorial (http://docs.oracle.com/javase/tutorial/java/) Bryn Mawr College CS206 Intro to Data Structures

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises dvanced Java Concepts Unit 5: Trees. Notes and Exercises Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will focus

More information

Chapter 10 Input Output Streams

Chapter 10 Input Output Streams Chapter 10 Input Output Streams ICT Academy of Tamil Nadu ELCOT Complex, 2-7 Developed Plots, Industrial Estate, Perungudi, Chennai 600 096. Website : www.ictact.in, Email : contact@ictact.in, Phone :

More information

Heaps. Heaps. A heap is a complete binary tree.

Heaps. Heaps. A heap is a complete binary tree. A heap is a complete binary tree. 1 A max-heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node. A min-heap is defined

More information