Skip navigation

Introductory Programming In Java

HW 1 HW 2 HW 3 HW 4 HW 5 HW 6 HW 7

Homework 5

This homework consists of two unrelated parts, each dealing with a different aspect of Java programming. The size of the whole exercise, therefore, is larger than usual. But since it's due after the mid semester break, some additional work seems appropriate, n'est pas?

Part 1: Reading input and formatting output

The 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 a ‘%’ 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 see already printf() at work?)

> 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%

The most appropriate way to achieve this is to handle exception which the Integer.parseInt() throws when the input is bad.

Part 2: Managing a bottle shop

The problem

Create the Bottle class with the field attributes: beer, volume (double), alcoholContent(double), glassColour (use enum type), price (this can be an object of 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 filed 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, if you present a complete solution to the Homework exercise during the next week lab. If you complete only one of the two parts (whichever), you will receive one point only.

HW 1 HW 2 HW 3 HW 4 HW 5 HW 6 HW 7

Updated:  Tue 14 May 2013 01:03:20 EST Responsible Officer:   JavaScript must be enabled to display this email address. Page Contact:   Course Webmaster