|
Fundamental Programming Concepts
Summer 2000 |
|
Overview | |
The goals of this lab are to:
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):
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:
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
ComputationThe 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:
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. OutputYour final output should be in the following form:
|
|
Submitting | |
The files you should submit for this lab are:
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. |