Recent Question/Assignment

programming assignment
ITECH1000 Programming 1
Assignment 2 – Development of a Simple Program Involving Multiple Classes
Due Date: see Course Description for full details
Please see the Course Description for further information related to extensions for assignments and Special Consideration.
Project Specification
Please read through the entire specification PRIOR to beginning work. The assignment should be completed in the stages mentioned below. The objectives of this assignment are for you to:
Write your own classes from a specification
Instantiate objects of these classes
Perform required tasks by creating and manipulating objects in the code of one class through the public interfaces of the classes of the objects being manipulated.
Resources Required
The following files/links are available on Moodle:
An electronic copy of this assignment specification sheet
A sample program involving multiple classes
Starting code for two of the classes to be developed
Note: If you use any resources apart from the course material to complete your assignment you MUST provide an in-text citation within your documentation and/or code, as well as providing a list of references in APA formatting. This includes the use of any websites, online forums, books, or text books. If you are unsure of how to do this please ask for help.
Design Constraints
Your program should conform to the following constraints. It should:
Use a while-loop when the number of iterations is not known before loop execution.
Use a for-loop when the number of iterations is known before loop execution.
Indent your code correctly and use a consistent curly bracket placement scheme.
Use white space to make your code as readable as possible.
If a loop is a sentinel controlled loop use a priming and a repetitive read as described in the lecture slides. The loop should process then read NOT read then process!
Validation of input should be limited to ensuring that the user has entered values that are within correct bounds. You do not have to check that they have entered the correct data type.
Comment your code appropriately.
Part A – Code Comprehension
Using the uncommented sample code has been provided in Moodle answer the following questions:
Draw a UML diagram of the Shoe class using the code that has been provided. Complete this using the examples that have been provide in the lecture slides.
Draw a UML diagram of the Store class using the code that has been provided. Complete this using the examples that have been provide in the lecture slides.
Explain in what the purpose of the call to hasNext() in readFromFile.
Briefly explain the code in sortInventory. What is the inventory being sorted by? What is the sort algorithm that is being used. Explain in words how it works. Why do need to use two for loops?
Briefly explain what other sorting algorithms could have been used.
What method of searching for shoes has been used in the program? What other algorithm could have been used.
Explain the use of the return value (result) of the compareTo method in the Shoe class.
Briefly explain why the line to open the input file:
Scanner inputFile = new Scanner(new File(-C:\orders.txt-));
has two \ in the filename specification. Where is the file located? What happens if only one is used? Is there another option that would not require these?
Examine the code in the saveToFile method. How could this be improved for readability of the output file? What is the purpose of the ‘ ’?
Briefly explain why a throws has included in the code to read from the file. Remove this from the code and take note of the error. Re-add the line and investigate what happens if the “orders.txt” file is not present.
Part B – Development of a basic Class
Your first coding task is to implement and test a class that represents a City. For this assignment a City has a name, population (in millions eg 1.5F is 1.5 million), an elevation above sea level and an indication of whether the city is a capital.
To complete this task you are required to:
Use the UML diagram to implement the class City in a file called City.java. You are to ensure that you adhere to the naming used below as this class will be used by other classes that you will develop.
Ensure that your class includes the following validation within the mutator (set) methods:
The name of the City cannot be null nor an empty String (ie “”). If an attempt is made to set the name to an invalid value the method should return false.
The population of the City must be greater than or equal to 1. If an attempt is made to set the value of the population to an invalid value the method should return false.
Select two cities contained in the sample data within the Appendix – ensure that these two cities are in the one country. Write a class called TestClasses and within the main method write code that will :
Instantiate an instance of the Class city one of the cities you selected using the City() constructor
Sets all of the instance variables to the appropriate values using the mutator methods (set)
Increase the population of the city by 10%.
Instantiates an instance of the Class city for a different city contained in the table below using the City(initName: String, initPopulation: float, initElevation: int, initIsCapital: boolean) constructor
Decrease the population of the city by 5%
Display both of the cities that you have created using the toString() method
Display both of the cities that you have created using accessor (get) methods


City
- String name;
- population: float //millions
- elevation: int
- isCapital: boolean
+ City(initName: String, initPopulation: float, initElevation: int, initIsCapital: boolean)
+ City()
+ getName(): String
+ getPopulation(): float
+ getElevation(): int
+ getIsCapital(): boolean
+ setName(newName: String): boolean
+ setPopulation(newPopulation: int): boolean
+ equalsIgnoreCase(aCity: City) int
+ toString():String
Figure 1: UML Diagram for the class City
Part C – Development of the Country and World classes
Using the UML diagrams provided and the information below you are required to implement and test classes that represent Country and World objects. For this assignment a Country contains multiple cities up to a specified capacity. The World contains multiple countries up to a specified capacity.
Country.java

Country
- name: String
- cities: City[]
- numCities: int
+ Country(initName: String, capacity: int)
+ getName():String
+ getCity(indexToGet: int): City
+ getNumCities():int
+ setName(newName: String): boolean
+ addCity(newCity: City): boolean
+ removeCity(cityToRemove: String): boolean
- nameToIndex(cityToFind: String): int
+ toString():String
Figure 2: UML Diagram for the class Country
NOTE: nameToIndex method accepts the name of a city and returns its index in the cities array.

You have been provided with starting point in Country.java . You are to ensure that you adhere to the naming used below as this class will be used by the World class that you will also develop.
Ensure that your class includes the following validation within the mutator methods:
The name of the Country cannot be null nor an empty String (ie “”). If an attempt is made to set the name to an invalid value the method should return false.
The population of the city must be greater than or equal to 1.
The number of cities added to the Country class cannot be greater than the capacity.
As a City is added to the Country the instance variable numCities should be increased by 1.
As a City is deleted (removed) from the Country the instance variable numCities should be decreased by 1.
Update the class TestClasses adding in code to:
Create an instance of the Country class using one of the countries contained in the table in the Appendix
Add the two Cities created to the Country class using the addCity(City) method
Display both of the cities that you have created using the toString() method – these should be accessed via the instance of the Country class that you have created
Display both of the cities that you have created using accessor methods – each city should be accessed via the instance of the Country class you have created
World.java

World
- countries: Country[]
- numCountries: int
+ World(capacity int)
+ Country getCountry(indexToGet: int)
+ getNumCountries(): int
+ addCountry(newCountry: Country): boolean
+ removeCountry(countryToRemove: String): boolean
+ nameToIndex(countryToFind: String): int
+ toString():String
Figure 3: UML Diagram of the World
NOTE: nameToIndex method accepts the name of a country and returns its index in the countries array.

You have been provided with starting point in World.java . You are to ensure that you adhere to the naming used below as this class will be used by other classes that you will develop.
Ensure that your class includes the following validation within the mutator methods:
The number of countries added to the World cannot exceed the capacity set when the World is constructed
Update the class TestClasses so that it now:
Creates an instance of the Country class using one of the countries contained in the table in the Appendix
Adds the Country to the World using the addCountry(Country) method
Display the World using the toString() method
Display details of the Country added
Display both of the cities that you have created using the toString() method
Display both of the cities that you have created using accessor methods

Part D – Using the Classes
Create a new class called WorldDriver. This class is to be used to implement a basic menu system. The menu should include the following options:
Populate the World
Display Data
Sort World data
Run queries
Import data (ITECH5000 students only)
Export data (ITECH5000 students only
Exit

Populate the World – use the data provided and at least 3 Countries from the table below and an addition 2 cities of your own selection
Display Data – display the data of all countries and their cities contained within the World.
Sort data should utilise some form of sorting algorithm to sort the Countries of the World into alphabetical order.
Run queries should:
Display the Details of the cities with a population of 1 million or more
Display the Names of the cities with more than one word in their name
Delete all Australian cities with an elevation below 100m-
Allow the user to search for a city and/or country
For students enrolled in ITECH5000 ONLY:
Import data – this menu should read in data from the filed called c:sampleData.txt. You are required to add the additional data described above to this file.
Export data – save the data to a new file c:updatedData.txt
Assignment Submission
The following criteria will be used when marking your assignment:
successful completion of the required tasks
quality of code that adheres to the programming standards for the course; including:
comments and documentation
code layout
meaningful variable names

You are required to provide documentation, contained in an appropriate file, which includes:
a front page - indicating your name, a statement of what has been completed and acknowledgement of the names of all people (including other students and people outside of the university) who have assisted you and details on what parts of the assignment that they have assisted you with
a table of contents and page numbers
answers to the questions from Part A
list of references used (APA style); please specify if none have been used.
An appendix containing copies of the code your code

Using the link provided in Moodle, please upload the following:
All of the classes created in a single zip file – SurnameStudentIdAssign2.zip
A copy of your report - surnameStudentIDAssign1.docx
Marking Guide

PART A – Code Comprehension
/20
PART B – City class + Testing
/20
PART C – Country and World Classes + Testing
/35
PART D – Implementation of Menu and Queries
/35
Quality of code created
/15
Presentation of documentation / report
/5

Total /20 /130
Appendix
Sample Data for Countries and Cities


Country City Name Population (millions) Elevation (m) Country Capital?
Australia -Alice Springs- .026F 608 false
Australia -Canberra-
.37F 580 true
Australia -Melbourne- 4.1F 31 false
Australia -Adelaide- 1.2F 40 false
Australia -Sydney- 4.6F 9 false
Malaysia -Kuala Lumpur- 1.6F 22 true
Malaysia -Johor Bahru- 1.5F 37 false
South Africa -Johannesburg- 1.0F
1753 false
South Africa -Cape Town- .83F 0 false
Output of the WorldDriver Class

Sample output of the given WorldDriver class is shown below. You can use this to compare your output with the given output (The indentation and spacing might be different)


***The World***

***Country: Australia

City: Alice Springs
Pop.: 0.026 million
Elevation: 608m
Is not a Capital

City: Canberra
Pop.: 0.37 million
Elevation: 580m
Is a Capital

City: Melbourne

Pop.: 4.1 million
Elevation: 31m
Is not a Capital

City: Adelaide
Pop.: 1.2 million
Elevation: 40m
Is not a Capital

City: Sydney
Pop.: 4.6 million
Elevation: 9m
Is not a Capital

***Country: Malaysia

City: Kuala Lumpur
Pop.: 1.6 million
Elevation: 22m
Is a Capital

City: Johor Bahru
Pop.: 1.5 million
Elevation: 37m
Is not a Capital

***Country: South Africa

City: Johannesberg
Pop.: 1.0 million
Elevation: 1753m
Is not a Capital

City: Cape Town
Pop.: 0.83 million
Elevation: 0m
Is not a Capital





Details of the cities with a population of 1 million or more are:

City: Melbourne
Pop.: 4.1 million
Elevation: 31m
Is not a Capital

City: Adelaide
Pop.: 1.2 million
Elevation: 40m
Is not a Capital

City: Sydney
Pop.: 4.6 million
Elevation: 9m
Is not a Capital

City: Kuala Lumpur
Pop.: 1.6 million
Elevation: 22m
Is a Capital

City: Johor Bahru
Pop.: 1.5 million
Elevation: 37m
Is not a Capital

City: Johannesberg
Pop.: 1.0 million
Elevation: 1753m
Is not a Capital

Names of the cities with more than one word in their name are:

Alice Springs
Kuala Lumpur
Johor Bahru
Cape Town

Delete all Australian cities with an elevation below 100m
3 cities deleted.

***The World***

***Country: Australia

City: Alice Springs
Pop.: 0.026 million
Elevation: 608m
Is not a Capital

City: Canberra
Pop.: 0.37 million
Elevation: 580m
Is a Capital

***Country: Malaysia

City: Kuala Lumpur
Pop.: 1.6 million
Elevation: 22m
Is a Capital


City: Johor Bahru
Pop.: 1.5 million
Elevation: 37m
Is not a Capital

***Country: South Africa

City: Johannesberg
Pop.: 1.0 million
Elevation: 1753m
Is not a Capital

City: Cape Town
Pop.: 0.83 million
Elevation: 0m
Is not a Capital

Looking for answers ?