IC211: Lab 1

Due: End of class on Thursday 13 Jan.
Concepts
  1. Introduce the Netbeans IDE
  2. Write, compile, and run programs using Netbeans
You will be demonstrating three things to your instructor. If you complete all three in lab then there is nothing to turn in. If you do not complete all three, then complete them before the next class and demo them to your instructor before class starts. The lab is intended to get you up and running with java from the command line and using Netbeans. 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 can't accomplish. You will need to be able to write, compile and run java programs starting with homework 2 that will be assigned with the next class. Netbeans is available on the lab machines and you can download and install it on your personal machine. 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.

Running Netbeans

  1. Run Netbeans using either the menu of from the command line.
  2. Create a new Project and name it Lab1a.
  3. Put the following code in the main function:
    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]);
    }
    
    Look at the left of the source code window where the line numbers are, do you see a light bulb with a red circle? Point your mouse at the light bulb without clicking. You should get a pop up box telling you that thae variable K is unknown. Errors will be indicated as you type there in the left column. Go ahead and fix that now.
  4. Run the program, and check the output.
  5. Modify the program by replacing the for loop with a while loop. Show this to the instructor.

Your first Program

  1. Make a new project named Lab1b.
  2. Add a line saying import java.util.*; right below the line that begins with the work package.
  3. 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);
    
  4. What happens if you type your name instead of an integer?
  5. 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. There is a brief description of Scanner in your text.
  6. 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.
  7. 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++.
  8. Show your instructor.

Adding functions

  1. Take the code you use to compute the answer in the previous part and move it to a new function.
  2. Place that function immediately after the } that finishes main. Note that functions (for now) should start with the magic incantation public static, otherwise they look just like C++ functions.
  3. Show this to your instructor.