Concept explainers
Do Practice
Define a Beer class that contains the following instance variables with accessors/mutators:
String name; // The name of the beer
double alcohol; // The percent alcohol or the beer, e.g. // 0.05 for 5%
Add the following method:
// This method returns the number of drinks that a person
// of (weight) pounds can drink using the alcohol percentage
// in the beer, assuming a drink of 12 ounces. This is an
// estimate. The method assisted chat the legal limit is 0.08 blood
// alcohol.
public double intoxicated(double weight)
{
double numDrinks;
// This is a simplification of the Widmark formula
numDrinks = (0.08 + 0.015) + weight / (12 + 7.5 + alcohol);
return numDrinks;
}
Write code in a main method that creates two Beer objects with different alcohol percentages. Invoke the intoxicated method for a light individual and a heavy individual and output the estimated number of drinks to become legally intoxicated.
Want to see the full answer?
Check out a sample textbook solutionChapter 6 Solutions
Java: An Introduction To Problem Solving And Programming Plus Mylab Programming With Pearson Etext -- Access Card Package (8th Edition)
Additional Engineering Textbook Solutions
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Problem Solving with C++ (10th Edition)
C How to Program (8th Edition)
Starting out with Visual C# (4th Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
- This composition example contains classes Date, Employee and EmployeeTest .Class Date declares instance variables month, day and year to represent a date. The constructor receives three int parameters. It has method checkMonth to validate the month—if the value is out-of-range the method throws an error message. same goes for year and day. Provide getters, setters and toString method. Class Employee has instance variables firstName, lastName, birthDate and hireDate.Members firstName and lastName are references to String objects. Members birthDate and hireDate are references to Date objects. This demonstrates that a class can have as instance variables references to objects of other classes. The Employee constructor takes four parameters—first, last, dateOfBirth and dateOfHire. The objects referenced by the parameters are assigned to the Employee object’s instance variables. When class Employee’s toString method is called, it returns a String containing the employee’s name and the…arrow_forwardThis is the first exercise for the TODO comments in the main method. Complete the actions described in these comment lines in the main method:// TODO: Add a public static constant called PRIMARY_NAME// to the Student class. Set the name of the Primary to// a real University. In other words: Add a public static final String variable called PRIMARY_NAME to the fields of the Student class. Initialise the variable in the same line it is declared (not in the constructor) to the name of a real Primary. Add code to the Main class in this project to access the PRIMARY_NAME field and print it out. Remember that, because the field is public, it can be accessed directly from another class without having to provide a getter method for it. In the lecture discussing static fields and methods, we showed that the nextID field in the Student class is used to store the value of the next…arrow_forwardProblem 2. Write a java programming language that uses constructor and methods in the TV class as defined public so they can be accessed from other classes. Note: The channel and volume level are not changed if the TV is not on. Before either of these is changed, its current value is checked to ensure that it is within the correct range. Listing 4 gives a program that uses the TV class to create two objects. Set Channel to 40 and Set Volume t0 5 Find out TV1 Channel and Volume, and Find out TV2 Channel and Volume? Filename: Encapsulation2.javaarrow_forward
- You are asked to write a discount system for a beauty saloon, which provides services and sells beauty products. It offers 3 types of memberships: Premium, Gold, and Silver. Premium, gold and silver members receive a discount of 20%, 15%, and 10%, respectively, for all services provided. Customers without membership receive no discount. All members receives a flat 10% discount on products purchased (this might change in the future). Your system shall consist of three classes: Customer, DiscountRate and Visit, as shown in the class diagram. It shall compute the total bill if a customer purchases $x of products and $y of services, for a visit. Also, write a test program VisitDriver (are not to be graded and optional) to exercise all the classes. Important: "Visit Class diagram - ERROR" Constructor method of Visit class must recieve Customer object instead of String name. Visit(Customer customer, Date date) The class DiscountRate contains only static variables and methods (underlined in…arrow_forwardYou are asked to write a discount system for a beauty saloon, which provides services and sells beauty products. It offers 3 types of memberships: Premium, Gold, and Silver. Premium, gold and silver members receive a discount of 20%, 15%, and 10%, respectively, for all services provided. Customers without membership receive no discount. All members receives a flat 10% discount on products purchased (this might change in the future). Your system shall consist of three classes: Customer, DiscountRate and Visit, as shown in the class diagram. It shall compute the total bill if a customer purchases $x of products and $y of services, for a visit. Also, write a test program VisitDriver (are not to be graded and optional) to exercise all the classes. Important: "Visit Class diagram - ERROR" Constructor method of Visit class must recieve Customer object instead of String name. Visit(Customer customer, Date date) The class DiscountRate contains only static variables and methods (underlined in…arrow_forwardI just want you to do the second part only using java class, but i have attached the answer for assignment 1. Assignment 1 question Loan Account Class: Create class LoanAccount. Use a static variable annualInterestRate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable principal indicating the amount the person is borrowing. Provide method: public double calculateMonthlyPayment(int numberOfPayments) to calculate the monthly payment by using the following formula: double monthlyPayment = principal * ( monthlyInterest / (1 - Math.pow(1 + monthlyInterest, -numberOfPayments))); where monthly interest = annualInterestRate/12. Provide a static method setAnnualInterestRate that sets the annualInterestRate to a new value. Set the initial loan amount (Principal) for a new loan through the constructor. Write a program to test class LoanAccount. Instantiate two LoanAccount objects, loan1 and loan2, with principal loan…arrow_forward
- Question: 2Implement the design of the Chillox and Customer classes so that the following code generates the output below:print(f"It's been a good day!. \nToday, we have already served {Chillox.order_id} customers.")print("**************************************************")outlet1 = Chillox()print("1.==========================================")print("Burger Menu:")print(Chillox.menu)print("2.==========================================")outlet1.addBurger("Chicken Burger",180,"Chicken with Cheese",200)print("3.==========================================")print("Burger Menu:")print(Chillox.menu)print("4.==========================================")c1 = Customer("Samin")c1.order = "2xChicken Burger"outlet1.placeOrderof(c1)print("5.==========================================")outlet1.addBurger('Chicken Smoky BBQ Cheese',225)c2 = Customer("Rafi")c2.setOrder("2xChicken with Cheese,3xChicken Smoky BBQ…arrow_forward1) Write a class called Room, which has three private instance variables: a) a double width, representing the width of the room in feet, b) a double length, representing the length of the room in feet, and c) an int floor, representing the building floor that the room is on. 2) Write a default constructor for the class Room that sets the width to 10, the length to 12.5, and the floor to 1. 3) Write get and set methods (“getters” and “setters”) for the three instance variables. For the set methods for the width and length, only positive values should be set. If the input is 0 or a negative number, the variables should not be changed. 4) Write a constructor for the class Room that takes in two double parameters and an int, and sets length to the larger double, width to the smaller double, and floor to the int. Use the setters from part (b). 5) Override the default toString method for the class Room, so when called by an instance of Room created by the default constructor from part (a)…arrow_forwardDefine a class named Doctor whose objects are records for a clinic’s doctors. Derive this class from the class Person given in Listing 8.1. A Doctor record has the doctor’s name—defined in the class Person—a specialty as a string (for example Pediatrician, Obstetrician, General Practitioner, and so on), and an office-visit fee (use the type double). Give your class a reasonable complement of constructors and accessor methods, and an equals method as well. Write a driver program to test all yourmethods.arrow_forward
- 1. Making a class called Day. Two of its variables are month and year. The best type for the year would be? it will be Int. Can you explained it! 2. While unit testing, what is the last step before running the test? The Answer is Implement your class or methods. Please explain! 3. With respect to unit testing, what would it mean for your code to have 100% coverage? This means All class constructors and class methods have tests. Please explainarrow_forwardCould you tell me how to fix the error in testing the following code: class Employee:""" The employee class has a constructor and getter methods.The constructor has input arguments"""# Assigns values for id, name, salary, and email from input argumentsdef __init__(self, i, n, s, e):self.id = iself.name = nself.sal = sself.email = e# getter function for namedef get_name(self):return self.name# getter function for IDdef get_id(self):return self.id# getter function for salarydef get_sal(self):return self.sal# getter function for emaildef get_email(self):return self.emaildef make_employee_dict(ids, names, sal, emails):"""Function to create employee object and the dictionaryusing those objects"""# k is the length of all the listsk = len(ids)emp_dict = dict()# Making employee object and adding to dictionaryfor i in range(k):e = Employee(ids[i], names[i], sal[i], emails[i])emp_dict[ids[i]] = e# return the dictionaryreturn emp_dict# Testingemp_names = ["Jean", "Kat", "Pomona"]emp_ids = ["100",…arrow_forwardConsider the following Student Class An. EngineeringStudent "is-a" Student whose major is Engineering. In additional to the name, number of credit hours, and GPA, an Engineering electrical, computer, etc. Write the complete defoe the Engineeringstudent class including the implementation of the methods.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning