QuestionQuestion

Transcribed TextTranscribed Text

Introduction to Programming Java Programming Assignment 5: Objects and Decisions Your last Alice program implemented decision making. This assignment will apply the same concepts to Java, starting with the code from your last Java program (Java Assn 4), which implemented objects. The Java Assn 4 program ran all of the code sequentially. Decision statements will now be used to run some of the statements conditionally, by implementing at least one of each of the following conditional programming constructs: if statement or if/else statement, multiple alternative if statement, nested if statement, and switch statement. Problem Summary Suppose you wanted to further modify your BAC Calculator, to make it more versatile. Starting with Java programming assignment 4, you could add the following features: x Let the user specify if they are male or female. o Create an object o Calculate and display results using this information. x Let the user adjust the metabolic rate used, based on what type of metabolism they have. x Adjust the output, depending on the BAC level calculated. Overview of Program This program will still contain two classes, in two separate files within your project: x A modified Drinker class to define the data fields and methods for a Drinker object, containing: o Ten data field definitions for a Drinker object o Two constructor methods to create a Drinker object (one constructor without parameters and one constructor with parameters). o Six setter methods to set the values for six of the data fields o An instance method to calculate the male volume distribution o An instance method to compute the female volume distribution o An instance method to compute the current BAC o An instance method to display the drinker’s info and the current BAC. o An instance method to analyze the current BAC. x The main BACcalculator class, modified to define only two methods, containing: o A static method to display a description of what the program will do. o A main method to display the program description, to read the inputs from the user, to create a Drinker object, and to call the other methods to calculate the current BAC, then display and analyze it. Program Requirements Modify the program you wrote for Java Assignment 4, as follows: 1. Within the Drinker class: x Add the following additional private data field: o int weight in pounds o int height in inches o char gender (will store ‘M’ or ‘F’) o double metabolic rate o double current BAC x Add initialization statements to the original constructor, for each of these new fields. Use zero values for the numeric data fields and a single space character (' ') for the gender. x Modify the weight setter so that it will set the both the weight data fields at once. This method will still have one parameter: an integer weight in pounds. It will: o Set the new weight in pounds data field, using the parameter value o Use the original code to convert and set the weight in kilograms data field x Modify the height setter so that it will set the both the height data fields at once. This method will still have one parameter: an integer height in inches. It will: o Set the new height in inches data field, using the parameter value o Use the original code to convert and set the height in meters data field x Add a setter for the metabolic rate data field. This method will: o Have one parameter: a metabolic type (char with value ‘V’, ‘S’, ‘A’, or ‘F’) o Have two constants, representing the base metabolic rates for males and females (move these constant values from the assn 4 BACcalculator class to this method) o Use decisions to set the initial metabolic rate ƒ Begin with a base metabolic rate, based on the drinker’s gender o Then use a switch statement to adjust the metabolic rate, if necessary, using the metabolic type parameter, as follows: metabolic type is V (very slow) – reduce the metabolic rate by 0.002 metabolic type is S (slow) – reduce the metabolic rate by 0.001 metabolic type is A (average) – do not change the metabolic rate metabolic type is F (fast) – increase the metabolic rate by 0.001 If the parameter contains any other letter, do not adjust the metabolic rate. Just display an error message telling the user that since his/her choice was invalid, no adjustment will be made. x Modify the setter for the drinker identifier data field. o Setter will now only have one parameter -- the last name (will no longer use the zip code). o The drinker identifier field will now be created using the first 7 letters (uppercased) of the last name. ƒ If the last name has less than 7 letters, the code should append Xs to the end of the name, so that there will be 7 letters total. o The code in the body of the setter method will: ƒ Declare an integer constant to hold an id length of 7. ƒ Declare a second String constant to hold seven Xs (“XXXXXXX”). ƒ Use decisions with these constants and the last name to create the drinker identifier, WITHOUT using brute force. o NOTE: You will need to check the length of the last name BEFORE trying to extract any letters, and then use a decision statement to decide which letters to extract. Otherwise the program may crash when there are less than 7 letters in the last name. x Add a second constructor to instantiate a Drinker type object. The new constructor with have seven parameters: o the drinker’s last name (String) o the gender (char) o the weight in pounds (int) o the height in inches (int) o the number of drinks consumed (int) o the elapsed time in hours (int) o metabolism type (char) The constructor will: o Assign initial values to the gender, drinks consumed, and elapsed time data fields, using the constructor parameters for the values. o Initialize the current BAC value to 0. o Call the modified setters for the drinker id, weights, and heights, using the constructor parameter as the setter arguments. o Call the new setter for the metabolic rate, using the constructor metabolism type parameter as the setter argument. NOTE: The original getters for this class will no longer be necessary. All data fields will be accessed via other instance methods. x Modify the method that computes and displays the current BAC, so that it will ONLY compute the BAC and store the computed value into the current BAC data field (no displaying of anything). o Remove the parameters and use data fields instead. o Use a decision statement to call the correct volume distribution method for the Drinker’s gender. Store the result in a local variable. o Modify the formulas to use only data fields and the local variable with the volume distribution result to compute the current BAC. Program Requirements Modify the program you wrote for Java Assignment 4, as follows: x Create a new instance method to display the drinker info and current BAC, as described below. o Use decision statement to display the gender as a String ƒ Display “male” when the gender data field is ‘M’ ƒ Otherwise, display “female” o Indent the lines with the drinker data. o Display the BAC to 3 decimal places. Sample for an average metabolism: x Create a new instance method to analyze the current BAC and display results, as described below. o Create three constant for BAC limits ƒ A possible impairment limit (0.02) ƒ A commercial driver legal limit (0.04) ƒ A standard legal limit for all drivers (0.08) o Use a multiple alternative if statement to decide what to tell the user about their impairment ƒ When the BAC is below the possible impairment limit, display it is okay to drive ƒ When the BAC is at or above the possible impairment limit, but below the commercial driver legal limit, display that the user can legally drive but should be cautious because they could be partially impaired. ƒ When the BAC is at or above the commercial driver legal limit, but below standard legal limit, display that the user cannot legally drive any commercial vehicles because they have exceeded the commercial driver legal limit and list the limit value in your output. ƒ When the BAC is at or above the standard driver legal limit, display that the user cannot legally drive ANY vehicle, because they have exceeded the standard driver legal limit and list the limit value in your output. Sample o Then use a nested if statement to give the user more information ƒ When the BAC is at or above the standard legal limit: x Subtract the drinker’s metabolic rate from their current BAC to compute what their BAC level would be in one hour. x If the BAC level in one hour is still at or above the standard legal limit: o Tell the user to find a ride home x Otherwise o Tell the user that the can legally drive in one hour, but should think hard about doing it. Calculations for: male drinker, id JONESXX who is 68 inches tall and weighs 160 pounds after drinking 5 drinks, in 2 hours: Current BAC is approximately 0.103 You may not legally drive ANY vehicle, because you have exceeded the legal limit of 0.080. Sample x After creating the above two instance methods, be sure to delete the old display methods from the BACcalculator class and the Drinker class. 2. Within the original class BACcalculator class that contains the main method: x Modify the main method to: o Still display the program description, as in Java Assignment 4. o Modify what data is read from the user. ƒ Read the drinker’s last name, as in Java Assignment 4. ƒ Remove the code that reads the zip code. ƒ Read the drinker’s height and weight, as in Java Assignment 4. ƒ Add code to read the drinks consumed and hours elapsed. ƒ Read the metabolism type (as shown in the Sample Inputs below): x Display a menu to the user, with letters as choices. x Prompt for the user’s choice, and read the letter chosen by the user. Sample Inputs o Display a couple of lines to separate the input from the output. o Remove the code that creates a blank object and calls setter for it. o Use the new constructor to create an object of the Drinker class type, passing in all necessary arguments. o Display a couple of lines to separate the input from the output. o Use the new constructor to create an object of the Drinker class type, passing in all necessary arguments. o Remove the code that computes the volume distributions (a call to the correct volume distribution should now be in the method to compute the current BAC). o Using the Drinker class object: x Call the method to compute the current BAC. Enter drinker's last name: jones Enter drinker's gender (M for male, F for female): m Enter drinker's height (in whole inches): 68 Enter the drinker's weight (in whole pounds): 160 How many standard American drinks have been consumed: 5 How many whole hours have elapsed since drinking began? 2 Metabolism type choices V - Very slow metabolism S - Slow metabolism A - Average metabolism F - Fast metabolism Please select the drinker's metabolism type: a Even waiting an hour will not be enough. Find a ride home! x Call the method to display the drinker’s data and current BAC. x Call the method to analyze the drinker’s current BAC. WARNING: As with your previous Java programs, the objects, classes, and methods must be implemented exactly as specified above, or you will lose a significant number of points. Coding Standards The program must also follow the Coding Standards from Content section 6.10. You must include the following comments: o Comments at the top of the file, above the main class, describing what the class does ƒ Include tags with the author’s name (i.e. your full name) and the version of the code (e.g. @version 1.0, Java Assn 3) o Comments at the top of each method, above the method header, describing what the method does (only this method – do not refer to actions of any other methods) ƒ Include tags with names and descriptions of each parameter and return value. Delete any default comments supplied by the IDE that you did not use. Debugging and Testing Run, test, and debug your Java program, until you confirm that all of the decision control structures work. Be sure to test your program with different inputs to make sure it provides correct results.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Scanner;

public class Drinker {

private String identifier;
private double weight;
private double height;
private int numberOfDrinksConsumed;
private int numberOfHoursElapsed;
private int weightInlbs;
private int heightInInch;
private char gender;
private double metabolicRate;
private double currentBAC;


public Drinker() {
    identifier = "";
    height = 0.0;
    weight = 0.0;
    numberOfDrinksConsumed = 0;
    numberOfHoursElapsed = 0;
    weightInlbs = 0;
    heightInInch = 0;
    gender = ' ';
    metabolicRate = 0.0;
    currentBAC = 0.0;
}

public Drinker(String lastName, char gen, int weightInlbs, int heightInInch, int consumed, int hoursElapsed, char metabolismType) {
    gender = gen;
    numberOfDrinksConsumed = consumed;
    numberOfHoursElapsed = hoursElapsed;
    currentBAC = 0.0;
    setIdentifier(lastName);
    setHeight(heightInInch);
    setWeight(weightInlbs);
    setMetabolicRate(metabolismType);
}

public void setIdentifier(String lastName) {
    final int IDLength = 7;
    final String ID = "XXXXXXX";
    String IDAsArray[]...

By purchasing this solution you'll be able to access the following files:
Drinker.java, BACCalculator.java and BACCalculatorOUTPUT.png.

$45.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats