Java 4 task console | Computer Science homework help

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 C[C]DDDDD[C][C]

where D = a digit and C = a character and [C] means that the character at this position

is optional.

The only valid characters at the start of the ticket are ‘A’, ‘B’, ‘C’ or “AA”

If there is only one optional character at the end of the ticket String, then that character may

only be ‘M’ or ‘D’

If there are two optional characters at the end of the ticket String, then the characters can

only be ‘M’ and ‘D’, in that order.

So an example of a valid ticket could be:

B09876D

The user may enter a ticket String of any length. The valid length of a ticket is between 6 and

9 depending on the conditions above. Any ticket that is not in this length range is

automatically invalid. A ticket that is invalid for any reason will result in a message being

displayed to the screen saying that the ticket is invalid, and no processing will be done with

that ticket.

Every effort should be made to give the exact reason why the ticket is invalid. For example,

wrong length, starts with an invalid character, or ends with an invalid character. These are

not the only reasons why a ticket is invalid, part of your task is to list all the conditions that

make a ticket invalid and write code to cover those cases.

If a ticket is a valid length, you may assume that the digit part of the ticket is entered

correctly. That is, it is exactly 5 digits and follows the character, or characters in the case of

“AA” at the start of the ticket String.

If the ticket is valid, then the cost of the ticket is calculated and displayed to the screen.

An “AA” class ticket costs $200

An ‘A’ class ticket costs $150

A ‘B’ class ticket costs $100

A ‘C’ class ticket costs $50

The character ‘M’ at the end of the ticket String indicates a Meal, which costs $35.75,

regardless of the ticket class.

The character ‘D’ at the end of the ticket String indicates a Drink, which costs $24.90,

regardless of the ticket class.

User input should be accepted in both upper and lower case and any combination of the two

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

NOTE: the samples DO NOT cover all of reasons a ticket can be invalid.

nor do they display appropriate invalid messages in most cases.

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

> java CalcTickets

Enter ticket code >> C12345

The cost of ticket C12345 is $50.0

> java CalcTickets

Enter ticket code >> B00009D

The cost of ticket B00009D is $124.9

> java CalcTickets

Enter ticket code >> AA00001MF

This is an invalid ticket

> java CalcTickets

Enter ticket code >> Z45678

This is an invalid ticket

> java CalcTickets

Enter ticket code >> aa00005md

The cost of ticket AA00005MD is $260.65

> java CalcTickets

Enter ticket code >> B12345MD67

Ticket format is not correct length

This is an invalid ticket

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 Task 2 may be copied from the csilib area

cp /home/student/csilib/cse1oof/assignAB/codes01.dat .

An example input file for Task 3 may be copied from the csilib area

cp /home/student/csilib/cse1oof/assignAB/dest01.dat .

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:

IATA code, Airport name, Country

where

IATA code, this is a String (text) and always consists of 3 upper case characters

and is immediately followed by a comma (,) and a space.

don’t forget the dot

don’t forget the dot

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

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).

An example of some of the lines of this file might be:

IXE, Mangalore, India

BOM, Mumbai, India

PPP, Proserpine Queensland, Australia

RKT, Ras Al Khaimah, United Arab Emirates

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 the name of an airport.

The program then reads through the file. If a matching airport name is found in the file,

the IATA code is displayed to the screen.

Airport names are unique in the file, so there will be at most only one match.

User entered Airport names must be case insensitive, that is, any combination of

uppercase and lowercase letters must give the same result.

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):

(Note that the sample runs do not necessarily show all the functionality required)

> java AirportCodes1

Enter file name >> codes01.dat

Enter airport name >> ProserPINE QUeensLaNd

Airport: Proserpine Queensland has IATA code: PPP

> java AirportCodes1

Enter file name >> codes01.dat

Enter airport name >> Mumbai

Airport: Mumbai has IATA code: BOM

> java AirportCodes1

Enter file name >> codes01.dat

Enter airport name >> Beijing

Airport name: beijing was not found

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

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 (lines).

Each record has the format

IATA code, Airport name, IATA code, Airport name

where

IATA code, these are Strings (text) and always consists of 3 upper case

characters and are immediately followed by a comma (,) and a space.

Airport name is the name of an Airport. These are Strings (text) and may contain

more than one word. The first Airport name is immediately followed by a

comma (,) and a space, the second Airport name is the end of the record.

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.)

An example of an input file is shown below:

ADL, Adelaide, AUH, Abu Dhabi

IXE, Mangalore, BOM, Mumbai

BOM, Mumbai, MEL, Melbourne

Once this file has been opened, the program checks if this file is empty (you may assume

that the user always enters a valid file name). If the file is empty the program displays

an appropriate message to the screen and closes, without using System.exit( ).

If the file is not empty, then the user is presented with a menu, as shown below:

Airport Finder

1. Enter Airport

2. Close

Enter choice >>

1. Enter Airport

If the user selects this choice, then the program prompts (asks) the user for an IATA code.

The program then reads the contents of the file, line by line, and displays to the screen

all the airports that connect to the airport whose IATA code has been entered by the user.

Unlike Task 2, this will require that your program reads the entire file. There may be more

than one match for the user entered IATA code in the input file.

If there is no match for the requested IATA code, then an appropriate message is displayed

to the screen.

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

Note that you need to consider that the IATA code entered by the user may the first or

second IATA code on the line from the file. If the user entered IATA code is matched

as the first one on the line, then the second IATA code and Airport name are displayed to the

screen, indented by a tab. If the user entered IATA code is matched as the second one on

the line then the first IATA code and Airport name are displayed to the screen.

The user is then presented with the menu again. The program continues to loop until the user

selects choice 2, when the program closes. You may assume that the user always enters an

integer, but, your program must correctly deal with an invalid integer entered by the user.

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

(Note that the sample runs do not necessarily show all the functionality required)

> java AirportCodes2

Enter input file name >> dest02.dat

The input file is empty

Program will now close

> java AirportCodes2

Enter input file name >> dest01.dat

Airport Finder

1. Enter Airport

2. Close

Enter choice >> 1

Enter IATA code >> MEL

Melbourne (MEL) airport connects to:

Mumbai airport (BOM)

Chennai airport (MAA)

Los Angeles airport (LAX)

Brisbane airport (BNE)

Airport Finder

1. Enter Airport

2. Close

Enter choice >> 1

Enter IATA code >> ZZZ

Invalid IATA code

Airport Finder

1. Enter Airport

2. Close

Enter choice >> 6

Invalid menu choice

Airport Finder

1. Enter Airport

2. Close

Enter choice >> 1

Enter IATA code >> PPP

Proserpine Queensland (PPP) airport connects to:

Brisbane airport (BNE)

Hamilton Island airport (HTI)

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

Airport Finder

1. Enter Airport

2. Close

Enter choice >> 2

Program now closing

As a final note for this task, consider that your program has to read through the entire

file every time the user chooses option 1. So your program has to re-open the file

each time option 1 is chosen.

Also note: you may NOT use the split( ) from the String class in this task, although you

are free to use any other methods in the String class, provided only that those

methods do NOT return an array. See Lecture/Workshop 2 for revision on the String class.

Task 4 (Part B) – LandYourShip.java

Write a Java program called LandYourShip.java that lands a space ship on a planet.

The space ship has just come out of hyper space and due to the characteristics of the planet,

it is not possible to come out in an exact position above the base.

The initial position is thus random, within certain limits.

Also the base is surrounded by high mountains. If the position of the space ship at any time is

greater than or equal to +10 or less than or equal to -10 (on the x axis) then the space ship

has crashed.

The space ship has to land at the base, co-ordinates 0,0, within 10 turns, otherwise the

space ship runs out of fuel and crashes.

The program starts by assigning a random number, between -10 and +10 (inclusive) for

the starting x coordinate of the space ship and another random number, between +5 and

+10 (inclusive) for the starting y coordinate of the space ship.

The program then prompts (asks) the user to enter a command (explained below). The

program calculates the new position ( x and y coordinates) of the space ship. If the

x and y coordinates of the space ship 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 command) of the space ship are not the

same as the base, the program loops around and asks for another command for the space

ship. (This may be the same command as the previous command or a different command)

and the program calculates the new x and y coordinates of the space ship, checks to see if

the space ship has landed at the base (success message, program ends), crashes (fail

message, program ends) or loops back

again for another command.

The space ship (user) has a maximum of 10 attempts to land at the base, else it has crashed.

Each time a command is entered, the calculations made, the check done and the

space ship has not landed at the base, 1 is subtracted from the remaining attempts.

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

The space ship (user) may not touch the -10 and +10 x boundaries. Any calculation that

would put the space ship at these x boundaries or below the y boundary (y < = 0 without

being at x = 0) means that the space ship has crashed.

Only certain commands are allowed, these are entered as text (not numbers)

The allowed commands and the change they make to the x and y coordinates of the space

ship (subject to the boundary condition above) are:

Full up y coordinate increased by 2, x coordinate stays the same

Half up y coordinate increased by 1, x coordinate stays the same

Down y coordinate decreased by 1, x coordinate stays the same

Left y coordinate stays the same, x coordinate decreased by 1

Right y coordinate stays the same, x coordinate increased by 1

Maintain y coordinate stays the same, x coordinate stays the same

Eject ends the program with an appropriate message

Regardless of the command entered above, due to the effect of gravity, y coordinate is

decreased by 1 every time the user enters a command. So, for example, if Full up

command is entered, the nett effect on the y coordinate would be new value of y =

old value of y + 2 – 1.

As another example, if Left command is entered, the nett effect would be

new value of x = old value of x – 1,

new value of y = old value of y – 1 (due to gravity) even though the information

above states for Left, the y coordinate stays the same. This is true for the Left command,

it does not alter the y coordinate, the -1 subtracted from the y coordinate is the -1 subtracted

from every command (except Eject)

Recall that the space ship starts at an x coordinate of between -10 and +10 and a y

coordinate between +5 and +10 and the aim is to land at the base which is at x

coordinate 0 and y coordinate 0.

There must be a space between the first 2 commands and all commands must be case

insensitive. This means that Down or DOwn or down should all produce the same

result.

You may assume that the user always enters a correct command

To safely land at the base the space ship must get the exact coordinates of the base.

coordinates are always integers, including the initial coordinates.

The space ship must not be able to go out of bounds. The bounds are:

-10 < x < 10

y >= 0

The user enters the command, then the calculation is done. If the new value, of either x, y or

both would be out of bounds, then the space ship has crashed.

The user can exit the program at any time by entering the command

Eject

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

This does not update anything and does not display anything else except some

message to say “try harder” or something similar.

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

(Note: not all options, functionality and messages are shown.)

> java LandYourShip

The starting position of the spaceship is x = 4 y = 7

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> left

You are now at coordinates x = 3 y = 6

You have 9 attempts remaining

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> left

You are now at coordinates x = 2 y = 5

You have 8 attempts remaining

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> left

You are now at coordinates x = 1 y = 4

You have 7 attempts remaining

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> left

You are now at coordinates x = 0 y = 3

OOF Assignment Part A and Part B – due: 10:00 am Mon 13th April this is the first and final hand in date p. 11 of 13

You have 6 attempts remaining

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> down

You are now at coordinates x = 0 y = 1

You have 5 attempts remaining

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> maintain

You are now at coordinates x = 0 y = 0

Well done pilot, you have the right stuff!!

> java LandYourShip

The starting position of the spaceship is x = 7 y = 6

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> right

You are now at coordinates x = 8 y = 5

You have 9 attempts remaining

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> right

You are now at coordinates x = 9 y = 4

You have 8 attempts remaining

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> right

OOF Assignment Part A and Part B – due: 10:00 am Mon 13th April this is the first and final hand in date p. 12 of 13

You are now at coordinates x = 10 y = 3

You were lucky to escape, try harder next time

> java LandYourShip

The starting position of the spaceship is x = 1 y = 5

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> left

You are now at coordinates x = 0 y = 4

You have 9 attempts remaining

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> down

You are now at coordinates x = 0 y = 2

You have 8 attempts remaining

Space ship landing control

Full up

Half up

Down

Left

Right

Maintain

Eject

Enter choice >> down

You are now at coordinates x = 0 y = 0

Well done pilot, you have the right stuff!!

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 latcs8 machine.

• You submit your files from your latcs8 account. Make sure you are in the same directory

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

submit OOF CalcTicket.java

submit OOF AirportCode1.java

submit OOF AirportCode2.java

submit OOF LandYourShip.java

 

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more