联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2018-06-21 03:01


COMP-202, Summer 2018

Due: Wednesday, June 20th, 11:59pm

Please read the entire PDF before starting. You must do this assignment individually.

Question 1: 100 points

100 points total

It is very important that you follow the directions as closely as possible. The directions, while

perhaps tedious, are designed to make it as easy as possible for the TAs to mark the assignments by letting

them run your assignment, in some cases through automated tests. While these tests will never be used to

determine your entire grade, they speed up the process significantly, which allows the TAs to provide better

feedback and not waste time on administrative details. Plus, if the TA is in a good mood while he or she is

grading, then that increases the chance of them giving out partial marks. :)

Up to 30% can be removed for bad indentation of your code as well as omitting comments, coding structure,

or missing files. Marks will be removed as well if the class and method names are not respected.

To get full marks, you must:

• Follow all directions below

• Make sure that your code compiles

– Non-compiling code will receive a very low mark

• Write your name and student ID as a comment in all .java files you hand in

• Indent your code properly

• Name your variables appropriately

– The purpose of each variable should be obvious from the name

• Comment your work

– A comment every line is not needed, but there should be enough comments to fully understand

your program

1

Part 1 (0 points): Warm-up

As usual, do NOT submit this part, as it will not be graded.

Warm-up Question 1 (0 points)

Write a class describing a Cat object. A cat has the following attributes: a name (String), a breed

(String), an age (int) and a mood (String). The mood of a cat can be one of the following: sleepy,

hungry, angry, happy, crazy. The cat constructor takes as input a String and sets that value to

be the breed. The Cat class also contains a method called talk(). This method takes no input and

returns nothing. Depending on the mood of the cat, it prints something different. If the cat’s mood is

sleepy, it prints meow. If the mood is hungry, it prints RAWR!. If the cat is angry, it prints hsssss. If

the cat is happy it prints purrrr. If the cat is crazy, it prints a String of 10 gibberish characters (e.g.

raseagafqa).

The cat attributes are all private. Each one has a corresponding public method called getAttributeName()

(ie: getName(), getMood(), etc.) which returns the value of the attribute. All but the breed also

have a public method called setAttributeName() which takes as input a value of the type of the attribute

and sets the attribute to that value. Be sure that only valid mood sets are permitted. (ie, a cat’s

mood can only be one of five things). There is no setBreed() method because the breed of a cat is set

at birth and cannot change.

Test your class in another file which contains only a main method. Test all methods to make sure they

work as expected.

Warm-up Question 2 (0 points)

Using the Cat type defined in the previous question, create a Cat[] of size 5. Create 5 Cat objects and

put them all into the array. Then use a loop to have all the Cat objects meow.

Warm-up Question 3 (0 points)

Write a class Vector. A Vector should consist of three private properties of type double: x, y, and

z. You should add to your class a constructor which takes as input 3 doubles. These doubles should

be assigned to x, y, and z. You should then write methods getX(), getY(), getZ(), setX(), setY(),

and setZ() which allow you to get and set the values of the vector. Should this method be static or

non-static?

Warm-up Question 4 (0 points)

Add to your Vector class a method calculateMagnitude which returns a double representing the

magnitude of the vector. Should this method be static or non-static? The magnitude can be computed

by taking:

magnitude =

p

x

2 + y

2 + z

2

Warm-up Question 5 (0 points)

Write a method scalarMultiply which takes as input a double[], and a double scale, and returns

void. The method should modify the input array by multiplying each value in the array by scale.

Should this method be static or non-static?

Warm-up Question 6 (0 points)

Write a method deleteElement which takes as input an int[] and an int target and deletes all

occurrences of target from the array. By “delete” we mean create a new array (of smaller size) which

has the same values as the old array but without any occurrences of target. The method should return

the new int[]. Question to consider: Why is it that we have to return an array and can’t simply change

the input parameter array like in the previous question? Should these methods be static or non-static?

Warm-up Question 7 (0 points)

Write the same method, except this time it should take as input a String[] and a String. What is

different about this than the previous method? (Hint: Remember that String is a reference type.)

Page 2

Warm-up Question 8 (0 points)

Write a program that opens a .txt, reads the contents of the file line by line, and prints the content

of each line. To do this, you should look up how to use the BufferedReader or FileReader class1

.

Remember to use the try and catch blocks to handle errors like trying to open an non-existent file. A

sample file for testing file reading is found in the provided files as dictionary.txt.

Warm-up Question 9 (0 points)

Create a new method from your program which takes an array of Strings as input as well as a String

indicating a file name. Use the FileWriter and BufferedWriter classes to access the file and write the

elements of the array of Strings. In the output file, there should be one String per line.

1The documentation for the BufferedReader class is available at http://docs.oracle.com/javase/8/docs/api/java/

io/BufferedReader.html. You can find an example on how to use it at http://www.tutorialspoint.com/java/io/

bufferedreader_readline.htm

Page 3

Part 2

The questions in this part of the assignment will be graded.

Question 1: Booking System (100 points)

For this question, you will write several classes, and then create and use instances of those classes in

order to simulate a hotel booking system. Note that in addition to the required methods below, you are

free to add as many other private methods as you want (no additional public method is allowed).

(a) (15 points) Write a class Room. A Room has the following private attributes:

• A String type

• A double price

• A boolean availability

The Room class also contains the following public methods. It is up to you to determine whether

each method should be static or non-static.

• A constructor that takes as input the type of the room and uses it to initialize the attributes.

Note that there are only 3 type of Rooms supported by the program: double,

queen, and king. If the input is not one of these types, then the constructor should throw

an IllegalArgumentException explaining that no room of such type can be created. The

price of the room is based on its type as follows: $90 for a double, $110 for a queen, $150 for a

king. The constructor should set the availability for a new room to be true.

• getType which returns the type of the room.

• getPrice which returns the price of the room.

• getAvailability which returns the availability of the room.

• changeAvailability which takes no input and sets the value stored in the availability attribute

to be the opposite of the one currently there.

• A findAvailableRoom method which takes as input an array of Rooms as well as a String

indicating the room type. The method should return the first available room in the array of

the indicated type. If no such room exists (either because all rooms of said type are occupied,

or because no room of such type is in the array), the method returns null.

(b) (5 points) Write a class Reservation. A Reservation has the following private attributes:

• A String name

• A Room roomReserved

The Reservation class also contains the following public methods. It is up to you to determine

whether each method should be static or non-static.

• A constructor that takes as input a Room and the name under which the reservation is made.

The constructor must use its inputs to initialize the attributes.

• getName which returns the name under which the reservation was made.

• getRoom which returns the Room that has been reserved.

(c) (50 points) Write a class Hotel. This class has three private attributes:

• A String name

• An array of Room

• An array of Reservation

Page 4

It also has the following methods. It is up to you to determine whether each method should be

static or non-static.

• A public constructor that takes as input the name of the hotel, and an array of rooms. It

initializes the Room array and copies the Room references from the input array to the Hotel

array.

• A private method addReservation that takes a Reservation as input and adds it to the array

of Reservation of the hotel. Note that the array of Reservations of the hotel should never

contain null elements. The number of elements of such array matches exactly the number of

Rooms reserved in the hotel.

• A private method removeReservation that takes as input a name and a type of room. If no

reservation has been made under the given name for the given type of room, then the method

should throw a NoSuchElementException with the appropriate message. If a matching reservation

is found, then the method should remove such reservation from the array of reservations

of the hotel, and make sure that the availability of the Room is modified accordingly. Note

that you need to import java.util.NoSuchElementException in order to be able to use it.

Note also that the array of Reservations of the hotel should never contain null elements. The

number of elements of such array matches exactly the number of Rooms reserved in the hotel.

• A public method createReservation that takes as input a name and the type of the room

that should be reserved. The method should use findAvailableRoom (from the Room class) to

verify whether a room of the given type is available in the hotel. If not, the method should

return -1. Otherwise, the method should: create a reservation for such room under the given

name, change the availability of the room, and add the reservation to the array of reservations

of the hotel using the addReservation method. In this case, the method should also return

the total number of reservations for the hotel.

• A public method cancelReservation that takes as input the name under which the reservation

was made and the room type reserved. This method should use the removeReservation method

to cancel the reservation. If no matching reservation was found, the method should inform the

user that no reservation under such name has been made for the given type of room. Otherwise,

the method should print a message letting the user know that the operation was successful. To

receive full marks, a try/catch block should be used.

• A public method printInvoice that takes as input a name and prints out a message letting

the user know how much the given person owes the hotel based on all the reservations made

under such name.

• The toString method that returns a String containing the name of the hotel as well as how

many available rooms of each type the hotel has.

• A public method loadReservations. This method takes as input a filename as a String parameter,

and throws an IOException. The method must use a FileReader and a BufferedReader

to read from the specified file. You can assume that the file has the following format: each

reservation is on a separate line and for each reservation the name and the type of the Room

are separated by a tab character. As an example, see the file reservationsInfo.txt provided with

the assignment. Note that your method should work with any file that follows the format described

above, including blank text files. The method should read from the file line by line (use

readLine to do that) and create the appropriate reservation. Hint: you can use the method

split() from the String class to split each line of the file.

• A public method saveReservations. This method takes as input a filename as a String parameter,

and throws an IOException. The method must use a FileWriter and a BufferedWriter

to write on the specified file. The method goes through the array of reservations of the hotel

and saves the appropriate information on the file. The file should have the following format:

each reservation is on a separate line and for each reservation the name and the type of the

Page 5

Room are separated by a tab character.

(d) (30 points) Write a class BookingSystem. In this class you will write the following methods:

• A private method loadRooms. This method takes as input a filename as a String parameter,

and returns an array of Rooms. The method must use a FileReader and a BufferedReader to

read from the specified file. Make sure to have two catch blocks to catch both FileNotFoundException

and IOException when reading from the file. In these cases the method should print an appropriate

error message. You can assume that the format of the file is exactly as follow, one

value per line:

– Integer indicating the number of double rooms.

– Integer indicating the number of queen rooms.

– Integer indicating the number of king room.

As an example, see the file roomInfo.txt provided with the assignment. The method should

then create the appropriate number of double, queen, and king rooms. Store all the Rooms

in an array and return such array. Note that, if a IOException is thrown, the method should

return an array of Rooms with 0 elements.

• A main method that creates an Hotel where the name of the hotel is chosen by the user via

a Scanner object. The Rooms of the hotel are obtained using loadRooms and the provided

file roomInfo.txt. After the hotel has been created, its reservations should be loaded using the

provided file reservationsInfo.txt.

Then, the program should print on the screen a menu from which the user can chose one of the

following options:

1. Make a reservation

2. Cancel a reservation

3. See an invoice

4. See the hotel info

5. Exit the booking system

Depending on the option chosen, you should use Scanner to obtain from the user the information

needed (if any) to carry forward the operation selected. After each operation, the main menu

should be displayed again, unless the user had chosen to exit the booking system. In such case,

the program should save the reservations of the hotel in a file named reservationsInfo.txt and

terminate. If the user enters an incorrect option, then the main menu should also be displayed

again. Note that you can assume that the user will always be entering an integer. Note:

– Both when making and cancelling a reservation, your program should ignore whether the

rooms type has been typed using lowercase or capital letters. That is, double or DouBLe

both refer to a double Room.

– When making a reservation, the program should print an appropriate message letting the

user know whether the reservation was successful or not. For instance, if unsuccessful print

a message informing the user that the hotel has no available rooms of the requested type.

Otherwise, print a message letting the user know that the reservation was successfully

completed.

Sample interaction and output is shown below. Note that since we are loading and saving

the reservations from/to the same file, each time we run the program changes made to the

reservations of the hotel (no matter its name) in the previous runs will be taken into account.

Page 6

First example:

Page 7

Page 8

Page 9

Page 10

Second example:

Page 11

What To Submit

Please put all your files in a folder called Assignment4. Zip the folder (please DO NOT rar it) and submit it

in MyCourses. Inside your zipped folder there must be the files listed below. Do not submit any other

files, especially .class files.

BookingSystem.java

Hotel.java

Reservation.java

Room.java

Confession.txt (optional) In this file, you can tell the TA about any issues you ran into doing

this assignment. If you point out an error that you know occurs in your problem, it may lead

the TA to give you more partial credit. On the other hand, it also may lead the TA to notice

something that otherwise they would not.

Page 12


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp