Lab Exercises
- Exercise 1: Build and run Java programs that use Java array of int (30 minutes)
- Exercise 2: Build and run Java programs that use Java array of String (30 minutes)
- Homework Exercise (for people who are taking Sang Shin’s “Java Programming online course”)
Exercise 1: Build and run Java programs that use Java array of int
- Build and run a Java program that uses single-dimensional array of int
- Build and run a Java program that uses two-dimensional array of int
- Build and run a Java program that computes the greatest number
(1.1) Build and run a Java program that uses single-demensional array of int
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 MyJavaArrayProject.
- For the Create Main Class field, enter JavaArray. (Figure-1.10 below)
- Click Finish.
Figure-1.10: Create a new NetBeans project
- Observe that the MyJavaArrayProject project node is created under Projects pane of the NetBeans IDE and IDE generated JavaArray.java is displayed in the editor window of the IDE.
- Modify the JavaArray.java as shown in Code-1.11 below.
public class JavaArray {
/** Creates a new instance of JavaArray */ /** // Declare and create int array whose size is 10 // Display the value of each entry in the array } |
Code-1.11: Modified JavaArray.java
3. Build and run the program
- Right click MyJavaArrayProject and select Run.
- Observe the result in the Output window. (Figure-1.12 below)
0 0 0 0 0 0 0 0 0 0 |
Figure-1.12: Result
- Just before the for loop that prints out the value of each entry of the ages[] array, create another for loop in which a value of 100 is assigned to the first entry of the array, ages[0], 101 to the next entry of the array, ages[1], and so on.
(1.2) Build and run a Java program that uses two-dimensional array of int
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 MyJavaTwoDimensionArrayProject.
- For the Create Main Class field, enter JavaTwoDimensionArray. (Figure-1.10 below)
- Click Finish.
- Observe that the MyJavaTwoDimensionArrayProject project node is created under Projects pane of the NetBeans IDE and IDE generated JavaTwoDimensionArray.java is displayed in the editor window of the IDE.
- Modify the JavaTwoDimensionArray.java as shown in Code-1.11 below.
public class JavaTwoDimensionArray {
/** Creates a new instance of JavaTwoDimensionArray */ /** // Declare and create two dimensional int array whose size is 10 by 5 // Display the number of rows and columns // Display the value of each entry in the array |
Code-1.11: Modified JavaTwoDimensionArray.java
3. Build and run the program
- Right click MyJavaTwoDimensionArrayProject and select Run.
- Observe the result in the Output window. (Figure-1.12 below)
ages.length = 10 ages[1].length = 5 Starting row 0 |
Figure-1.12: Result
- Declare and initialize 3-dimensional array of int
- Initialize each cell with increasing integer number starting from 1000, 1001, 1002, and so on.
(1.3) Build and run a Java program that finds the greatest number
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 MyGreatestNumberProject.
- For the Create Main Class field, enter GreatestNumber. (Figure-1.10 below)
- Click Finish.
- Observe that the MyGreatestNumberProject project node is created under Projects pane of the NetBeans IDE and IDE generated GreatestNumber.java is displayed in the editor window of the IDE.
- Modify the GreatestNumber.java as shown in Code-1.11 below.
import javax.swing.JOptionPane;
public class GreatestNumber { public static void main(String[] args) { // Prompt a user to enter numbers num[counter] = Integer.parseInt // Compute the greatest number up to this point // Display the greatest number. } |
Code-1.11: Modified GreatestNumber.java
3. Build and run the program
- Right click MyGreatestNumberProject and select Run.
- Enter 3 numbers.
- Observe that the greatest number is displayed.
- Compute the smallest number and display it.
Summary
In this exercise, you learned how to use Java array.
Exercise 2: Build and run Java programs that use Java array of String
(2.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 MyDaysOfTheWeekProject.
- For the Create Main Class field, enter DaysOfTheWeek.
- Click Finish.
- Modify the DaysOfTheWeek.java as shown in Code-2.11 below.
public class DaysOfTheWeek {
public static void main(String[] args) { // Declare and initialize String array of the days of the week // Display days of the week using while loop // Display days of the week using do-while loop // Display days of the week using for loop } } |
Code-2.11: Modified DaysOfTheWeek.java
3. Build and run the program
- Right click MyDaysOfTheWeekProject and select Run.
- Observe that the days of the week are displayed 3 times. (Figure-2.12 below)
Display days of week using while loop Sunday Monday Tuesday Wednesday Thursday Friday Saturday Display days of week using do-while loop Sunday Monday Tuesday Wednesday Thursday Friday Saturday Display days of week using for loop Sunday Monday Tuesday Wednesday Thursday Friday Saturday |
Figure-2.12: Result
Homework exercise (for people who are taking Sang Shin’s “Java Programming online course”)
- Ask the English version of the full names (first-name last-name) of three of your family members using showInputDialog() method of the JOptionPane class.
- Display the name of the family member who has the longest first name (not the longest of the total name) as following. (If there is a tie, just display one of the two.)
- Daniel Shin has the longest first name
// Get the first name from a name string using split() instance (non-static) method String[] nameArrayForPerson1 = person1NameInstance.split(” “); String[] nameArrayForPerson2 = person2NameInstance.split(” “); // Get the lengths of strings using length() instance (non-static) method // Compare the lengths of the first names between person1 and person2 |
- Zip file of the the MyOwnJavaArrayProject 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 MyOwnJavaArray directory> (assuming you named your project as MyOwnJavaArrayProject)
- jar cvf MyOwnJavaArrayProject.zip MyOwnJavaArrayProject (MyOwnJavaArrayProject should contain nbproject directory)
- Captured output screen – name it as JavaIntro-javaarray.gif orJavaIntro-javaarray.jpg (or JavaIntro-javaarray.<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.