teaching machines

CS 145 Lab 2

January 27, 2012 by . Filed under cs145, labs, spring 2012.

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:

Plea

The purpose of our lab meetings 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.

Reminder

Please check off any finished but unsubmitted checkpoints from the previous lab in the first 20 minutes of this lab.

Variables and types

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

Write a class named WhatsMyGrade whose main method:

  1. declares a bunch (11) of variables for each graded item,
  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 us your 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 the 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 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. Scanner knows how to grab each of the three datatypes we’ve used so far. We just have to ask it to in the right way. Here’s how:

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

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

import java.util.Scanner;

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. To make it a little more obvious that the user is to type something, modify your StringFun class to prompt the user for a month, day, and year. Then retrieve these values with a Scanner. At the end, you should have a program that can format and print anyone’s birthday.

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

TODO