Workshop 2
Workshop 2
Workshop 2
Duration: 90’
======================================================================
Software Requirements
Netbean 8.2 or later, JDK 8, Notepad, Command Prompt, WinRAR / WinZip with Windows
Explorer (File Explorer) on Windows 7 and above.
Students are ONLY Allowed to use:
His / her study materials like sample codes and program examples are stored on his / her
computer only.
Instructions
Step 1: Students download the given materials from the exam software.
Step 2: Students read questions and prepare answers in the given template.
Step 3: Prepare to submit the answer:
o For each question (e.g., question 1), please create two sub-folders: run and src.
o Copy the *.jar file into the run folder, and the entire project source code file into the
src folder.
Step 4: Submit a solution for each question:
o Choose the question number (e.g., 1) in PEA software, and then attach the
corresponding solution folder (e.g., 1). Click the Submit button to finish submitting
this question.
o
Note:
o Solutions will be marked by Automated Marking Software.
o The use of tools other than those allowed in the above section whether intentionally or
unintentionally, is considered a violation of the exam rules, and the exam result will be 0.
o Do not change the names of the folders and files specified (or required ) in the exam and the
name of the submit folders in Step 04 must be correct. If you change it (incorrectly), the
marking software can not find the execute file (.jar) to mark, thus the exam result will be 0.
======================================================================
Question 1: (3 marks)
Write a class Bike (in the default package of the NetBean) with the following information:
1
Bike Where:
- id : int Bike () - default constructor
- name : String Bike(id:int, name:String, price:double) -
constructor, which sets values to id,
- price: double
name, price)
getName ():String – return bike name
+ Bike () with uppercase
+ Bike(id:int,name:String,price:double) getSalePrice():double – increase price by
+ getName(): String 10%.
+ getSalePrice ():double Override toString() method to return a
+ toString():String string that contains all the information of
the Bike: id, name, and price. The price is
formatted by two decimal places.
Question 2: (3 marks)
Write a class Product (in the default package of the NetBean) with the following information:
Product Where:
- id : int Product () - default constructor
- name: String Product(id:int, name:String,
price:double, quantity:int,
- price: double
discount:int) - constructor,
- quantity: int which sets values to id, name,
- discount: int price, quantity, and discount
+Product () (default discount = 0)
+Product(id:int,name:String,price:double,quantity:int setDiscount(int discount):void –
, discount:int) update discount
+setDiscount(int discount): void subTotal():double – return
subtotal [subtotal = quantity *
+subTotal():double price * (100-discount)/100 ]
2
Enter id:1 Enter id:1
Enter name:coca Enter name:coca
Enter price:5.5 Enter price:5.5
Enter quantity:20 Enter quantity:20
1.Test subTotal 1.Test subTotal
2.Test setDiscount 2.Test setDiscount
Enter TC(1 or 2):1 Enter TC(1 or 2):2
OUTPUT: Enter discount(%):50
110.00 OUTPUT:
55.00
Question 3: (4 marks)
Write a class Trapezium (in the default package of the NetBean) with the following information:
Trapezium Where:
-firstEdge:float Trapezium() - default constructor.
-secondEdge:float Trapezium(firstEdge:float, secondEdge:float,
height:float) - constructor, which sets values to
-height:float
first edge, second edge and height of the
+Trapezium() trapezium.
+Trapezium(firstEdge:float, getAcreage():float – Returns the area of the
secondEdge:float, height:float) trapezium with 2 decimal places.
+getAcreage():float getInfo(): String – Returns information about
+getInfo():String whether the trapezium is a “Regular trapezium”
+setFirstEdge(fe:float):void (firstEdge is different secondEdge) or “Isosceles
trapezium” (first edge equals to second edge)
+setSecondEdge(se:float):void
setFirstEdge(fe:float): void – update value to
firstEdge and print out “Success” if Valid values
are greater than 0. Other else, print out “Invalid
value”
setSecondEdge(se:float):void – update value to
secondEdge and print out “Success” if Valid
values are greater than 0. Other else, print out
“Invalid value”
Do not format the result.
The program output might look something like this:
Enter first edge: 12.5 Enter first edge: 12.5
second edge: 17 second edge: 17
height: 8.3 height: 8.3
1. Test getAcreage() 1. Test getAcreage ()
2. Test getInfo() 2. Test getInfo()
3. Test setFirstEdge() 3. Test setFirstEdge()
4. Test setSecondEdge() 4. Test setSecondEdge()
Enter TC (1, 2, 3 or 4): 1 Enter TC (1, 2, 3 or 4): 2
OUTPUT: OUTPUT:
122.43 Regular trapezium
3
Enter first edge: 12.5 Enter first edge: 18
second edge: 17 second edge: 18
height: 8.3 height: 7
1. Test getAcreage () 1. Test getAcreage ()
2. Test getInfo() 2. Test getInfo()
3. Test setFirstEdge() 3. Test setFirstEdge()
4. Test setSecondEdge() 4. Test setSecondEdge()
Enter TC (1, 2, 3 or 4): 3 Enter TC (1, 2, 3 or 4): 2
Enter new First edge: 0 OUTPUT:
OUTPUT: Isosceles trapezium
Invalid value