Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

DONT KNOW WHY THIS CODE IS GIVING ME NEGATIVE TAX RESULT i.e (-1666666.999999)PL

ID: 3684308 • Letter: D

Question

DONT KNOW WHY THIS CODE IS GIVING ME NEGATIVE TAX RESULT i.e (-1666666.999999)PLEASE HELP, DUE IN 2HRS

class RentalItem{

private String itemId;

private String type;

private double baseFee;

private double hourPrice;

private Date rentalStartDate;

private String clientName;

public final double TAXES_RATE = 0.07;

public RentalItem(String itemId, String type) {

this.itemId = itemId;

if(type.equalsIgnoreCase("bike")||type.equalsIgnoreCase("kayak")||type.equalsIgnoreCase("segway"))

this.type = type;

else

this.type = "bike";

this.rentalStartDate = null;

this.clientName = null;

}

public RentalItem(String itemId, String type, double baseFee, double hourPrice) {

this.itemId = itemId;

if(type.equalsIgnoreCase("bike")||type.equalsIgnoreCase("kayak")||type.equalsIgnoreCase("segway"))

this.type = type;

else

this.type = "bike";

this.baseFee = baseFee;

this.hourPrice = hourPrice;

this.rentalStartDate = null;

this.clientName = null;

}

public void rent(String clientName){

setClientName(clientName);

setRentalStartDate(new Date());

}

public boolean available(){

if(getRentalStartDate()== null){

return true;

} else { return false;

}

}

public Receipt returnItem(){

Date now = new Date();

long hours = (getRentalStartDate().getTime()-now.getTime());

double subTotal = getBaseFee() + (getHourPrice() * hours);

double taxes = TAXES_RATE * subTotal;

Receipt receipt = new Receipt(itemId, type, baseFee, hourPrice, rentalStartDate, clientName, subTotal, taxes);

rentalStartDate = null;

clientName = null;

return receipt;

}

public String getItemId() {

return itemId;

}

public void setItemId(String itemId) {

this.itemId = itemId;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public double getBaseFee() {

return baseFee;

}

public void setBaseFee(double baseFee) {

this.baseFee = baseFee;

}

public double getHourPrice() {

return hourPrice;

}

public void setHourPrice(double hourPrice) {

this.hourPrice = hourPrice;

}

public Date getRentalStartDate() {

return rentalStartDate;

}

public void setRentalStartDate(Date rentalStartDate) {

this.rentalStartDate = rentalStartDate;

}

public String getClientName() {

return clientName;

}

public void setClientName(String clientName) {

this.clientName = clientName;

}

}

Explanation / Answer

Hey. To run this code completely, I also need both the Receipt class and the Date class. But anyways, the fault with your code seems to be nothing. And the probable reason you are getting a negative tax result is because, you didn't initialized the baseFee, and hourPrice in the first constructor. Therefore, if you create an object with first constructor, where you only initialized itemId, and itemType, and therefore, if you call the returnItem() method with that object, before initializing the baseFee, and hourPrice, there the possibility is when you initialize the subTotal variable, using the statement,

double subTotal = getBaseFee() + (getHourPrice() * hours);

And further when you call the getBaseFee, it will try to extract the baseFee and hourPrice, which are not initialized by that respective constructor, and therefore may return some garbage value. And once the subTotal is initialized with some garbage value(may be negative), and obviously the subsequent variable initialization in the statement

double taxes = TAXES_RATE * subTotal; will also lead to garbage value.

You have 2 solutions to this problem:

1. You can initialize the variable baseFee and hourPrice to some default value in that constructor. You can do something like this:

public RentalItem(String itemId, String type)
{
this.itemId = itemId;
if(type.equalsIgnoreCase("bike")||type.equalsIgnoreCase("kayak")||type.equalsIgnoreCase("segway"))
this.type = type;
else
this.type = "bike";
this.rentalStartDate = null;
this.clientName = null;
this.baseFee = 0.0;
this.hourPrice = 0.0;
}

And therefore, if you didn't specify either of the baseFee or hourPrice, and therefore the tax will be 0, and not negative.

2. Second option is make sure to initialize both the variable by calling the methods.

object.setBaseFee(fee);

object.setHourPrice(price);

before calling the returnItem() method. This will ensure that the values are entered by use, to calculate or will default to 0.

If you need any further refinements, just get back to me.