Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

Computer Science Courses

Related sites

Introductory Programming In Java

HW 5

Homework 5

This homework comes after a long brea. It warrants an extended scope, and therefore has two parts, each worth of 1 mark.The part One is due in week 8, the Part Two is due in week 9.

Part 1: Reading input and formatting output

Due in week 8

The problem

Write a Java program called Percent which repeatablyprompts the user to input an integer, then (after the user enters an empty line, that is hits "return" key withoutentering anything)calculates the total sum and percentage which every inputintegers represents.

The program shall operate as follows: It shall prompt for alist of integers from the standard input, one per line,terminated by a blank line. It shall then calculatethe total of those numbers and the percentage of that total thateach represents. It shall present the output as a table, withtwo columns separated by exactly four spaces. The left columnshall contain the original number, right-justified in a field ofwidth four characters. The right column shall contain thepercentages, followed by a ‘%’ symbol,right justified in a field of width six characters (up to threedigits before the decimal point, the decimal point, one figureafter the decimal point, and the percent symbol). Percentagesshall be rounded to one decimal place. (Did you see alreadyprintf() at work?)

> java Percent473781216657The 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 Percent21713The 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 thelast line of input and the start of the output. Notice also thatthe 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, theprogram should print an error message and stop (rather thantrying to divide by zero). If one of the inputs is not aninteger, it should print an error message and prompt for itagain.

Make the program robust when the user enters a negative integer,or a non-integer, so the program does not crash, but corrects the userand offers another try:

> java Percent2-17Non-negative integers only, try again:seventeenNon-negative integers only, try again:1713The numbers and percentage: 2 8.7% 17 73.9% 1 4.3% 3 13.0% 23 100.0%

You should tackle the validity of input with your own exception type,named, eg, BadValueException, and make your program to throwthis 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-timeerror (and to achieve the behaviour which is exemplified above). Which standardexception should your BadValueException extend? Which part of yourprogram should be programmed to throw this exception?

Part 2: Managing a bottle shop

Due in Week 9

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 Priceclass, 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 caseuse long type). The beer field should be an object of theBeer class which has fields — brandName (String), strength(double value for the amount of alcohol per unit volume), andperhaps 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 fieldsdetermine it.

Then write the BottleShop class which allows to store a number ofbottles of various kind. Include a number of methods needed to manage thisinventory, like calculating the number of bottles of a given beer brand,price of the whole stock, etc. Also define some methods to sort and printthe collection. Sorting can be various: by the beer brand name, by thealcohol 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 1 mark, if you present a complete solution to the Part One during the lab of Week 8, and 1 more mark if you present a complete solution to the Part Two during the lab of Week 9.If you complete only one of the two parts(whichever), you will receive one mark only.

HW 5

Updated:  Sun 12 Jun 2016 17:27:37 AEST Responsible Officer:   JavaScript must be enabled to display this email address. Page Contact:  

+61 2 6125 5111
The Australian National University, Canberra
CRICOS Provider : 00120C       ABN : 52 234 063 906