CSCI 200 Lab 2 Inheritance, Polymorphism & Data Streams

Similar documents
CSCI 200 Lab 3 Using and implementing sets

CSCI 200 Lab 1 Implementing and Testing Simple ADTs in Java

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

Core Java Contents. Duration: 25 Hours (1 Month)

Certification In Java Language Course Course Content

Active Learning: Streams

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS 251 Intermediate Programming Java I/O Streams

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

Streams and File I/O

CS111: PROGRAMMING LANGUAGE II

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

JAVA. Duration: 2 Months

1.00 Lecture 30. Sending information to a Java program

Contents Chapter 1 Introduction to Programming and the Java Language

School of Informatics, University of Edinburgh

Java Programming Lecture 9

104. Intermediate Java Programming

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Data Structures. 03 Streams & File I/O

Training topic: OCPJP (Oracle certified professional Java programmer) or SCJP (Sun certified Java programmer) Content and Objectives

Lecture 11.1 I/O Streams

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

Introduction Unit 4: Input, output and exceptions

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Java Input/Output. 11 April 2013 OSU CSE 1

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012

Murach s Beginning Java with Eclipse

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

Complete Java Contents

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Starting Out with Java: From Control Structures Through Objects Sixth Edition

CN208 Introduction to Computer Programming

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Exceptions and Working with Files

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Job Migration. Job Migration

Example: Copying the contents of a file

CS 209 Sec. 52 Spring, 2006 Lab 6 - B: Inheritance Instructor: J.G. Neal

Streams and File I/O

Sri Vidya College of Engineering & Technology Question Bank

Lecture 7. File Processing

Name: Checked: Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of Lab.

Simple Java Input/Output

What is Serialization?

Object-Oriented Programming in the Java language

Lab 2: File Input and Output

Lab 5: Java IO 12:00 PM, Feb 21, 2018

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

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

HST 952. Computing for Biomedical Scientists Lecture 8

Object Oriented Design with UML and Java. PART VIII: Java IO

Programming Languages and Techniques (CIS120)

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

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

Java Programming Training for Experienced Programmers (5 Days)

Lecture 19 Programming Exceptions CSE11 Fall 2013

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Come & Join Us at VUSTUDENTS.net

JAVA. 1. Introduction to JAVA

CS 170 Java Programming 1. Week 10: Loops and Arrays

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

Compile error. 2. A run time error occurs when the program runs and is caused by a number of reasons, such as dividing by zero.

Object-Oriented Design. March 2005 Object Oriented Design 1

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

I/O streams. Byte Streams Character Streams InputStream ByteArrayInputStream FileInputStream FilterInputStream

Documentation (and midterm review)

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

Chapter 12. File Input and Output. CS180-Recitation

Final Exam. Final Exam Review. Ch 1: Introduction: Object-oriented analysis, design, implementation. Exam Format

More on Objects in JAVA TM

1.00 Lecture 31. Streams 2. Reading for next time: Big Java , The 3 Flavors of Streams

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Enhanced Entity- Relationship Models (EER)

PIC 20A Streams and I/O

ASSIGNMENT 5 Data Structures, Files, Exceptions, and To-Do Lists

Java SE 8 Programmer I and II Syballus( Paper codes : 1z0-808 & 1z0-809)

CISC 323 (Week 9) Design of a Weather Program & Java File I/O

When we reach the line "z = x / y" the program crashes with the message:

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

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

Name: CS 159 Practice Final Fall 2015

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

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1

Software 1. Java I/O

Java file manipulations

Principles of Computer Science I

Name: Checked: Preparation: Investment Calculator with input and output to text files Submit through Blackboard by 8:00am the morning of Lab.

Chapter 11: Exceptions and Advanced File I/O

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

Transcription:

CSCI 200 Lab 2 Inheritance, Polymorphism & Data Streams In this lab you will write a set of simple Java interfaces and classes that use inheritance and polymorphism. You will also write code that uses data (text and object) streams. Like last time, you are to continue doing pair programming with your assigned partner. Each pair must work with one set of files. Objectives: to implement and document interrelated interfaces and classes to use polymorphism in a program to make use of several types of input and output streams to read and write files from within a program to create a set of independent classes that interact with each other

Problem 1: Workers Hierarchy Part 1.1: Implement a class hierarchy Use the following UML class diagram to implement the indicated interfaces and classes in Java. Unless the word <<interface>> appears, the entity is either a class or abstract class. Start by creating a worker package inside your JavaPackages directory on your home (which will make the package accessible in later labs) and place all the interfaces and classes inside that package. <<interface>> Worker -------------------- +getname():string +equals(otherobject:object):boolean +tostring():string ^ //implements //extends ----------------------- -------------------- <<abstract class>> HourlyWorker <<interface>> Employee ------------------------------- ---------------------- -hours:int +getmonthlypay():double +gethours():int ^ +addhours(hours:int):void +resethours():void ^ //extends //implements ---------- --------- ---------------------------- Volunteer HourlyEmployee SalariedEmployee -------------- ---------------- -hourlypay:double -monthlysalary:double +gethourlypay():double +setmonthlypay(pay:double) +sethourlypay(pay:double)

Note the following clarifications and requirements: Do not add or remove methods or fields to or from any of the classes and interfaces The Worker interface must extend Serializable from java.io.* The classes and/or interfaces where the name field belongs are not indicated; you need to decide where it best belongs in order to reduce code redundancy as much as possible. It may not be possible to put it in only one place All classes must override the equals method so that it compares workers by name; two workers are considered equal if they have the same name, even if they are not the same kind of Worker. If the other object is not some kind of Worker, the equals method should return false. Since this comparison is based on the name field, put the equals method in the same class or classes that contain the name field. The HourlyWorker class must be an abstract class because it cannot implement the tostring method specified by the Worker interface. This method must be implemented in all three bottom-level subclasses; it should return a string naming the type of the Worker subclass this object represents: "Volunteer", "HourlyEmployee" or "SalariedEmployee". The hours field in the HourlyWorker class represents the number of hours worked so far during the current month You also need to decide if any of the methods requires a precondition; if so, they should throw an appropriate exception when the precondition is not met Supply appropriate constructors for all the regular and abstract classes. Where appropriate, include alternate constructors that allow users to create instances with default for some or all of the instance variables. Discuss your design choices with your lab partner and come to agreement. Add appropriate Javadoc comments to all interfaces and classes. Do this as you write your code NOT after you are done! Work on the interfaces and classes until they have all the indicated inheritance relationships and there are no compiler errors. Have the lab instructor or a TA review your work before you proceed.

Part 1.2: Testing the class hierarchy The lab folder contains two complete JUnit test classes: VolunteerTest and WorkerPolymorphismTest. Keep them inside your lab folder do not move them to your JavaPackages folder or to the worker package folder. The VolunteerTest class tests the constructor and all the methods of the Volunteer class; you can use it as soon as you have the Volunteer class and its superclasses/interfaces implemented. Note that the instance variable here is type Volunteer rather than Worker; in this case, using the specific variable type allows us to avoid casting before every method call. The WorkerPolymorphismTest class determines that objects of the three regular classes are instances of the appropriate classes and interfaces and that they are not instances of any of the other classes or interfaces. You can use this test as soon as you have all six classes and interfaces compiling without errors. Note that it is necessary to use the type Worker for the local variables here to permit all the type comparisons; we also (perhaps unnecessarily) test to see that each object is an instance of its own class. Use the VolunteerTest class as a model to create test the HourlyEmployee and SalariedEmployee classes. For each class, you will need to test its constructor(s) and all methods that pertain to it. In particular, you will need to add test methods for the methods that handle pay in these classes. Methods in the HourlyEmployee class that are implemented in the HourlyWorker class can use essentially the same tests as those for the Volunteer class. When you are finished with all four tests, show them to the TA or lab instructor. Note that you must pass all test cases in the four test classes and you must show that the tests you designed are sufficiently thorough. (If you discover that the provided tests are insufficiently thorough, please point that out to the instructor right away.) We will return to these classes in the next lab, so it is important that you complete them by then.

Problem 2: Exercises in Data & Object Streams In this problem you will you will complete a Java class called StringsStream with the following three static methods: input2textfile: reads input from the standard input (System.in) and writes it to a standard text file textfile2objectfile: reads a text file, enters its data into a List, and writes the List as a single object to an object file objectfile2output: reads a List object from an object file and displays its contents to the standard output (System.out) An incomplete StringsStream class with the first method completed is in your lab folder. There is also an incomplete driver class called StringStreamDriver that allows you to test your methods in the StringsStream class. Be sure to add all necessary Javadoc comments to both files as you complete them. Part 2.1: Copying text from the keyboard to a text file The file StringsStream.java contains the StringStreams class shell along with the code shell and Javadoc comments for the input2textfile method. Complete this method according to the instructions below. The method takes a String parameter textfilename. It first creates a Scanner object wrapped around System.in, using code similar to that in the run method of the driver class, and a PrintWriter object wrapped around a File object created with the parameter textfilename. The method then reads lines of text from standard input (the DrJava console) using the Scanner and prints those lines (except for the last period line) to the PrintWriter object. The last input line is an end-of-input-indicator and will consist of just the '.' character. (Compare the input line to the String "." using the equals method.) When the method reads this last line, it does not write it to the output; instead, it closes the Scanner and PrintWriter objects and then returns. You can verify that this method is working correctly by running the accompanying driver program and viewing the text file it produces in a text editor; make sure that it has line breaks exactly and only where you typed them. You can avoid retyping your input over an over by typing a few lines of sample input in a separate DrJava window and then copying them into the program when you test it.

Part 2.2: Copying text from a text file to an object file using a List The second static method is textfile2objectfile. Use the input2textfile method as a model. The method takes two String parameters: a text file name and an object file name. As with the previous method, it declares that it throws an IOException. It creates a new empty List<String> (either an ArrayList or a LinkedList), a Scanner object wrapped around a FileReader object to read from the text file parameter, and an ObjectOutputStream wrapped around a FileOutputStream to write to the object file parameter. The method uses the Scanner to read lines of text, appending each line to the List until it encounters the end of file (which occurs when the hasnextline method returns false); it then writes the List object to the ObjectOutputStream using the writeobject method. Finally, it closes the Scanner and ObjectOutputStream objects and returns. In order to verify that this method is working correctly, you ll need to expand the run method in the driver program to accomplish the following (Part 2 in the method s Javadoc). In a try/catch block, get the name of an object file from the user. Then call the method textfile2objectfile, passing the existing text file name and object file name as parameters. As in Part 2.1, put the method call into a try/catch block; if the code catches an IOException, it should print an error message and exit. If your method is working correctly, it will generate an object file, but you won't be able to see the contents of the object file; you'll complete your testing of Part 2.2 in Part 2.3.

Part 2.3: Reading a List from an object file and displaying the result The third static method is objectfile2output. Use the other two methods as templates. This method takes the name of an existing object file as a String parameter; it may throw any of three exceptions: IOException, ClassNotFoundException, or ClassCastException, so it uses the throws clause to declare these. (Separate the exception names with commas.) It creates an ObjectInputStream wrapped around a FileInputStream using the parameter. It then reads an Object from the stream and casts it to a List<String>. This read can throw either an IOException or a ClassNotFoundException; the cast can throw a ClassCastException. (That s a lot of possible exceptions in one statement!) The method then displays the elements of the List to System.out, closes the stream, and returns. In order to verify that this method is working correctly you ll need to expand the run method in the driver program once again to accomplish Part 3 of the method s Javadoc: Call the objectfile2output method on the same StringsStreams instance passing the existing object file name as a parameter. Again, put this part in a try/catch block, this time separately catching the three possible exceptions. Verify that Parts 2.2 and 2.3 are working correctly by running the completed driver class. Output from Part 2.3 should exactly match the original input from Part 2.1, (except that the last line with only the period character is missing).