public static void main(String args[])
{
long a,g,x;
double b,c,y;
boolean d;
final double pi;
String e,f,h,z,name;
a=100;
b=2.3;
c=-52.2;
d=true;
e="I am ";
f="a student";
g=0;
h="!";
pi=3.142;
name=" sadiq ";
//Q1
y=a+b;
System.out.println(y);
//Q2
System.out.println("no answer");
//Q3
z=e+f;
System.out.println(z);
//Q4
y = b*c;
System.out.println(y);
//Q5
System.out.println("no answer");
//Q6
System.out.println("no answer");
//Q7
System.out.println("no answer");
//Q8
x=10;
System.out.println(Math.pow(x,3));
//Q9
z=name+" is "+f+h;
System.out.println(z);
//Q10
System.out.println("no answer");
//Q11
System.out.println("no answer");
//Q12
y=100.3;
y=(y/(a + b))-c;
System.out.println(y);
//Q13
System.out.println("no answer");
//Q14
System.out.println("no answer");
//Q15
y=((pi+1)/(pi+2))/(pi+3);
System.out.println(y);
//Q16
y=-2;
System.out.println(Math.pow((Math.pow(y, 2)/b),(1.0/3.0)));
//Q17
System.out.println("no answer");
//Q18
z=name;
z=b/g+z;
System.out.println(z);
//Q19
y=-2.3;
y=a*(Math.pow(y,2))+b*y+c;
System.out.println(y);
//Q20
System.out.print("no answer");
}
}
,1.) 102.3
2.) No answer (Boolean error)
3.) I am a student
4.) -120.06
5.) No answer (pi is constant)
6.) No answer (string / 0)
7.) No answer (z is string)
8.) 1000.0
9.) sadiq is a student!
10.) No answer
11.) No answer
12.) 53.18044965786902
13.) No answer (string)
14.) No answer (cant subtract a char)
15.) 0.13114997439694126
16.) 1.2025709773288682
17.) No answer (cannot divide by 0)
18.) Infinity sadiq
19.) 471.50999999999993
20.) No answer
, CS1702 Introductory Programming (2018-2019)
Laboratory 3 – Week 4
Conditional Statements in Java
3.1 Introduction
The laboratory session covers conditional statements in Java. Again we will be implementing
some of the examples given during the lecture, followed by some additional examples. We
will then look at a few sections of the help pages.
Note that: this worksheet is one of the worksheets from which your laboratory
worksheets portfolio of work will be assessed [CodeRunner tests].
3.2 Relational operators
Create a new project and Java class called CS1702_Lab3. Add the following code:
static public void main(String args[])
{
int a = 1000,b = -22;
if (a < b)
{
System.out.println("a is less than b");
}
else
{
System.out.println("a is NOT less than b");
}
}
Run the program. It should display the message “a is NOT less than b”. Test the program on a
number of different values of a and b such that both parts of the if statement is run. Now
add a similar set of program code that tests if a is greater than b.
Using the above code “snippet” as a basis, write a set of Java if statements that determines if
the following statements are true, note that you must declare the variables first and choose the
correct type.
Let x = 100, y = 204, z = -23.1, a = true, b = false, c = -204
1) x<y
2) x > z and a = b
3) 2c > y
4) x=b
5) c ≠ y or c = y
6) z ≠ y and c = a
7) y ≥ y and a+3 ≠ 2
3/26