Page 11 - KMK PREPSPM2 1819 ANSWER
P. 11
SULIT No. Matrik…………………………………….… No. Kad Pengenalan………………………….. SC015
import java.util.Scanner;
class Egg{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
char grade;
int qty;
double pay=0;
grade=sc.next().charAt(0);
qty=sc.nextInt();
if(grade=='A')
pay = qty * 4.50/12;
else if(grade=='B')
pay = qty * 3.50/12;
else if(grade=='C')
pay = qty * 2.90/12;
else
System.out.println("Invalid grade.");
System.out.println(pay);
}
}
(b) An identifier is a word that you make up to refer to a Java programming
element by name. Identifiers most commonly used for variables, classes,
methods, and parameters.
(i) Suppose the value for variables a, b, c, d, e and f is 1, 3, 5, 7, 9 and 1
respectively.
Evaluate the following expressions:
a. b / f – (a + a * b)
[1 mark]
-1
b. 5 - (a + b * f) – a / b
[1 mark]
1
c. a == b + c || true && d / e >= (f – e)
[1 mark]
true
(ii) Construct the following expressions in correct Java syntax.
2
a. p + 3q
20
[1 mark]
Math.pow(p,20) + 3*q*q;
2
b. b – √4ac
11 SULIT

