Starting from:

$30

CSE 110 - Lab 6

CSE 110 - Lab 6
Lab Topics
● Reusable methods
● Variable scopes
● Overloading
Lab Problem: Compute After-tax Prices
In the United States, each state has a slightly different sales tax rate paid to the local government.
Price tags you see in a store do not necessarily include after-tax prices which may affect your
purchase decisions. In this lab, we are going to create a program that calculates after-tax prices for
common stuff most college students have in their daily lives and see whether you may save some
money if you buy them in another state.
Part 1. Reusable Methods
We have learned in a previous lab about how to get an input from a user and check whether the input
is valid or not. If you remember the code we had, you may find the code for this process is a little
lengthy. If we need to do the same thing multiple times, your program will become very complicated
due to a lot of duplicate code with little difference. To keep the readability and maintainability of a
computer program, it’s common to use a method to wrap up a certain code logic and then reuse it
without repeating the same code many times. In this lab, we are going to design a method
“readRetailPrice” which takes a product name and a Scanner and then asks the user for the retail price
of the product. Specifically, the method has the following signature:
1. private static double readRetailPrice(String name, Scanner
scan)
This method should also validate the user input by checking whether the input is a non-negative
number. If not, the method should prompt the user to input again. See the sample output (#3) for
detail. Once you finish this method, you have to use it to ask the user to input the retail prices of their
laptop, mobile phone, and backpack.
Part 2. Method Overloading
“Method overloading” refers to multiple methods with the same method name but different
parameters. This feature allows you to create a flexible method which automatically chooses what to
do according to the types of parameters passed to it. In this lab, when we calculate an after-tax price,
we have two cases: 1) the after-tax price in a given state and 2) the after-price in AZ. To do this, we
can create two “addTax” methods where one uses parameters (double price, double rate), which
calculates the after-tax price according to the given rate, and the other only uses (double price), which
internally calculates the after-tax price by the sales tax rate in AZ. This design provides some
flexibility to programmers by making certain methods have “default” parameters, which can be very
useful for code management.
In this part, your job is to create those two “addTax” with different parameter lists, specifically, you
need to define the following to methods which return after-tax prices:
1. private static double addTax(double price, double rate)
2. private static double addTax(double price)
You can find how these two methods are used in the template file. These two methods have to be used
effectively in your main method. If you do not use them anywhere in your program, you will lose
points for code logic.
Grading Policy
● The requirements of code logic (up to 4pt):
○ -2 if the user input process is not wrapped in the method readRetailPrice
○ -2 if the method readRetailPrice is not reused
○ -2 if the program gets input from the user without using readRetailPrice
○ -2 if the three methods are not used anywhere in the program;
○ -2 if the two addTax methods do not return any meaningful values;
○ -2 if the two addTax methods are not overloaded properly;
○ -2 if the method addTax with one parameter does not reuse the method addTax with
two parameters
● A non-functional program will receive no points. In other words, your program should be
free from syntax errors and pass compilation. A non-functional program will not receive any
point of lab work.
Sample Output
Below is an example of what your output should roughly look like when this lab is completed. The
RED texts are user inputs.
Sample Input #1
What's the retail price of your laptop
1299
What's the retail price of your mobile phone
349
What's the retail price of your backpack
49
Enter one state you'd like to compare after-tax prices:
UT
The total after-tax price in AZ: 1839.55
The total after-tax price in UT: 1818.84
You may save 20.70 for those stuff in UT.
Sample Input #2
What's the retail price of your laptop
1899
What's the retail price of your mobile phone
999
What's the retail price of your backpack
199
Enter one state you'd like to compare after-tax prices:
OR
The total after-tax price in AZ: 3357.15
The total after-tax price in OR: 3097.00
You may save 260.15 for those stuff in OR.
Sample Input #3
What's the retail price of your laptop
-189
[ERR] a price must be non-negative. Please type it again.
What's the retail price of your laptop
1899
What's the retail price of your mobile phone
-699
[ERR] a price must be non-negative. Please type it again.
What's the retail price of your mobile phone
699
What's the retail price of your backpack
-1
[ERR] a price must be non-negative. Please type it again.
What's the retail price of your backpack
99
Enter one state you'd like to compare after-tax prices:
CO
The total after-tax price in AZ: 2923.55
The total after-tax price in CO: 2903.32
You may save 20.23 for those stuff in CO.

More products