Self Review Exercise 4 Java Programming
Self Review Exercise 4 Java Programming
4.3 Write four different Java statements that each add 1 to integer variable x.
a) a++;
b) ++a;
c) a = a + 1;
d) a + =1;
4.6 Combine the statements that you wrote in Exercise 4.5 into a Java application
that calculates and prints the sum of the integers from 1 to 10. Use a while
statements to loop throught the calculation and increment statements. The loop
should terminate when the value of x become 11.
public class Excercise45
{
public static void main (String [] args)
{
int x , sum ;
x = 1;
sum = 0;
while (x <= 11)
{
sum = sum + x;
x++;
}
System.out.printf("The sum is: %d",sum);
}
4.7 Determine the value of the variable in the statement product *=x++; after
the calculation is performed. Assume that all variables are type int and initially
have the value 5.
Product = 25, x = 6
4.8 Identify and correct the errors in each of the following sets of code :
a) while (c <= 5)
{
product *= c;
++c
Kurang tanda } setelah c++
b) if (gender == 1)
{
System.out.println(Woman);
else ;
System.out.println(Man);
Setelah else tidak usah ditambahi tanda ;
a) if (y == 8)
if (x == 5)
System.out.println(@@@@@);
else
System.out.println(
a)