Computer Science 252
Problem Solving with Java

Fall 2014, The College of Saint Rose

Lecture 1: Introduction and Overview
Date: Tuesday, August 26, 2014


Agenda

Lecture 1 Assignment

Due at the start of class, Thursday, August 28.

Please submit answers to these questions in Submission Box under "LA1" or in hard copy by the start of our next class. We will discuss these questions at the start of class, so no late submissions are accepted. Please be sure that your name is clearly indicated in all submissions.

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 you and to make sure you know how to find me, please stop by my office (Albertus Hall 400-2) and introduce yourself as a Computer Science 252 student (some time this week). (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. Suppose your program contains the following variables and values:
      int a = 5;
      int b = 12;
      double c = 3.0;
    

    Evaluate each of the expressions below. (3 points)

      b + c
    
      a / b
    
      a + b + c
    
      a / c
    
      a + b / c
    
      (a + b) / c
    
  5. Assume you have a Java program that includes the following construction of a random number generator object:
      Random r = new Random();
    

    Complete the following Java statements so that they accomplish the task as described in the preceding comment. (1 point each)

    // 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 =
    
  6. 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;
    }
    
  7. Rewrite this for loop as a while loop. (3 points)
      for (int x = 0; x < 40; x += 2) {
        sum += x;
      }
    

Examples

Late penalty graph: