CS 113 - Computer Science 1

Lab 3

while loops and a little more Unix

In this lab you will:
  1. Learn one more Unix command
  2. Write two Java programs using while loops
  3. Learn (a little about) using the Java Scanner class to get user input.

Unix

(If you have it with you) get out a laptop and open a terminal window on your laptop.

ssh

The ssh command allow you to work on your laptop exactly as it you were working within a terminal window on a lab computer. This can be confusing as when you you are working in ssh, you do not affect your laptop, but rather your account on the department UNIX server.

To start an ssh session

        ssh UNIX_NAME@goldengate.cs.brynmawr.edu
    
where UNIX_NAME is your UNIX account name. When prompted, enter your password.

Unlike every other Unix command you have used, this one does not complete quickly, rather it effectively opens a terminal on the machine goldengate. So you end up with a terminal onto the UNIX servers that is running inside a terminal that is running on your laptop!

Once you have the ssh terminal running, set up -- on the UNIX servers -- for todays lab.

        cd 
        cd CS113 
        mkdir Lab3
    

If you have not done so already, open and log into a UNIX machine in the lab and open a terminal. Verify that the Lab3 directory exists.

Finally, close your ssh session (on your laptop) by entering exit.

Using ssh means that, in the future, you do not have to set foot in the lab when you want to submit an assignment (if you are doing your work on a laptop). Everything that you would do on a lab computer can be done from within an ssh session.

Java

Reverse an Integer

Write a program called Reverse that takes a positive integer, n, as a runtime argument and then prints its digits in reverse order, using the algorithm described below.

For instance
$ java Reverse 1234 
  4321
$java Reverse 19103
  30191
Once your program has read n as a runtime argument, it should use the following algorithm to reverse the digits and store the value in the variable r:
    initialize r to 0
    while n != 0
         set d to the last digit of n 
         multiply r by 10 and add d
         divide n by 10
When this algorithm terminates, you can print the variable r to see the digits in reverse.

The trick here is figuring out how to set d to the last digit of n. Think about the different arithmetic operators we have seen in class this semester!

Guessing Game

Write a program that draws an integer in the range 0 to 100, then asks the user, repeatedly to guess the number. If to high, then give the feedback "too high", if too low, give the feedback "too low" until the guesser gets the correct number.

To get an integer in the range 0--100, you can do the following:
    double d = Math.random();
    int number = (int)(d*100);
Question: what happens when you make the line above just int number = (int)d*100? Why?

Next you will need to get input from the user. Here is a little program to et a single number from a user.

import java.util.Scanner;

public class Scanr {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        System.out.println("Enter an integer");
        int enteredInteger = scnr.nextInt();
        System.out.println("You entered " + enteredInteger);
    }
}
    

For example, here is the output from two runs of my guessing program (which I cleverly named "Guessing").

$ java Guessing 
Enter a number in 0..100
50
Too High
Enter a number in 0..100
254
Too High
Enter a number in 0..100
25
Too low
Enter a number in 0..100
40
Too High
Enter a number in 0..100
30
Too low
Enter a number in 0..100
35
Got it

$ java Guessing
Enter a number in 0..100
50
Too High
Enter a number in 0..100
25
Too low
Enter a number in 0..100
37
Too low
Enter a number in 0..100
44
Too High
Enter a number in 0..100
40
Too High
Enter a number in 0..100
38
Too low
Enter a number in 0..100
39
Got it

What to hand in

Use your phone to take a picture of VSC screen(s) with your java programs. Send the picture to gtowell@brynmawr.edu. Or, if convenient, copy / paste your Java programs into and email and send me that.