Java代写:KIT101 Meal

Introduction

基础Java作业,考察基本的布尔逻辑运用,做一个简单的食物分类系统。

Background

You have a meal to eat. But should you consume it? Obviously if it’s solely chocolate or alcohol then the answer is probably ‘yes’. Obviously if it’s some kind of green vegetable or green meat (or in fact anything green) then the answer is most definitely ‘no’!
If you can eat it, should you? Well, not if it’s too hot or too cold — and not if there’s too much of it! Everything in moderation after all (except green food).
You are to write a Java application program named AssigOne216.java which provides the user with advice as to whether their meal is edible or inedible and why. If it’s edible then the amount of food left over (if any) should be shown. The details (specifications) of this task are given below. Note that the correctness marks you receive for your program will depend on how well it matches this specification. If you decide to implement something that is more elaborate than specified, you should understand that:

  • There will be no marks awarded for the elaborations you have designed and penalties may be applied for confusing/extraneous code.
  • Your program MUST STILL meet the basic specifications given below.

The program is to determine whether the meal is edible or inedible and why. The user will be asked three initial questions:

  • The description of the meal to be consumed (which may be a multi-word answer which contains spaces).
  • The volume of the meal in millilitres (a whole number).
  • Whether or not the meal is at the ambient room temperature (of 22.0 degrees Celsius). If the meal is not at room-temperature then the user should be asked for the temperature (as a floating point number).

The user may opt not to provide any information about meals at all.
NOTE: Part of the assessment of your program may be done automatically, so it is important that you follow the specification exactly, for example the questions must be asked in the order given and the method the user is given to indicate that they want to enquire about.

Algorithm

  • Firstly, the program should ask the user whether they would like to consume the food they have.
  • If the user does wish to consume their food then
    • The user will be asked to enter a description of the meal they have. (Their entry may contain spaces and upper- and lower-case letters.)
    • The user will be asked to enter the volume of their food in millilitres (as a whole number).
    • The user will be asked to enter whether or not their meal is at room-temperature (using a true or false value).
    • If the user indicates that their meal is not at room temperature then
    • They will be asked to enter the current temperature of their meal (a floating-point number value which may be positive, negative, or zero).
    • The program will then work out whether the meal is edible or not and what, if any food remains after the first 1500 ml is consumed… Starting with the assumption that the meal is edible, if there is insufficient food to eat (i.e. 0 ml or less) then
    • the meal is inedible because there is not enough of it
    • Otherwise
    • If the meal is too hot (65 degrees or hotter) or too cold (–5 degrees or colder) then
    • The meal is inedible because of its temperature
    • Otherwise
    • If the meal is either “chocolate” or “alcohol” then
    • The meal is edible because it’s a favourite food
    • Otherwise
    • If the description of the meal starts with “green” then
      • The meal is inedible because it is green
    • Otherwise
      • The meal is edible but for no special reason
    • If the meal is edible
    • The amount of food which remains after the maximum of 1500 ml is consumed is calculated.
    • The description of the meal, its volume, and its temperature should then be displayed to the user
    • The state (edible) of the meal should then be given together with the reason (see sample output).
    • The volume remaining should also be displayed (again, see sample output).
    • Otherwise
    • The description of the meal, its volume, and its temperature should then be displayed to the user
    • The state (inedible) of the meal should then be given together with the reason (see sample output).
  • After the meal’s contents has been considered the user will be asked if they wish to consider another meal. To analyse another the user must enter a line beginning with ‘y’ or ‘Y’ (for yes). The program should repeat until they enter a response commencing with a letter other than ‘y’ or ‘Y’.
  • The user will be shown a message saying how many meals were analysed.

Three example executions are shown below:

Would you like to eat the food you have? n
0 meals were attempted.
Would you like to eat the food you have? y

Please describe the food you have: green beans
What is the volume of the food you have (in millilitres)? 200
True/False -- Is the food at room temperature? false
What temperature is the food (in Celsius)? 50

Your 200 ml meal of green beans, currently at 50.0 degrees Celsius
is inedible because food contains something green -- yecccchhh!

Would you like to eat more of the food you have? Yes

Please describe the food you have: chocolate
What is the volume of the food you have (in millilitres)? 30
True/False -- Is the food at room temperature? true

Your 30 ml meal of chocolate, currently at 22.0 degrees Celsius
is edible because food is either chocolate or alcohol!
There will be 0 millilitres of food remaining after you've eaten

Would you like to eat more of the food you have? yep

Please describe the food you have: alcohol
What is the volume of the food you have (in millilitres)? 2000
True/False -- Is the food at room temperature? false
What temperature is the food (in Celsius)? 0

Your 2000 ml meal of alcohol, currently at 0.0 degrees Celsius
is edible because food is either chocolate or alcohol!
There will be 500 millilitres of food remaining after you've eaten

Would you like to eat more of the food you have? no
3 meals were attempted.

Would you like to eat the food you have? YES

Please describe the food you have: beef steak
What is the volume of the food you have (in millilitres)? 350
True/False -- Is the food at room temperature? false
What temperature is the food (in Celsius)? -10.5

Your 350 ml meal of beef steak, currently at -10.5 degrees Celsius
is inedible because food must be between -5.0 and 65.0 degrees to be
consumed!

Would you like to eat more of the food you have? y

Please describe the food you have: water
What is the volume of the food you have (in millilitres)? 100
True/False -- Is the food at room temperature? false
What temperature is the food (in Celsius)? 100

Your 100 ml meal of water, currently at 100.0 degrees Celsius
is inedible because food must be between -5.0 and 65.0 degrees to be
consumed!

Would you like to eat more of the food you have? y

Please describe the food you have: biscuit
What is the volume of the food you have (in millilitres)? 5
True/False -- Is the food at room temperature? true

Your 5 ml meal of biscuit, currently at 22.0 degrees Celsius
is edible because of nothing special.
There will be 0 millilitres of food remaining after you've eaten

Would you like to eat more of the food you have? n

3 meals were attempted.

Planning

This program (like all programs) should be developed in stages, with each stage fully implemented and tested before the next stage is attempted. Here are some suggestions that should help with your planning and development of the program.
Write a program in a file with the correct name — put very simple code in it, for example just display a message such as “program starts”.

  1. Work out the types of variables that will be needed to hold the necessary information and write their declarations. Some of the things that will be needed are:
    • a String entered by the user (for the description of the meal)
    • an int entered by the user (for the volume of the meal)
    • a boolean entered by the user (for whether the meal is at room-temperature)
    • a double entered by the user (for the temperature of the meal)
    • a String for the description of why the meal is edible or inedible
    • a boolean to remember whether it is edible or inedible
    • an int for the volume of excess food
    • the char entered by the user to indicate they wish to enter more meal details
    • the count of the number of meal analyses undertaken (an int)
    • a Scanner object
  2. Work out what ‘constant’ values will be needed to be stored in the program; these can be implemented as final variables. Some of these could be:
    • the current room-temperature
    • the minimum and maximum temperatures for edible food
    • the minimum and maximum volumes that may be consumed
    • the favourite foods and the colour of the inedible food
    • the letters that allow the repetition of the program
  3. Work out how to use the Scanner class provided for this unit to read in values of different types entered by the user. NB you will need to mix calls of nextLine(), nextDouble(), nextInt(), and nextBoolean(). To do this effectively follow the latter three with calls to nextLine().
  4. At this stage you should be able to write a program that will do all the things that are needed to work out the result for a single meal.
  5. To allow the user to see more you will need another loop. Think about what the loop will need and which loop (for, do, while) will be most appropriate:
    • initialisation
    • test (should this be before or after?)
    • body
    • update

Program Style

The program you write for this assignment must be in a single file called AssigOne216.java.
Your program should follow the following coding conventions:

  • final variable identifiers should be used as much as possible, should be written all in upper case and should be declared before all other variables
  • variable identifiers should start with a lower case letter and all variables should be declared at the top of the main() method (but after any constants)
  • every if and if-else statement should have a block of code (i.e. collections of lines surrounded by { and }) for both the if part and the else part (if used)
  • every loop should have a block of code for its body
  • the keyword continue should not be used
  • the keyword break should only be used as part of a switch statement
  • opening and closing braces of a block should be aligned
  • all code within a block should be aligned and indented 1 tab stop (or 4 spaces) from the braces marking this block
  • commenting:
    • There should be a block of header comment which includes at least
    • file name
    • student name
    • student identity number
    • a statement of the purpose of the program
    • date
  • Each variable declaration should be commented
  • There should be a comment identifying groups of statements that do various parts of the task
  • Comments should describe the strategy of the code and should not simply translate the Java into English.