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
- Login into cs2.uwsuper.edu machine by using WinScp.
Use the following login data:
login: your UWS login
password: your student ID number |
- Change to your CSCI201 working directory in the right part of the WinScp
window.
- Open Netbeans IDE and start a new Java project. Name it First.
- 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!");
}
}
|
- Compile and run it.
- 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
- Close your first project and start another one named Second.
- Use the following Java code to
compose the main() method of your project. Then compile and run it.
- 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:
- the sum (store it in variable sum)
- the difference (store it in variable diff)
- the product (store it in variable prod)
- the average (store it in variable ave)
- the average of their squares (store it in variable sqave)
- the absolute value of the difference (use method Math.abs(a - b))
(store it in variable abs)
- the maximum (the largest of two, use Math.max(a, b))
(store it in variable max)
- the minimum (the smallest of two, use Math.min(a, b))
(store it in variable min)
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
- Read the part "Primitive data types" in the
online document
- Download the program KbInput2.java
for input numbers. Compile and run it, and be sure to understand how it
works.
- Modify your Second project
so that the integers a and b are entered from the keyboard.
Compile and run the modified code.
- 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
- Create a new project MathDial2.java and
understand how it works.
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.
- Compile the file MathDial2.java and copy
the obtained file MathDial2.class to your local
Windows computer and run it from there.
- Check Java docs for JOptionPane