Homework 2

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

In this homework you will Note that in this homework 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 with the following exceptions

Program 1 -- Converting Time

Part 1

Copy the following program into a file named Time.java
    public class Time {
        public static void Main(String[] args) {
            int minutes = Integer.parseInt(args[0]); // convert String to int
            System.out.pritln(minutes + " minutes") 
        }
     }
If you copied it exactly, you will find when compiling that there are errors. The output of the compilation tells you where "compile time" errors are (or at least gives you strong hints as to where). However the compile does not tell you about run time errors.Fix this program.

VSC also tells you about compile time errors. In general, they are underlined in red squiggles. When you hover over a red underline, you will see pretty much the same error message that you would get from the compiler.

Part 2

Saying "473 minutes" is not very useful as people do not think about time that way. So rather than "473 minutes", modify your program so that it outputs "7 hours and 53 minutes". Here are some samples
geoffreytowell@Geoff2020Mac HW2 % java Time 473
7 hours and 53 minutes
geoffreytowell@Geoff2020Mac HW2 % java Time 53
0 hours and 53 minutes
geoffreytowell@Geoff2020Mac HW2 % java Time 152
2 hours and 32 minutes

You may assume that the input to the program is a positive integer, i.e. a whole number greater than 0. You do not have to worry about the case in which the input is missing, a negative number, a fraction, something that is not a number, etc.

Program 2 -- Estimated Time of Arrival

Once a map/directions application has determined a route from a starting point to the destination, it can tell the user the approximate time that they will arrive, based on knowing the current time and how long it will take to travel.

Write a Java program called Arrival that takes as input the current time, the distance to travel, and the expected average speed, and prints the estimated arrival time. The four program inputs (or command line arguments) should be as follows, in this order:
  1. h = the hour portion of the current time, ranging from 0-23
  2. m = the minutes portion the current time, ranging from 0-59
  3. d = the distance to be traveled, in miles
  4. s = the expected average speed, in miles per hour
Here are a few sample runs of the program:
java Arrival 11 10 15 60
Arrival Time is 11:25

java Arrival 15 50 125 50
Arrival Time is 18:20

java Arrival 22 15 110 60
Arrival Time is 0:5
Note that in the last example, the time would ordinarily be displayed as “00:05” but it is okay to omit the leading “0” for the purposes of this assignment.

After reading the four runtime arguments, your program should be implemented according to these steps:
  1. Calculate the travel time t = d / s * 60; note that you need to multiply by 60 since s is miles per hour but we want t in minutes. Hint! Think about which datatypes you should use to store t, d, and s, keeping in mind what you have learned about integer division. Update the minutes portion of the arrival time: m = m + t
  2. Since this number of minutes may exceed one hour, update the hours portion of the arrival time: h = h + m / 60
  3. Since the minutes portion of the arrival time may exceed 59, calculate the correct value to display: m = m % 60
  4. Since the hours portion of the arrival time may exceed 23, calculate the correct value to display: h = h % 24
  5. Display the results using "h:m"
You may assume that the four inputs to the program exist and are positive integers, and that h is between 0-23 and that m is between 0-59.

Program 3 -- Calculating Distance

For very long journeys, for instance between cities on two different continents, a map/directions application will estimate the distance using geographical latitude and longitude coordinates.

Since the earth is a sphere and not a plane, though, the application cannot use simple Euclidean distance but rather must use the "great-circle distance", which measures the distance between two points on a sphere.

Write a program called Distance that takes four runtime arguments as doubles — lt1, lg1, lt2, lg2, representing the latitude and longitude, in degrees, of two points on the earth — and prints the great-circle distance between them.

The great-circle distance d is given by the following equation (using the spherical law of cosines):
d = earthRadius * arccos(sin(lt1) * sin(lt2) + cos(lt1) * cos(lt2) * cos(lg1 - lg2))
where you can use the earth's radius = 3986 miles.

Since the inputs are meant to be doubles, you will need to use the Double.parseDouble function to convert the program inputs from Strings to doubles.

To calculate the distance, you will need to use the Math.sin, Math.cos, and Math.acos functions to perform the sine, cosine, and arccosine trigonometric functions, respectively. For instance:
double s = Math.sin(angle);
returns the sine of angle and stores it in the variable s.

However, the latitude and longitude of the two points are in degrees, whereas Java's trigonometric functions use radians. Use Math.toRadians(a) to convert the angle a from degrees to radians before using the sine and cosine functions.

The following example shows the output for the distance between Philadelphia (39.9526 N and 75.1652 W) and Honolulu (21.3069 N and 157.8583 W):
java Distance 39.9526 75.1652 21.3069 157.8583 
4945.290632589314 miles
 
//This next example shows the distance between Paris (48.87 N and -2.33 W) and San Francisco (37.8 N and 122.4 W):
 
java Distance 48.87 -2.33 37.8 122.4
5598.250822014901 miles
 
You may assume that the four inputs to the program exist and are all valid latitudes (floating point numbers between -90.0 and 90.0) and longitudes (floating-point numbers between -180.0 and 180.0).

Submitting

Create a readme

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

You should have 3 files in your HW2 directory: Time.java, Arrival.java, Distance.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 first need to go into the lab and create a HW2 directory within you CS113 directory. Recall, the can be done with the following commands:
    cd
    cd CS113 
    mkdir HW2 
Once you have made the HW2 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 files from your computer to the lab
    scp Time.java UNIX_NAME@goldengate.cs.brynmawr.edu:CS113/HW2/Time.java
    scp Arrival.java UNIX_NAME@goldengate.cs.brynmawr.edu:CS113/HW2/Arrival.java
    scp Distance.java UNIX_NAME@goldengate.cs.brynmawr.edu:CS113/HW2/Distance.java
    scp Readme UNIX_NAME@goldengate.cs.brynmawr.edu:CS113/HW2/Readme
In each of the above commands, when you read "UNIX_NAME" put in your UNIX user name. Also, with each scp command you will need to enter your UNIX password.

scp is much like cp except that scp allows copying between machines. "UNIX_NAME@goldengate.cs.brynmawr.edu:" in the scp command given the name of the machine to copy to -- in this case goldengate.cvs.brynmawr.edu -- and the account name to put it in.

Actually submit

Open a terminal and execute the following Unix commands (assuming you put HW2 directory into a CS113 directory in your home directory).
    cd
    cd CS113
    /home/gtowell/bin/submit -c 113 -d HW2 -p 2
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.