Explanation of Solution
a.
Method heading for each method:
- • Method heading for read rating from user is “public void readRating()”.
- • Method heading for gets maximum rating is “public int getMaximumRating()”...
Explanation of Solution
b.
Preconditions and postconditions of each method:
- • Precondition and postcondition of “public void readRating()” method.
- ○ Precondition: Display maximum rating range for given description using object of “RatingScore” class.
- ○ Postcondition: The “the_rating” is assigned to “value”.
- • Precondition and postcondition of “getMaximumRating()” method.
- ○ Precondition: None...
Explanation of Solution
c.
Test class for some Java statement:
//Create object "movieScore" from "RatingScore" class
RatingScore movieScore = new RatingScore();
//Create object "restaurantScore" from "RatingScore" class
RatingScore restaurantScore = new RatingScore();
//Call "setRatingScore" method for object "movieScore"
movieScore.setRatingScore("Merry's excellent movie", 8);
//Call "setRatingScore" method for object "restaurantScore"
restaurantScore.setRatingScore("Food quality", 20);
//Read rating for object "movieScore" by calling the method "readRating".
movieScore.readRating();
//Display the given statement
System...
Explanation of Solution
d.
Implementation of class:
RatingScore.java:
//Import package
import java.util.Scanner;
//Define a class "RatingScore"
public class RatingScore
{
//Declare required instance variable
private String desc;
private int maximum_rating;
private int the_rating;
//Set values to "RatingScore" attributes
public void setRatingScore(String d, int m)
{
desc = d;
maximum_rating = m;
the_rating = -1;
}
//Method definition for read rating from user
public void readRating()
{
//Display given statement for description
System.out.println("Enter rating for " + desc + ":");
//Display given maximum rating
System.out.println("Please enter an integer from 0 to " + maximum_rating);
//Create object for Scanner class
Scanner r = new Scanner(System.in);
//Assign "value" to "-1"
int value = -1;
//Assign required rating to "true"
boolean requiredRating = true;
//Check rating condition using "while" loop
while(requiredRating)
{
//Read input from user
value = r.nextInt();
if(value >= 0 && value <= maximum_rating)
{
requiredRating = false;
}
else
{
System.out.println("Error: The given rating is out of range.");
System.out.println("Enter an integer from 0 to " + maximum_rating);
}
}
the_rating = value;
}
//Method definition for gets maximum rating
public int getMaximumRating()
{
//Returns maximum rating
return maximum_rating;
}
//Method definition for gets rating
public int getRatingValue()
{
Want to see the full answer?
Check out a sample textbook solutionChapter 5 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Write a Java Program, Create a class with a method that prints "This is parent class" and its subclass with another method that prints "This is child class". Now, create an object for each of the class and call 1 - method of parent class by object of parent class 2 - method of child class by object of child class 3 - method of parent class by object of child class In the above example, declare the method of the parent class as private and then repeat the first two operations (You will get error in the third).arrow_forwardIn Java please. Implement a class that simulates a traffic light. The next method advances the color in the usual way, from green to yellow to red, then again to green. Provide two constructors, as described in the documentation of the public interface. Also supply a method that yields the number of times that this traffic light has been red. Help me with this code (fill in /* Your code goes here */): /** A simulated traffic light.*/public class TrafficLight{ private String color; private int reds; /** Constructs a green traffic light. */ public TrafficLight() { /* Your code goes here */ } /** Constructs a traffic light. @param initialColor the initial color "green", "yellow", or "red" */ public TrafficLight(String initialColor) { /* Your code goes here */ } /** Moves this traffic light to the next color. */ public void next() { /* Your code goes here */ } /** Returns the current color of this traffic…arrow_forwardWrite a java program in a class CountPoor that counts the number of families that are considered poor. Write and use a class Family that has the attributes. income—a double value that is the income for the family size—the number of people in the family and the following methods: Family(income, size)—a constructor that sets the attributes isPoor(housingCost, foodCost)—a method that returns true if housingCost + foodCost * size is greater than half the family income (foodCost is the average food cost for an individual, while housingCost is for the family) toString—a method that returns a string containing the information about the family The program should read an integer k from the keyboard and then create an array of size k whose base type is Family. It should then create k objects of type Family and put them in the array, reading the income…arrow_forward
- Write a Java Program , Create a class named 'Shape' with a method to print "This is This is shape". Then create two other classes named 'Rectangle', 'Circle' inheriting the Shape class, both having a method to print "This is rectangular shape" and "This is circular shape" respectively. Create a subclass 'Square' of 'Rectangle' having a method to print "Square is a rectangle". Now call the method of 'Shape' and 'Rectangle' class by the object of 'Square' class.arrow_forwarduse java Implement a class Car with the following properties. A car has a certain fuel efficiency(measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gastank. The efficiency is specified in the constructor, and the initial fuel level is 0.Supply a method drive that simulates driving the car for a certain distance, reducing theamount of gasoline in the fuel tank. Also supply methods getGasInTank, returning thecurrent amount of gasoline in the fuel tank, and addGas, to add gasoline to the fuel tank.Sample usage:Car myHybrid = new Car(50); // 50 miles per gallonmyHybrid.addGas(20); // Tank 20 gallonsmyHybrid.drive(100); // Drive 100 milesdouble gasLeft = myHybrid.getGasInTank(); // Get gas remaining in tankYou may assume that the drive method is never called with a distance that consumes morethan the available gas. Supply a CarTester class that tests all methodsarrow_forwardWrite a Java program with comments that explain your block of code. Use appropriate names for classes, methods, and variables. 1) A method to calculate and return the average time taken completing assessment for eachmodule.arrow_forward
- Write a ThreeWayLamp class that models the behavior of a lamp that uses a three-way bulb.These bulbs have actually four possible states: off, low light, medium light, and high light. Each tim the switch is activated, the bulb goes to the next state (from high, the next state is off, from off to low etc). The ThreeWayLamp class has a single method called switch() which takes a single int parameter indicating how many times the switch is activated. (you need to throw an exception if its negative). The switch() method should simply print out a message indicating the state of the bulb after it has changed Only in c++ languagearrow_forwardWrite a ThreeWayLamp class that models the behavior of a lamp that uses a three-way bulb. These bulbs have actually four possible states: off, low light, medium light, and high light. Each tim the switch is activated, the bulb goes to the next state (from high, the next state is off, from off to low etc). The ThreeWayLamp class has a single method called switch() which takes a single int parameter indicating how many times the switch is activated. (you need to throw an exception if its negative). The switch() method should simply print out a message indicating the state of the bulb after it has changed.arrow_forwardWrite method headers for methods with the following descriptions using java code Printing the calendar for a given month and year Computing the weekday for a given day, month, and year (as a string such as "Monday") Generating a random integer between 1 and narrow_forward
- Write Java code for a Baby class. A Baby has a name of type String and an age of type integer.Supply two constructors: one will be the default constructor, that just setsdefault values for the name and age; the second constructor will take twoparameters, a string to set the name and an integer to set the age. Also, supply methods for setting the name, setting the age, getting the name and getting the age.The class should not contain I/O methods; input of values to the instancevariables must be done with a set method or constructor, output of values from the instance variables must be done with get methods. The set method for the name instance variable should ensure that the input is not empty or contain whitespaces (otherwise set a default value). The set method for the age instance variable should validate the input to be between 1 and 4 inclusive(otherwise set a default value).Give Java code for an equals method for the Baby class. Babies count asbeing the same (i.e. equal) if their…arrow_forwardUse Java program to add a method printReceipt to the CashRegister class. Then create a driver class that asks the user to input the 5 items that they have bought recently and also the prices for each item, and print the items, their prices, and the total amount due. Hint: You will need to form a string of all prices. Use the concat method of the String class to add additional items to that string. To turn a price into a string, use the call String.valueOf(price). Also, the payment information will need to be the same as the price information. Here is the CashRegister class: /** A cash register totals up sales and computes change due.*/public class CashRegister{ private double purchase; private double payment; /** Constructs a cash register with no money in it. */ public CashRegister() { purchase = 0; payment = 0; } /** Records the sale of an item. @param amount the price of the item */ public void recordPurchase(double amount) {…arrow_forwardWrite a class in java with the name AgeMessages , which does the following:a. Ask the user to enter age using a Scannerb. If the age is less than 13, print: “too young to create a Facebook account”c. If the age is less than 16, print: “too young to get a driver's license”d. If the age is less than 18, print: “too young to get a tattoo”e. If the age is less than 21, print: “too young to drink alcohol”f. If the age is less than 35, print: “too young to run for President of the U.S.” and on nextline print: “(How sad!)”g. If the age is greater and equal to 35, print: “able to do anything” and on next line print:“(How happy!)”Tips Use only an if statementCopy and paste your code Screen shot the console with user input of 2 Screen shot the console with user input of 18 Screen shot the console with user input of 91arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education