Question: Consider a City class that has: private instance variables: private String CityName private String countryName private double[] averageMonthlyRainfall An appropriate constructor The following public methods:

Consider a City class that has:

private instance variables:

private String CityName

private String countryName

private double[] averageMonthlyRainfall

An appropriate constructor

The following public methods:

public String getCityName( )

public String getCountryName( )

public double[ ] getAverageMonthlyRainfall( )

public void addMonthlyAverageRainfall(double rainfall) throws IllegalArgumentException

public void modifyMonthlyRainfall(double rainfall, int monthNum) throws IllegalArgumentException

public String toString( )

public boolean equals(Object obj) - comparison based on cityName and CountryName

Implement a well-structured Java program that uses the City class and CityDriver class to enable a climate scientist to maintain city monthly average rainfalls of up to 12 months. The average rainfall information is kept in a text-file rainfall.txt of the form:

where each line of the text-file contains a city name (a single word), a country name (a single word), followed by up to twelve monthly average rainfall values in millimeters (The above table shows the monthly average rainfall for January, February, and March only)

Note: The initial input file has no rainfall information. It is in the form:

Arusha Tanzania 50.0 80.0 170.0
Athens Greece 60.0 45.0 40.0
DarEsSalaam Tanzania 90.0 90.0 90.0
Dubai UAE 76.3 54.9 138.1
Dublin NorthenIreland 18.8 25.0 22.1
London UK 70.0 60.0 70.0
Manila Philippines 55.2 40.9 41.6
Marrakesh Morocco 20.0 10.0 10.0

1. Display Rainfall Infomation for all cities.Your CityDriver program must have the following main menu:

2. Display Rainfall Infomation for a particular city.

3. Display total rainfall for each city.

4. Modify a particular rainfall average for a particular city and country pair.

5. Add monthly rainfall average for the current next month for all cities.

6. Add New city.

7. Delete a city.

8. Exit.

Please select your choice:

Your program must loop as long as option 8 has not been selected. It must display an appropriate error message if an invalid choice is entered. After executing each of the options 1 to 7, your program must pause and display the message: Press Enter key to continue . . . . Your program must display the main menu after pressing the Enter key. Each of the options 1 to 7 must be implemented in a separate private static method. The code for Press Enter key to continue . . . must also be implemented is a separate private static method. Your main method must handle IOException, IllegalArgumentException, and InputMismatchException by displaying an appropriate error message, then waiting for a key press, before returning control to the main menu.

The options must have the following behaviors:

Option 1: Display Rainfall Info for all cities

It displays the rainfall information of all cities if rainfall.txt contains at least one column of rainfall information; otherwise the error message: There is no rainfall information. is displayed. The option must be implemented by reading directly from rainfall.txt without using an array of City references. It then waits for the Enter key to be pressed before returning control to the main menu.

Below are sample runs for this option when there are 0, 1, or 3 columns of monthly rainfall averages in rainfall.txt :

Option 2: Display Rainfall Info for a particular city

If there is no rainfall information in rainfall.txt an error message There is no rainfall information. is displayed; otherwise the option prompts for and reads a city name and its country name. It then searches, without case-sensitivity, for this city name and country name pair in rainfall.txt. If the name pair is not found an appropriate error message is displayed, otherwise; the city rainfall information is displayed, together with the annual rainfall of this city in millimiters. In both cases, the option waits for the Enter key to be pressed before returning control to the main menu. The option must be implemented by reading directly from rainfall.txt without using an array of City references.

Below are sample runs for this option when there is 0 and 3 of monthly rainfall averages in rainfall.txt and when an invalid city and country pair is entered:

Option 3: Display total rainfall for each city.

If there is no rainfall information in rainfall.txt an error message There is no rainfall information. is displayed; otherwise the option displays the monthly rainfall averages and the total rainfall of all cities by reading directly from rainfall.txt without using an array of City references. Control is then returned to the main menu after pressing the Enter key.

Below are sample runs for this option when there are 0, 1, or 3 columns of monthly rainfall averages in rainfall.txt :

Option 4. Modify a particular rainfall average for a particular city and country pair

If there is no rainfall information in rainfall.txt an error message There is no rainfall information. is displayed; otherwise the option prompts for and reads a city name, a country name, and the month number, and the average rainfall. If the month number is invalid (less than 1 or greater than the current number of months in the text-file), or the new average rainfall is negative or greater than 10000 millimeters, an appropriate error message is displayed; otherwise it searches, without case-sensitivity, for this city and country pair in rainfall.txt. If the pair is not found an appropriate error message is displayed, otherwise; the contents of the text-file are copied into an array of City references (Count the number of lines in the text-file, and use this number as the size of the City array. Count the number of rainfall averages in any line and use it as the size of each averageMonthlyRainfall array). The city average rainfall for the required month is updated in the City array, and the rainfall.txt is then updated and the modified city averages are displayed:

Below are sample runs of this option if there are 0 and 3 columns in rainfall.txt and if invalid month and city/country pair are entered:

Option 5: Add monthly rainfall average for the current next month for all cities

It prompts the user to enter rainfall averages for month n + 1, where n is the current number of months for each city. If n is 12 an error message is displayed, otherwise the contents of the rainfall.txt are copied into an array of City references (Count the number of lines in the text-file, and use this number as the size of the City array. Count the number of rainfall averages n in a line and use an array of size n + 1 for each averageMonthlyRainfall array). The program then prompts for and reads the new rainfall average of each city. The file rainfall.txt is then updated.

Below are sample runs if this option if there are 0, 1, or 2 columns of average rainfalls in rainfall.txt and if an invalid monthly average rainfall value is entered:

Option 6: Add New City

It prompts for and read the city name and the country name of the city to be added. It then searches the file rainfall.txt for the name pair. If the pair exists, an error message is displayed and control is returned to the main menu; otherwise the option prompts for and reads n monthly rainfall averages, where n is the current number of monthly rainfall averages for each city in rainfall.txt. The program finally appends the city name, the country name, and the monthly rainfall averages to rainfall.txt. If n is zero, the city name and country name are appended without rainfall information.

Note: Append the new city information to the end of the text-file by an output statement that first generates a new line; but that does not generate an extra new line at the end of the text-file.

Below are sample runs if this option:

Option 7: Delete City

To implement option 7, search rainfall.txt for the city name and country name pair of the city to be deleted, keeping track of its line number in the file. If the pair does not exist, display an error; otherwise read the rainfall informations from the text-file into an array of City references, then overwrite the text-file with the contents of the array, but skipping the information of the city you want to delete. Make sure you write the last line without generating an extra blank line at the end of the text-file.

Below are sample runs of this option:

Note:

Your project must not use parallel arrays. IT MUST USE an array of City references.

Your project must not use 2D-arrays.

Your project must not use static variables.

Your code must not use fixed arrays.

The class City must not contain code that is to be in the ClassDriver class.

The class City must not have reference leaks.

Your project must not update the textfile by using a temporary file.

You may use helper private static methods in your code.

Use the clause throws FileNotFoundException or throws IOException for each method that performs File I/O.

Hints:

When you update the rainfall.txt, make sure you do not insert a blank line at the end of the file, because this will generate reading errors. Write the last file line without generating

Whenever you use nextLine( ) that follows next( ), nextInt( ) or nextDouble( ) that read at the end of a line, make sure you use a dummy nextLine( ) to remove from the input buffer.

To return a formatted string from a method use:

String.format(FormatString, ExpressionList)

Example:

return String.format(ID = %d, Balance = %,.2f Saudi Riyals, id, balance);

The following is a table of some cities and their average monthly rainfall in millimeters. You may use some of this data:

City Country Pair

Jan

Feb

March

April

May

Jun

July

Aug

Sept

Oct

Nov

Dec

Arusha Tanzania

50.0

80.0

170.0

360.0

210.0

30.0

10.0

10.0

20.0

30.0

110.0

100.0

Athens Greece

60.0

45.0

40.0

30.0

23.0

10.0

8.0

8.0

15.0

55.0

60.0

90.0

Bali Indonesia

90.0

90.0

90.0

70.0

70.0

50.0

40.0

40.0

50.0

60.0

70.0

90.0

DarEsSalaam Tanzania

76.3

54.9

138.1

254.2

197.8

42.9

26.6

24.1

22.8

69.3

125.9

117.8

Dubai UAE

18.8

25.0

22.1

7.2

0.4

0.0

0.0

0.0

0.0

1.1

2.7

16.2

Dublin NorthernIreland

70.0

60.0

70.0

50.0

50.0

70.0

50.0

80.0

60.0

80.0

60.0

80.0

London UK

55.2

40.9

41.6

43.7

49.4

45.1

44.5

49.5

49.1

68.5

59.0

55.2

Manila Philippines

20.0

10.0

10.0

30.0

120.0

260.0

400.0

360.0

340.0

190.0

130.0

60.0

Marrakesh Morocco

32.0

38.0

38.0

39.0

25.0

5.0

2.0

3.0

6.0

24.0

42.0

15.0

Miami USA

53.1

59.2

76.2

81.3

126.5

210.1

110.5

161.8

200.2

113.5

69.6

52.1

Moscow Russia

52.0

41.0

35.0

37.0

49.0

80.0

85.0

82.0

68.0

71.0

55.0

52.0

Moscow USA

80.0

59.9

68.0

64.0

64.0

48.0

24.9

24.9

31.0

55.1

92.0

76.0

Mumbai India

0.6

1.3

0.2

0.7

12.5

523.1

799.7

529.7

312.3

55.8

16.8

5.3

NewYork USA

80.0

60.0

85.0

90.0

115.0

95.0

100.0

80.0

105.0

80.0

65.0

80.0

Paris France

18.0

22.0

24.0

25.0

26.0

24.0

23.0

22.0

16.0

25.0

23.0

26.0

Riyadh SaudiArabia

10.0

10.0

30.0

30.0

10.0

0.0

0.0

0.0

0.0

0.0

0.0

10.0

Sydney Australia

96.0

136.6

109.4

137.0

117.6

117.8

80.8

91.8

69.2

82.2

104.8

79.4

Sydney Canada

19.0

13.0

15.0

13.0

7.0

5.5

4.5

7.0

8.0

16.0

21.0

24.0

Tokyo Japan

40.0

53.0

80.0

120.0

130.0

170.0

125.0

140.0

170.0

155.0

70.0

40.0

Waterloo Canada

28.7

29.7

36.8

68.0

81.8

82.4

98.6

83.9

87.8

66.1

75.0

38.0

Waterloo Belgium

65.0

55.0

70.0

58.0

67.0

79.0

75.0

62.0

58.0

70.0

80.0

78.0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!