teaching machines

CS 145 Lab 2

September 14, 2011 by . Filed under cs145, fall 2011, labs.

This lab builds on your readings and lecture discussion of variables and arithmetic operators. We’ll also see two new types, Scanner and String. Before you get started:

Reminder

The purpose of lab is to get you generating code in a place where you can ask questions. It’s very easy to watch your instructor write code in class and think it looks easy, only to go back home and find that you don’t even know where to begin on a homework. Please take advantage of this opportunity.

Variables and types

Suppose you have a class (not this one) with the following graded items:

Write a class named WhatsMyGrade whose main method:

  1. declares a bunch of variables for the graded items,
  2. assigns to these variables the points received (make them up!),
  3. calculates the final percentage,
  4. and prints it to the System.out.

Use types that are appropriate for the kind of data you are storing. Integers should go into int variables, numbers with fractions in doubles.

Here are some things to remember before you show off your sparkling code:

Checkpoint #1: show your instructor or TA your code.

String

These days computing is as much about text as it is numbers. Let’s have a look at a new data type called String. These aren’t covered in detail until later in your textbook, but they are introduced in Section 1.2 in your book.

You’ve seen how we declared int and double variables with:

int nCitations = 5;
double balance = 37.28;

Both int and double are primitive types, which means they are very low level. (The computer’s processor actually has physical hardware for manipulating primitive types. Not so for Strings.) Strings are declared similarly, but Strings are far from primitive:

String text = "One turn of the wheel and everything changes.";

We can print Strings just like ints and doubles:

System.out.println(text);

We can also join Strings to other data using the + operator:

String a = "abc";
String b = "123";
System.out.println(a + b);

double finalGrade = 95.0;
System.out.println("My grade: " + finalGrade);

Make a new class named StringFun with a main method. Complete the following task:

Scanner

So far our programs have been a lot like a fish tank. They’re fun to watch, but they don’t play fetch or interact with users in anyway. Let’s change that by introducing another type that, like String, is far from primitive. It’s called Scanner. We make a Scanner like this:

Scanner in = new Scanner(System.in);

Our Scanner variable is named in, and the way we make it is very different from our other data types. We’ll cover more on what’s going on with this later in the semester.

We added and multiplied ints and doubles. We joined Strings. What can we do with Scanner? Get data from the user. Here’s how:

int usersFavoriteNumber = in.nextInt();
double salary = in.nextDouble();
String name = in.next();

Try this out in code. You’ll see that there’s an error with your Scanner — noted  by the red underlines in Eclipse. That’s because that Scanner is defined in another package, one named java.util. You are in the lab2 package. To make classes from another package available you have to import them with a line like this at the top of your file:

import java.util.Scanner;

You can also ask Eclipse to automatically import it by hitting Control-Shift-o.

If you run this code, it’ll look like nothing happened, but the computer is actually sitting there waiting for you to type something in at the console.

Scanner is an object, whence this course gets its name. All objects have these actions we can ask them to perform. For example, the first line can be interpreted, “Hey in, give me an int.” It does so by listening for keypresses at the keyboard. When you are done typing and hit enter, it gathers them up and spits back an int, which you then assign to your int variable.

Modify your StringFun class to prompt the user for a month, day, and year and retrieve these values with a Scanner — instead of using literals.

Checkpoint #2: show your instructor or TA your code.

TODO