Você está na página 1de 5

Java Chapter 2

Java Unit 2

Lab Activities 2
Aktiviti Makmal
2
Lab Exercises Java Chapter 2

Java Unit 2
Aktiviti Makmal 2

Act1:
class Arith
{
public static void main(String args[])
{
double p=12.5;
double q=3.0;
p*=q-1;
q-=p+1;
System.out.println("p is" + p + "q is" + q );
}
}

Act2:
class Arith_Assign_Opr
{
public static void main(String args[])
{
double p=12.5;
double q=3.0;
p*=q-1;
q-=p+1;
System.out.println("p is" + p + "q is" + q );
}
}

Act3:
class IdentifyConst
{
public static void main(String args[])
{
int radius=2;
final double pi=3.14;
double AreaofCircle=pi*radius*radius;
System.out.println(AreaofCircle);
}
}

2
No Pain No Gain
Lab Exercises Java Chapter 2

Act4:
class IdentifyVar
{
public static void main(String args[])
{
int qty=2;
System.out.println("There are "+qty+" pens.");
}
}

Act5:
class Post_Incr_Opr
{
public static void main(String args[])
{
int x=99;
int y=10;
y=x++;
System.out.println("x: " + x + "y: " + y);
}
}

Act6:
class pre_incr_op
{
public static void main(String args[])
{
int x=99;
int y=10;
y=++x;
System.out.println("x: " + x + "y: " + y);
}
}

3
No Pain No Gain
Lab Exercises Java Chapter 2

Lab Exercises

Lab Exercise 1: Write a program to declare two variables


hours_worked and pay_rate of integer data type and
assign the values 8 and 300. Print the value of the
variables hours_worked and pay_rate.
Lab Exercise 2: Write a program to declare two variables length_var
and breadth_var for representing the length and breadth
of the rectangle and assign the value 2 and 3,
respectively. Print the value of both the variables.
Lab Exercise 3: Write a program to declare a variable total_classroom
of integer data type and assign the value 30. Print the
value.
Lab Exercise 4: Write a Java program to declare a variable d of type
double. Assign the value 102.5 to d. Declare a float
variable f. Assign the value of d to f by performing an
explicit conversion and print the value on the screen.
Lab Exercise 5: Calculate the total marks for the given five subjects.
Declare appropriate variables, assign values as given
below, and calculate the total marks:
English = 85
Maths = 100
History = 75
Geography = 70
Art = 85
Lab Exercise 6: Write a program to compute the average rainfall for
the month of August, September, November and
December in Malaysia with the following information:

4
No Pain No Gain
Lab Exercises Java Chapter 2

5
No Pain No Gain

Você também pode gostar