IC211: Lab 1
Due: January 17.
Concepts:
- Introduce Java programming in Linux
- Write, compile, and run programs using javac and java
This lab is intended to get you up and running with java from the command line. We strongly encourage you to get help from your instructor or another student in the class if there is any portion of this lab that you cannot accomplish, because you will need to be able to write, compile and run java programs very, very soon.
This lab will also demonstrate, by example, the use of a Scanner object to get data from the keyboard and System.out to display information to the console.
Even if you have not finished the lab, attempt to submit what you have before you leave, so you know how this works for your instructor. You can overwrite your submission later, after you've finished.
1. Writing/Compiling/Running Java code
- Create a directory called Lab1, and download this file to it.
- Open it using your text editor of choice by typing
editor Lab1a.java
where "editor" is one of gedit, nano, emacs, gvim, or vi. gedit is the easiest for most people. vi (the base editor for gvim) and emacs are the most powerful if you want to take the time to learn them. They have lot of built-in functionality, such as letting you build, run, and debug programs from within the editor. Know that you have just chosen sides in a holy war.

- Add the following to the main method (NOTE - you can copy and paste from Windows<->Linux VMs):
int [] x = new int[10]; int k = 0; while(K < 10) { x[k] = k*k; k++; } for (int j = 0; j<10;j++) { System.out.println(j+ " squared is " + x[j]); }What do you think it will do when run? - Now it is time to compile this program. You do this by typing the
command
javac Lab1a.java
(javac = "java compiler"). If you typed it in exactly as above, you should have gotten an error. Take a look at it, and notice how helpful it's being. Fix the error. - Recompile the program (so you got no errors). If you type
ls
you'll notice a new file, Lab1a.class, which is the bytecode to be run by the java interpreter. Run the interpreter on your program with the commandjava Lab1a
- Modify the program by replacing the while loop with a for loop. Compile it and run it again.
2. Your first Program
- Make a new file called Lab1b.java. Write the lines you need to write to make it a Java program.
- Add a line at the very top of the file saying import java.util.*; This is like "#import < iostream >", and allows us to use the Scanner class.
- Add the following code to main. Run to see what it does:
Scanner in = new Scanner(System.in); System.out.print("Please input an integer "); int n = in.nextInt(); System.out.print("Please input a second integer "); int k = in.nextInt(); System.out.println("The two ints were " + n + " and " + k); - What happens if you type your name instead of an integer?
- Scanner is a class that is part of Java's utility package (java.util). A package is "roughly" analogous to a C++ library. Scanner is used to facilitate reading information from an input stream. In this case, we are reading from System.in which is that standard input stream. In order to use the Scanner class, you have to import the java.util package. That's the purpose of the 'import' line of the file.
- Did you know that if you want to play a simple lottery where there are n possible numbers (the number of balls), and you have to pick k of them, then your odds of winning are 1 in (n * (n-1) * (n-2) * ... * (n-k+1))/k! Probably not since you haven't taken discrete math yet.
- Anyway, modify your program so that it reads in the n and k, and computes that value. You should be able to do this with code that (not counting I/O) looks just like C++.
- You may wonder what else the Scanner class can do. The Scanner class, and much of the rest of what is built into Java, is described in your book. To find Scanner, look under java.util.*, and it is alphabetical from there (alternatively, google for "java scanner"). You'll notice a long list of functions included in the Scanner class. Which one do you think will read in a whole line? What type does it return? Change your code so that before the integers are entered, the user enters his/her name, and this is printed back to him in the final println() command.
Adding functions
- Add a function which takes an int and returns the factorial of that integer. Place that function immediately after the } that finishes the main function. Note that functions (for now) should start with the magic incantation public static, otherwise they look just like C++ functions. Note there are no prototypes. Test your function before continuing on.
- Add another function which, given n and k from the above program, returns the product n*(n-1)*...*(n-k+1).
- Modify your main function to use these two new functions to calculate the odds from the above section.
3. Your second program
Let's write a guessing game. The following line creates a pseudo-random number generator, seeded on the current time in milliseconds.
Random rand = new Random(System.currentTimeMillis());
After that point, every time we call rand.nextInt(anInt), it will return a random number between 0 (inclusive) and anInt (exclusive). Random, by the way, is in java.util, and has a lot more interesting functions you can see in your book.
Write a program that comes up with a random number between 0 and 10, and then asks the user for a guess until they get it correct. The more violent the berating on each wrong guess, the better. Code this in a file called Lab1c.java
Submission
Submit all three of these to your instructor by lab next week.