THIS IS JAVA public class Grocery { //Instance Variables private double item1; p
ID: 3733247 • Letter: T
Question
THIS IS JAVA
public class Grocery {
//Instance Variables
private double item1;
private double item2;
//Default Constructor
public Grocery() {
this.item1 = 0.0;
this.item2 = 0.0;
}
//Constructor w/ parameters
public Grocery(double value1, double value2) {
this.setItem1(value1);
this.setItem2(value2);
}
//Getter and Setter methods
public double getItem1() {
return this.item1;
}
public void setItem1(double value1) {
this.item1 = value1;
}
public double getItem2() {
return this.item2;
}
public void setItem2(double value2) {
this.item2 = value2;
}
public double compute() {
double result = Math.abs(item1 * Math.sqrt(item2));
return result;
}
}
What is worng with my class? I keep getting an error.
Explanation / Answer
Yeah. If you give negative value for item2 then Math.sqrt(item2) throws an error.
I edited the code to convert the item2 to its absolute value.
//Grocery.java
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.