Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Spring 2015
Thursday Lab #2
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Monday, February 2 2015

# Topic Review: Variables and Data Types

• A variable is a name for a location in memory where a program stores a value. You can always get the currently stored value by referring to the variable's name (the Windchill.java program in Lab #1 has several examples).

• Every variable must be defined before it is used, giving the name, the type of value that this variable holds, and (optionally) an initial value:

`<type> <name> = <initial value>;`

For example, we can define a variable x, which stores only full-precision floating-point values and has initial value 1.0 as

```double x = 1.0;
x = x + 3;```
• Once we declare a variable, we can read its value (by using its name), and we can change its value with an assignment statement. For example, we can increase the value of x by 3 with

`double x = 1.0;`

# Integer and Floating Point Data types

There are several different number types. We saw double in class, which is used for floating-point values. There are also integer types, where the decimal component is ignored. The most common one is int, but you can also use long for very large (or very small) numbers.

In this lab, we will explore the int data type.

To begin, ...

Eck, Sections 2.1 and 2.2 (link).

# Example Program

Here's a cute example, which will display the current time of day in the Eastern time zone (accurate both with and without Daylight Savings Time). it uses the long data type, as the first couple of values involve numbers too large to represent with int. Pay particular attention to the use of the truncation of integer division (/) and the way that the remainder operation (%) is used:

```public class Time {
public static void main (String[] args) {

long t = System.currentTimeMillis();
// Returns the number of milliseconds elapsed since January 1, 1970 UTC

long seconds = t / 1000;  // We only need to know the number of seconds
int minutes = (int) seconds / 60 ;
// There are 60 seconds in a minute, and we don't care about the
// fractional part.  We'll use an int here since the number is smaller

int hours = minutes / 60;
// 60 minutes per hour, fractional part irrelevant

seconds = seconds % 60;
// Since we're only tracking the seconds elapsed since the last minute,
// we'll throw the minute part away

minutes = minutes % 60;
// Likewise, we only need the minutes since the last hour

hours = (hours - 5) % 12 ;
// We want hours in 12 hour format.  The -5 corrects the time from UTC
// to Eastern

System.out.printf("%d:%02d:%02d\n", hours,minutes,seconds);
// printf is documented in section 2.4.1

// This won't quite work for times between 12:00 and 12:59.  Why not?
// Can you think of a way to fix this?
} // main
}```

You can run this as:

```John-Lasseter:~ jlasseter\$  java Time
9:19:09```

# Problems

1. First, some practice with what you already know. Write a program, annual deposit into a savings account, an annual percentage rate (expressed as an integer: for example, 2.4% would be given as 2.4). The program should print the value of this investment after 7 years have passed.

An example interaction:

```John-Lasseter:~ jlasseter\$  java Compound 1001 2.3
8683.17```

NOTE: If you use System.out.println() to dispaly the answer, you'll get a messier-looking result, though it will still be correct. That's fine. I did this more tightly-formatted version with System.out.printf(), but you don't have to.

2. Write a program, Weekday, that will tell you the day of the week on which a date occurred. Specifically, your program whould take three integer arguments representing a date. The arguments should be given in the order year month day. The program should print a single integer: 0 for Sunday, 1 for Monday, and so on, up to 6 for Saturday.

#### Example Interactions

```John-Lasseter:~ jlasseter\$ java Weekday 2014 9 17
3
John-Lasseter:~ jlasseter\$ java Weekday 1874 3 7
6
```

#### How to do this.

There's a set of formulae for doing this:

Let's assume we already have the values m (month), d (day) and y (year). Then the weekday for m/d/y (which we'll denote by w) can be found by calculating the following values:

• yZ = y - ((14 - m) / 12)

(If the month is January or February, we'll consider them as the 13th or 14th months of the previous year)

• mZ = m + (12 * ((14 - m) / 12))

(This keeps the values for March through December at 3 .. 12. January becomes 13 and February becomes 14)

• B = yZ % 100

(Years since the century began: i.e., the last two digits of y)

• C = yZ / 100

(The century: i.e., the first two digits of y)

• leapDays = (B / 4) + (C / 4) + 5*C

(The total number of leap days that have occurred since 0 C.E.)

• w = (d + ( ((mZ+1) * 26) / 10) + B + leapDays + 6) % 7

All of the divisions above are integer divisions (i.e. with the fractional parts discarded). Hence any variables you use should be of int type (if you use double values, the formulae won't be quite right).

This formula, known as "Zeller's Congruence", is due to Julius Christian Johannes Zeller, a German mathematician of the 19th century. Although opaque, it is possible, with a little work, to understand how he found it. For the curious, the Wikipedia entry is quite nice: http://en.wikipedia.org/ wiki/Zeller_congruence

Let's start with the data types you'll need. You're working with remainders and integer division, so the best choice is clearly int. So we'll start out the program as

```public class Weekday {
public static void main(String[] args) {
int y = Integer.parseInt(args[0]);
int m = Integer.parseInt(args[1]);
int d = Integer.parseInt(args[2]);

}
}
```

Finish the rest.

3. Chances are that you modeled every value in your program using the int data type (if not, you should have). What happens if instead you declare every value as a double? What do you think is going wrong? Specifically, which lines of your program have errors in their calculations?

• Your code for both programs must be syntactically and semantically correct, which means that it has to compile successfully.

I cannot emphasize this enough. Any solution that cannot be successfully compiled with javac will receive no credit. If you run into any trouble here, please ask me for help, which I am always happy to give (as are the TAs). I know this seems draconian, but I promise you'll thank me for it later.

• Naturally, your code for both programs must also be behaviorally correct, which means that it should print the right output in the right format. However, partial credit will be given for partial solutions. At a minimum, be sure to test on the examples you planned beforehand.

`/classes/cs124/<your user name>/`