Langara College
Department of Computing Science & Information Systems
CPSC1181 – Object-oriented Computing
Lab1: Java Essentials
Objectives:
- Create Java projects and packages
- Use Javadoc to create Java documentation
- Review on the Java essentials
- Work on variables, conditions, loops, methods, and arrays
Notes on Installing Java and Eclipse to your computers
1. Go to the following site to download Java and install it
[Link]
2. Set the path so that you can run the Java compiler javac and run your Java class.
In order to set the path, you need to edit your Environment Variables. The steps shown
below is for Windows 10. For other OS, the steps may be different.
1) Right-click This PC, then Properties.
2) Click Advanced system settings.
3) Click the Environment Variables… button.
4) In the Environment Variables dialog box, go to the top section called User variables.
5) If you already have a variable called Path, select the line for Path and then click the
Edit button. Add a new line and then copy and paste the JDK bin path to this new
line, which is like the following
C:\Program Files\Java\jdk-19\bin
Click Move Up until this new item reaches the top line.
6) If you do not have a Path variable, then click the New button, Type Path in the
Variable name field, and copy and paste the path given above to the Variable value
field. Click OK.
7) Click OK to close the Environment Variables dialog box.
8) Click OK to close the System Properties window.
3. Go to the following site to download Eclipse and install it
1
[Link]
Problems [35 marks]
Instructions:
1. Create a folder named LabProjects to store all the lab files for this course
2. Open Eclipse and make LabProjects as the workspace.
3. Inside Eclipse, create a project named Lab1 to store all the files for this lab. Make sure
uncheck Create [Link] file.
4. Add Javadoc comments and regular comments to your code
Problem 1: [10 marks] Find denominations (filename: [Link])
1. Inside Eclips, right-click project Lab1->New->Class to add a new class
2. Enter lab1 as the package name
3. Enter FindDenominations as the class name
4. Leave the modifiers as public and none
5. Check public static void main(String[] args) and Generate comments
6. Click Finish
7. Write code that asks the user to enter an amount of money from 0 to 99 cents. The
program then calculates and displays the number of coins from each denomination:
quarters (25 cents), dimes (10 cents), nickels (5 cents), and cents.
8. Click Run to run the program.
For example, if the user enters 93, your program should display
There are 3 quarters, 1 dimes, 1 nickels, and 3 cents in 93 cents
Problem 2: [10 marks] Shopping Receipt (filename: [Link])
1. Add another new class to the project Lab1 and name the new class as ShoppingReceipt
2. Write a program that asks the user to enter a shopping item name, unit price and the
quantity of the item, and then calculates and displays the subtotal (which is the unit price
times the quantity but every third piece gets half price), GST (5% of the subtotal), PST
2
(7% of the subtotal), and the total which is the subtotal plus GST plus PST. Keep two
decimals for all the calculated values.
A sample run is given below:
Enter the item name: Cap
Enter the item price: 20.5
Enter the quantity: 7
Your Receipt
Item name: Cap
Quantity: 7
Item price: $ 20.50
Subtotal: $ 123.00
GST (5%): $ 6.15
PST (7%): $ 8.61
Total: $ 137.76
Problem 3: [15 marks] Find the smallest value of each row of a 2D array
(filename: [Link])
1. Add another new class to the project Lab1 and name the new class as
FindSmallestValues
2. Write a method with the following header to return an array of integer values which
are the smallest values of each row of a 2D array of integer values
public static int[] smallestValues(int[][] num)
For example, if the 2D array passed to the method is:
85266
36545
75450
88516
The method returns an array of integers which are the smallest values from
each row as follows:
2
3
0
1
3
3. In the main method, create a 2D array of integer values with the array size 4
by 5 and invoke the method to find and display the smallest values from each
row of the 2D array.
Generate the values of the 2D array using the [Link]() method with the
range from 0 to 9.
What to hand in
1. Click Project->Generate Javadoc… to geneate the Javadoc documents for all the java
files inside Lab1->src->lab1. Leave all the other selections unchanged.
2. Zip the folder Lab1 and upload it to D2L.
When to hand in
By 11:59pm, Wednesday, January 11, 2023.