Fundamental Programming Concepts
Summer 2000

Lab 2

Overview
The goals of this lab are to:
  • Use the Console class to get input from the user
  • Use several variables and types
  • Continue improving skills with methods by using return values and parameters

If you're having trouble understanding method calls, you should go through the online handout called the Method Call Walkthrough. It's linked under Lecture 2 on the lecture page.

 

Part I: The Console Class
In the last lab, we learned to produce output by using the println method of the System.out object. Unfortunately, there is no corresponding method for getting input from the user. Keyboard input in Java actually requires a lot of programming. Therefore, the Console class is being provided to you to make input easier.

Start by creating a new project (if you don't remember all the steps, they can be found in Lab 1). Call it, and the file you add to it, "Lab2a" -- minus the quotes, of course.

In order to use the Console class, you will have to add it to your project, since it isn't part of the standard Java libraries (it was written by your instructor for use in the course). Follow these instructions to do so. After you have, try entering and running the following program:

/**
 * Standard class header with lab #, name, NetID, date...
 */
class Lab2a {
    public static void main(String[] args) {
        System.out.print("Please enter a number: ");
        int i = Console.readInt();
        System.out.println("The number you entered was: " + i);
    }
}

Notice these things (do be sure to read these thoroughly):

  • The first line uses print instead of println so that the number the user enters will be on the same line as the prompt. (Try changing it to println to see the difference.) Although the choice of whether to use print or println won't always be important, it is important that you always give the user a prompt before trying to get input. This makes your programs more user-friendly.
  • The second line uses a method of the Console class to read an integer from the keyboard. Try running the program several times, entering things other than integers (e.g., "John", 3.14, true). What happens? Note that we could also have written this line in two lines, as:
        int i;
        i = Console.readInt();
    Both are correct, the first way is just a bit shorter. It combines declaring the variable with initializing it. The second declares the variable, then makes an assignment to it.
  • The third line combines a string literal with an integer using the plus operator. How is this possible -- you can't add a string and a number, can you? The answer is that the plus operator is overloaded to mean different things when it has a string as one of its operands. Then it becomes the string concatenation operator. Concatenation means "join". For example, "Hello " + "world" = "Hello world". When one of the operands is not a string, as in the third line, it converts it to a string. So the integer 5 becomes the string "5" when it's part of a concatenation operation.

Now, modify this program so that it reads three integers from the user and then prints their sum, product, and average. There's no need to divide the program into methods, you can just put everything in main for now. You'll be submitting this average program as Lab2a, so make sure to save your work.

Make sure to chose appropriate data types for any variables you use. Also, make sure to include specifications for any local variables you declare, as described in the style guide (yes, this may seem silly now, but it's a habit you need to start practicing before we get to the point that it's actually useful). Your class should be preceded with a comment including the lab #, your name, NetID, and date.

Your output should look like:

Enter 3 numbers and I'll find their sum, product, and average.
Enter the 1st number: 3
Enter the 2nd number: 4
Enter the 3rd number: 5
Sum = 12
Product = 60
Average = 4.0

Try your program on the numbers 0, 0, and 1. Do you get the correct answer? If not, did you divide by 3 somewhere? Try changing it to 3.0 and see if that fixes it. Why is this necessary? (hint: think about automatic type promotions from your reading -- LL p. 73)

The Console class has several other methods that you might find useful. Go ahead and browse the documentation for them to find out what's available.

 

 

Part II: The Distance Application
Read through this entire part of the lab before doing any typing! (Actually, you should always do that.)

For this program, you'll write an application that reads the Cartesian coordinates of two points: (x1, y1) and (x2, y2). You'll then compute the distance between the two points and output it. Don't forget to create a new project for it! Call this project Lab2b. The same style guidelines apply to this as in Part I (comments, specifications, etc.)

Input

  • Don't forget to prompt the user for the values you input. Make your prompts specific (i.e., don't just ask for a number; ask for x1 if that's the coordinate you're inputting)

  • You can input doubles using one of the methods in the Console class 

Computation

The formula for the distance is:

In order to compute this formula, you need to know how to take square roots and exponentiate. The Java libraries provide a Math class with two methods for doing this:

  • double pow(double a, double b): returns the value of the first argument raised to the power of the second argument

  • double sqrt(double a): returns the square root of the argument

Since these are members of the Math class, you call them with statements like:

x_squared = Math.pow(x, 2);
y = Math.pow(Math.sqrt(x + 1), 2);  // y = square of square root of x + 1

You should make the computation of the distance its own method in your program. In other words, you are not permitted to do any of the computation inside the main method. Your computation method should have a signature similar to:

static double computeDistance(double x1, double y1, double x2, double y2)

Don't forget that you'll need a return statement at the end of the method that returns the distance that you've computed. You should give this method a specification as described in the style guide.

Want to know more about the Math class? You can read Sun's documentation of it.

Output

Your final output should be in the following form:

The distance between (0.0, 0.0) and (0.0, 1.0) is 1.0
 
Submitting
The files you should submit for this lab are:
  • Lab2a.mcp
  • Lab2a.java
  • Lab2b.mcp
  • Lab2b.java

These will be submitted on floppy, which will be collected at the beginning of class. Hopefully, this will be the last lab we have to do on floppies.

Because of the Independence Day holiday, you have a one day extension on this lab. It will be due July 5th at the beginning of your section. Lab 3 will not be affected by the holiday -- it will still be posted on July 3rd and due July 6th.

Don't forget to include a comment at the beginning of each of your classes, similar to Lab 1. Also, make sure to follow the style guidelines for all your variables and methods.