Lab Test 2
In your week 7 lab, you will take one of the five tests that follow.
Lab Test 2, Version A
No Materials permitted. Questions:- Write a program 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. (5 Marks)
- Write a program that uses an abstract class, Shape,
that stores information about different shapes. The shapes should
include squares, circles, and triangles (equilateral).
Shape includes a method, perimeter(), which
returns the length of the perimeter of the shape. Draw a UML class
diagram and implement your solution along with a demo class. If the
following code was executed:
Shape s1 = new Square(10.0); // a square with sides of 10.0 units Shape s2 = new Circle(1.0); // a circule 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());
we would expect the following output:40.0 6.28 39.0
(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). Demonstrate your solution. (2 Marks)
Lab Test 2, Version B
No Materials permitted. Questions:- Write a program 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. (5 Marks)
- Write a program 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. Draw a UML class diagram and implement your program along with a demonstration class. (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). (2 Marks)
Lab Test 2, Version C
No Materials permitted. Questions:- Write a program 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. (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; } }Modify this class 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 that creates an ArrayList of Item, add a number of items to this ArrayList and then, using the Collections class, sorts these items, print this sorted list. (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. Write a program 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. (2 Marks)
Lab Test 2, Version D
No Materials permitted. Questions:- Design a class which 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. (5 Marks)
- Create a class 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). 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. (2 Marks)
Lab Test 2, Version E
No Materials permitted. Questions:- Design a class which stores an xy point. Both x and y should be stored as doubles. The class should have two methods. The first is toString(), which displays the point as a string (e.g. if x = 10.0 and y = 3.0 then it would return the string (10.0, 3.0)). The second returns, as a double, the distance between the point and the origin (i.e. the distance between the point and (0.0, 0.0)). Implement this class along with a program that demonstrates its use. (5 Marks)
- Implement a program that prints 10 randomly generated integers. Each time you run the program it should generally print a different set of integers. The integers should be strictly between 9 and 16. That is, each of the numbers will be in the set of numbers {10, 11, 12, 13, 14, 15}. (3 Marks)
- Write a program that draws the path of an ant that is walking
randomly around a JavaFX window. This should look a little like a
scribbling on a page. If the ant walks off the edge of the screen
re-center the ant and continue the random walk. Note that you may
need to slow the ant down so that you can see them walking around the
screen. The 'boilerplate' for creating the window ('scene')
is provided for you here:
import javafx.application.Application; ... public class Q3 extends Application { @Override public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Ant"); Group root = new Group(); Scene scene = new Scene(root, 300, 300); primaryStage.setScene(scene); /* your code goes here */ primaryStage.show(); } public static void main(String[] args) { launch(args); } }(2 Marks)
