Computer Science 252
Problem Solving with Java

Spring 2016, The College of Saint Rose

Lecture 1: Introduction and Overview
Date: Tuesday, January 19, 2016


Agenda

Lecture 1 Assignment

Due at the start of class, Thursday, January 21.

Please submit answers to these questions either as a hard copy (typeset or handwritten are OK) or by email to terescoj AT strose.edu by the start of class. Please use a clear subject line when submitting by email (e.g., CSC 252 Lecture 1 Assignment , Joe Student). We will discuss these questions at the start of class, so no late submissions are accepted.

Most of these are review of some of the Java concepts and constructs you should be familiar with from your previous programming experience.

  1. I would like to get a better sense of everyone's backgrounds coming in. Please answer each of the following. (4 points)
  2. In an effort to get to know your names and to make sure you know how to find me, please stop by my office (Albertus Hall 400-6) and introduce yourself as a Computer Science 252 student (no later than January 26). (3 points)
  3. Consider the following names that a programmer is considering for use as variable names in a Java program. For each one, indicate whether it is a valid Java identifier. If it is valid, does it conform to the Java naming convention for variables? For all names that are either invalid or do not conform to the naming convention, suggest a similar name that would be appropriate to use instead. (5 points)
    KNIGHT
    252assignments
    FavoriteColor
    darth vader
    BugsBunny!
    WHEN_IS_LUNCH?
    gotCash$$
    jeter2
    little Red Wagon
    school_name
    
  4. Under what circumstances would you use the int data type, and when would you use the double data type? The double data type seems to be able to store everything you can store in an int and more, so why not always use a double? (4 points)
  5. What is printed by the following? Please work through it by hand rather than typing in and running the program. (3 points)
    public class PrintOuts {
    
      public static void main(String args[]) {
    
        int a = 5;
        int b = 12;
        double c = 3.0;
    
        System.out.println("" + (b + c));
        System.out.println("" + (a / b));
        System.out.println("" + (a + b + c));
        System.out.println("" + (a / c));
        System.out.println("" + (a + b / c));
        System.out.println("" + ((a + b) / c));
      }
    }
    
  6. Complete the following Java program so that it assigns random values to the variables as described in the preceding comment. (1 point each)
    public class RandomNumbers {
    
      public static void main(String args[]) {
    
        Random r = new Random();
    
        // x should contain an integer between 0 and 999
        int x = 
    
        // y should contain an integer between 1 and 50
        int y = 
    
        // z should contain an integer between -10 and 10
        int z =
    
        // a should contain a floating-point value between 0.0 and 2.0
        double a = 
    
        // b should contain a floating-point value between 1.0 and 2.0
        double b =
    
        // c should contain a floating-point value between 10.0 and 50.0
        double c =
    
        System.out.println("x=" + x + ", y=" + y + ", z=" + z +
            ", a=" + a + ", b=" + b " + ", c=" + c);
      }
    }
    
  7. Convert the following Java conditional to use a switch statement that accomplishes the exact same thing. (3 points)
    if ((day == 1)||(day == 2)||(day == 3)||(day == 4)) {
        hours = 9;
    }
    else if (day == 5) {
        hours = 8;
    }
    else {
        hours = 0;
    }
    
  8. Rewrite this for loop as a while loop. (3 points)
      for (int x = 0; x < 40; x += 2) {
        sum += x;
      }
    

Examples

Late penalty graph: