, , ,

[SOLVED] Comp 1406 assignments 1 to 6 solution

$25

File Name: Comp_1406_assignments_1_to_6_solution.zip
File Size: 348.54 KB

5/5 - (1 vote)

Problem 1 (Electronics Store – Version 1)
Your goal for this problem is to create a simple implementation of an electronics store. You must
implement the following five classes with the defined functionality:
Desktop Class: Must store the CPU speed (in Ghz as a double), amount of RAM (in GB as an
integer), amount of storage (in GB as an integer), and whether or not the storage is an SSD or
HDD (a Boolean, with true meaning SSD and false meaning HDD). The class must have a fourargument constructor that accepts an input parameter for each of these values in the same
order they are listed above (CPU, RAM, storage, SSD). The class must have a toString method
that returns a string indicating the object is a desktop PC along with the object’s properties. Two
examples of what the toString method could return are included below:
Desktop PC with 3.5ghz CPU, 8GB RAM, 500GB HDD drive.
Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
Laptop Class: Must store the CPU speed (in Ghz as a double), amount of RAM (in GB as an
integer), amount of storage (in GB as an integer), whether or not the storage is an SSD or HDD
(a Boolean, with true meaning SSD and false meaning HDD), and the screen size (in inches as
an integer). The class must have a five-argument constructor that accepts an input parameter
for each of these values in the same order they are listed above (CPU, RAM, storage, SSD,
screen size). The class must have a toString method that returns a string indicating the object is
a laptop PC along with the object’s properties. Two examples of what the toString method could
return are included below:
15″ Laptop PC with 3.1ghz CPU, 32GB RAM, 500GB SSD drive.
13″ Laptop PC with 2.5ghz CPU, 8GB RAM, 250GB HDD drive.
Fridge Class: Must store the size of the fridge (in cubic feet, as a double), whether it includes a
freezer or not (a Boolean that is true if it has a freezer), and the color (as a String). The class
must have a toString method that returns a string indicating the object is a fridge along with the
object’s properties. Two examples of what the toString method could return are included below:
15.6 cubic foot Fridge with Freezer (Gray)
10.5 cubic foot Fridge (White)
COMP 1406 – W20 – A1 Due Friday, January 31st at 11:59 PM
2
ElectronicStore Class: Must have an instance variable called name to store the name of the
store. Must have a one-argument constructor that accepts a string to specify the store name.
The constructor for this class must also create three instances of each of the previous three
classes (9 items in total, using the constructors defined in those classes) and store them within
the ElectronicStore instance being created. For storage purposes in the ElectronicStore class,
you can use one or more arrays, lists, or hash maps to store the various products (your design
choice). You can hard-code the argument values for the product constructors (i.e., CPU speed,
RAM, color, etc.) or use a random number generator to randomly assign them. This class
should also have a void method called printStock() that will iterate over all of the store’s stock
and print them to the console in a readable format. Additionally, this class should have a
searchStock(String) method. The searchStock method should accept a string argument and
return true if the toString() of any product in the store’s stock contains the given string argument
(otherwise, the method should return false). The searchStock method should also be case
insensitive. That is, searches for “desk” and “hdd” should both return true if the store contains a
“Desktop PC with 3.5ghz CPU, 8GB RAM, 500GB HDD drive”.
ElectronicStoreTester Class: This class should have a single main method, which will first
instantiate a single ElectronicStore and call the printStock() method. The test class should then
repeatedly prompt the user to enter an item to search for. If the user enters the word “quit”, this
process should stop, and the program should end. Otherwise, the searchStock method of the
ElectronicStore instance should be used to search for the given search term and the result
should be printed out. You should use the searchStock method’s return value to determine what
message should be displayed. The output from this tester class should be similar to below (user
input text is highlighted):
The store stock includes:
Desktop PC with 3.5ghz CPU, 8GB RAM, 500GB HDD drive.
Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
Desktop PC with 4.3ghz CPU, 32GB RAM, 500GB SSD drive.
15″ Laptop PC with 3.1ghz CPU, 32GB RAM, 500GB SSD drive.
13″ Laptop PC with 2.5ghz CPU, 8GB RAM, 250GB HDD drive.
15″ Laptop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
16.5 cu. ft. Fridge with Freezer (Black)
12.0 cu. ft. Fridge (White)
23.0 cu. ft. Fridge with Freezer (Stainless Steel)
Enter a term to search for: desk
A matching item is contained in the store’s stock.
Enter a term to search for: LaPtOP
A matching item is contained in the store’s stock.
Enter a term to search for: Toast
No items in the store’s stock match that term.
Enter a term to search for: television
COMP 1406 – W20 – A1 Due Friday, January 31st at 11:59 PM
3
No items in the store’s stock match that term.
Enter a term to search for: GB
A matching item is contained in the store’s stock.
Enter a term to search for: quit

For this assignment, you will apply the OOP principles of encapsulation and inheritance to the
redesign of the electronic store classes from Assignment #1. A list of the classes that must be
contained in your program are given below, along with a summary of their state and behaviour.
It is up to you to decide on and implement the appropriate class hierarchy. You will likely need
to add more classes to define the best class hierarchy – look for shared state/behaviour
between different classes and define a parent class if you need to. Note that you will not need
to define/implement all these methods/attributes in each class if you make proper use of
inheritance. Additionally, you must use proper encapsulation in designing these classes. All
your instance variables should be private, unless you can justify them being protected/public.
You should be able to complete the assignment without creating any public/protected variables.
If you include a protected/public variable, also include a comment with your justification. You
can add additional methods (e.g., getter/setter methods) to the classes as you see fit. A large
portion of your mark will come from the quality of your class design and proper use of
encapsulation/inheritance.
Two test classes are included on the assignment page. Your code should work with these test
case classes. When you run the test case classes, the output should be very similar to the
output included at the end of this document. The only difference may be in the formatting of the
objects, though your toString() methods should closely mirror the formatting included here. You
should test your code with your own additional test cases as well. The supplied test classes only
provide a minimal amount of testing.
Desktop Class:
State:
1. double price – represents how much the desktop costs
2. int stockQuantity – represents how many units of this desktop are in stock
3. int soldQuantity – represents how many units of this desktop have been sold
4. double cpuSpeed – the CPU speed in Ghz
5. int ram – the amount of RAM in GB
6. boolean ssd – whether the hard-drive is an SSD (true) or HDD (false)
7. int storage – the size of the hard-drive in GB
8. String profile – a string describing the size of the desktop case
COMP 1406 – W20 – A2 Due Friday, February 14th at 11:59 PM
2
Behaviour:
1. Desktop(double price, int quantity, double cpuSpeed, int ram, boolean ssd, int
storage, String profile) – constructor for the class
2. double sellUnits(int amount) – simulates selling amount units. If there are
enough units in stock to meet the request, the quantity attributes must be
updated appropriately and the total revenue (revenue = units * price) should
be returned. If there are not enough units in stock, no sale should take place
and 0.0 should be returned.
3. String toString() – returns a string representing the desktop. This should
summarize the state of the desktop, including each attribute. See the
example output included at the bottom for examples.
ElectronicStore Class:
State:
1. final int MAX_PRODUCTS = 10 – the maximum number of different product
instances that this store can contain. This value should be set to 10 and
never changed. Your class should refer to this variable so that the maximum
number of products can be changed easily.
2. String name – the name of the electronics store
3. double revenue – the total revenue the store has made through sales. This
should initially be 0 but should be updated as products are sold.
4. Product[ ] products – an array to store product objects that are in the store.
This array should have size equal to MAX_PRODUCTS. In case you are
working on this before we have finished discussing polymorphism in
class, all you need to know is that you can store any class that is below
Product in the class hierarchy within this array. So if you have additional
classes that extend Product (or in general, are further down the hierarchy),
you can store them in this array without any problems.
Behaviour:
1. ElectronicStore(String name) – constructor for the class.
2. String getName() – returns the name of the store
3. boolean addProduct(Product p) – if there is space remaining in the products
array, this method should add p to the products array at the next available
array slot and return true. If there is no space remaining in the products array,
this method should just return false.
4. void sellProducts() – this method should print out the store’s products (see
example output below for an idea of how it should look), read an integer from
the user representing the product to sell (i.e., what index in the products
array), and read an integer from the user representing how many units of the
product to sell. You may assume the user will only enter integer numbers but
must verify that the values are valid (e.g., a valid item index, an amount
greater than 0). If the values supplied by the user are valid, the specified
COMP 1406 – W20 – A2 Due Friday, February 14th at 11:59 PM
3
number of units of the specified product should be sold, if possible. All
appropriate variables must be updated in all instances (e.g., revenue, number
of units in stock, etc.). If any of the input is invalid, no sales should take place.
5. void sellProducts(int item, int amount) – should sell amount units of the
product stored at index item in the products array, if possible. All appropriate
variables must be updated in all instances (e.g., revenue, number of units in
stock, etc.). If any of the input is invalid, no sales should take place.
6. double getRevenue() – returns the total revenue the store has made through
sales.
7. void printStock() – should print out the products of the store. See the example
output at the bottom of the document for examples.
Fridge Class:
State:
1. double price – represents how much the fridge costs
2. int stockQuantity – represents how many units of this fridge there are in stock
3. int soldQuantity – represents how many units of this fridge have been sold
4. int wattage – the wattage rating of the fridge
5. String color – the color of the fridge
6. String brand – the brand name of the fridge
7. double cubicFeet – The volume of the fridge in cubic feet
8. boolean hasFreezer – Whether the fridge has a freezer or not
Behaviour:
1. Fridge(double price, int quantity, int wattage, String color, String brand,
double cubicFeet, boolean freezer) – constructor for the class
2. double sellUnits(int amount) – simulates selling amount units. If there are
enough units in stock to meet the request, the quantity attributes must be
updated appropriately and the total revenue (revenue = units * price) should
be returned. If there are not enough units in stock, no sale should take place
and 0.0 should be returned.
3. String toString() – returns a string representing the fridge. This should
summarize the state of the fridge, including each attribute. See the example
output included at the bottom for examples.
Laptop Class:
State:
1. double price – represents how much the laptop costs
2. int stockQuantity – represents how many units of this laptop there are in stock
3. int soldQuantity – represents how many units of this laptop have been sold
4. double cpuSpeed – the CPU speed, in Ghz
5. int ram – the amount of RAM in GB
6. boolean ssd – whether the hard-drive is an SSD (true) or HDD (false)
COMP 1406 – W20 – A2 Due Friday, February 14th at 11:59 PM
4
7. int storage – the size of the hard-drive in GB
8. double screenSize – the size of the screen in inches
Behaviour:
1. Laptop(double price, int quantity, double cpuSpeed, int ram, boolean ssd, int
storage, double screenSize) – constructor for the class
2. double sellUnits(int amount) – simulates selling amount units. If there are
enough units in stock to meet the request, the quantity attributes must be
updated appropriately and the total revenue (revenue = units * price) should
be returned. If there are not enough units in stock, no sale should take place
and 0.0 should be returned.
3. String toString() – returns a string representing the laptop. This should
summarize the state of the laptop, including each attribute. See the example
output included at the bottom for examples.
Product Class:
State:
1. double price – represents how much the product costs
2. int stockQuantity – represents how many units of this product are in stock
3. int soldQuantity – represents how many units of this product have been sold
Behaviour:
1. Product(double price, int quantity) – constructor for the class
2. double sellUnits(int amount) – simulates selling amount units. If there are
enough units in stock to meet the request, the quantity attributes must be
updated appropriately and the total revenue for selling (revenue = units *
price) should be returned. If there are not enough units in stock, no sale
should take place and 0.0 should be returned.
ToasterOven Class:
State:
1. double price – represents how much the toaster oven costs
2. int stockQuantity – represents how many units of this toaster are in stock
3. int soldQuantity – represents how many units of this toaster have been sold
4. int wattage – the wattage rating of the toaster oven
5. String color – the color of the toaster oven
6. String brand – the brand name of the toaster oven
7. int width – the width of the toaster oven
8. boolean convection – whether the toaster has convection heating or not
Behaviour:
1. ToasterOven(double price, int quantity, int wattage, String color, String brand,
int width, boolean convection) – constructor for the class
2. double sellUnits(int amount) – simulates selling amount units. If there are
enough units in stock to meet the request, the quantity attributes must be
COMP 1406 – W20 – A2 Due Friday, February 14th at 11:59 PM
5
updated appropriately and the total revenue for selling (revenue = units *
price) should be returned. If there are not enough units in stock, no sale
should take place and 0.0 should be returned.
3. String toString() – returns a string representing the toaster oven. This should
summarize the state of the toaster oven, including each attribute. See the
example output included at the bottom for examples.
ElectronicStoreStockTester Class Output
If you run the ElectronicStoreStockTester class, you should get the following output (bold font
added to help find specific parts of the output, your code does not need to produce bold output):
Watts Up Electronics’s Stock Is:
0. Compact Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB HDD drive.
(100.0 dollars each, 10 in stock, 0 sold)
1. Server Desktop PC with 4.0ghz CPU, 32GB RAM, 500GB SSD drive.
(200.0 dollars each, 10 in stock, 0 sold)
2. 15.0 inch Laptop PC with 2.5ghz CPU, 16GB RAM, 250GB SSD drive.
(150.0 dollars each, 10 in stock, 0 sold)
3. 16.0 inch Laptop PC with 3.5ghz CPU, 24GB RAM, 500GB SSD drive.
(250.0 dollars each, 10 in stock, 0 sold)
4. 15.5 cu. ft. Sub Zero Fridge (White, 250 watts) (500.0 dollars
each, 10 in stock, 0 sold)
5. 23.0 cu. ft. Sub Zero Fridge with Freezer (Stainless Steel, 125
watts) (750.0 dollars each, 10 in stock, 0 sold)
6. 8 inch Danby Toaster (Black, 50 watts) (25.0 dollars each, 10 in
stock, 0 sold)
7. 12 inch Toasty Toaster with convection (Silver, 50 watts) (75.0
dollars each, 10 in stock, 0 sold)
Buy-nary Computing’s Stock Is:
0. Compact Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
(150.0 dollars each, 5 in stock, 0 sold)
1. Server Desktop PC with 3.5ghz CPU, 32GB RAM, 500GB SSD drive.
(250.0 dollars each, 5 in stock, 0 sold)
2. 15.0 inch Laptop PC with 2.5ghz CPU, 16GB RAM, 250GB HDD drive.
(100.0 dollars each, 15 in stock, 0 sold)
3. 16.0 inch Laptop PC with 3.5ghz CPU, 24GB RAM, 500GB SSD drive.
(175.0 dollars each, 15 in stock, 0 sold)
4. 15.5 cu. ft. Sub Zero Fridge (Black, 250 watts) (350.0 dollars
each, 10 in stock, 0 sold)
COMP 1406 – W20 – A2 Due Friday, February 14th at 11:59 PM
6
5. 23.0 cu. ft. Sub Zero Fridge with Freezer (White, 125 watts) (600.0
dollars each, 10 in stock, 0 sold)
6. 6 inch Danby Toaster (Graphite, 50 watts) (25.0 dollars each, 10 in
stock, 0 sold)
7. 10 inch Toasty Toaster with convection (Red, 50 watts) (75.0
dollars each, 10 in stock, 0 sold)
Ohm-y Goodness Electronics’s Stock Is:
0. Low-Profile Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
(175.0 dollars each, 10 in stock, 0 sold)
1. Standard Desktop PC with 3.5ghz CPU, 32GB RAM, 1000GB HDD drive.
(150.0 dollars each, 15 in stock, 0 sold)
2. 16.0 inch Laptop PC with 3.5ghz CPU, 16GB RAM, 500GB SSD drive.
(350.0 dollars each, 5 in stock, 0 sold)
3. 13.0 inch Laptop PC with 2.5ghz CPU, 8GB RAM, 125GB SSD drive.
(500.0 dollars each, 5 in stock, 0 sold)
4. 12.0 cu. ft. Sub Zero Fridge (Black, 250 watts) (250.0 dollars
each, 5 in stock, 0 sold)
5. 15.0 cu. ft. Sub Zero Fridge (White, 125 watts) (275.0 dollars
each, 5 in stock, 0 sold)
6. 8 inch Danby Toaster (Graphite, 50 watts) (30.0 dollars each, 10 in
stock, 0 sold)
7. 12 inch Toasty Toaster with convection (Red, 50 watts) (80.0
dollars each, 10 in stock, 0 sold)
8. Low-Profile Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
(175.0 dollars each, 10 in stock, 0 sold)
9. Standard Desktop PC with 3.5ghz CPU, 32GB RAM, 1000GB HDD drive.
(150.0 dollars each, 15 in stock, 0 sold)
ElectronicStoreSellingTester Class Output
If you run the ElectronicStoreSellingTester class, you should get the following output (bold font
added to help find specific parts of the output, your code does not need to produce bold output):
Watts Up Electronics’s Starting Stock Is:
0. Compact Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB HDD drive.
(100.0 dollars each, 10 in stock, 0 sold)
1. Server Desktop PC with 4.0ghz CPU, 32GB RAM, 500GB SSD drive.
(200.0 dollars each, 10 in stock, 0 sold)
2. 15.0 inch Laptop PC with 2.5ghz CPU, 16GB RAM, 250GB SSD drive.
(150.0 dollars each, 10 in stock, 0 sold)
COMP 1406 – W20 – A2 Due Friday, February 14th at 11:59 PM
7
3. 16.0 inch Laptop PC with 3.5ghz CPU, 24GB RAM, 500GB SSD drive.
(250.0 dollars each, 10 in stock, 0 sold)
4. 15.5 cu. ft. Sub Zero Fridge (White, 250 watts) (500.0 dollars
each, 10 in stock, 0 sold)
5. 23.0 cu. ft. Sub Zero Fridge with Freezer (Stainless Steel, 125
watts) (750.0 dollars each, 10 in stock, 0 sold)
6. 8 inch Danby Toaster (Black, 50 watts) (25.0 dollars each, 10 in
stock, 0 sold)
7. 12 inch Toasty Toaster with convection (Silver, 50 watts) (75.0
dollars each, 10 in stock, 0 sold)
Buy-nary Computing’s Starting Stock Is:
0. Compact Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
(150.0 dollars each, 5 in stock, 0 sold)
1. Server Desktop PC with 3.5ghz CPU, 32GB RAM, 500GB SSD drive.
(250.0 dollars each, 5 in stock, 0 sold)
2. 15.0 inch Laptop PC with 2.5ghz CPU, 16GB RAM, 250GB HDD drive.
(100.0 dollars each, 15 in stock, 0 sold)
3. 16.0 inch Laptop PC with 3.5ghz CPU, 24GB RAM, 500GB SSD drive.
(175.0 dollars each, 15 in stock, 0 sold)
4. 15.5 cu. ft. Sub Zero Fridge (Black, 250 watts) (350.0 dollars
each, 10 in stock, 0 sold)
5. 23.0 cu. ft. Sub Zero Fridge with Freezer (White, 125 watts) (600.0
dollars each, 10 in stock, 0 sold)
6. 6 inch Danby Toaster (Graphite, 50 watts) (25.0 dollars each, 10 in
stock, 0 sold)
7. 10 inch Toasty Toaster with convection (Red, 50 watts) (75.0
dollars each, 10 in stock, 0 sold)
Ohm-y Goodness Electronics’s Starting Stock Is:
0. Low-Profile Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
(175.0 dollars each, 10 in stock, 0 sold)
1. Standard Desktop PC with 3.5ghz CPU, 32GB RAM, 1000GB HDD drive.
(150.0 dollars each, 15 in stock, 0 sold)
2. 16.0 inch Laptop PC with 3.5ghz CPU, 16GB RAM, 500GB SSD drive.
(350.0 dollars each, 5 in stock, 0 sold)
3. 13.0 inch Laptop PC with 2.5ghz CPU, 8GB RAM, 125GB SSD drive.
(500.0 dollars each, 5 in stock, 0 sold)
4. 12.0 cu. ft. Sub Zero Fridge (Black, 250 watts) (250.0 dollars
each, 5 in stock, 0 sold)
5. 15.0 cu. ft. Sub Zero Fridge (White, 125 watts) (275.0 dollars
each, 5 in stock, 0 sold)
COMP 1406 – W20 – A2 Due Friday, February 14th at 11:59 PM
8
6. 8 inch Danby Toaster (Graphite, 50 watts) (30.0 dollars each, 10 in
stock, 0 sold)
7. 12 inch Toasty Toaster with convection (Red, 50 watts) (80.0
dollars each, 10 in stock, 0 sold)
8. Low-Profile Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
(175.0 dollars each, 10 in stock, 0 sold)
9. Standard Desktop PC with 3.5ghz CPU, 32GB RAM, 1000GB HDD drive.
(150.0 dollars each, 15 in stock, 0 sold)
Watts Up Electronics’s Ending Stock Is:
0. Compact Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB HDD drive.
(100.0 dollars each, 0 in stock, 10 sold)
1. Server Desktop PC with 4.0ghz CPU, 32GB RAM, 500GB SSD drive.
(200.0 dollars each, 10 in stock, 0 sold)
2. 15.0 inch Laptop PC with 2.5ghz CPU, 16GB RAM, 250GB SSD drive.
(150.0 dollars each, 10 in stock, 0 sold)
3. 16.0 inch Laptop PC with 3.5ghz CPU, 24GB RAM, 500GB SSD drive.
(250.0 dollars each, 0 in stock, 10 sold)
4. 15.5 cu. ft. Sub Zero Fridge (White, 250 watts) (500.0 dollars
each, 10 in stock, 0 sold)
5. 23.0 cu. ft. Sub Zero Fridge with Freezer (Stainless Steel, 125
watts) (750.0 dollars each, 10 in stock, 0 sold)
6. 8 inch Danby Toaster (Black, 50 watts) (25.0 dollars each, 10 in
stock, 0 sold)
7. 12 inch Toasty Toaster with convection (Silver, 50 watts) (75.0
dollars each, 0 in stock, 10 sold)
Buy-nary Computing’s Ending Stock Is:
0. Compact Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
(150.0 dollars each, 0 in stock, 5 sold)
1. Server Desktop PC with 3.5ghz CPU, 32GB RAM, 500GB SSD drive.
(250.0 dollars each, 4 in stock, 1 sold)
2. 15.0 inch Laptop PC with 2.5ghz CPU, 16GB RAM, 250GB HDD drive.
(100.0 dollars each, 0 in stock, 15 sold)
3. 16.0 inch Laptop PC with 3.5ghz CPU, 24GB RAM, 500GB SSD drive.
(175.0 dollars each, 15 in stock, 0 sold)
4. 15.5 cu. ft. Sub Zero Fridge (Black, 250 watts) (350.0 dollars
each, 10 in stock, 0 sold)
5. 23.0 cu. ft. Sub Zero Fridge with Freezer (White, 125 watts) (600.0
dollars each, 10 in stock, 0 sold)
6. 6 inch Danby Toaster (Graphite, 50 watts) (25.0 dollars each, 10 in
stock, 0 sold)
COMP 1406 – W20 – A2 Due Friday, February 14th at 11:59 PM
9
7. 10 inch Toasty Toaster with convection (Red, 50 watts) (75.0
dollars each, 10 in stock, 0 sold)
Ohm-y Goodness Electronics’s Ending Stock Is:
0. Low-Profile Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
(175.0 dollars each, 10 in stock, 0 sold)
1. Standard Desktop PC with 3.5ghz CPU, 32GB RAM, 1000GB HDD drive.
(150.0 dollars each, 0 in stock, 15 sold)
2. 16.0 inch Laptop PC with 3.5ghz CPU, 16GB RAM, 500GB SSD drive.
(350.0 dollars each, 0 in stock, 5 sold)
3. 13.0 inch Laptop PC with 2.5ghz CPU, 8GB RAM, 125GB SSD drive.
(500.0 dollars each, 5 in stock, 0 sold)
4. 12.0 cu. ft. Sub Zero Fridge (Black, 250 watts) (250.0 dollars
each, 1 in stock, 4 sold)
5. 15.0 cu. ft. Sub Zero Fridge (White, 125 watts) (275.0 dollars
each, 0 in stock, 5 sold)
6. 8 inch Danby Toaster (Graphite, 50 watts) (30.0 dollars each, 10 in
stock, 0 sold)
7. 12 inch Toasty Toaster with convection (Red, 50 watts) (80.0
dollars each, 10 in stock, 0 sold)
8. Low-Profile Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.
(175.0 dollars each, 10 in stock, 0 sold)
9. Standard Desktop PC with 3.5ghz CPU, 32GB RAM, 1000GB HDD drive.
(150.0 dollars each, 8 in stock, 7 sold)
Watts Up Electronics’s total revenue was: 4250.0
Buy-nary Computing’s total revenue was: 2500.0
Ohm-y Goodness Electronics’s total revenue was: 7425.0

For this assignment, you will build a graphical user interface to attach to the electronic store
model that you have developed over the previous two assignments. You can use your own
model classes from the previous assignment, or you can download the model classes included
within the zip file on the assignment page and incorporate those into your IntelliJ project as a
starting point (these will be posted on Tuesday February 18th). Your assignment must maintain
a separation between the GUI and the underlying electronic store model. You should ensure
you understand the Model/View/Controller paradigm before beginning the assignment.
You must also continue to apply the principles of OOP, such as encapsulation. A recording
showing a demonstration of how the GUI should work is included on the assignment page.
1) The GUI
An example of what the graphical user interface window should include is given in the
picture below. The code for this view should be created in a class called
ElectronicStoreView. You should make the GUI window non-resizable. The ratio of the
window width/height should be approximately 2:1 (e.g., 800 wide, 400 high). It does not
have to look exactly as the picture but should closely match.
COMP 1406 – W20 – A3 Due Friday, March 6
th at 11:59 PM
2
2) When the Application Starts
Create a class called ElectronicStoreApp, which will represent the controller component
of your program. This class should extend from the JavaFX Application class. When the
application starts, your controller should create an instance of ElectronicStore, as well as
an instance of your view defined in part 1. To create the ElectronicStore instance, you
must use the static createStore() method from the ElectronicStore.java file included on
the assignment page and save the returned ElectronicStore instance in a variable within
the ElectronicStoreApp. If you are using your own code as a base, you can copy/paste
the createStore() method from the example. The ElectronicStore instance will represent
the model that the GUI application will interact with.
When the application starts, the window title must include the name of the store. The
‘Add to Cart’, ‘Remove from Cart’, and ‘Complete Sale’ buttons should initially be
disabled. The ‘# Sales’, ‘Revenue’, and “$ / Sale” TextFields should be initialized to
default values representing the starting state of an electronic store. The store stock
ListView should display all the items currently in stock within the electronic store model.
The most popular items ListView should show 3 products that exist in the store’s stock.
At this point, all products will have an equal popularity since no sales have taken place.
The image below shows an example of what the initial window should look like:
COMP 1406 – W20 – A3 Due Friday, March 6
th at 11:59 PM
3
3) Event Handling
You must add event handling to the ElectronicStoreApp class to meet the requirements
outlined below.
Store Stock:
1) If an item has been selected inside of the Store Stock ListView, the ‘Add to
Cart’ button should be enabled. If no item has been selected, the button
should be disabled.
2) When an item in the Store Stock ListView has been selected and the ‘Add to
Cart’ button is clicked, one unit of the selected item should be added to the
current cart (see requirements for Current Cart below).
3) If a product does not have any items left in stock (i.e., they have all been sold
or added to the current cart), it should not be displayed in the Store Stock
ListView. Note that when a product is added to the cart, the amount of that
product available in stock should decrease.
Current Cart:
1) The Current Cart ListView should show the products that have been added to
the cart. Additionally, if the same product has been added to the cart x times,
the product should be included x times in the Current Cart ListView.
2) If no products have been added into the cart, the ‘Complete Sale’ button
should be disabled. If products are in the cart, the button should be enabled.
3) The total dollar amount in brackets beside ‘Current Cart’, representing the
total value of all products in the current cart, should be updated each time a
product is added to the cart or removed from the cart.
4) If an item in the Current Cart ListView has been selected, the ‘Remove from
Cart’ button should be enabled. Otherwise, it should be disabled. When an
item is clicked in the Current Cart ListView, it is acceptable to have your
ListView select ANY item that is the same as the originally clicked item (this is
the default behaviour of the ListView).
5) If an item in the Current Cart ListView is selected and the ‘Remove from Cart’
button is clicked, that item should be removed from the Current Cart and
placed back into the store’s stock.
Complete Sale:
1) When there is at least one product in the current cart and the ‘Complete Sale’
button is clicked, those items should be sold. You must update the ‘# Sales’
(increase by 1), the ‘Revenue’ (increase by the total value of the cart), and
the ‘$ / Sale’ (calculate using the other two values) fields on the GUI window
and the related variables in the underlying model.
2) The total dollar amount listed in brackets beside ‘Current Cart’ should be
reset to $0.00 when a sale is completed.
COMP 1406 – W20 – A3 Due Friday, March 6
th at 11:59 PM
4
Most Popular Items:
1) The most popular items ListView should show the 3 products that have sold
the most units. You do not need to worry about ties between products.
2) The most popular items list view should only account for products that have
officially been sold (i.e., it should not account for items that are added to the
cart).
Reset Store:
1) When the ‘Reset Store’ button is clicked, the model should be reset to its
initial state (i.e., the electronic store should be recreated). The GUI should
also update to show the reset state of the store.

In this assignment, you will simulate an on-line music centre called the MusicExchangeCenter where Users log in and download Songs from other users’ computers. It is a similar idea to the operation of the original Napster program that operated many years ago where users shared songs. You won’t be doing any downloading or internet programming. Instead, you will just simulate what happens in real life. The assignment will give you familiarity with ArrayLists and HashMaps. To get started with the assignment, download the Assignment4-Starting-Code.zip file from the assignment page, which contains an IntelliJ project with a number of classes that will be used in the assignment. (1) The Song/ User Classes The Song and User classes represent a song that is available at the Music Exchange Center and a user of the Music Exchange Center that logs in to download music. Do the following in the User class: Add a songList attribute which is an ArrayList of Song objects. This list will contain all the songs that this user has on his/her hard drive to be made available to the Music Exchange Center community. Adjust the 2nd constructor to ensure that this attribute is initialized properly. Write a get method for the attribute as well. Adjust the toString() method so that the XXX is replaced by the number of songs available in the user’s song list. Create a method called addSong(Song s) which adds a given song to the user’s song list. Create a method called totalSongTime() that returns an integer indicating the total duration (i.e., amount of time), in seconds, that all of the user’s songs would require if played. COMP 1406 – W20 – A4 Due Friday, March 20th at 11:59 PM Due Friday, March 20th at 11:59 PM (2) The MusicExchangeCenter Class Implement a class called MusicExchangeCenter which represents the company website that users log in and out of in order to download music. The class should have this attribute: users – an ArrayList of all registered Users (which may be either logged on or not logged on). Create the following methods: A zero-parameter constructor that sets attributes properly. An onlineUsers() method that returns an ArrayList of all Users that are currently online. Note that this method creates and returns a new ArrayList each time it is called. An allAvailableSongs() method that returns a new ArrayList of all Songs currently available for download (i.e., all songs that are available from all logged-on users). Note that this method creates and returns a new ArrayList each time it is called. A toString() method that returns a string representation of the music center showing the number of users currently online as well as the number of songs currently available. (e.g., “Music Exchange Center (3 users on line, 15 songs available)”). A userWithName(String s) method that finds and returns the user object with the given name if it is in the list of users. If not there, null should be returned. A registerUser(User x) method that adds a given User to the music center’s list of users, provided that there are no other users with the same userName. If there are other users with the same userName, then this method does nothing. Use the userWithName(String s) method above. An availableSongsByArtist(String artist) method that returns a new ArrayList of all Songs currently available for download by the specified artist. Note that this method creates and returns a new ArrayList each time it is called. Go back to the User class and add these methods: A register(MusicExchangeCenter m) that makes use of the registerUser(User x) method that you just wrote to register the user into the given MusicExchangeCenter m. (Note that this should be a one-line method). A logon() method that simulates a user going online. A logoff() method that simulates a user going offline. Now you should test your code. To do so, run the MusicExchangeTestProgram.java file and compare your results to the expected results below. COMP 1406 – W20 – A4 Due Friday, March 20th at 11:59 PM Due Friday, March 20th at 11:59 PM Here is the output that you should see: Status: Music Exchange Center (0 users on-line, 0 songs available) On-Line Users: [] Available Songs: [] Status: Music Exchange Center (2 users on-line, 8 songs available) On-Line Users: [Disco Stew: 4 songs (online), Ronnie Rocker: 4 songs (online)] Available Songs: [“Hey Jude” by The Beatles 4:35, “Barbie Girl” by Aqua 3:54, “Only You Can Rock Me” by UFO 4:59, “Paper Soup Cats” by Jaw 4:18, “Rock is Cool” by Yeah 4:17, “My Girl is Mean to Me” by Can’t Stand Up 3:29, “Only You Can Rock Me” by UFO 4:52, “We’re Not Gonna Take It” by Twisted Sister 3:9] Status: Music Exchange Center (4 users on-line, 16 songs available) On-Line Users: [Disco Stew: 4 songs (online), Ronnie Rocker: 4 songs (online), Country Candy: 3 songs (online), Peter Punk: 5 songs (online)] Available Songs: [“Hey Jude” by The Beatles 4:35, “Barbie Girl” by Aqua 3:54, “Only You Can Rock Me” by UFO 4:59, “Paper Soup Cats” by Jaw 4:18, “Rock is Cool” by Yeah 4:17, “My Girl is Mean to Me” by Can’t Stand Up 3:29, “Only You Can Rock Me” by UFO 4:52, “We’re Not Gonna Take It” by Twisted Sister 3:9, “If I Had a Hammer” by Long Road 4:15, “My Man is a 4×4 Driver” by Ms. Lonely 3:7, “This Song is for Johnny” by Lone Wolf 4:22, “Bite My Arms Off” by Jaw 4:12, “Where’s My Sweater” by The Knitters 3:41, “Is that My Toenail ?” by Clip 4:47, “Anvil Headache” by Clip 4:34, “My Hair is on Fire” by Jaw 3:55] Available Songs By Jaw: [“Paper Soup Cats” by Jaw 4:18, “Bite My Arms Off” by Jaw 4:12, “My Hair is on Fire” by Jaw 3:55] Status: Music Exchange Center (2 users on-line, 9 songs available) On-Line Users: [Ronnie Rocker: 4 songs (online), Peter Punk: 5 songs (online)] Available Songs: [“Rock is Cool” by Yeah 4:17, “My Girl is Mean to Me” by Can’t Stand Up 3:29, “Only You Can Rock Me” by UFO 4:52, “We’re Not Gonna Take It” by Twisted Sister 3:9, “Bite My Arms Off” by Jaw 4:12, “Where’s My Sweater” by The Knitters 3:41, “Is that My Toenail ?” by Clip 4:47, “Anvil Headache” by Clip 4:34, “My Hair is on Fire” by Jaw 3:55] Available Songs By Jaw: [“Bite My Arms Off” by Jaw 4:12, “My Hair is on Fire” by Jaw 3:55] Status: Music Exchange Center (0 users on-line, 0 songs available) On-Line Users: [] Available Songs: [] Available Songs By Jaw: [] (3) Downloading Music Go back to the Song class and add to it an attribute called owner which will be a User object representing the user who happens to own this copy of this song. Note that a Song object may only be owned by one User and that many users can have copies of the same song. Set it to null initially. In the User class, adjust the addSong(Song s) method so that it sets the owner properly. In the User class, add a method called requestCompleteSonglist(MusicExchangeCenter m). This method should gather the list of all available songs from all users that are online at the given music exchange center (i.e., the union of all of their local song lists), and then return an ArrayList formatted as follows (note: there should be 1 String element in the ArrayList per line): COMP 1006A/1406A – F18 – A4 Due Friday, March 20th at 11:59 PM 4 TITLE ARTIST TIME OWNER 1. Hey Jude The Beatles 4:35 Disco Stew 2. Barbie Girl Aqua 3:54 Disco Stew 3. Only You Can Rock Me UFO 4:59 Disco Stew 4. Paper Soup Cats Jaw 4:18 Disco Stew 5. Rock is Cool Yeah 4:17 Ronnie Rocker 6. My Girl is Mean to Me Can’t Stand Up 3:29 Ronnie Rocker 7. Only You Can Rock Me UFO 4:52 Ronnie Rocker 8. We’re Not Gonna Take It Twisted Sister 3:09 Ronnie Rocker Notice that the songs are numbered and that the title, artist, time and owner are all lined up nicely. You should use the String.format() method as described in the notes (Chapter 1). Recall that %-30s in the format string will allow you to display a leftjustified 30-character string. Also, %2d and %02d will allow you to display numbers so that they take 2 places, the 0 indicating that a leading zero character is desired. In the User class, add a method called requestSonglistByArtist(MusicExchangeCenter m, String artist). This method should gather the list of all available songs by the given artist from all users that are online at the given music exchange center (i.e., the union of all of their local song lists), and then return an ArrayList formatted similar to that shown above. In the MusicExchangeCenter class, add a method called getSong(String title, String ownerName) which returns the Song object with the given title owned by the user with the given ownerName, provided that the user is currently online and that the song exists. Return null otherwise. (Hint: you will need to search through the center’s users to find User with the matching ownerName and then search through that user’s songs to find the Song with the given title. It may be a good idea to write an extra helper method in the User class called songWithTitle(String title) that you can make use of). In the User class, add a downloadSong(MusicExchangeCenter m, String title, String ownerName) method that simulates the downloading of one of the songs in the catalog. It should use the getSong(String title, String ownerName) method that you just wrote, and add the downloaded song to the user’s songList (if not null). Download and execute the MusicExchangeTestProgram2.java file to test your new code. Here is the expected output: COMP 1006A/1406A – F18 – A4 Due Friday, March 20th at 11:59 PM 4 Status: Music Exchange Center (5 users on-line, 18 songs available) Complete Song List: TITLE ARTIST TIME OWNER 1. Hey Jude The Beatles 4:35 Disco Stew 2. Barbie Girl Aqua 3:54 Disco Stew 3. Only You Can Rock Me UFO 4:59 Disco Stew 4. Paper Soup Cats Jaw 4:18 Disco Stew 5. Rock is Cool Yeah 4:17 Ronnie Rocker 6. My Girl is Mean to Me Can’t Stand Up 3:29 Ronnie Rocker 7. Only You Can Rock Me UFO 4:52 Ronnie Rocker 8. We’re Not Gonna Take It Twisted Sister 3:09 Ronnie Rocker 9. Meadows Sleepfest 7:15 Sleeping Sam 10. Calm is Good Waterfall 6:22 Sleeping Sam 11. If I Had a Hammer Long Road 4:15 Country Candy 12. My Man is a 4×4 Driver Ms. Lonely 3:07 Country Candy 13. This Song is for Johnny Lone Wolf 4:22 Country Candy 14. Bite My Arms Off Jaw 4:12 Peter Punk 15. Where’s My Sweater The Knitters 3:41 Peter Punk 16. Is that My Toenail ? Clip 4:47 Peter Punk 17. Anvil Headache Clip 4:34 Peter Punk 18. My Hair is on Fire Jaw 3:55 Peter Punk Disco Stew before downloading: Disco Stew: 4 songs (online) Disco Stew after downloading: Disco Stew: 7 songs (online) Disco Stew after downloading Ronnie’s: Disco Stew: 8 songs (online) Song’s by Jaw: TITLE ARTIST TIME OWNER 1. Paper Soup Cats Jaw 4:18 Disco Stew 2. Bite My Arms Off Jaw 4:12 Disco Stew 3. Bite My Arms Off Jaw 4:12 Peter Punk 4. My Hair is on Fire Jaw 3:55 Peter Punk (4) Paying the Price Now, in order to make all of this legal, we must have a way to compensate the musical artists for their hard work and wonderful music. An artist should receive 25 cents each time one of their songs is downloaded. Hence, for each artist, we will keep track of how much money in royalties they have. Add the following attributes to the MusicExchangeCenter class: royalties – a HashMap with the artists’ names as the keys and the values are floats representing the total amount of royalties for that artist so far. It should only contain artists who have had songs downloaded. You will update this HashMap later. COMP 1006A/1406A – F18 – A4 Due Friday, March 20th at 11:59 PM 4 downloadedSongs – an ArrayList containing all of the songs that have been downloaded. This list will, in general, contain duplicate Song objects. Write a method in the MusicExchangeCenter class called displayRoyalties() that displays the royalties for all artists who have had at least one of their songs downloaded. It should display a two-line header and then one line per artist showing the royalty amount as well as the artist name as follows: Amount Artist ————— $0.75 Sleepfest $1.50 Clip $0.25 Jaw $0.50 Long Road $0.25 Yeah $0.25 UFO In the getSong() method in the MusicExchangeCenter, adjust the code so that if the song is found (i.e., able to be downloaded), then it is added to the downloadedSongs list. Additionally, the royalties for the artist of the song should be updated within this method. Write a method in the MusicExchangeCenter class called uniqueDownloads() that returns (i.e., not displays) a TreeSet of all downloaded Song objects such that the set is sorted alphabetically by song title. There should be no duplicates songs in this set. To add the Song objects to the TreeSet, the Song class will need to implement Comparable and you will need to write the corresponding compareTo(Song s) method in the Song class. Write a method in the MusicExchangeCenter class called songsByPopularity() that returns (i.e., not displays) an ArrayList of Pair<Integer,Song> objects where the key of the pair is an Integer representing the number of downloads and the value is the Song object. The integer key should represent the number of times that the song was downloaded. The list returned should be sorted in decreasing order according to the number of times the song was downloaded. To sort a list of Pair objects, you can use the following code: Collections.sort(yourListOfPairs, new Comparator<Pair<Integer, Song>>() { public int compare(Pair<Integer, Song> p1, Pair<Integer, Song> p2) { // PUT YOUR CODE IN HERE } }); COMP 1006A/1406A – F18 – A4 Due Friday, March 20th at 11:59 PM 4 Just insert the missing code so that it returns an appropriate integer indicating whether pair p1 comes before or after pair p2 in the sort order (see notes, Chapter 8, Page 281). Run the MusicExchangeTestProgram3.java test file to make sure it works with your code. Here is the expected output: Status: Music Exchange Center (5 users on-line, 18 songs available) Here are the downloaded songs: “Bite My Arms Off” by Jaw 4:12 “Meadows” by Sleepfest 7:15 “If I Had a Hammer” by Long Road 4:15 “Is that My Toenail ?” by Clip 4:47 “Anvil Headache” by Clip 4:34 “Is that My Toenail ?” by Clip 4:47 “If I Had a Hammer” by Long Road 4:15 “Anvil Headache” by Clip 4:34 “Meadows” by Sleepfest 7:15 “Only You Can Rock Me” by UFO 4:52 “Is that My Toenail ?” by Clip 4:47 “Is that My Toenail ?” by Clip 4:47 “Rock is Cool” by Yeah 4:17 “Meadows” by Sleepfest 7:15 Here are the unique downloaded songs alphabetically: “Anvil Headache” by Clip 4:34 “Bite My Arms Off” by Jaw 4:12 “If I Had a Hammer” by Long Road 4:15 “Is that My Toenail ?” by Clip 4:47 “Meadows” by Sleepfest 7:15 “Only You Can Rock Me” by UFO 4:52 “Rock is Cool” by Yeah 4:17 Here are the downloaded songs by populariry: (4 downloads) “Is that My Toenail ?” by Clip 4:47 (3 downloads) “Meadows” by Sleepfest 7:15 (2 downloads) “Anvil Headache” by Clip 4:34 (2 downloads) “If I Had a Hammer” by Long Road 4:15 (1 downloads) “Bite My Arms Off” by Jaw 4:12 (1 downloads) “Only You Can Rock Me” by UFO 4:52 (1 downloads) “Rock is Cool” by Yeah 4:17 Here are the royalties: Amount Artist ————— $0.75 Sleepfest $1.50 Clip COMP 1006A/1406A – F18 – A4 Due Friday, March 20th at 11:59 PM 4 $0.25 Jaw $0.50 Long Road $0.25 Yeah $0.25 UFO

A trie, also known as a prefix tree, is a tree-based data structure that stores key/value pairs (like a
HashMap). The keys in tries are usually strings. In this assignment, you will implement a trie data structure
that uses recursion to store string keys and their associated string values. The root node in the trie will be
empty (no key or value). Each node in the trie, including the root, will store a hash map with character keys
and trie node values. This structure will allow you to search for a string key in the trie by repeatedly looking
up the node associated with the next character in the key (if it exists). As an example, consider the picture
below (ignore the red for now). This trie contains the keys/values “A”, “ATE”, “ARE”, “BE”, “SIT”, and “SO”
(assume the key and value are the same, null values indicate nothing has been added there).
If you were to look up the key “ATE” in the trie, you would end up following the red node path. Starting at
the root node, look up and go to the node associated with ‘A’. From that node, look up and go to the node
associated with ‘T’, and then look up and go to the node associated with ‘E’. If the value stored in this final
node is null, the key does not exist in the trie. If the value is not null, the node’s value represents the value
associated with the given key (alternatively, if you are performing a put operation, you can add the value to
the node). If you were to look up the key “BAT”, you would start at the root node, look up and go to the
node associated with ‘B’, and then determine that the key “BAT” is not in the trie since ‘A’ is not associated
COMP 1406 – W20 – A5 Due Friday, April 3
rd at 11:59 PM
with the current node (i.e., is not in that node’s hashmap). If the additional keys “AT”, “BE”, and “SOD”
were added, the trie would then look like:
An interesting property of the trie structure is that the key for any node contained within a subtree rooted at
a node X has the key that would lead to X as a prefix. As examples, “SIT”, “SO”, and “SOD” all begin with
the character ‘S’ and are all contained within the sub-trie rooted at the node you would reach if you looked
up the key ‘S’. If “SOME”, “SODA”, and “SOAR” were added to the above trie, they would all be contained
in the subtrees of the key “S” and the key “SO”. This allows tries to easily produce a set of keys that begin
with a particular prefix.
To start the assignment, download the Assignment5-Base-Code.zip file from cuLearn. This zip contains an
IntelliJ project containing 4 Java files:
1. TrieMapInterface: This interface defines the methods that your TrieMap class must support.
These methods should work similarly to how they would for a hash map or any other map data
structure. The put method should add the associated key/value pair to the trie. If the key is
already in the trie, the value should be updated. The get method should return the value
associated with the given key, if the key exists. If the key does not exist in the trie, the get
method should return null. The containsKey method should return true if the trie contains the
given key and false otherwise. The getValuesForPrefix method must return an ArrayList of
String values that contains all keys within the trie that start with the specified prefix. The print
method should print all of the values contained within the trie.
2. TrieMap: This class contains skeleton code outlining the methods required to implement the
TrieMapInterface. I have also left method signatures from my own solution, which may provide
hints regarding possible solutions. You can remove or otherwise change the class however you
wish, so long as your class still implements the TrieMapInterface. All method implementations
COMP 1406 – W20 – A5 Due Friday, April 3
rd at 11:59 PM
must function recursively. This is the only class that you must add code to in order to
complete the assignment. You are free to add code to the other classes if you wish.
3. TrieMapNode: The class representing a node in the trie. It contains a hash map with character
keys and TrieMapNode values (similar to a binary tree but with more than two possible children),
along with a constructor and necessary get/set methods. You don’t need to change this class but
can make changes if you want.
4. TrieMapTester: This class will run several tests on your TrieMap implementation and count the
number of errors. This will run a significant number of operations, so it is probably a good idea to
run your own smaller tests first to verify your TrieMap implementation is likely correct.
The implementation of the TrieMap class may seem difficult initially. However, after becoming familiar with
recursively moving through the trie structure, you will find that most methods are quite similar. So
completing the first few methods is likely to be significantly more difficult than completing the others.
Personally, I would recommend implementing and testing the methods in this order: constructor, put, print,
containsKey, get, getValuesForPrefix.
Grade Breakdown
Put Method: 10 marks
Get Method: 5 marks
Contains Key Method: 5 marks
Get Values for Prefix Method: 20 marks
Print Method: 10 marks
Total: 50 marks

In this assignment, you will practice using recursion, as well as file operations. Submit a single ZIP file
called assignment6.zip containing your IntelliJ project. This assignment has 50 marks – see the marking
scheme posted on cuLearn for details.
Assume that we have an assembly line that can take a picture of a machine part which moves along a
conveyor belt. The picture (or image) is represented as a 2D grid of pixels which are either black or
white. The pixels can be accessed by specifying the row and column of the pixel where rows and
columns are specified by an integer value. The machine examines the images and attempts to
determine whether or not the parts are broken. A broken part will appear as a set of black pixels which
are not all connected together (i.e., there is a separation between one or more sets of black pixel groups.
Here are some examples of four possible images. Note that (c) and (d) represent images of broken
parts. The red border represents the perimeter of a part composed of black pixels.
COMP 1406 – W20 – A6 Due Friday, April 24th at 11:59 PM Due Friday, March 20th at 11:59 PM
(1) The PartImage class
Download the provided A6-Base-Code.zip file and extract the included IntelliJ project. This project
contains a PartImage class, which represents one of these images using a 2-dimensional array of
booleans.
You must complete the following methods in the PartImage class. Note that all the recursive methods may
be implemented in a ‘destructive’ sense. That is, you can change the state of the object within your
recursive implementation and do not need to recover to the original starting state.
• Complete the static readFromFile(String fileName) method that will open the specified file,
which will contain the data for a single part’s image. This method must read the part image data
from the file and return a new PartImage object with the correct attributes to represent that part
image. The data in the file will be organized like a 2D array, with each line representing a row
of the part’s image data. Each row will contain some number of comma-separated 0s and 1s,
indicating a white or a black space respectively. For example, the data contained in a file
representing part (a) from above might look like:
0,0,0,0,0,0,0,0,0,0
0,1,1,1,1,1,1,0,0,0
0,1,1,1,1,1,1,0,0,0
0,0,0,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,0,0,0
0,0,0,0,1,1,1,0,0,0
0,0,0,0,0,0,0,0,0,0
Note that you cannot assume that you know how wide each row will be or how many rows are
present. You will have to compute this in order to initialize the size of any 2D boolean arrays
your class uses. Three possible exceptions could arise within this method:
1. FileNotFoundException – if the specified file is not found, your method’s code should
handle the exception gracefully and return a null object.
2. IOException – if there is another type of input error, your method’s code should
handle the exception gracefully and return a null object.
3. InvalidPartImageException – if any line in the file does not contain the same number
of values as all other lines (e.g., 9 columns in one line and 10 in another), or one of
the values is not 0/1, your method must throw a new InvalidPartImageException with
the current filename. This exception will be handled by the provided test code.
• Complete the print() method that will print out the image in the console window which mayprint
something like what is shown below for the image in picture (a) above.
———-
-******—
-******—
-********-
—******-
-********-
-********-
-******—
—-***—
———-
COMP 1406 – W20 – A6 Due Friday, April 24th at 11:59 PM Due Friday, March 20th at 11:59 PM
• Complete the findStart() method that returns the location (row/column) of any black pixel in the
image. It uses the Point2D as the return value. If no black pixel is found, the method should
return null.
• Complete the partSize() method so that it returns an int representing the number of black
pixels in the image. This can be done using a double FOR loop.
• Complete the method called expandFrom(int r, int c) that recursively examines the image by
traversing its pixels, examining (i.e., “visiting”) black pixels in all directions that are connected to a
starting point black pixel (specified by the parameters). The idea is that this method will determine
all black pixels that are connected to the starting pixel. Additionally, the method should set the
black pixels to white (hints: when a white pixel is encountered during the recursion, this is an indication
that you don’t need to keep recursively checking in that direction). This method MUST be done
recursively without loops, otherwise you will receive 0 marks for this part.
• Complete the method called perimeterOf(int r, int c) which should return an integer representing
the perimeter of the part which starts at the given (r,c) point. The method MUST be done
recursively without loops, otherwise you will receive 0 marks for this part. For example, in
the image (A) shown above, the perimeter is 36 (which is the red border length, not the number of
black cells on the border) and in image (B) it is 106. As for the other two images, it depends
where you started expanding from, since the piece is broken into multiple parts. (hints: You will
need to mark pixels as being visited (perhaps use another 2D array). Make sure not to re-visit these
pixels. Also, make sure to handle borders correctly. A black pixel on the top/left border corner, for
example, will add 2 to the perimeter count for the top and the left.)
• Complete the method called countPieces() which should return an integer representing the
number of separate pieces contained in the part image. For example, part (a) above contains 1
piece, where part (d) contains 8 pieces.
Once you have implemented these, you can use the PartImageTester class to test your solutions.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] Comp 1406 assignments 1 to 6 solution[SOLVED] Comp 1406 assignments 1 to 6 solution
$25