public class TaxComputer { private static double basicRate = 4.0; private static
ID: 3633302 • Letter: P
Question
public class TaxComputer {private static double basicRate = 4.0;
private static double luxuryRate = 10.0;
1. Create a class that will bundle together several static methods for tax computations. This class should not have a constructor. Its attributes are
• basicRate—the basic tax rate as a static double variable that starts at 4 percent - a private static method
• luxuryRate—the luxury tax rate as a static double variable that starts at 10
percent - a private static method
Its methods are
• computeCostBasic(price) —a static method that returns the given price
plus the basic tax, rounded to the nearest penny.
• computeCostLuxury(price) —a static method that returns the given price
plus the luxury tax, rounded to the nearest penny.
• changeBasicRateTo(newRate) —a static method that changes the basic tax
rate.
• changeLuxuryRateTo(newRate) —a static method that changes the luxury
tax rate.
• roundToNearestPenny(price)—a private static method that returns the
given price rounded to the nearest penny. For example, if the price is 12.567, the method will return 12.57.
Explanation / Answer
import java.text.DecimalFormat;
public class TaxComputer
{
private static double basicRate = 4.0;
private static double luxuryRate = 10.0;
public static double computeCostBasic(double price)
{
return roundToNearestPenny(price + ((basicRate/100) * price));
}
public static double computeCostLuxury(double price)
{
return roundToNearestPenny(price + ((luxuryRate/100) * price));
}
public static void changeBasicRateTo(double newRate)
{
basicRate = newRate;
}
public static void changeLuxuryRateTo(double newRate)
{
luxuryRate = newRate;
}
private static double roundToNearestPenny(double price)
{
DecimalFormat CurrencyFormat = new DecimalFormat("#.##");
return Double.valueOf(CurrencyFormat.format(price));
}
//TestMethod for the static classes
public static void main(String[] args)
{
System.out.println("" + computeCostBasic(1232.2312));
System.out.println("" + computeCostLuxury(1232.2312));
changeBasicRateTo(6.0);
changeLuxuryRateTo(12.0);
System.out.println("" + computeCostBasic(1232.2312));
System.out.println("" + computeCostLuxury(1232.2312));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.