Homework 3

Due: Sep 28, prior to 11:59:59 PM

In this homework you will Note that in this homework, as in the past, you will be getting input from the command line. As a result, you cannot use the very convenient "run" buttons in VSC.

Setup

Start by replicating much of HW 1 and HW 2 setup with the following exceptions

Part 1 -- Better Time

In Programming Assignment #2, you wrote a program called “Time” that read a runtime argument (program input) representing a number of minutes, and then printed out the corresponding time as hours and minutes, e.g. java Time 127 produced “2 hours and 7 minutes”.

In this part of the assignment, you will modify your "Time" program so that it addresses various cases that may come up in performing this conversion and displaying the result.

Start by making a copy of the time program. Recall that to copy a file you use the Unix cp command. Assuming that you have been using the suggested names, the following commands should do the trick:

            cd 
            cd CS113 
            cp HW2/Time.java HW3/Time.java
        

You modified program should cover the following cases:
Case #1: Negative input
Modify the program so that, if the program input is a negative number or zero, then the program should display an error message and exit, e.g. java Time -8 should produce “The input should be a positive number” and then exit. You do not have to handle the case in which the input value is missing or is not an integer.
Case #2: 1 hour or 1 minute
In the original version of the code, the number of hours and number of minutes were followed by the words "hours" and "minutes", respectively, even if there were just 1 hour or 1 minute. Modify the program so that, if the conversion yields 1 hour and/or 1 minute, the output shows "1 hour" or "1 minute" instead of "1 hours" or "1 minutes". For instance
                $ java Time 72 
                1 hour and 12 minutes
                $ java Time 121 
                2 hours and 1 minute
            
Case #3: 0 hours or 0 minutes
Likewise, whereas the original version would display “0 hours” or “0 minutes,” modify the program so that, if the conversion yields 0 hours or 0 minutes, that value is not displayed. For instance:
                $java Time 180 
                3 hours
                $java Time 45 
                45 minutes
                $java Time 60 
                1 hour
            
Note that you do not have to handle the case in which there are no hours and no minutes, since Case #1 should address the situation in which the input is 0. This part of the assignment assumes, of course, that you have a working version of the Time program from Assignment #2. If this is not the case, please speak with me right away!

Part 2: Temperature Change

In this part of the assignment, you will write a program that determines which of three cities had the biggest temperature change over a span of time, based on the reported starting and ending temperatures.

Write a program called TempChange that takes the name, starting temperature, and ending temperature for three cities as runtime arguments, and then displays the name of the city that had the largest temperature change, along with the value of the change. For instance:
        $ java TempChange London 54.4 54.7 Paris 55.8 56.3 Madrid 59.0 59.1
        Paris had the biggest change: 0.5
    

In this case:
  1. London's change was 54.7 - 54.4 = 0.3
  2. Paris' change was 56.3 - 55.8 = 0.5
  3. Madrid's change was 59.1 - 59.01 = 0.1
Therefore Paris had the biggest change.

One issue that you will surely run into in this assignment is that Java does not always store the results of operations involving floating point (non-integer) numbers with correct precision. For instance:
 
    java TempChange Dallas 91.1 92.2 Houston 94.4 94.6 Atlanta 92.2 92.5
    Dallas had the biggest change: 1.1000000000000085
    
This is okay! While clearly not great, but it is not a bug in your program! This is not something you need to address here but we will talk about it later in the semester.

Note that when specifying the names of cities that have spaces in them, you can put quotes around the name and Java will treat it as a single String, for instance: java TempChange "New York" 78.0 78.9 Philadelphia 81.1 81.5 Boston 77.4 77.5 New York had the biggest change: 0.9000000000000057

Keep in mind that your program needs to identify the largest change, not the largest increase, so for instance:
    java TempChange Seoul 68.6 67.6 Beijing 71.0 71.2 Hanoi 74.1 74.5
    Seoul had the biggest change: 1.0
    

Hint! You can use the Math.abs function to get the absolute value of a number. For instance:
    double y = Math.abs(x);
    
will set y to the absolute value of x.

You may assume that all nine program inputs are provided in the correct order and are of the expected types, i.e. Strings for the city names and doubles for the temperatures, but your program should be able to handle negative numbers, which are allowed.

Additionally, you may assume that the temperature changes for the three cities are all distinct, i.e. you don't have to handle the case where two cities have the same change in temperature.

Tax

The USA has a progressive system for income tax rates. The somewhat abbreviated rule is (assuming you are single) :
IncomeRate
0--11,00010%
11,001--44,72512%
44,726--95,37522%
95,376 + 24%
So suppose you income is 100,000. Then your tax is:
        11,000 * 10%
        (44725-11000) * 12%
        (95376-44725) * 22%
        (100000-95376) * 24%
    or
        11000*.1 +
        33725*0.12 +
        50651*0.22 +
        4624*0.24
    or
        1100 +
        4047 +
        11143 +
        1109
    or
        17399
    
So although the last bit of income is taxed at 24% the average tax rate is only 17%. (yea?)

To rephrase

With a marginal tax rate, you pay that rate only on the amount of your income that falls into a certain range. To understand how marginal rates work, consider the bottom tax rate of 10%. For single filers, all income between $0 and $11,000 is subject to a 10% tax rate. If you have $11,200 in taxable income, the first $11,000 is subject to the 10% rate and the remaining $200 is subject to the tax rate of the next bracket (12%). (from https://smartasset.com/taxes/current-federal-income-tax-brackets)

When doing the calculations at each step, the result should be an integer (as shown above). To get the result of an integer (the income) times a double (the tax rate) back into an integer, you can do the following:
        double rate = 0.22;
        int income = 100;
        int tax = (int)(income*rate);
    
Putting the "(int)" into the last line above tells Java that you really mean for the result of the multiplication to be an integer. Without this assurance from the programmer, the Java compiler will fail at this line.

Write a program called IncomeTax that takes one integer from the command line. Interpret that integer as the total taxable income. The program should then calculate the tax and print the tax owed. You may assume that the thing given on the command line is an integer, but you should check that it is a valid income.

Here are some more examples:

$ java IncomeTax 100000
17399
$ java IncomeTax 50000
6307
$ java IncomeTax 5000 
500
$ java IncomeTax 77101
12269

    

Submitting

Create a readme

Use VSC to create another file in your HW3 directory. This file should be named "Readme". The contents of this file should follow this sample.

You should have 4 files in your HW2 directory: Time.java, TempChange.java, IncomeTax.java and Readme. (You might also have .class files.)

Submit

If you did this work on your own computer

You will first need to copy the files from you own computer to a lab computer. To do so, you can go into the lab as with HW2 or use ssh. Either way, you will need to create HW3 directory within you CS113 directory on the Unix machines. Recall, the can be done with the following commands:
        cd
        cd CS113 
        mkdir HW3 
    
Once you have made the HW3 directory in Unix, open a terminal on you own computer and in that terminal use "cd" to navigate to the directory containing your work for this assignment. Assuming you use the same directory structure on your own computer and in the lab, this process can be accomplished with the following commands
        cd
        cd CS113
        cd HW2
    
Then use the scp command to copy each of the files you want to submit from your computer to the lab. For example:
        scp Readme UNIX_NAME@goldengate.cs.brynmawr.edu:CS113/HW3/Readme
    
As always, when you read "UNIX_NAME" put in your UNIX user name. Also, with each scp command you will need to enter your UNIX password.

Actually submit

Open a terminal in UNIX (again, you can use SSH to do so from your laptop) and execute the following Unix commands (assuming you put HW3 directory into a CS113 directory in your home directory).
        cd
        cd CS113
        /home/gtowell/bin/submit -c 113 -d HW3 -p 3
    
In response to the submit command you should see a series of messages ending with:
        
        Submitting archive...
        Submission complete! Submission timestamp is 2023-08-08-15-30-28-EDT.