Task 1 Stars.java

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

Name~Proper Name~HR number~HD number~distance

where

Name is the common name of a star. This is a String (text) and may contain more than one word. This name is followed by a '~' character, there are no spaces before or after the '~'.

Proper Name is the proper name of a star. This is a String (text) and may contain more than one word. This name is followed by a '~' character, there are no spaces before or after the '~'.

HR number is the HR classification number of the star. It is always the characters HR then a space and a number of digits, followed by a '~' character, there are no spaces before or after the '~'.

HD number is the HD classification number of the star. It is always the characters HD then a space and a number of digits, followed by a '~' character, there are no spaces before or after the '~'.

distance is the distance in light years from Earth. The format of this distance is always two digits after the decimal point, there may be between one and four digits to the left of the decimal point.

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

Acrux~Alpha 1 Crucis~HR 4730~HD 108248~320.00
Achird~Eta Cassiopeiae~HR 219~HD 4614~19.41
Ain Al Rami~Nu 1 Sagittarii~HR 7116~HD 174974~1850.59

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 proper star name.

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

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

User entered Proper star 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 Stars
Enter file name >> starData.txt
Enter star proper name >> Alpha Cephei
Star: proper name: Alpha Cephei distance: 49.05 light years HD num: HD 203280
HR num HR 8162 common name: Alderaimin
> java Stars
Enter file name >> starData.txt
Enter star proper name >> Alpha Centuri
No star with the proper name "Alpha Centuri" was found
> java Stars
Enter file name >> e.dat
"e.dat" is an empty file

An example input file for Task 1 and 2 may be copied from the csilib area

cp /home/student/csilib/cse1oof/assignA/a.dat .
cp /home/student/csilib/cse1oof/assignA/f.dat .

Task 2 Distance.java

Write a Java program called Distance.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( ).

If the file is not empty, then the user is prompted (asked) to enter a minimum distance (double) and a maximum distance (double). You may assume that the user always enters the maximum distance greater than the minimum distance.

The program then displays to the screen all the details of Stars (the complete record) of those Stars that are within the minimum and maximum distances 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 Star that meets the distance requirements.

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

Your program needs to convert the distance in the input file line into a double.

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 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 distance is fixed in the input String, this allows us to assemble the double from the String

As mentioned above the characters are converted to digits by subtracting '0'

If there were two digits to the left of the decimal point, then the first converted digit is multiplied by 10 and added to the second converted digit, we have assembled the number part on the left of the decimal point.

If the third converted digit, the first after the decimal point, is divided by 10.0 then added to the first 2, we have the total up to the first decimal place. The fourth converted digit is divided by 100.0 and added to the total, then we have assembled the distance as a double.

Once we have the distance as a double then, of course, we can compare this figure with the double entered by the user and either display the file line if it meets the requirements, or move on to the next line and do the same thing, until the end of file is reached.

If there are 3 or 4 digits to the left of the decimal point, then it is the same principle, just each time we need to start multiplying by 100 or 1000

Note the output format is slightly 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 Distance
Enter file name >> starData.txt
Enter minimum distance >> 350.56
Enter maximum distance >> 410.34
Star: proper name: Beta Centauri distance: 390.0 light years HD num: HD 122451
HR num HR 5267 common name: Agena
Star: proper name: Sigma Hydrae distance: 370.92999999999995 light years HD num:
HD 73471
HR num HR 3418 common name: Al Minliar Al Shuja
Star: proper name: Gamma Pegasi distance: 390.18 light years HD num: HD 886
HR num HR 39 common name: Algenib
> java Distance
Enter file name >> e.dat
"e.dat" is an empty file
> java Distance
Enter file name >> starData.txt
Enter minimum distance >> 4000.00
Enter maximum distance >> 4500.00
No star within 4000.0 and 4500.0 light years was found

Task 3 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 DDDC[CDD][C]

where D = a required digit and C = 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 4 or greater than 8, must automatically be invalid.

In that case, an appropriate message must be displayed to the screen and no further processing is done.

The DDD represents the starting price of a ticket. As in Task 2, this must be converted from characters into an integer. Since the Ticket has at least one character, it is necessary to take in the input as a String.

It is necessary to check that the first three characters are digits, without using Integer.parseInt( ) Use the techniques you applied in Task 2

Once you have the integer, then the base cost of the ticket is the integer divided by 10, as a double.

If any of the first three characters are not digits, then an appropriate message is displayed to the screen and no further processing is done, and no cost is displayed to the screen.

Assuming that the first 3 characters are digits, then the next character must be either 'F', 'M' or 'B'

If that fourth character is not one of the above, then the ticket is invalid, an appropriate message is displayed to the screen and no further processing is done.

If the fourth character is an 'F' then the base cost of the ticket is increased by 20%, if the fourth character is an 'M', then the base cost of the ticket is increased by 10%, if the fourth character is a 'B', then the base cost of the ticket remains the same.

If there are more characters in the ticket, then there must 3 extra characters. If there are less than 3 characters, then the ticket is invalid, an appropriate message is displayed to the screen and no further processing is done. No cost is to be displayed to the screen.

If there are 3 characters, then the first character of this group of 3 must be either 'D' or 'S', if the character is not one of these, then the ticket is invalid, an appropriate message is displayed to the screen and no further processing is done. No cost is to be displayed to the screen.

If the character is either 'D' or 'S' then the next 2 characters must be digits. If they are not digits, then the ticket is invalid, an appropriate message is displayed to the screen and no further processing is done. No cost is to be displayed to the screen.

If the 2 characters are digits, they are converted into an integer, using the techniques described above, Integer.parseInt( ) is not to be used (this task will be awarded 0 if it is).

If the first character (of this group) is 'D', then the 2 digits become the discount that is applied to the cost of the ticket, as worked out above. For example, if the base cost of the ticket had been calculated as $20.00 and the 3 characters were D50, then the discount would be 50%, resulting in the new cost being $10.00

If the first character (of this group) is 'S', then the 2 digits become the surcharge (extra) that is applied to the cost of the ticket, as worked out above. For example, if the base count of the had been calculated as $20.00 and the 3 characters were S50, then the surcharge would be 50%, resulting in the new cost being $30.00

If there is one more character at the end of the ticket, then that character must be either an 'S' or a 'P'. If there is an extra character and it is not one of 'S' or 'P' then the ticket is invalid, an appropriate message is displayed to the screen and no cost is displayed to the screen.

If the character is an 'S' then final cost of the ticket is reduced by 10%. If the character is a 'P' then the final cost of the ticket is reduced by 15%

So an example of a valid ticket could be: 735MS05P

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 digits, doesn't have a valid character in the right place, doesn't have digits 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.

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 4 and 8 inclusive, 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 just digits, that is, three digits and a valid character.

Once you have this working correctly, solve the problem assuming the user correctly enters an optional group of a valid character and 2 digits.

Then extend the program to solve the problem for the user entering a correct optional character at the end of the Ticket.

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.

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 >> 400F
Ticket cost = $48.0
> java Ticket
Enter ticket >> 50FF
Not a valid ticket
Third character must be a digit
> java Ticket
Enter ticket >> 230MS50
Ticket cost = $37.95
> java Ticket
Enter ticket >> 400t
Not a valid ticket
Fourth character must be one of 'F', 'M' or 'B'
> java Ticket
Enter ticket >> 500FS4
Not a valid ticket
Valid tickets cannot be length 6
> java Ticket
Enter ticket >> 100BS10P
Ticket cost = $9.35

Note that we haven't yet learnt how to control the number of decimal places that are displayed so this will not be an issue.

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

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.