CSCI 201: Intro to Programming (Java)

Lab 1: Getting started with Java and Netbeans

Resources

Lab Task I: compiling and saving your first Java project

  1. Login into cs2.uwsuper.edu machine by using WinScp. Use the following login data:
    login: your UWS login
    password: your student ID number
  2. Change to your CSCI201 working directory in the right part of the WinScp window.
  3. Open Netbeans IDE and start a new Java project. Name it First.
  4. Use the following Java code to compose the main() method of your project:
    public class First 
    {
       public static void main(String[] args)
       {
          System.out.println("Hello World!");
       }
    }
    
  5. Compile and run it.
  6. ZIP your project folder and copy the archived folder into the server account by dragging the ZIP fil into the right WinScp window.

Lab Task II: simple arithmetic with Java

  1. Close your first project and start another one named Second.
  2. Use the following Java code to compose the main() method of your project. Then compile and run it.
  3. Modify it so that it declares two integers named a and b instead, and initializes them with some values (your choice). The program should then compute:

    Output all computed values to the screen in the form
    "The sum is ...", etc.

Implementation hints

Arithmetic operations

Java supports the following arithmetic operations:

Java code Comments
a + b addition
a - b subtraction
a * b multiplication
a / b integer division
a % b the remainder of a/b

The order of precedence can be controlled by means of parenthesis. For example, ((a + b) * 6) / 5


Lab Task III: working with basic Java data types

  1. Read the part "Primitive data types" in the online document
  2. Download the program KbInput2.java for input numbers. Compile and run it, and be sure to understand how it works.
  3. Modify your Second project so that the integers a and b are entered from the keyboard. Compile and run the modified code.
  4. Further modify the program so that the entered numbers a and b are of type double. Output the computed values with 3 digits after the decimal point.
    Hint: use the format string "%s %.3f" with printf.

Implementations hints

Numbers and variables

Before using a variable for the first time, one should declare its type. The below examples are only for illustrative purposes.

Java code Comments
5 an integer number
6.8 a floating point number
2.0 another floating point number
int n = -4; declaration and initialization of an integer variable
double p = 4.75; declaration and initialization of a floating point variable
double q = a + (b / 3.5); initialization of q with an expression

Lab Task IV: (if you wish a challenge)

Adding numbers with a graphics dialog

  1. Create a new project MathDial2.java and understand how it works.
  2. Try it in action. You can skip this step.

    Your browser must be configured to report a MIME type of application/x-java-jnlp-file for files with extension jnlp (JNLP is a short for Java Network Launch Protocol). Usually, the browser is automatically configured for JNLP in case the SUN JRE is installed. You can check/configure the browser by assigning the javaws application (a part of JRE) with the file type application/x-java-jnlp-file.

  3. Compile the file MathDial2.java and copy the obtained file MathDial2.class to your local Windows computer and run it from there.

  4. Check Java docs for JOptionPane