Lab Exercises
- Exercise 1: Declare, Initialize, Print variables (30 minutes)
- Exercise 2: Conditional operator (30 minutes)
- Exercise 3: Averaging numbers (20 minutes)
- Exercise 4: Find greatest number (20 minutes)
- Homework Exercise (for people who are taking Sang Shin’s “Java Programming online course”)
Exercise 1: Declare, Initialize, Print variables
(1.1) Build and run OutputVariable Java program using NetBeans IDE
1. Create a NetBeans project
- Select File from top-level menu and select New Project.
- Observe that the New Project dialog box appears.
- Select Java under Categories section and Java Application under Projects section.
- Click Next.
- Under Name and Location pane, for the Project Name field, enter MyOutputVariableProject. This is the name you are giving to the newly createdly NetBeans project.
- For the Create Main Class field, enter OutputVariable. (Figure-1.10 below) This is to create OutVariable.java, in which main(..) method will be created for you.
- Click Finish.
Figure-1.10: Create OutVariable project
- Observe that the MyOutputVariableProject project node is created under Projects pane of the NetBeans IDE and IDE generated OutputVariable.java is displayed in the editor window of the IDE.
2. Modify the IDE generated OutputVariable.java.
- Modify the OutputVariable.java as shown in Code-1.11 and Figure-1.12 below. The code fragments that need to be added are highlighted in bold and blue-colored font.
/* * OutputVariable.java * * Created on January 19, 2007, 6:30 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /** /** // Variable value is int primitive type and it is initialized to 10 // Variable x is char primitive type and it is initialized to ‘A’ // Variable grade is a double type // Display the value of variable “value” on the standard output device // Display the value of variable “x” on the standard output device // Display the value of variable “grade” on the standard output device } |
Code-1.11: Modified OutputVariable.java
Figure-1.12: Modified OutputVariable.java
3. Build and run the program
- Right click MyOutputVariableProject and select Run.
- Observe the result in the Output window of the NetBeans IDE. (Figure-1.13 below)
Figure-1.13: Result of running OutputVariable program
(1.2) Build and run OutputVariable Java program using “javac” compiler and “java”
1. Go to a directory where you are going to write Java programs
2. Write OutputVariable.java using your editor of choice (in this example, I am using jedit) as shown in Code-1.20 below.
C:\myjavaprograms>jedit OutputVariable.java
public class OutputVariable {
public static void main( String[] args ){ // Variable value is int primitive type and it is initialized to 10 // Variable x is char primitive type and it is initialized to ‘A’ // Display the value of variable “value” on the standard output device // Display the value of variable “x” on the standard output device } } |
Code-1.20: OutputVariable.java
3. Compile OutputVariable.java using javac compiler. The javac compiler comes with J2SE SDK you’ve download. It resides in %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (Solaris/Linux) directory. The result of compilation will be the creation of OutputVariable.class file.
4. Run the OutputVariable program using java command. The java command starts the Java Virtual Machine and runs the OutputVariable program in this example. A Java program can be made of multiple Java classes and and a set of libraries. In this example, the OutputVariable program just contains a single class called OutputVariable.class. You can regard the java command as Java interpreter.
10
The value of x=A
7. Modify OutputVariable.java as shown in Code-1.21 below. You are modifying it to add double type variable called grade and displays the value of it. The code fragments that need to be added are highlighted in bold and blue-colored font.
public class OutputVariable {
public static void main( String[] args ){ // Variable value is int primitive type and it is initialized to 10 // Variable x is char primitive type and it is initialized to ‘A’ // Variable grade is a double type // Display the value of variable “value” on the standard output device // Display the value of variable “x” on the standard output device // Display the value of variable “grade” on the standard output device } } |
Code-1.21: Modified OutputVariable.java
8. Compile and run the program. Observe that new message is displayed.
C:\myjavaprograms>java OutputVariable
10
The value of x=A
The value of grade =11.0
Summary
In this exercise, you have built and run OutputVariable Java program using javac compiler and java command and NetBeans IDE.
Return to the top
Exercise 2: Conditional operator
(2.1) Build and run a Java program that uses conditional operators
1. Create a NetBeans project
- Select File from top-level menu and select New Project.
- Observe that the New Project dialog box appears.
- Select Java under Categories section and Java Application under Projects section.
- Click Next.
- Under Name and Location pane, for the Project Name field, enter MyConditionalOperatorProject.
- For the Create Main Class field, enter ConditionalOperator. (Figure-1.20 below)
- Click Finish.
- Observe that the MyConditionalOperatorProject project node is created under Projects pane of the NetBeans IDE and IDE generated ConditionalOperator.java is displayed in the editor window of the IDE.
- Modify the ConditionalOperator.java as shown in Code-2.21 below. The code fragments that need to be added are highlighted in bold and blue-colored font.
public class ConditionalOperator {
/** Creates a new instance of ConditionalOperator */ /** // Declare and initialize two variables, one String type variable // Get status of the student. // Print status. } |
Code-2.21: Modified ConditionalOperator.java
3. Build and run the program
- Right click MyConditionalOperatorProject and select Run.
- Observe the result in the Output window of the NetBeans IDE. (Figure-2.22 below)
Figure-2.22: Result of running the program
4. Modify ConditionalOperator.java as adding the following lines of code at the appropriate place, build and run the program
- int salary = 100000;
- Print “You are rich!” if the salary is over 50000. Print “You are poor!” otherwise.
Summary
In this exercise, you have built and run a Java application using NetBeans IDE.
Exercise 3: Averaging numbers
(3.1) Build and run a Java program
1. Create a NetBeans project
- Select File from top-level menu and select New Project.
- Observe that the New Project dialog box appears.
- Select Java under Categories section and Java Application under Projects section.
- Click Next.
- Under Name and Location pane, for the Project Name field, enter MyAverageNumberProject.
- For the Create Main Class field, enter AverageNumber.
- Click Finish.
- Observe that the MyAverageNumberProject project node is created under Projects pane of the NetBeans IDE and IDE generated AverageNumber.java is displayed in the editor window of the IDE.
- Modify the AverageNumber.java as shown in Code-3.11 below. The code fragments that need to be added are highlighted in bold and blue-colored font.
public class AverageNumber {
/** Creates a new instance of AverageNumber */ /** //get the average of the three numbers //prints the output on the screen } |
Code-3.11: Modified AverageNumber.java
3. Build and run the program
- Right click MyAverageNumberProject and select Run.
- Observe the result in the Output window of the NetBeans IDE. (Figure-3.12 below)
Figure-3.12: Result of running AverageNumber program
Exercise 4: Find greatest number
(4.1) Build and run a Java program that uses conditional operators
1. Create a NetBeans project
- Select File from top-level menu and select New Project.
- Observe that the New Project dialog box appears.
- Select Java under Categories section and Java Application under Projects section.
- Click Next.
- Under Name and Location pane, for the Project Name field, enter MyGreatestValueProject.
- For the Create Main Class field, enter GreatestValue. (Figure-4.10 below)
- Click Finish.
Figure-4.10: Create MyGreatestValueProject
- Observe that the MyGreatestValueProject project node is created under Projects pane of the NetBeans IDE and IDE generated GreatestValue.java is displayed in the editor window of the IDE.
- Modify the GreatestValue.java as shown in Code-4.11 below. The code fragments that need to be added are highlighted in bold and blue-colored font.
public class GreatestValue {
/** Creates a new instance of GreatestValue */ /** //determines the highest number //prints the output on the screen } |
Code-4.11: Modified GreatestValue.java
3. Build and run the program
- Right click MyGreatestValueProject and select Run.
- Observe the result in the Output window of the NetBeans IDE. (Figure-4.12 below)
Figure-4.12: Result of running MyGreatestValueProject
Homework exercise (for people who are taking Sang Shin’s “Java Programming online course”)
- Compute the smallest number and display it (as you did compute and display greatest number)
- If the smallest number is less than 10, display “The smallest number is less than 10!”. Otherwise, display ” The smallest number is greater than or equal to 10!”.
- Zip file of the the MyGreatestValueProject2 NetBeans project. (Someone else should be able to open and run it as a NetBeans project.) You can use your favorite zip utility or you can use “jar” utility that comes with JDK as following.
- cd <parent directory that contains MyGreatestValueProject2 directory> (assuming you named your project as MyGreatestValueProject2)
- jar cvf MyGreatestValueProject2.zip MyGreatestValueProject2 (MyGreatestValueProject2 should contain nbproject directory)
- Captured output screen – name it as JavaIntro-javaprogbasics.gif orJavaIntro-javaprogbasics.jpg (or JavaIntro-javaprogbasics.<whatver graphics format>)
- Any screen capture that shows that your program is working is good enough. No cosmetic polishment is required.
- If you decide to use different IDE other than NetBeans, the zip file should contain all the files that are needed for rebuilding the project – war file with necessary source files is OK.