关键词 > CSE1OOF/4OOF

CSE1OOF/4OOF Semester 1, 2021 Assignment Part A

发布时间:2021-04-16

Department of Computer Science and Information Technology

La Trobe University


CSE1OOF/4OOF Semester 1, 2021 Assignment

Part A


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


Due Date

Due : Wednesday 14th April at 10.00 a.m.

Execution test: Thursday 15th & Friday 16th during normal labs

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 in your normal lab (Week 6). After the submit server has closed, NO assignments can be accepted. Non-attendance at the week 6 lab you have signed up for on the LMS page will also result in your assignment being awarded 0, except as detailed below.


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

Testing techniques covered in:

Lectures 1, 2, 3, 4, 5 ,6,7,8

Workshop 1, 2, 3,4

Labs 1, 2, 3,4


Submission Details and marking

Full instructions on how to submit electronic copies of your source code files from your latcs8 account are given on page 2. All assignments in OOF are marked, face to face/online, in the lab, in an execution test. This means that we mark running code. Your code must compile and display a result to the screen. The smallest amount of code that produces and displays a correct result will gain more marks than lots of code that doesn't compile, run or display something to the screen. If you cannot attend the lab you have signed up for on the LMS page, please email me ([email protected]) to arrange another time.


Marking summary

This assignment is worth 10% of your final mark in this subject.

Implementation (Execution of code) 90%, explanation of code 10%

  Instant zeros or heavily reduced marks
  Not submitting code
  Not attending marking session
  Using code that has not been taught yet
  Does not run on latcs8
  Uses System.exit()
  Flying.java
  25%
  EmptyName.java
  10%
  Maker.java
  15%
  Range.java
  20%
  Ticket.java
  20%
  Explanation
  10%


Using code not taught in OOF

Please also note carefully that whilst we encourage innovation and exploring java beyond what has been presented in the subject to date, above all, we encourage understanding.

All of the Tasks that follow can be solved using techniques that have been presented in lectures, lecture / workshops and labs so far. You do not need any external information.

These are the techniques and knowledge that we will later be examining in the Real Time Test and the exam.

Code and techniques that are outside the material presented will not be examined, of course.

You are only allowed to use techniques that have been covered in the following:

Lectures 1, 2, 3, 4, 5 ,6,7,8

Workshop 1, 2, 3,4

Labs 1, 2, 3,4

Using any techniques outside of these will result in a mark of 0 for the entire assignment.


Electronic Submission of the Source Code

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 OOF1 Flying.java

submit OOF1 EmptyName.java

submit OOF1 Maker.java

submit OOF1 Range.java

submit OOF1 Ticket.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.


Background - Limitations and Notes

Error checking

In this program you can assume that the data in the input files are without errors and are in the specified format. Unless otherwise specified.

File name

The input must accept any file name, i.e. do not hard code the file name.

Variable names

All variables must use camelCase and be meaningful

Class names

All classes must use TitleCase as provided

Indentation

All code must be appropriately indented


Key Points Repeated:

There are no days late or extensions on this assignment.

Your code must compile and display a result to the screen.

Using any techniques outside of the lectures, workshops and labs specified will result in a mark of 0 for the entire assignment.

The code must run under Unix on the latcs8 machine.

This is an individual Assignment.

Do not leave this assignment for last minute, try to do little every day.


Task 1 – Flying.java

Write a Java program called Flying.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:

Num of passengers^range^name^manufacturer^model

where

Num of passengers is the total number of people in the plane.

This represents an integer number, but remember that it is still part of the String so each "digit" is actually a character. It is not an integer until it is extracted from the String and converted into an actual integer.

The Num of passengers is followed by a '^' character, there are no spaces before or after the '^'.

range is the range, in kilometres, of the plane.

This represents an integer number, but remember that it is still part of the String so each "digit" is actually a character. It is not an integer until it is extracted from the String and converted into an actual integer.

The range is followed by a '^' character, there are no spaces before or after the '^'.

name, is the name of plane. This is a String (text) and may have spaces in it. The name may be empty as not all planes have "names".

The name is followed by a '^' character, there are no spaces before or after the '^'.

manufacturer, is the name of the company that makes the plane.

This is a String (text) and may have spaces in it.

The manufacturer is followed by a '^' character, there are no spaces before or after the '^'.

model, is the name of the model of the plane.

This is a String (text) and may have spaces in it.

The model is the end of the String, there is only a newline character (not seen) after model, as this is the end of the line.

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

1^835^Butcher Bird^Focke-Wulf^Fw 190

853^15700^^Airbus^A380-800

10^3200^Flying Fortress^Boeing^B17-G

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 (asked) for a manufacturer and a model.

The program then reads through the file. If a matching manufacturer and model is found in the file, all the information for that plane is displayed to the screen. The order of display is shown in the example runs below.

Manufacturer's can be repeated in the file, but, the combination of manufacturer AND model is unique in the file, so there will be at most only one match.

User entered manufacturer's and model's 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.

The data is surrounded by " ". Output is on one line, even though it is shown wrapped around, just to fit on this page.

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 Flying

Enter file name >> planes.txt

Enter manufacturer >> Sukhoi

Enter model >> Su-27

Name: "Flanker" model: "Su-27" manufacturer: "Sukhoi" range: "3530kms"

passengers: "1"


> java Flying

Enter file name >> planes.txt

Enter manufacturer >> Focke-Wulf

Enter model >> Ta-152

No plane with the model "Ta-152" made by the manufacturer "Focke-Wulf" was found


> java Flying

Enter file name >> e.txt

The file "e.txt" is an empty file, program closing


Copy input file from the assignment folder on LMS


Task 2 EmptyName.java

Write a Java program called EmptyName.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 Task4.

The program then displays to the screen all the details of all Planes that have no Name. That is the Name 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 Name 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 EmptyName

Enter file name >> planes.txt

Name: "" model: "A380-800" manufacturer: "Airbus" range: "15700kms" passengers: "853"


Task 3 Maker.java

Write a Java program called Maker.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 manufacturer.

The program then displays to the screen all the details of Planes (the complete record) of those Planes made by that manufacturer. This time you have to read the entire file as there may be more than Plane in the file made by this manufacturer.

User input is case insensitive, that is Boeing and bOEing should produce the same result. If the entire file has been searched and not one match has 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 is 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 Maker

Enter file name >> planes.txt

Enter manufacturer name >> Boeing

Manufacturer: "Boeing" Model: "B17-G" Name: "Flying Fortress"

Passengers: "10" Range: "3200 kms"

Manufacturer: "Boeing" Model: "747-400" Name: "Jumbo jet"

Passengers: "650" Range: "14200 kms"


> java Maker

Enter file name >> planes.txt

Enter manufacturer name >> focke-wulf

Manufacturer: "Focke-Wulf" Model: "Fw 190" Name: "Butcher Bird"

Passengers: "1" Range: "835 kms"


> java Maker

Enter file name >> planes.txt

Enter manufacturer name >> OOF ness

No Planes made by the manufacturer "OOF ness" were found


> java Maker

Enter file name >> e.txt

The file "e.txt" is an empty file Program closing


Task 4 – Range.java

Write a Java program called Range.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 minimum range (integer) and a maximum range (integer). You may assume that the user always enters the maximum range greater than the minimum range.

The program then displays to the screen all the details of Planes (the complete record) of those Planes that have a range within the minimum and maximum ranges as entered by the user.

To do this, your program will need to read the entire contents of the file, line by line. Unlike Task 1, there may be more than one Plane that meets the range requirements.

If there are no Planes within the ranges entered by the user, then an appropriate message is displayed to the screen.

Your program needs to convert the range in the input file line into an integer.

The use of Double.parseDouble( ) is not allowed for this Task (using Double.parseDouble( ) will result in this Task being awarded 0, regardless of the correctness of the result). This mainly because there are no doubles in the program.

What is described below is a way of taking "digits" that are really characters out of a String, one by one and converting them into a "proper" integer, that is an int type variable.

This assignment is to consolidate the String class and basic selection (if, if - else, switch)

Consider that when we enter 3 what we are really entering is the character '3' with ASCII/Unicode number 51. We need to convert the character 3 to the integer 3 by subtracting character '0'.

Given that the format of the range is fixed in the input String, (that is, it is always in the same position in the String) this allows us to assemble the integer from the String

Characters are converted to digits by subtracting the character '0' from the digit, as a character, in the String

How many digits are there? First you have to extract the range from the entire, as a String still (hint: use indexOf and substring from lecture / workshop 2)

Then you can take the length of the String, this tells us how many digits there are in the String. Use a for loop to convert each digit in the String to an integer.

The idea is like this, say the range in the line is 658

Then, take the first character, '6' and convert it to the integer 6

'6' - '0' = 6

Now get the next character, the '5' and convert it to an int '5' - '0' = 5

multiply the first int by 10, to make it 60, then add the 5 for a total of 65 The range is now 65

Get the next character, '8' and convert it to the integer 8, multiple the existing range by 10, making it 650 and add the 8, giving a total of 658.

Note: if you do use other methods, you MUST be ready to explain how these methods work. Just copying methods from other sources and not being able to explain them will result in this Task being awarded 0. This is detailed on page 3 of the assignment above.

Note the output format is very different to Task 1

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 Range

Enter input file >> planes.txt

Enter min distance >> 500

Enter max distance >> 3600

Range: "835 kms" model: "Fw 190"

Range: "3200 kms" model: "B17-G"

Range: "3530 kms" model: "Su-27"


> java Range

Enter file name >> e.txt

The input file "e.txt" is an empty file, Program closing


> java Range

Enter input file >> planes.txt

Enter min distance >> 4000

enter max distance >> 7500

No planes were found in the required range


Task 5 – Ticket.java

Write a Java program called Ticket.java that prompts (asks) the user to enter a ticket number. The format of a valid ticket is CCDDD[C][DC]

where D means a required digit and C means a required character. [C] means that the character at this position is optional and [D] means the digit at this position is optional.

Given the specification above, any ticket entered by the user that has a length of less than 5 or greater than 8, must automatically be invalid.

Also, any ticket of exactly length 7 must be invalid, this will be explained below.

In that case, an appropriate message must be displayed to the screen and no further processing is done. This includes not displaying any costs

There are many reasons why a user Ticket may be invalid, even if it is within the correct length range.

Every effort should be made to give the exact reason why the ticket is invalid. For example, wrong length, doesn't start with valid characters, doesn't have a valid character in the right optional place(s), doesn't have an optional digit in the right place. Part of your task is to list all the conditions that make a ticket invalid and write code to cover those cases.

User input that should be accepted in both upper and lower case. That is the ticket input must be case insensitive.

The first 2 required characters must be one of "AB", "BA" or "CB"

If the first 2 characters are not one of these groups, then the whole ticket is invalid, no further processing is done and an appropriate message is displayed to the screen.

If the first 2 characters are one of the valid groups, then we move on to process the digits. Remember that the digits are actually part of a String, so they will be characters.

It is NOT necessary to check if these 3 characters are digits. If the first 2 characters are a valid group, then it is guaranteed that there will be 3 digits as characters, assuming the length is correct, of course.

As we are not allowed to use any methods from the Integer or Double class, we need to convert the characters into a single integer (int)

This assignment is to consolidate the String class and basic selection (if, if - else, switch), you may also make use of for if you are comfortable using it. When we enter a digit from the keyboard, what we are really entering is the character for that digit.

For example, if we enter 3, what we are really entering is the character '3'

If we look up the ASCII/Unicode value for the character '3', we see that it is 51, not 3 We can convert digits as characters to integers by subtracting the character '0'

In ASCII/Unicode, then '3' - '0' = 51 - 48 = 3

As we do this conversion, we can assemble the final integer result.

The first integer will be hundreds, the second integer, tens and the third integer will be units.

Now that we have this integer, this becomes the basic cost of the ticket.

If the first 2 characters are "AB", the cost of the ticket is the basic cost increased by 15%

If the first 2 characters are "BA", the cost of the ticket is the basic cost increased by 7%

If the first 2 characters are "CB", the cost of the ticket is just the basic cost.

If there are no optional characters, then the final cost is displayed to the screen, see the examples, and the program finishes, there is no looping.

If there is an optional character after the 3 digits, then that character must be one of 'P' or 'D'

If the optional character is not one of these 2, the whole ticket is invalid, an appropriate message is displayed to the screen and no cost is shown.

If the optional character is 'P' then the cost of the ticket is increased by 25% If the optional character is 'D' then the cost of the ticket is decreased by 20%

Once again, if there are no more characters in the ticket, then this final cost is displayed to the screen and the program finishes.

If there are any more characters after the optional 'P' or 'D' then there must be 2 characters. We have already checked that the ticket cannot be of length 7, which would mean just one character and we have already checked that the length is not greater than 8, so we know that there are exactly 8 characters.

The first character must be a digit between '2' and '9'. If it is anything other than a digit between '2' and '9', then an appropriate message is displayed to the screen, no further processing is done and no cost is displayed.

After converting this character to a digit, multiply the digit by 10 and add it to the cost of the ticket.

We then have to check that the last optional character is one of 'S' or 'V'. If the last optional character is not one of these 2, an appropriate message is displayed to the screen, no further processing is done and no cost is displayed.

If the last optional character is 'S', then add 10 to the final cost of the ticket. If the last optional character is 'V', then add 20 to the final cost of the ticket.

If we get to this point and everything is valid, then display the final cost of the ticket.

At first, this Task can seem quite complex. Here is a suggested strategy to break the problem down into smaller parts, to "eat the dragon in little bits".

Start by checking the length of the user input. If the length is not between 5 and 8 inclusive, then the Ticket is invalid, nothing further needs to be done except to display the appropriate error message.

Then check if the length is exactly 7. Again, if it is, then the Ticket is invalid, nothing further needs to be done except to display the appropriate error message.

If the length is correct, solve the problem assuming that the user enters a correct set of starting characters and the three digits.

Extend the program to solve the problem for the user entering just the one correct optional character.

Finally, extend the program to solve the problem for the user entering both the correct optional character and the optional, correct, digit and character.

Once these are working, then start working on testing for user input errors and build up your program bit by bit to detect these errors and display the appropriate error messages.

Meaning that you go back and have the program correctly detect if the first 2 characters are not one of the valid groups, then if the length is 6, the optional character is not one of 'P' or 'D', and so on, until your program is complete.

Remember that it is better to have some parts of the program working correctly than none of the program compiling.

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, that is, they do not cover all test cases. Identifying and covering all of the cases is part of your Task.

The samples do not always display appropriate invalid messages in most cases, your assignment submission will display the appropriate messages.


> java Ticket

Enter ticket >> ba100

Cost of the ticket is $107.0


> java Ticket

Enter ticket >> eb200

First character must be one of 'A', 'B' or 'C'


> java Ticket

Enter ticket >> CA100

The character 'B' must follow 'C'


> java Ticket

Enter ticket >> ab100p

Cost of the ticket is $143.74


> java Ticket

Enter ticket >> ab23

Not enough entries.

The minimum length of a valid ticket is 5


> java Ticket

Enter ticket >> ab100p1v

7th character must be a digit between 2 and 9


> java Ticket

Enter ticket >> ba100d5v

Cost of the ticket is $155.60