ASSIGNMENT 5 Objects, Files, and More Garage Management

Similar documents
ASSIGNMENT 5 Objects, Files, and a Music Player

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

ASSIGNMENT 4 Records and Objects

ASSIGNMENT 3 Classes, Objects and the Robot World

ASSIGNMENT 4 Classes and Objects

CSE115 Lab 9 Fall 2016

Assignment 5: Part 1 (COMPLETE) Sprites on a Plane

ASSIGNMENT 2. COMP-202A, Fall 2013, All Sections. Due: October 20 th, 2013 (23:59)

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

ASSIGNMENT 6. COMP-202, Winter 2015, All Sections. Due: Tuesday, April 14, 2015 (23:59)

ASSIGNMENT 4. COMP-202, Fall 2013, All Sections. Due: December 6 th,2013(23:59)

You should see something like this, called the prompt :

Com S 227 Assignment Submission HOWTO

You should now start on Chapter 4. Chapter 4 introduces the following concepts

CMPSCI 187 / Spring 2015 Implementing Sets Using Linked Lists

ASSIGNMENT 4. COMP-202C, Summer Due: Monday June 29th, 2015 (23:30)

Exceptions and Libraries

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

ASSIGNMENT 1 First Java Assignment

Tips from the experts: How to waste a lot of time on this assignment

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

CPSC 150 Laboratory Manual. Lab 1 Introduction to Program Creation

Programming Assignment 2 ( 100 Points )

ASSIGNMENT 1 Expressions, Data Types, and Simple Calculations

Code Listing H-1. (Car.java) H-2 Appendix H Packages

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

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

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

ASSIGNMENT 4. COMP-202, Fall 2014, All Sections. Due: November 9 th, 2014 (23:59)

Java Programming. Manuel Oriol, March 22nd, 2007

ASSIGNMENT 3. COMP-202, Winter 2015, All Sections. Due: Tuesday, February 24, 2015 (23:59)

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

Tips from the experts: How to waste a lot of time on this assignment

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Lab Exercise 4: Inheritance and Polymorphism CS 2334

CSE 373: Homework 1. Queues and Testing Due: April 5th, 11:59 PM to Canvas

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

COMP-202A: Introduction to Computing 1

CSCI 1301: Introduction to Computing and Programming Spring 2018 Project 3: Hangman 2.0 (A Word Guessing Game)

CS2 Practical 2 CS2Ah

CSE 143: Computer Programming II Winter 2019 HW6: AnagramSolver (due Thursday, Feb 28, :30pm)

CSSE2002/7023 The University of Queensland

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

COMP-202A: Introduction to Computing 1

You must bring your ID to the exam.

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

Shell Interface Assignment

Regis University CC&IS CS310 Data Structures Programming Assignment 2: Arrays and ArrayLists

Maps and Trees. CS310: Program 2. 1 Overview. 2 Requirements. Instructor Provided Files. Late Work: Up to 72 hours Penalty: 10% per 24

Introduction IS

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

CS201 - Assignment 3, Part 1 Due: Friday February 28, at the beginning of class

Homework 5: Aspect-Oriented Programming and AspectJ

COMP220/285 Lab sessions 1-3

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

CS 211 Programming Practicum Fall 2018

CS165 Practice Final Exam

10 Java Collections; Equality, JUnit

ASSIGNMENT 3 Methods, Arrays, and the Java Standard Class Library

COMP-202C: Foundations of Programming

CS201 - Assignment 7 Due: Wednesday April 16, at the beginning of class

CS2 Practical 1 CS2A 22/09/2004

Our second exam is Monday, April 3. Note that it will not be possible to get all the homework submissions graded before the exam.

In this lab we will practice creating, throwing and handling exceptions.

CSCI 200 Lab 1 Implementing and Testing Simple ADTs in Java

Assignment Submission HOWTO

Homework #10 due Monday, April 16, 10:00 PM

Java Review: Objects

CS 11 java track: lecture 1

School of Informatics, University of Edinburgh

AP Computer Science A Summer Assignment 2017

Programming Assignment 2 (PA2) - DraggingEmoji & ShortLongWords

Web-CAT Guidelines. 1. Logging into Web-CAT

CSE 1102 Fall 2009 Lab Assignment 2

1. The Apache Derby database

CMPSCI 187 / Spring 2015 Hangman

Project #1 Computer Science 2334 Fall 2008

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

Programming Project 5: NYPD Motor Vehicle Collisions Analysis

Computer Science 62 Lab 8

1. Go to the URL Click on JDK download option

COMP 3500 Introduction to Operating Systems Project 5 Virtual Memory Manager

Soda Machine Laboratory

ASSIGNMENT 3 Object Oriented Programming

Programming Assignment 8 ( 100 Points )

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

CPSC150L Lab 0 Lab Set Up

School of Informatics, University of Edinburgh

COMP-202B: Introduction to Computing 1

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

CMSC 341 Lecture 7 Lists

What will this print?

Homework Assignment #3

CSC116: Introduction to Computing - Java

Transcription:

ASSIGNMENT 5 Objects, Files, and More Garage Management COMP-202B, Winter 2010, All Sections Due: Wednesday, April 14, 2009 (23:55) You MUST do this assignment individually and, unless otherwise specified, you MUST follow all the general instructions and regulations for assignments. Graders have the discretion to deduct up to 10% of the value of this assignment for deviations from the general instructions and regulations. Note that the weight of this assignment in your final grade is equivalent to the combined weight of two of the previous assignments. Also note that the usual lateness policy does NOT not apply to this assignment; instead, late submissions will be accepted with NO penalty until Wednesday, April 21, at 23:55. A lateness penalty of 5% per day late or fraction thereof (including weekends and holidays) will be applied to all submissions sent after 23:55 on Wednesday, April 21, up to a maximum of two full days past this date, after which submissions for this assignment will NOT be accepted and therefore receive a grade of 0. Part 1, Question 1: Part 2, Question 1: Part 2, Question 2: Part 2, Question 3 or 4: 0 points 40 points 60 points 100 points 200 points total Part 1 (0 points): Warm-up Do NOT submit this part, as it will not be graded. However, doing these exercises might help you to do the second part of the assignment, which will be graded. If you have difficulties with the questions of Part 1, then we suggest that you consult the TAs during their office hours; they can help you and work with you through the warm-up questions. Warm-up Question 1 (0 points) Write a Java program which consists of one class called ReverseFile. method, main(), which does the following: This class should define one Ask the user to enter the full paths of two files on disk as Strings, and read these paths from the keyboard. Attempt to open the file whose path is given by the first String entered by the user for reading. Attempt to open the file whose path is given by the second String entered by the user for writing. Read the input file line by line and reverse the characters within each line (that is, the String "abcde" should become the String "edcba"). Also, it should write the reversed lines to the output file, but in the reverse order; that is, the first line in the input file should be the last line in the output file, the second line in the input file should be the second last line in the output file, and so on. 1

Once it has read and reversed all the lines from the input file, and written the reversed lines to the output file in the reverse order, it should close the files. Make sure that the header of your main() method is the following: public static void main(string[] args) throws IOException The throws IOException clause of the method header will allow your program to compile despite the fact that opening a file for reading or for writing can result in IOExceptions being thrown. Hint: In order to reverse the order in which the lines appear in the output file, your program will most likely have to store them in memory. Note that your program should be able to handle a file containing any number of lines, up to the amount of memory available to the Java Virtual Machine. Part 2 (40 + 60 + 100 = 200 points) The questions in this part of the assignment will be graded. Introduction In this assignment, you will build on the work you did for Assignment 4 by writing object types which will then be usable in a garage management software application which manages information about the repairs performed on the customers cars. The classes that you write in this assignment will include the Car class, the GarageDatabase class, the DatabaseLoader class, and the DatabaseWriter class. A Car stores information about a car that a customer brings to a garage for repairs. This information includes: the make of the car the model of the car the year during which the car was built the serial number of the car the repairs performed on the car A GarageDatabase keeps track of all the Car objects representing cars that customers brought to the garage for repairs. It is possible to add a Car to a GarageDatabase, or to query a GarageDatabase to retrieve a Car with specific characteristics. The purpose of the DatabaseLoader is to read files which contain information about a GarageDatabase, and recreate the Cars the latter contains from the information read from these files. Likewise, the purpose of DatabaseWriters is to write the information stored in Cars contained in a GarageDatabase to a file in such a way that equivalent objects can later be recreated by a DatabaseLoader. To do this assignment, you will need the following files: Repair.java: A file containing the definition of the Repair class; the classes you will write for this assignment depend on this class. a5-docs.zip: An archive file containing documentation about the Repair class as well as classes that you will write for this assignment. a5-tests.zip: An archive file containing test programs for each of the classes that you will write for this assignment. junit.jar: An archive file containing all the classes implementing a test framework called JUnit. The test programs in a5-tests.zip depend on JUnit in order to work. Page 2

GarageManager.java: A file containing the definition of the class implementing the user interface for the garage management application. This application will use the classes that you write for this assignment in order to keep track of the cars that have been repaired at a garage. comp202-garage.txt: A sample garage database file, for use with the the garage management application. What You Need To Do Although there are four questions in this part of the assignment, in order to receive full credit, you only need to complete: Question 1 Question 2 Your choice between Question 3 and Question 4 If you complete and submit files for both Question 3 and Question 4, only your submission for Question 3 will be graded; in such a case, your submission for Question 4 will be ignored. However, it is strongly recommended that you attempt the question for which you do not send a submission, as it will constitute good practice for the final exam. Question 1: The Car (40 points) Write a Java class called Car. Car objects store information about a car repaired at a garage. This information includes the make of the car, its model, the year during which it was built, and its serial number. A Car object also stores information about the repairs performed on a car; information about each repair is stored in a Repair object, and the Repair class is provided to you. Because the same repair can be performed multiple times on the same car, the Car class supports the addition of two Repairs whose attribute values are equal. The Car class MUST provide ALL of the following INSTANCE methods: A constructor, which initializes a newly-created Car boolean addrepair(repair), which adds a Repair to a Car java.util.arraylist<repair> getallrepairs(), which returns all the Repairs associated with a Car String getmake(), which returns the make of the Car String getmodel(), which returns the model of the Car int getserialnumber(), which returns the serial number of the Car int getyear(), which returns the year during which this Car was built String tostring(), which returns a String representation of a Car; this representation consists of the year during which the Car was built, followed by the String " ", followed by the make of the Car, followed by the String " ", followed by the model of the Car, followed by the String " (#", followed by the serial number of the Car, followed by the String ")". The internal workings of Car objects can be implemented in whatever way you wish, as long as your implementation of the Car class follows these instructions: It MUST behave exactly as described in the specification given in the documentation for the Car class found in a5-docs.zip. It MUST NOT use Java features, constructs, or Java standard library classes and methods not covered in the lectures by the time the assignment is due, in accordance the general instructions and Page 3

regulations. In particular, you can store the Repairs associated with each Car using either arrays or ArrayLists, but you MUST NOT use any other class that belongs to the Java Collection Framework such as LinkedList, nor any other class you obtain from third-party libraries such as the Apache Commons Collections) AT ALL within the Car class. If you wish to use advanced data structures, you MUST implement these data structures yourself. Hint: Use an ArrayList instead of an array to store the Repairs associated with a Car. Question 2: The Car (70 points) Write a Java class called GarageDatabase. A GarageDatabase object is used to store information about all the Cars that customers brought to a particular garage for repairs. Information about each of these cars is stored in a Car object which is then added to the GarageDatabase object. Unlike the Car class, the GarageDatabase class does not support the addition of duplicate elements; in other words, adding a Car to a GarageDatabase has no effect when the Car s make, model, year, and serial number attributes are equal to the make, model, year, and serial number attributes of a Car already stored in the GarageDatabase. For the purposes of comparing the attributes of two Cars, String comparisons are performed in a case-sensitive manner. The GarageDatabase class MUST provide ALL of the following INSTANCE methods: A constructor, which initializes a newly-created GarageDatabase so that it contains no Cars boolean addcar(car), which adds a Car to a GarageDatabase if the latter does not contain an equivalent Car (that is, one whose make, model, year, and serial number attributes are equal to the make, model, year, and serial number attribute of the specified Car, respectively) java.util.arraylist<car> getallcars(), which returns all the Cars currently stored in the GarageDatabase Car getcar(string, String, int, int), which returns the Car in the GarageDatabase whose make, model, year, and serial number attributes are equal to the parameters The internal workings of GarageDatabase objects can be implemented in whatever way you wish, as long as your implementation of the GarageDatabase class follows these instructions: It MUST behave exactly as described in the specification given in the documentation for the GarageDatabase class found in a5-docs.zip. It MUST NOT use Java features, constructs, or Java standard library classes and methods not covered in the lectures by the time the assignment is due, in accordance the general instructions and regulations. In particular, you can store the Cars that each GarageDatabase contains using either arrays or ArrayLists, but you MUST NOT use any other class that belongs to the Java Collection Framework such as LinkedList, nor any other class you obtain from third-party libraries such as the Apache Commons Collections) AT ALL within the GarageDatabase class; in particular, you MUST NOT use any class or method from the Java Platform API (or any third party library) which does automatic searching for you. If you wish to use advanced data structures, you MUST implement these data structures yourself. Hint: Use an ArrayList instead of an array to store the Cars a GarageDatabase contains. Be careful before using the contains() method of the ArrayList class, as it may not do exactly what you need them to do due to the fact that the Car class does not override the equals() method. Question 3: Loading the Cars (100 points) Modify the provided DatabaseLoader class to add the body of the method defined below. An object which belongs to the DatabaseLoader class reads garage database files; these files contain information from which it is possible to create Cars. It then creates Cars from this information, adds them to a GarageDatabase that it also creates, and returns this new GarageDatabase. The DatabaseLoader class MUST provide the following method: Page 4

A GarageDatabase load(java.io.file) method, which reads a garage database file whose path is given by a java.io.file object, creates Cars from the information it contains, adds these Car to a GarageDatabase it also creates, and returns this new GarageDatabase The documentation for the DatabaseLoader class included in a5-docs.zip describes in detail the format in which information about each Car is stored in the garage database file. To summarize, each Car is saved to the disk as follows: 1. The make of the Car is written on a line in the file. 2. The model of the Car is written on the following line. 3. The year during which the Car was built, the serial number of the Car, and the number of Repair objects containing information about repairs performed on the Car are written on the same line, in this order, on the line which immediately follows the one containing the model of the Car. These values are separated by one or more whitespace characters (space and/or tab). There can be any number of white space characters (including none) before the first value on the line, as well as after the last value on the line. 4. Finally, all the information about repairs performed on the Car is written to the file. The description of a repair is written alone on its line, while the cost of this repair is written on the next line. No empty lines occur between the line containing the cost of a repair and the description of the next repair. Note that any number of empty lines (including none) can occur before the line containing the value of the make attribute of the first Car in the garage database file, after the line containing the cost of the last repair performed on the last Car in the garage database file, and between the line containing the cost of the last repair associated with a Car and the line containing the value of the make attribute of the next Car in the garage database file. Question 4: Saving the Cars (100 points) Modify the provided DatabaseWriter class to add the body of the method defined below. An object which belongs to the DatabaseWriter class writes the information contained in Cars to garage database files in such a way that a DatabaseLoader can recreate the corresponding Cars from this information. The DatabaseWriter class MUST provide the following method: A void save(garagedatabase, java.io.file) method, which writes the information stored in the Cars contained in the specified GarageDatabase to a garage database file whose path is given by a java.io.file object The documentation for the DatabaseWriter class included in a5-docs.zip describes in detail the format in which information about each Car is stored in the garage database file. To summarize, it is the same format as the one understood by the GarageDatabase load(java.io.file) method of the DatabaseLoader class (see Question 3). Additional Information Important instructions: You MUST save the classes you write for the above questions in the following files: Question 1: Car.java Question 2: GarageDatabase.java Question 2: DatabaseLoader.java Question 3: DatabaseWriter.java Page 5

Submit THREE of these files to mycourses: Car.java, GarageDatabase.java, and EITHER DatabaseLoader.java OR DatabaseWriter.java, at your option. If you implement other classes that are used by the classes required by the assignment, you MUST submit the files containing the source code (files with extension.java) for these classes as well. In accordance to the general instructions and regulations, submit to mycourses ONLY source code files relevant to the assignment. Full specifications for the all the methods you have to write in the four required classes (that is, Car, GarageDatabase, DatabaseLoader, and DatabaseWriter), including the parameters these methods take, what these parameters represent, the return types of these methods, the values these methods return in various circumstances, the effects that calling each method has on the state of the target object (if any), and so on, can be found in the documentation for the Car, GarageDatabase, DatabaseLoader, and DatabaseWriter classes; this documentation is included in a5-docs.zip. The methods you implement MUST respect ALL the specifications for the Car, GarageDatabase, DatabaseLoader, and DatabaseWriter classes that are found in the documentation. Remember that, in accordance with the general instructions and regulations for assignments, all attributes of the classes you write MUST be private, and all the methods you write which are required by the above specification and the documentation MUST be public. If you define helper methods not listed in the above specification or in the documentation, these methods MUST be private. Also remember that not specifying an access modifier results in an access modifier which is neither public nor private. Other tips and information: The assignment specification does not require that you submit any test programs. However, it will be necessary for you to write / use such programs in order to verify that the classes required by this assignment work properly. Here are a couple approaches you can take to perform this task: You can write a program that creates dummy Car objects with dummy values. You can then use these dummy Car objects to verify that the classes you wrote for this work properly. You can use the test programs provided in a5-tests.zip. Extract the contents of a5-tests.zip to the folder in which you have saved the files containing the classes required by this assignment. Then, compile the test files and run them. Note that if you use the provided test programs, you need make sure to test your classes in the following order, as the test for each of these classes depends on the correctness of the implementation of the previous class: 1. Car 2. GarageDatabase 3. DatabaseLoader / DatabaseWriter You can use the garage management application. However, note that unless you implement both the DatabaseLoader class and the DatabaseWriter class, the garage management application will be unable to either read garage database files or save them. To combine the JAR file containing the test framework with the test programs and the classes you write so that everything compiles and runs using the JDK, you need to specify when compiling or running the test programs that the JAR file containing the test framework should be on the Classpath. To do so, follow the steps below: 1. Place the test programs in the same directory / folder as the one in which the files containing the classes that you wrote are stored. 2. Place the JAR file containing the test framework in the same directory / folder as the one in which the files containing the classes that you wrote are stored. Page 6

3. Open the command-line interface (Command Prompt under Windows, Terminal under Mac OS X, Linux, or other Unix-like operating systems) and navigate normally to the directory / folder containing your files. 4. To compile a test program stored a file called MyTest.java under Windows, issue the following command: javac -cp junit.jar;. MyTest.java To compile a test program stored in a file called called MyTest.java under Mac OS X, Linux, or another Unix-like operating system, issue the following command: javac -cp junit.jar:. MyTest.java The only difference between the Windows and Unix versions of the above command is that in the Windows version, a semi-colon (;) separates each Classpath entry; in the Unix version, a colon (:) separates each Classpath entry instead. Make sure to replace MyTest.java with the actual name of the file containing the test program you wish to compile. 5. To run the main() method of the test program defined in a class called MyTest under Windows, issue the following command: java -cp junit.jar;. MyTest To run the main() method of the test program defined in a class called MyTest under Mac OS X, Linux, or another Unix-like operating system), issue the following command: java -cp junit.jar:. MyTest Again, the only difference between the Windows and Unix versions of the above commands is that in the Windows version, a semi-colon (;) separates each Classpath entry; in the Unix version, a colon (:) separates each Classpath entry instead. Make sure to replace MyTest with the actual name of the class containing the main() method of the test program you wish to run. You should now be able to compile and run the provided test programs without the compiler reporting errors related to missing classes. Note that the -cp option is ONLY necessary when the code you want to compile / run requires libraries located in another directory or a JAR file; in this assignment, this occurs only when you want to run the test programs provided in the file a5-tests.zip. To combine the JAR file containing the test framework with the test programs and the classes you write so that everything compiles and runs using Eclipse, you need to add the JAR file containing the test framework to the Classpath of your project. To do so, follow the steps below: 1. In the Package Explorer or Navigator view, right-click on your project. In the context menu that appears, select the Properties option. 2. The window that appears will be split in two main portions. The smaller, left-hand portion contains a number of option categories, and you can select a category from those listed; the larger middle portion contains the options for the selected category. In the left-hand portion of the window, choose Java Build Path. 3. The middle portion of the window will now contain four tabs. Click on the Libraries tab. The libraries included in your project will now appear, along with a number of buttons; click on the button labelled Add External JARs... 4. Using the file selection window that appears, navigate to the directory / folder where you saved the JAR file containing the test framework, select the JAR file, and click on the confirmation button (it might be labelled OK, Open, or something else, depending on the operating system that you use). 5. Selecting the JAR file as described in the previous step you should bring you back to the Properties window, and the list of libraries included in your project will now contain the JAR Page 7

file you selected. At the bottom of the Properties window are two buttons; click on the one labelled OK. Once you added junit.jar to the Classpath of your project by following the above steps, you should now be able to compile all the test programs provided in a5-tests.zip without the compiler reporting errors related to missing classes. Note that it is necessary to perform the above steps ONLY when you want to run the test programs included in a5-tests.zip. Also, note that Eclipse gives you the option of running the provided test programs either as a Java Application, or as a JUnit test. Both result in similar displays, so you can use both and decide which one you prefer. What To Submit Car.java GarageDatabase.java DatabaseLoader.java (if you choose to send a submission for Question 3) DatabaseWriter.java (if you choose to send a submission for Question 4) Page 8