Tuesday, 9 October 2018

Lab 1 Problem Solving using Java (20/09/18)

Hi guys~~
Today is my second lab session for the programming class. We are given task which is solving the problem using JAVA program. There are 3 questions that I need to solve and you can see it as in below. The objective of this lesson is to learn how to identify and fix the errors, construct the Problem Analysis Chart (PAC) and write Java code.

QUESTION 1
Write each of the classes below in separate Java files. Compile, identify and fix the errors.

Test 1
a) Write the code in notepad and compile it by typing "javac PublicClassTest1.java" in cmd.

 


b) Error! Cannot find the public class. Why?
Take note that our filename must be the same as in our notepad (public class). Therefore, we change our filename from "PublicClassTest1" to "Test1". Then, we compile it again and there will be several errors need to be fixed in the notepad.


c) Fix the errors.
Row 3: We should put "static".
Row 9: Careful with the " sign.


c) After we fix the errors, we try to compile it again and run it.
The answer would be "J is 52.0 and K is 51.0".


d) Done for Test 1.


Test 2
a) Write the code and compile it.


b) Fix Errors.
We need to define what is " int k " in order for us to find " int i ". So, I put int k as number 3 (int k = 3).
 We can show the answer specifically by writing "value is:" +i. This means that the answer would be "value is: 5" instead of "5".


c) Compile and run the code again.


d) Finish for Test2.


Test 3
a) Write the code and compile it.


b) Fix errors.
 Line 2: Public must be in the small letter. Take note!
I define one by one for the method and put it in an equation.


c) Compile and run it again.


d) Finish for Test3.


Test 4
a) Write the code and compile it.



b) The answer is correct but we need to show it properly by writing " System.out.println("value is:" +amount/2) " in the notepad. It means that the answer would be " value is: 2 " instead of " 2 ".



c) Finish for Test4.


Test 5
a) Write the code and compile it.


b) Fix errors.
The terms must be the same including capital or small letter. Here I change the "interestrate" term to "interestRate". The R should be in capital letter.


c) Compile and run it again.


d) Finish for Test5.


Test 6
a) Write the code and compile it.



b) Fix errors.
Change the value 7 to 6.


c) Compile and run it again.


d) Finish for Test6.


Test 7
a) Write the code and compile it.



b) Fix errors.
Change the " - " into " + ".


c) Compile and run it again.


d) Finish for Test7.


Test 8
a) Write the code and compile it.



 b) Fix errors.


c) Compile and run it again.


d) Finish for Test8.


CelsiusToFahrenheit
a) Write the code and compile it.



b) Fix errors.
 Change the formula.


c) Compile and run it again.


d) Finish for CelsiusToFahrenheit.


Welcome
a) Write the code and compile it.



 b) Fix errors.
Be careful with the notations ( eg. "" and ; ).


c) Compile and run it again.


d) Finish for Welcome.



QUESTION 2
Analyse the following problems using Problem Analysis Chart (PAC).

(a) One acre of land is equivalent to 43,560 square feet. Calculate the number of acres in a tract of land with 389,767 square feet.

Data
Processing
Output
Number of square feet per one acre of land
Calculate the number of acres of land

Number of acres of land


(b) Calculate the average of three test scores and display all of them.

Data
Processing
Output
Three test scores


Calculate the average of 3 test scores

Average of three test scores



(c) An electronics company sells circuit boards at 40 percent profit. If you know the retail price of a circuit board, you can calculate its profit with the following formula :
Profit = Retail price x 0.4 
Calculate the amount of profit earned for that product based on the retail price and display the results.

Data
Processing
Output
Percentage of profit and retail price of a circuit board

Calculate the profit

Amount of profit earned



QUESTION 3
Choose one from the following problems and analyze it using PAC. Then write a complete Java program to solve it.

If you know the balance and the annual percentage interest rate, you can compute the interest on the next monthly payment using the following formula: 
Interest = balance X ( annualInterestRate/1200 ) 
Write a program that displays the interest for the next month if the balance is RM1000 and interest rate of 3.5%.

a) Write the PAC analysis.

Data
Processing
Output
Balance = b
Annual percentage interest rate = i
Calculate the interest for next monthly payment

Interest  =  balance x (annual interest rate/1200)
Interest for next month
b) Write the code in notepad.


b) Compile and run the code.


c) Done.




Henny Abigailwillyen Sinjus
183640
SSK3100 (Group 15)

Next blog: nurnadhirahzulkepli.blogspot.com

No comments:

Post a Comment