Java Code

profilenihil.khagram
193157_oof_assignment_part_a__b.pdf

OOF Assignment Part A and Part B - due: 10:00 am Mon 8 th Dec this is the first and final hand in date p. 1 of 10

Department of Computer Science and Information Technology

La Trobe University

CSE1OOF/4OOF Summer 1, 2014 – 15 Assignment Part A and Part B

Due Date: Monday, 8 th December, at 10.00 a.m.

First and Final date for SUBMISSION Monday 8 th December at 10.00 am

Delays caused by computer downtime cannot be accepted as a valid reason for a late submission without penalty. Students must plan their work to allow for both scheduled and unscheduled downtime. There are no days late or extensions on this assignment as execution test marking will begin on Tuesday 9

th December – in your normal lab (Week

7)

This is an individual Assignment. You are not permitted to work as a Pair Programming partnership or any other group when writing this assignment.

Copying, Plagiarism: Plagiarism is the submission of somebody else’s work in a manner that gives the impression that the work is your own. The Department of Computer Science and Information Technology treats academic misconduct seriously. When it is detected, penalties are strictly imposed. Refer to the subject guide for further information and strategies you can use to avoid a charge of academic misconduct.

Assessment Objectives:

• To practise using the Math class and the String class. (Lecture/Workshop 3)

• To practise reading from and writing to the console and reading from a text file. • To practise using loops (repetition)

Submission Details: Full instructions on how to submit electronic copies of your source code files from your latcs6 (or latcs5) account are given at the end. If you have not been able to complete a program that compiles and executes containing all functionality, then you should submit a program that compiles and executes with as much functionality as you have completed. (You may comment out code that does not compile.)

Note you must submit electronic copies of your source code files using the submit command on latcs6 or latcs5. Ensure you submit all required files, one file at a time. For example, the file CalcTickets.java would be submitted with the command:

> submit CSE13OOF CalcTickets.java

PLEASE NOTE: While you are free to develop the code for this progress check on any operating

system, your solution must run on the latcs6 (or latcs5) system.

Marking Scheme: This assignment is worth 5% of your final mark in this subject. Implementation (Execution of code) 90%, explanation of code 10%

You may be required to attend a viva voce (verbal) assessment (to be used as a weighting factor on the final mark).

Do NOT use the LMS to submit your files, use latcs5/6 only

OOF Assignment Part A and Part B - due: 10:00 am Mon 8 th Dec this is the first and final hand in date p. 2 of 10

Return of Mark sheets:

The face to face execution test marking in the lab (Week 7) constitutes a return of the assignment mark, subject to the conditions on page 10.

Please note carefully: The submit server will close at 10:00 am on Monday Dec 8

th

After the submit server has closed, NO assignments can be accepted. Please make sure that you have submitted all your assignment files before the submit server closes. (see page 10) There can be NO extensions or exceptions. Your assignment will be marked in your normal lab, starting week 7, Tuesday December 9th. You must attend your normal lab to have your assignment marked. Non-attendance will result in your assignment being awarded 0. You may come to any lab (if you can't make your normal lab). If you cannot attend any of the labs please email me ([email protected]) to arrange another time. Marking scheme: 90% for the code executing correctly 10%, you will be asked to explain (and / or alter ) parts of your code.

OOF Assignment Part A and Part B - due: 10:00 am Mon 8 th Dec this is the first and final hand in date p. 3 of 10

Task 1 – CalcTickets.java

Write a Java program called CalcTickets that prompts (asks) the user to

enter a ticket number. The format of a valid ticket is DDDCDD where D = a digit and C = a character. So an example of a valid ticket could be: 265F87 The user may enter a ticket number of any length. If the length is 6, then you may assume that the user has entered the ticket in the correct format. Tickets that are not length 6 are invalid and the program should reject these tickets by displaying an error message to the screen, then closing the program. If the ticket is valid, then the program works out, and displays to the screen, the cost of the ticket, based on the following rules. The first 3 characters of the ticket will be digits, these need to be converted from characters in the String to an integer. Use the java Integer.parseInt( String ) method. As an example: String str = "265"; int number = Integer.parseInt( str );

Once the first 3 characters have been converted from a String to an integer, they represent the base cost of the ticket in $ The 4

th character is the class of the ticket.

If this is 'B', then the final cost is the base cost * 5 If this is 'F', then the final cost is the base cost * 3 If this is 'E', then the final cost is the base cost * 1 If the 4

th character is not one of the 3 listed above, the ticket number is invalid, and a

message is output to the screen indicating this fact. If the ticket is valid, then the final cost is displayed to the screen.

Note: System.exit() is not be used anywhere in any of the Tasks. (Marks will be deducted if it is used anywhere in this assignment.) An example input file for Tasks 2 and 3 may be copied from the csilib area cp /home/student/csilib/cse1oof/assignAB/codes01.dat .

don't forget the dot

OOF Assignment Part A and Part B - due: 10:00 am Mon 8 th Dec this is the first and final hand in date p. 4 of 10

Some sample runs of the program are included below (user input is in bold): > java CalcTickets

Enter ticket >> 102B07

Ticket cost is $510

> java CalcTickets

Enter ticket >> 123E11

Ticket cost is $123

> java CalcTickets

Enter ticket >> 400G34

Invalid passenger class code

> java CalcTickets

Enter ticket >> 400F1009

Error: invalid ticket

> java CalcTickets

Enter ticket >> 102F44

Ticket cost is $306

Task 2 – AirportCodes1.java

Write a Java program called AirportCodes1.java that, firstly, prompts (asks) the user to enter an input file name. This is the name of a text file that can contain any number of records. A record in this file is a single line of text in the following format: Airport name, Country IATA code where Airport name is the name of an Airport. This is a String (text) and may contain more than one word. The Airport name is immediately followed by a comma (,) and a space. Country is the name of the country of the Airport name. This Country is a String (text) and may be more than one word. There is one space between each of the words in the Country (if more than one word) and there is always one space after the Country. IATA code, this is a String (text) and always consists of 3 upper case characters. An example of some of the lines of this file might be: Mangalore, India IXE

Mumbai, India BOM Proserpine Queensland, Australia PPP Ras Al Khaimah, United Arab Emirates RKT

The input file may have 0 to any number of records. The format of the input file is guaranteed to be correct. Your program does not have to check the format. Also, your program must work with any file name of the correct format. (Do not hard code the file name).

OOF Assignment Part A and Part B - due: 10:00 am Mon 8 th Dec this is the first and final hand in date p. 5 of 10

Once this file has been opened, the user is then prompted for an IATA code. The program then reads through the file. If a matching IATA code is found in the file, the name of the Airport (surrounded by ") with that code is displayed to the screen. IATA codes are unique so there will be at most only one match. If the entire contents of the file has been read and no match is found, then an appropriate message is displayed to the screen.

Some sample runs of the program are included below (user input is in bold):

> java AirportCode1

Enter file name >> codes01.dat Enter IATA code >> HKG

The airport name is "Hong Kong"

> java AirportCode1

Enter file name >> codes01.dat Enter IATA code >> RKT

The airport name is "Ras Al Khaimah"

> java AirportCode1

Enter file name >> codes01.dat Enter IATA code >> ZZZ

The IATA code ZZZ was not found

Task 3 – AirportCodes2.java

Write a Java program called AirportCodes2.java that, firstly, prompts (asks) the

user to enter an input file name. This is the name of a text file that can contain any number of records. The records are in exactly the same format as Task 2.

The input file may have 0 to any number of records. The format of the input file is guaranteed to be correct. Your program does not have to check the format. Also, your program must work with any file name of the correct format. (Do not hard code the file name).

Once this file has been opened, the user is then prompted for a Country. The program then reads through the input file and displays to the screen all the Airport names and associated IATA codes in, ( ), for that Country. As in Task 2, this requires reading the input file line by line and checking the Country on each line. Unlike Task 2, this will require that your program reads the entire file. There may be more than one match for the user entered Country in the input file. If there is no match for the requested Country, then an appropriate message is displayed to the screen.

OOF Assignment Part A and Part B - due: 10:00 am Mon 8 th Dec this is the first and final hand in date p. 6 of 10

Some sample runs of the program are included below (user input is in bold):

> java AirportCode2

Enter file name >> codes01.dat Enter country name >> Australia

Adelaide (ADL)

Proserpine Queensland (PPP)

Melbourne (MEL)

> java AirportCode2

Enter file name >> codes01.dat Enter country name >> India

Mangalore (IXE)

Mumbai (BOM)

Delhi (DEL)

> java AirportCode2

Enter file name >> codes01.dat Enter country name >> Mars

No airports for Mars were found

Task 4 (Part B) – FindYourBase.java

Write a Java program called FindYourBase.java that aids an explorer, who is lost, to find their way back to their base. The program starts by assigning a random number, between -10 and +10 (inclusive) for the x coordinate of the base and another random number, also between -10 and +10 (inclusive) for the y coordinate of the base. The program next assigns a random number, between -10 and +10 (inclusive) for the starting x coordinate of the explorer and another random number, also between -10

and +10 (inclusive) for the starting y coordinate of the explorer. The program then prompts (asks) the user to enter a direction (explained below). The program calculates the new position ( x and y coordinates) of the explorer. If the x and y coordinates of the explorer are the same as the x and y coordinates of the base, then the program displays a success message to the screen and the program ends. If the updated x and y coordinates (from the previous direction ) of the explorer are not the same as the base, the program loops around and asks for another direction for the explorer. (This may be the same direction as the previous direction or a different direction) and the program calculates the new x and y coordinates of the explorer, checks to see if the explorer has made it back to the base (success message, program ends) or loops back again for another direction. The explorer (user) has a maximum of 10 attempts to make it back to their base, else they are lost. Each time a direction is entered, the calculations made, the check done and the explorer has not reached the base, 1 is subtracted from the remaining attempts. The explorer (user) may not cross outside the -10 to +10 boundaries. Any calculation that would put the explorer outside the boundaries is assigned the value of the appropriate boundary.

OOF Assignment Part A and Part B - due: 10:00 am Mon 8 th Dec this is the first and final hand in date p. 7 of 10

For example, if the explorer was at x = 9 and the calculation would make x = 11, then x is assigned the value of 10 to keep it at the boundary. Only certain directions are allowed, these are entered as text (not numbers) The allowed directions and the change they make to the explorer (subject to the boundary condition above) are: North goes up 2 units ( x remains the same, y = y + 2 )

South goes down 2 units ( x remains the same, y = y - 2 )

East goes right 2 units ( x = x + 2, y remains the same )

West goes left 2 units ( x = x - 2, y remains the same )

North East goes up 1 unit, right 1 unit ( x = x + 1, y = y + 1 )

North West goes up 1 unit, left 1 unit ( x = x - 1, y = y + 1 ) South East goes down 1 unit, right 1 unit ( x = x + 1, y = y - 1 )

South West goes down 1 unit, left 1 unit ( x = x - 1, y = y - 1 ) There must be a space between the last 4 directions and directions must be case insensitive. This means that North or NOrtH or north should all produce the same result. Any user input that does not match one of the above directions should be ignored by the program (no changes) and a message displayed to the explorer (user). Although this does count as an attempt and so reduces the available attempts by 1. To safely get inside the base the explorer must get the exact coordinates of the base. coordinates are always integers, including the initial coordinates. The explorer must not be able to go out of bounds. The bounds are: -10 < x < 10 -10 < y < 10 The user enters the direction, then the calculation is done. If the new value, of either x, y or both would be out of bounds, then the explorer is warned that move is not valid and their x, y or both coordinates will be set to the applicable boundary. The user can exit the program at any time by entering the direction I'm lost

This does not update anything and does not display distance or anything else except some message to say "bad luck" or something similar. Some sample runs of the program are included below (user input is in bold): (Note: displaying the distance is optional) > java FindYourBase

The base is at coords x = 7 y = 9

You are currently at coords x = -6 y = -5

The distance to the base is 19.10

Enter direction >> east

The distance to the base is 17.80

You are now at x = -4 y = -5

OOF Assignment Part A and Part B - due: 10:00 am Mon 8 th Dec this is the first and final hand in date p. 8 of 10

You have 9 attempts left

Enter direction >> east

The distance to the base is 16.64

You are now at x = -2 y = -5

You have 8 attempts left

Enter direction >> east

The distance to the base is 15.65

You are now at x = 0 y = -5

You have 7 attempts left

Enter direction >> east

The distance to the base is 14.87

You are now at x = 2 y = -5

You have 6 attempts left

Enter direction >> east

The distance to the base is 14.32

You are now at x = 4 y = -5

You have 5 attempts left

Enter direction >> east

The distance to the base is 14.04

You are now at x = 6 y = -5

You have 4 attempts left

Enter direction >> north

The distance to the base is 12.04

You are now at x = 6 y = -3

You have 3 attempts left

Enter direction >> north

The distance to the base is 10.05

You are now at x = 6 y = -1

You have 2 attempts left

Enter direction >> north

The distance to the base is 8.06

You are now at x = 6 y = 1

You have 1 attempts left

Enter direction >> north

The distance to the base is 6.08

You are now at x = 6 y = 3

You have 0 attempts left

You have used up all your supplies better luck next time

> java FindYourBase

The base is at coords x = 6 y = -10

You are currently at coords x = 0 y = 10

The distance to the base is 20.88

Enter direction >> i'm lost

Your name will be remembered

> java FindYourBase

The base is at coords x = 6 y = 3

You are currently at coords x = 3 y = -8

The distance to the base is 11.40

Enter direction >> east

The distance to the base is 11.05

You are now at x = 5 y = -8

You have 9 attempts left

Enter direction >> north

The distance to the base is 9.06

You are now at x = 5 y = -6

You have 8 attempts left

Enter direction >> north

OOF Assignment Part A and Part B - due: 10:00 am Mon 8 th Dec this is the first and final hand in date p. 9 of 10

The distance to the base is 7.07

You are now at x = 5 y = -4

You have 7 attempts left

Enter direction >> north

The distance to the base is 5.10

You are now at x = 5 y = -2

You have 6 attempts left

Enter direction >> north

The distance to the base is 3.16

You are now at x = 5 y = 0

You have 5 attempts left

Enter direction >> north

The distance to the base is 1.41

You are now at x = 5 y = 2

You have 4 attempts left

Enter direction >> north east

Congratulations explorer you have found the base

> java FindYourBase

The base is at coords x = 6 y = -10

You are currently at coords x = -9 y = -2

The distance to the base is 17.00

Enter direction >> west

Too far west, you stay at x = -10

The distance to the base is 17.89

You are now at x = -10 y = -2

You have 9 attempts left

Enter direction >> sideways

The direction "sideways" is not valid

The distance to the base is 17.89

You are now at x = -10 y = -2

You have 8 attempts left

Enter direction >> south

The distance to the base is 17.09

You are now at x = -10 y = -4

You have 7 attempts left

Enter direction >> south

The distance to the base is 16.49

You are now at x = -10 y = -6

You have 6 attempts left

Enter direction >> south

The distance to the base is 16.12

You are now at x = -10 y = -8

You have 5 attempts left

Enter direction >> south

The distance to the base is 16.00

You are now at x = -10 y = -10

You have 4 attempts left

Enter direction >> south

Too far south, you stay at y = -10

The distance to the base is 16.00

You are now at x = -10 y = -10

You have 3 attempts left

Enter direction >> I'm lost

Your name will be remembered

OOF Assignment Part A and Part B - due: 10:00 am Mon 8 th Dec this is the first and final hand in date p. 10 of 10

Electronic Submission of the Source Code

• Submit all the Java files that you have developed in the tasks above. • The code has to run under Unix on the latcs6 (or latcs5) machine. • You submit your files from your latcs6 (or latcs5) account. Make sure you are in the same

directory as the files you are submitting. Submit each file separately using the submit command.

submit CSE13OOF CalcTicket.java submit CSE13OOF AirportCode1.java submit CSE13OOF AirportCode2.java submit CSE13OOF FindYourBase.java

After submitting the files, you can run the following command that lists the files submitted from your account:

verify

You can submit the same filename as many times as you like before the assignment deadline; the previously submitted copy will be replaced by the latest one. Please make sure that you have read page 2 about the submission close off date and time and the compulsory requirement to attend the execution test in Week 7 Failure to do both of these things will result in your assignment be awarded a mark of 0, regardless of the correctness of the program. Execution test marks are provisional and subject to final plagiarism checks and checks on the compliance of your code to this assignment document. As such final assignment marks may be lower or withdrawn completely.