Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Practice MT2: Q11 (getCost)

+8 votes

What answer did you get for this one?

(Again, you can use the <> "add code" button to add better-formatted Java code to your Q&A answers.)

asked Jan 25, 2015 in (Winter 14) by Forrest Stonedahl (3,438 points)

1 Answer

–1 vote
public static double getCost(int quantity, double price){
	//initalize discount rate
	double discountRate = 0.0;
	//determine how many items the user has purchased and apply the correct discount
	if (quantity <= 10){
		discountRate = .10;
	} else if (quantity > 11 && quantity <= 50){
		discountRate = .25;
	}else {
		discountRate = .40;
	}
	//calculate the discount amount
	double discountAmount = price * discountRate;
	//calcuate the final price
	double finalPrice = price - discountAmount;
	//return the final price
	return finalPrice;
}
}

 

answered Jan 26, 2015 by Ethan Blatti (100 points)
You could take out quantity  >  11 if the else if statement because it is redundant.
Actually, it goes beyond CJ's point.  What if the quantity parameter is fed in as exactly 11 -- will Ethan's code work properly?
...