CS 145 Lab 2
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:
- Load the workspace you made last lab. It should be on your H: drive — not C:.
- Make a new package named lab2 in your cs145 project.
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:
- homework: 400 points, worth 50% of the final grade (4 assignments worth 100 points each)
- labs: 40 points, worth 10% of the final grade (4 labs worth 10 points each)
- midterm 1: 100 points, worth 10% of the final grade
- midterm 2: 100 points, worth 10% of the final grade
- final exam: 100 points, worth 20% of the final grade
Write a class named WhatsMyGrade whose main method:
- declares a bunch of variables for the graded items,
- assigns to these variables the points received (make them up!),
- calculates the final percentage,
- 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:
- Class names start with a capital letter.
- Variable names start with a lowercase letter.
- Internal words in both classes and variables start with a capital letter.
- Code is most easily read when it’s properly indented. Every time you open a curly brace, indent a level. Hit Control-Shift-F in Eclipse to have it automatically formatted. (This only works when you don’t have any errors.)
Checkpoint #1: show your instructor or TA your code.
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:
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:
- Create three variables for your birth month, day, and year, using appropriate types. (What kind of data is a year? A month name?) Join them together into a String formatted like “September 14, 2011” and print it.
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:
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.
- Start working on preassignment 1. Preassignments are designed to give you a chance to solve smaller programming tasks and receive immediate feedback on the correctness of your solution. To make this happen, we give you a Java program that automatically grades. You are strongly encouraged to download and install this program and confirm that your setup is correct before you leave lab. See the separate post about the preassignment for instructions.
- The more code you write, the better your grade, the more satisfying your career. Try the book’s Practice-It website for some browser-based Java problems.