Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
Question
Book Icon
Chapter 6, Problem 1E
Program Plan Intro

Tax computation

Program plan:

  • • Define the class “TaxComputer”.
    • ○ Declare the necessary constant variables.
    • ○ Define the method “changeBasicRateTo()”.
      • ■ Assign “newRate” into the variable “basicRate”.
    • ○ Define the method “changeLuxuryRateTo()”.
      • ■ Assign “newRate” into the variable “luxuryRate”.
    • ○ Define the method “computeCostBasic()”.
      • ■ Calculate “tax”, and “price”.
      • ■ Return the value.
    • ○ Define the method “computeCostLuxury()”.
      • ■ Calculate “tax”, and “price”.
      • ■ Return the value.
    • ○ Define the method “roundToNearestPenny()”.
      • ■ Calculate the price.
      • ■ Return the value.
    • ○ Define the “main()” function.
      • ■ Print the result by calling the appropriate methods.

Expert Solution & Answer
Check Mark
Program Description Answer

Program to display the tax computation of the computer.

Explanation of Solution

Program:

//Define the class

public class TaxComputer

{

    //Declare the necessary constant variables

    private static double basicRate = 4.0;

    private static double luxuryRate = 10.0;

    //Define the function changeBasicRateTo()

    public static void changeBasicRateTo(double newRate)

    {

/*Assign "newRate" into the variable “basicRate”*/

        basicRate = newRate;

    }

    //Define the function changeLuxuryRateTo()

    public static void changeLuxuryRateTo(double newRate)

    {

/*Assign "newRate" into the variable “luxuryRate”*/

        luxuryRate = newRate;

    }

    //Define the function computeCostBasic()

    public static double computeCostBasic(double price)

    {

        //Calculate the “tax”

        double tax = price * basicRate / 100;

        //Calculate the “price”

        price = price + tax;

        //Return the value

        return roundToNearestPenny(price);

    }

    //Define the function computeCostLuxury()

    public static double computeCostLuxury(double price)

    {

        //Calculate the "tax"

        double tax = price * luxuryRate / 100;

        //Calculate the "price"

        price = price + tax;

        //Return the value

        return roundToNearestPenny(price);

    }

    //Define the function roundToNearestPenny()

private static double roundToNearestPenny(double price)

    {

        //Calculate the "price"

        price = price * 100;

        price = java.lang.Math.round(price);

        //Return the value

        return price/100;

    }

    //Define the main() function

    public static void main(String[] args)

    {

        //Print the statement

        System.out.println("Testing the basic rate computation.");

        //Print the statement

        System.out.println(" Item price no tax:10.00");

        //Print the statement

    System.out.println("cost with 4% tax: "+ TaxComputer.computeCostBasic(10.00));

        //Print the statement

        System.out.println("Testing the basic rate computation.");

        //Call the function changeBasicRateTo()

        TaxComputer.changeBasicRateTo(7.5);

        //Print the statement

        System.out.println(" Item price no tax: 10.00");

        //Print the statement

        System.out.println("cost with 7.5% tax: "+ TaxComputer.computeCostBasic(10.00));

        //Print the statement

        System.out.println("Testing the luxury rate computation.");

        //Print the statement

        System.out.println(" Item price no tax: 2019.25");

        //Print the statement

        System.out.println("cost with 10% tax: "+ TaxComputer.computeCostLuxury(2019.25));

        //Print the statement

        System.out.println("Testing the luxury rate computation.");

        //Call the function changeLuxuryRateTo()

        TaxComputer.changeLuxuryRateTo(20.0);

        //Print the statement

        System.out.println(" Item price no tax: 2019.25");

        //Print the statement

        System.out.println("cost with 20% tax: "+ TaxComputer.computeCostLuxury(2019.25));

        //Print the statement

        System.out.println("Testing the basic rate again, should still be 7.5%.");

        //Print the statement

        System.out.println(" Item price no tax: 210.99");

        //Print the statement

        System.out.println("cost with 7.5% tax: "+ TaxComputer.computeCostBasic(210.99));

    }

}

Sample Output

Output:

Testing the basic rate computation.

 Item price no tax: 10.00

cost with 4% tax: 10.4

Testing the basic rate computation.

 Item price no tax: 10.00

cost with 7.5% tax: 10.75

Testing the luxury rate computation.

 Item price no tax: 2019.25

cost with 10% tax: 2221.18

Testing the luxury rate computation.

 Item price no tax: 2019.25

cost with 20% tax: 2423.1

Testing the basic rate again, should still be 7.5%.

 Item price no tax: 210.99

cost with 7.5% tax: 226.81

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Java - Access Specifiers  Create a class named Circle that has attributes radius, area, and circumference and make the attributes private. Make a public method that sets the radius and a method that prints all attributes. Ask the user input for radius. Note: Use the PI from the math functions Inputs A line containing an integer 10 Sample Output Radius: 20   Area: 1256.64 Circumference: 125.66
Code In Java NetBeans   Q: Create an Inventory system program which has the following classes: A) . A Date class that includes three instance variables, a month, a day, and an year. Provide a constructor that initializes the three instance variables, validating the values. Provide a toString method that displays the day, month, year separated by the forward slash(/).
Create an Employee Class that will have ● Two instance variable: name and workingPeriod ● A class method named employeeByJoiningYear(): o To create an Employee object by joining year for calculating the working period o It will have two Parameter name and year ● A static method experienceCheck() to check if an Employee is experienced or not o It will take working period and gender as parameter o If an employee’s working period is less than 3, he or she is not experienced [You are not allowed to change the code below] # Write your code here employee1 = Employee('Dororo', 3) employee2 = Employee.employeeByJoiningYear('Harry', 2016) print(employee1.workingPeriod) print(employee2.workingPeriod) print(employee1.name) print(employee2.name) print(Employee.experienceCheck(2, "male")) print(Employee.experienceCheck(3, "female")) Output 3 5 Dororo Harry He is not experienced She is experienced

Chapter 6 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Ch. 6.2 - Can you reference an instance variable by name...Ch. 6.2 - Is the following valid, given the class...Ch. 6.2 - Prob. 13STQCh. 6.2 - Prob. 14STQCh. 6.2 - Prob. 15STQCh. 6.2 - Is the following valid, given the class...Ch. 6.2 - What values are returned by each of the following?...Ch. 6.2 - Suppose that speed is a variable of type double...Ch. 6.2 - Repeat the previous question, but instead assign...Ch. 6.2 - Suppose that nl is of type int and n2 is of type...Ch. 6.2 - Define a class CircleCalculator that hat only two...Ch. 6.2 - Which of the following statements are legal?...Ch. 6.2 - Write a Java expression to convert the number in...Ch. 6.2 - Consider the variable 5 of type String that...Ch. 6.2 - Repeat the previous question, but accommodate a...Ch. 6.2 - Write Java code to display the largest and...Ch. 6.3 - Prob. 27STQCh. 6.3 - Consider the variable allCents in the method...Ch. 6.3 - What is wrong with a program that starts as...Ch. 6.3 - Prob. 30STQCh. 6.3 - In your definition of the class OutputFormat. In...Ch. 6.4 - Prob. 32STQCh. 6.4 - Prob. 33STQCh. 6.4 - Prob. 34STQCh. 6.4 - Consider the class Species in Listing 5.19 of...Ch. 6.4 - Repeat the previous question for a method...Ch. 6.4 - Still considering the class Species in Listing...Ch. 6.4 - Rewrite the method add in Listing 6.16 so that it...Ch. 6.4 - In Listing 6.16, the set method that has a String...Ch. 6.5 - Give the definitions of three accessor methods...Ch. 6.6 - If cardSuit is an instance of Suit and is assigned...Ch. 6.7 - Suppose you want to use classes in the package...Ch. 6.7 - Prob. 43STQCh. 6.7 - Can a package have any name you might want, or are...Ch. 6.7 - On your system, place the class Pet (Listing 6.1)...Ch. 6.8 - The previous section showed you how to change the...Ch. 6 - Prob. 1ECh. 6 - Prob. 2ECh. 6 - Write a default constructor and a second...Ch. 6 - Write a constructor for the class...Ch. 6 - Consider a class characteristic that will be used...Ch. 6 - Create a class RoomOccupancy that can be used to...Ch. 6 - Write a program that tests the class RoomOccupancy...Ch. 6 - Sometimes we would like a class that has just a...Ch. 6 - Create a program that tests the class Merlin...Ch. 6 - In the previous chapter, Self-Test Question 16...Ch. 6 - Create a class Android whose objects have unique...Ch. 6 - Prob. 12ECh. 6 - Modify the definition of the class Species in...Ch. 6 - Prob. 2PCh. 6 - Using the class Pet from Listing 6.1, write a...Ch. 6 - Do Practice Program 4 from Chapter 5 except define...Ch. 6 - The following class displays a disclaimer every...Ch. 6 - Do Practice Program 5 from Chapter 5 but add a...Ch. 6 - We can improve the Beer class from the previous...Ch. 6 - Define a utility class for displaying values of...Ch. 6 - Write a new class TruncatedDollarFormat that is...Ch. 6 - Complete and fully test the class Time that...Ch. 6 - Complete and fully test the class Characteristic...Ch. 6 - Write a Java enumeration LetterGrade that...Ch. 6 - Complete and fully test the class Per n that...Ch. 6 - Write a Temperature class that represents...Ch. 6 - Repeat Programming Project 8 of the previous...Ch. 6 - Write and fully test a class that represents...Ch. 6 - Write a program that will record the votes for one...Ch. 6 - Repeat Programming Project 10 from Chapter 5, but...Ch. 6 - Create a JavaFX application that displays a button...
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
  • Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT