Week 09: Homework 5 - Introductory Programming in Java Skip navigation Introductory Programming in Java ANU College of Engineering & Computer Science Search query Search ANU web, staff & maps Search COMP6700 Lectures Labs Assignments menu Search query Search ANU web, staff & maps Search Search COMP6700 labs Week 01: Welcome Week 02: Lab 1 Week 03: Lab 2 and HW 1 Week 04: Lab 3 and HW 2 Week 05: Drop-in Lab, HW 3 Week 06: Lab 4 and HW 4 Week 07: Mid-sem Exam Week 08: Free (Anzac Day) Week 09: Lab 5 and HW 5 Week 10: Lab 6 and HW 6 Week 11: Lab 7 and HW 7 Week 12: Lab 8 and HW 8 related sites Wattle Piazza Week 09: Homework 5 Note This homework comes after a long break. It warrants an extended scope, and therefore has two parts, each worth of 1 mark. Part One: Reading input and formatting output Objectives To practice formatting output and use of exceptions when working with external (user) data and defining a custom exception to match the domain semantics. Problem Write a Java program called Percent which repeatably prompts the user to input an integer, then (after the user enters an empty line, that is hits “return” key ↵ without entering anything) calculates the total sum and percentage which every input integers represents. The program shall operate as follows: It shall prompt for a list of integers from the standard input, one per line, terminated by a blank line. It shall then calculate the total of those numbers and the percentage of that total that each represents. It shall present the output as a table, with two columns separated by exactly four spaces. The left column shall contain the original number, right-justified in a field of width four characters. The right column shall contain the percentages, followed by the % symbol, right justified in a field of width six characters (up to three digits before the decimal point, the decimal point, one figure after the decimal point, and the percent symbol). Percentages shall be rounded to one decimal place. (Did you use already printf() or System.format()? If not, this is the time!) % java Percent
47
37
81
21
66
57
The numbers and percentage:
47 15.2%
37 12.0%
81 26.2%
21 6.8%
66 21.4%
57 18.4%
309 100.0%
% java Percent
2
17
1
3
The numbers and percentage:
2 8.7%
17 73.9%
1 4.3%
3 13.0%
23 100.0%
Notice that there is a blank line between the prompt for the last line of input and the start of the output. Notice also that the last line of output holds the total of all the inputs and “full percentage”. If there are no numbers input, or if their total is zero, the program should print an error message and stop (rather than trying to divide by zero). If one of the inputs is not an integer, it should print an error message and prompt for it again. Make the program robust when the user enters a negative integer, or a non-integer, so the program does not crash, but corrects the user and offers another try: % java Percent
2
-17
Non-negative integers only, try again:
seventeen
Non-negative integers only, try again:
17
1
3
The numbers and percentage:
2 8.7%
17 73.9%
1 4.3%
3 13.0%
23 100.0%
You should check the validity of input with your own exception type, named, eg, BadValueException, and make your program to throw this exception if the entered input cannot be used to create a non-negative value of integer type, and to handle this exception to prevent the run-time error (and to achieve the behaviour which is exemplified above). Which standard exception should your BadValueException extend? Which part of your program should be programmed to throw this exception? Part Two: Managing a bottle shop Objectives To practice the use of Java Foundation Classes API (generic container from the java.util package), (anonymous) inner classes or lambda-expressions, and standard algorithms to manage large sets of data. The exercise will reinforce your experience obtained while working on Assignment One. Problem Create the Bottle class with the field attributes: beer, volume (double), alcoholContent (double), glassColour (enum type), price (this can be an object of a Price class, in which case define this as well; or it can be plain double), quantity (plain int will do unless you are a heavy drinker in which case use long type). The beer field should be an object of the Beer class which has fields — brandName (String), strength (double value for the amount of alcohol per unit volume), and perhaps few others (if you are a beer drinker you will think of something, surely), and also standard methods (toString() etc). Use the value of Beer.strength to determine the value of bottle.alcoholContent field (also using the value bottle.volume). The field alcoholContent is an example of the so called derived attribute – it has no independently set value, rather values of other object fields determine it. Then write the BottleShop class which allows to store a number of bottles of various kind. Include a number of methods needed to manage this inventory, like calculating the number of bottles of a given beer brand, price of the whole stock, etc. Also define some methods to sort and print the collection. Sorting can be various: by the beer brand name, by the alcohol strength or alcohol content of the single bottle (in some countries, the regulation limits the hours when strong alcoholic beverages can be sold), by the colour of the bottle glass (for recycling purposes), etc, etc, etc, let you practical fantasy flow… Write a client program which uses the BottleShop collection (this can be done via inclusion the main() method in the BottleShop.java). Assessment You will get up to two marks (one for each task; to receive 1 full mark for Part One, your code must contain a custom exception class which the main class uses, without such exception class your mark may not exceed 0.5), if you submit your work by pushing the local repository using git push command by Friday, 5 May 2017, in your GitLab repository following the instructions which are provided in the Git and GitLab. The code should be placed in the hw5 subdirectory of your locally cloned repository. You can optionally (if the opportunity will exist) present your solution to tutor during the Week 9 labs. Updated: 01 May 2017/ Responsible Officer: Head of School/ Page Contact: Alexei Khorev Contact ANU Copyright Disclaimer Privacy Freedom of Information +61 2 6125 5111 The Australian National University, Canberra CRICOS Provider : 00120C ABN : 52 234 063 906 You appear to be using Internet Explorer 7, or have compatibility view turned on. Your browser is not supported by ANU web styles. » Learn how to fix this » Ignore this warning in future