The purpose of this is to create a class and a driver that can be used for diffe
ID: 3667454 • Letter: T
Question
The purpose of this is to create a class and a driver that can be used for different applications and have it so you can reuse the class and driver for other programs. Create a class with 2 private attributes where one is a String, one constructor which accepts at least one parameter. Setters and getters are required for each field where applicable. The class will be called the Cheese class where cheeseType is a private String attribute and CheesePrice is a double. The constructor receives parameters for newCheeseType as a String and newCheesePrice as a double. The methods are getCheeseType() returning a String, getCheesePrice() returning a double, setCheeseType() excepting a String parameter and setCheesePrice() accepting a double parameter. The driver should prompt the user for the attributes, instantiate the object in memory using the input and display the attributes from the object using the getters. Thanks a lot! This is my second time posting this because I recieved an answer for this post that was not very helpful. before.
Explanation / Answer
Cheese.Java
package com.chegg.test;
public class Cheese {
private String cheeseType;
private double cheesePrice;
// The constructor receives parameters
Cheese(String type, double price) {
setCheesePrice(price);
setCheeseType(type);
}
/**
* @return the cheeseType
*/
public String getCheeseType() {
return cheeseType;
}
/**
* @param cheeseType
* the cheeseType to set
*/
public void setCheeseType(String cheeseType) {
this.cheeseType = cheeseType;
}
/**
* @return the cheesePrice
*/
public double getCheesePrice() {
return cheesePrice;
}
/**
* @param cheesePrice
* the cheesePrice to set
*/
public void setCheesePrice(double cheesePrice) {
this.cheesePrice = cheesePrice;
}
}
Driver.Java
package com.chegg.test;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String type = "";
double price = 0;
boolean validInput = false;
System.out.println("Enter the CheeseType:");
type = sc.next();
do {
try {
System.out.println("Enter the price:");
price = Double.parseDouble(sc.next());
validInput = true;
} catch (NumberFormatException nfe) {
System.out.println("Enter numerics only.");
validInput = false;
}
} while (!validInput);
Cheese cheese = new Cheese(type, price);
System.out.println("The CheeseType is:" + cheese.getCheeseType());
System.out.println("The Price is:" + cheese.getCheesePrice());
sc.close();
}
}
Output:
Enter the CheeseType:
DoubleToast
Enter the price:
87
The CheeseType is:DoubleToast
The Price is:87.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.