Lab 02 Solution
Version 1

 


Problem 01

  Type in the Hello World program, compile it, and run it.

Nothing to write here for this problem.


Problem 02

  Write a program that prints:
Hello World!
So long!
I must be going! 
Do not use any escape codes.

Click Here To Run This Program On Its Own Page
/* 
   Program: LAB02PR02
   Author:  Joshua S. Hodas 
   Date:    9/19/98
   Purpose: To print a silly saying
*/
 
  
class LAB02PR02 {
    
    public static void main(String args[]) {
 
        System.out.println("Hello World!");
        System.out.println("So Long!");
        System.out.println("I must be going!");
 
    }
    
}


Problem 03

  Rewrite the program you just wrote so that it has only one statement inside main.

(Recall that this is not really recommended programming style at this level, but is useful to know how to do later on in your career.)

We can solve this in two ways. We can do it in one statement, with only a single string literal, as in:

Click Here To Run This Program On Its Own Page
/* 
   Program: LAB02PR03a
   Author:  Joshua S. Hodas 
   Date:    9/19/98
   Purpose: To print a silly saying using only one statement
*/
 
  
class LAB02PR03a {
    
    public static void main(String args[]) {
 
        System.out.println("Hello World!\nSo Long!\nI must be going!");
 
    }
    
}

We can also break the string up into three substrings that get added together, but still in a single statement. This is visually a little easier to follow:

Click Here To Run This Program On Its Own Page
/* 
   Program: LAB02PR03b
   Author:  Joshua S. Hodas 
   Date:    9/19/98
   Purpose: To print a silly saying using only one statement
*/
 
  
class LAB02PR03b {
    
    public static void main(String args[]) {
 
        System.out.println("Hello World!\n" + 
                           "So Long!\n" + 
                           "I must be going!");
 
    }
    
}


Problem 04

 

Write a program that prints the text:

\\ is for backslash! 

Click Here To Run This Program On Its Own Page
/* 
   Program: LAB02PR04
   Author:  Joshua S. Hodas 
   Date:    9/19/98
   Purpose: To print \\ is for backslash!
*/
 
  
class LAB02PR04 {
    
    public static void main(String args[]) {
 
        // In order to print \\ we need to use \\ for each \:
 
        System.out.println("\\\\ is for backslash!");
 
    }
    
}


Problem 05

  Write a program that prints:
1 / 3 = 0.3333333333333333 
where the last number is the result of doing the appropriate computation, rather than a string or numeric literal appearing in the program.

Click Here To Run This Program On Its Own Page
/* 
   Program: LAB02PR05
   Author:  Joshua S. Hodas 
   Date:    9/19/98
   Purpose: To print 1 / 3 = 0.333333333333333
*/
 
  
class LAB02PR05 {
    
    public static void main(String args[]) {
 
        // The parens around 1.0/3.0 are not strictly necessary, but they
        // visually clarify the precedence.
 
        System.out.println("1 / 3 = " + (1.0/3.0));
 
    }
    
}

Last modified August 28 for Fall 99 cs5 by fleck@cs.hmc.edu


This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Sat, Sep 19, 1998 at 5:53:22 PM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_02/labsolutions.html