Reference no: EM132356888
Assessment Objectives:
• To practise using the String class. (Lecture/Workshop 2 and Lab 2)
• To practise using if, if-else statements (Lecture 5 and 6, Lecture/Workshop 3)
• To practise reading from and writing to the console and reading from a text file.
In all the Tasks that follow, the String for State maybe empty (how you can detect this is described below). If the State String is empty, then the text "No State Listed" is assigned to the State String.
Task 1 - Task1.java
Write a Java program called Task1.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-code)Airport-name)City)State)Country
where
Airport-code is the IATA airport code. This code uniquely identifies any airport in the world. It is always text, a String.
The Airport code is followed by a ')' character, there are no spaces
before or after the ')'.
Airport-name is the name of the airport associated with the IATA code. The Airport name will have at least 2 words in the name, but can have any number of words ,2 or more.
The Airport name is followed by a ')' character, there are no spaces before or after the ')'.
City, is the name of the city of the airport.
This is a String (text) and maybe than one word.
The City is followed by a ')' character, there are no spaces before or after the ')'.
State, is the name of the state, or province, that the airport is in. This is a String (text) and maybe more than one word.
The State may also be empty, it would look like this ))
The State is followed by a ')' character, there are no spaces before or after the ')'.
Country, is the name of the country, that the airport is in. This is a String (text) and maybe more than one word.
The Country is the end of the String, there is only a newline character (not seen)
after Country, as this is the end of the line.
An example of some of the lines of this file might be:
CHN)Jeonju Airport)Jeonju))South Korea FSZ)Shizuoka Airport)Shizuoka)Honshu)Japan CIY)Comiso Airport)Comiso)Sicily)Italy
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. See question 3, lab 3)
Once this file has been opened, the user is then prompted (asked) for an Airport Code
The program then reads through the file. If a matching Airport Code is found in the file, all the information for that airport is displayed to the screen. The order of display is shown in the example runs below. This means that you must break up the line into its parts,
as we did in lab2. Just printing the line straight out of the file will get very low marks.
Airport codes are unique, so once we have found a match and displayed the result, we can close the file and close the program.
User entered Airport codes must be case insensitive, that is, any combination of uppercase and lowercase letters must give the same result. This applies to all user input except, of course, file names which must be entered exactly as the file name on latcs8.
If the entire contents of the file have been read and no matching Airport code 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 Task1
Enter file name >> a.dat Enter airport code >> WSY Code = WSY
Country = Australia State = Queensland
City = Airlie Beach / Shute Harbour Airport = Whitsunday Airport
> java Task1
Enter file name >> a.dat
Enter airport code >> ZZZ
No Airport was found with the code ZZZ
> java Task1
Enter file name >> x.dat
"x.dat" is an empty file, program closing
Input files for all Tasks to help you develop your programs may be copied from the csilib area
cp /home/student/csilib/cse1oof/assignA/a.dat . cp /home/student/csilib/cse1oof/assignA/b.dat . cp /home/student/csilib/cse1oof/assignA/x.dat .
Task 2 - Task2.java
Write a Java program called Task2.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 same format as Task 1
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 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( ). This is the same as Task 1.
If the file is not empty, then the user is prompted (asked) to enter a Country.
The program then displays to the screen all the details of Airports from this country (the complete record). This time you must read the entire file as there may be more than one Airport in the file for the user entered Country.
User input is case insensitive, that is Bahamas and bAHamaS should produce the same result.
If the entire file has been searched and no matches have been found, then an appropriate message is displayed to the screen.
As in Task 1, the output must be on one line, even though it might be shown on more than one line in this document. Equally, a file that exists, but is empty, must have an appropriate message displayed.
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 Task2
Enter file name >> a.dat
Enter country >> Bahamas
Code = ATC Country = Bahamas
City = Arthur's Town State = Cat Island
Airport = Arthur's Town Airport
Code = CCZ Country = Bahamas City = Chub Cay
State = Berry Islands
Airport = Chub Cay International Airport
> java Task2
Enter file name >> a.dat
Enter country >> OOF Land
No Country was found with the name OOF Land
> java Task2
Enter file name >> x.dat
"x.dat" is an empty file, program closing
Task 3 - Task3.java
Write a Java program called Task3.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 same format as Task 1
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 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( ). This is the same as Task 1.
If the file is not empty, there is no further user input in this Task3.
The program then displays to the screen all the details of all Airports that have no State. That is the State String is an empty String, it just has the opening ')' immediately followed by the closing ')'. Read the top of page 4 again for the output (display) of empty State Strings.
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 Task3
Enter file name >> a.dat
Code = AFS
Country = Uzbekistan City = Zarafshan
State = No State Listed Airport = Zarafshan Airport
Code = AQB
Country = Guatemala City = Quiche
State = No State Listed Airport = Quiche Airport
Code = IXM Country = India City = Tamil Nadu
State = No State Listed Airport = Madurai Airport
Code = JHB
Country = Malaysia City = Johor Bahru
State = No State Listed
Airport = Senai International Airport
Code = CHN
Country = South Korea City = Jeonju
State = No State Listed Airport = Jeonju Airport
> java Task3
Enter file name >> x.dat
"x.dat" is an empty file, program closing
Task 4 - Task4.java
Write a Java program called Task4.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).
The records (lines) in this text file are different to the lines in Tasks 1 - 3
This time, each line is the record of an airline code, the name of the airline and the airport code that is the code for the airport that the airline is based at.
The format of the file is
Airline code(Airline name(Airport code
where:
Airline code is the code for the Airline. This code is unique and is always text
The Airline code is followed by a '(' character, there are no spaces before or after the '('.
Airline name is the name of the Airline. This is always text and can be any number of words (1 or more)
The Airline name is followed by a '(' character, there are no spaces before or after
the '('.
The Airport code is the same Airport code that we have been using in Tasks 1 - 3
The Airport code is always text. As it is the end of the String, there is nothing after the Airport code except the end of line (newline) marker.
Some example lines of this file
SND(Air Samarkand(AFS
FEA(Far Eastern Air Transport(RMQ IIC(Inter Island Air Charter(CCZ
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 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 prompted (asked) to enter an Airline code. The program then reads the airline codes input file, until it finds the matching Airline code in the input. If the program finds a match for the Airline code then
all three parts of the line are saved in separate Strings. This is just breaking up the line as we have done in Tasks 1 to 3
If a matching Airline code is not found, then an appropriate message is displayed to the screen and the program closes.
Regardless of match or not, remember to close the airline codes file. Now we open the Airports code file, just as we done in Tasks 1 - 3 Each record has the same format as Task 1
The same conditions apply to an empty input file as in Tasks 1 - 3
If the Airport codes file is not empty, then the program searches through the Airport codes input file, using the Airport code String from the Airlines code input file as the search key.
If a match for the Airport code is found, then the details of both the Airline codes and the Airport codes record is displayed to the screen. The format is shown below in the example runs.
If no match is found for the Airport code, an appropriate message is displayed to the screen.
Note the output format in the examples below
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 Task4
Enter Airlines input file name >> b.dat
Enter the airline code >> PNP
Please enter the airport codes input file >> a.dat Airline: Pineapple Air IATA code: PNP is based at Chub Cay International Airport (CCZ) in Bahamas
> java Task4
Enter Airlines input file name >> b.dat
Enter the airline code >> AFL
Please enter the airport codes input file >> a.dat
No airport with the code SVO was found
> java Task4
Enter Airlines input file name >> b.dat
Enter the airline code >> ABC
No airline with the code ABC was found Program closing
> java Task4
Enter Airlines input file name >> x.dat
"x.dat" is an empty file, program closing
> java Task4
Enter Airlines input file name >> b.dat
Enter the airline code >> QFA
Please enter the airport codes input file >> x.dat The airport codes input file x.dat is an empty file Program closing
Attachment:- Java Assignment.rar