Lab Test 2
In your week 7 lab, you will take one of the four tests that follow.
Lab Test 2, Version A
No Materials permitted.Each part of this lab test will be completed within a mercurial repository and Java project which you must first create.
- Using Eclipse, create a new mercurial repository called labtest2 locally (in your home directory).
- Create a new Eclipse project called Lab Test 2, whose location is the labtest2 repository you just created (not 'default location').
Questions:
- Within the Lab Test 2 project, create a class Names that will create an ArrayList of String, add five names to it, and print the names out on separate lines (using a 'for' loop ). You do not need to get input from the user, just hard-code the names. Add and commit your work. (5 Marks)
- Within the Lab Test 2 project, create an abstract
class, Shape, that stores information about different
shapes. Shape includes a method, perimeter(),
which returns the length of the perimeter of the shape. Create three additional classes, Square, Circle and Triangle, each of which extends Shape. Draw
a UML class diagram for your solution and implement it along with a demo
class, which includes the following code:
Shape s1 = new Square(10.0); // a square with sides of 10.0 units Shape s2 = new Circle(1.0); // a circle of radius 1.0 units Shape s3 = new Triangle(13.0); // an equilateral triangle with sides of 13.0 units System.out.println(s1.perimeter()); System.out.println(s2.perimeter()); System.out.println(s3.perimeter());
Your demo class should produce the following output:40.0 6.28 39.0
Add and commit your work. (3 Marks) - Add to the above example the ability of store the xy-position of each shape. Also add a method that is able to determine whether two shapes overlap each other. For simplicity, you only need to get this working for squares and circles (so the method should be able to determine whether a circle is overlapping with another circle, a circle with another square, or a square with another square). Add and commit your work. Demonstrate your solution. (2 Marks)
Lab Test 2, Version B
No Materials permitted.Each part of this lab test will be completed within a mercurial repository and Java project which you must first create.
- Using Eclipse, create a new mercurial repository called labtest2 locally (in your home directory).
- Create a new Eclipse project called Lab Test 2, whose location is the labtest2 repository you just created (not 'default location').
Questions:
- Within the Lab Test 2 project, create a class, Mean that will create an ArrayList of Double, add 5 doubles to it, and calculate and print the mean of the numbers in this list. You do not need to get input from the user, just hard code the numbers. Add and commit your work. (5 Marks)
- Within the Lab Test 2 project, create a class, Shop that maintains information about the inventory of a shop. Each item in the shop has a name, the number of these items the shop currently has and the price of the item in cents. The shop may have many different items in it. The Inventory class must have a totalStockValue() method which returns the sum of the value of all stock. Hand-draw a UML class diagram and implement your program along with a demonstration class. Add and commit your work. (3 Marks)
- Modify the above example such that for each item in the shop you also store: the rate at which the item is sold (in units per day) along with the number of days it takes to re-order the item. Use this information to generate a report of the items that need reordering such that the store does not run out of any items (you may assume that the information you store about each item is correct). Add and commit your work. (2 Marks)
Lab Test 2, Version C
No Materials permitted.Each part of this lab test will be completed within a mercurial repository and Java project which you must first create.
- Using Eclipse, create a new mercurial repository called labtest2 locally (in your home directory).
- Create a new Eclipse project called Lab Test 2, whose location is the labtest2 repository you just created (not 'default location').
Questions:
- Within the Lab Test 2 project, create a class, People that will store a table of people's names and ages (using a HashMap and the people's names as the key). Add five people to the HashMap. Look up and print the age of one person in the table. Then print the entire table (both names and ages). You do not need to get input from the user, just hard code the names and ages. Add, commit, and push your work. (5 Marks)
- Given the following class that stores information about the cost of shop items:
public class Item { String name; int cost; // in cents public Item(String n, int c) { name = n; cost = c; } public String toString() { return name + " " + cost; } }Create a copy of Item within your Lab Test 2 project, and modify the code such that it implements the Comparable interface. Items should be ordered based on the cost (lowest cost first), if two items cost the same amount then order these items alphabetically based on their name. Make a demo class Cost that creates an ArrayList of Item, and add a number of items to this ArrayList. Then, using the Collections class, sort the items and print out the sorted list. Add and commit your work. (3 Marks) - Suppose you are given a gift voucher for the above shop worth $100. Assume that the shop does not give change on gift vouchers, and you do not wish to spend any more than what is in the gift voucher. Within the Lab Test 2 project create a class, Voucher that will determine the most amount of your gift voucher you can spend. You may assume that for any particular item there are enough of them in stock that you could buy many instances of the same item. e.g. Say the items in the shop were worth: $87, $20, $99, $12, then the most you could spend would be the full $100 (just buy 5 of the $20 item), whereas, if the items in the shop were worth: $87, $22, $30, $45 then the most you could spend would be $97. Add and commit your work. (2 Marks)
Lab Test 2, Version D
No Materials permitted.Each part of this lab test will be completed within a mercurial repository and Java project which you must first create.
- Using Eclipse, create a new mercurial repository called labtest2 locally (in your home directory).
- Create a new Eclipse project called Lab Test 2, whose location is the labtest2 repository you just created (not 'default location').
Questions:
- Within the Lab Test 2 project, create a class, BMI that stores information about a person including: name, height, and weight. The class should have a method that returns the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). Also the class should have a method which returns, as a string, a short report on the person (e.g. "Hugh is 1.85m tall and is 91kg and has a BMI of 26.6 kg/m^2"). Implement this class along with a program that demonstrates its use. Add and commit your work. (5 Marks)
- Within the Lab Test 2 project, create a class, Sum that extends an ArrayList of Integer by adding a method, sum() that returns the sum of all elements in the list of ints (add the elements up each time the method is called). Add and commit your work. Demonstrate your code. (3 Marks)
- Modify the above solution such that the sum is stored and maintained as a private field of the class. Now when the sum() method is called, it should just be able to return the value of the sum field. Note that many of the standard methods of the ArrayList class must be overwritten so that they are able to adjust the sum as elements are added or removed from the list. For this question you need only override the four add mentods. Add and commit your work. (2 Marks)


