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

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, ...

READ

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?

Standards (READ ME)

Turn in (READ ME):

You are to submit both your source code files electronically and with a printed copy (I need to have paper to mark up and the digital file to run your program). Your source files should be saved in a folder named lab2 and this folder should be copied copied into your folder in the class directory

/classes/cs124/<your user name>/

NOTE: Make sure you're actually copying it to this folder and not your own home folder at /afs/afs.hws.edu/home/<your user name>! This is an obvious source of confusion, and will lead you to think you've turned in your work to me when you haven't.

Also, please respect my file name requests. I ask for precise names because it makes it easier for me to test your work. Thanks.


John H. E. Lasseter