Assalamu'alaikum and hello..
I'm going to share to all of you my Lab 7 on the topic repetitions. If you want to know, repetitions consists of while Loops, do-while Loops, for Loops, break and continue.
Question 1
What is the output of the following code?
Explain the reason.
1. int x = 8000000;
2. while ( x > 0 )
3. x++;
4. System.out.println( “x is “ + x
);
x is
-2147483648. When a variable is assigned a large value in size to be stored, it
causes an overflow.
Question
2
Question
3
Convert the following while loop into a do-while loop:
Scanner input = new Scanner(System.in);
int sum = 0;
System.out.println(“Enter an integer. The input ends if it is 0.
“);
int number = input.nextInt();
while(number != 0 )
{
sum += number;
System.out.println(“Enter
an integer. The input ends if it is 0.”);
number =
input.nextInt();
}
Answer :
Scanner input = new Scanner(System.in);
int sum = 0;
int number;
do
{
System.out.println("Enter
an integer. The input ends if it is 0.");
number =
input.nextInt();
sum += number;
}
while (number != 0);
Question
4
Write a for loop that prints
the numbers from 1 to 100.
Question
5
Convert the following for loop statement to a
while and do-while loop:
long sum = 0;
for ( int i = 0 ; i <= 1000; i++ )
{
sum = sum + I;
}
Answer:
a) while loop
long sum = 0;
int i = 0;
while (i <= 1000)
{
sum += i;
System.out.println(sum);
i++;
}
b) do-while loop
long sum = 0;
int i = 0;
do {
sum
+= i;
System.out.println(sum);
i++;
}
while (i <= 1000);
Question
6
Modify the following code to show how
many times the println statement executed in the following code. How many times
was it executed?
for ( int i = 0; i
< 10 ; i++ )
{
for
( int j = 0; j < i; j++)
{
System.out.println(
i * j );
}
}
Answer:
The statement was executed 45 times.
Question
7
Write a program that prompts the user
to enter a string and then displays the number of vowels in the string.
Question
8
Write a program that prompts the user to enter student’s name
and the student’s test 1 score and finally display the name of the student with
the highest score. Use the next() method in the Scanner class to read a name,
rather than using the nextLine() method.
Question
9
Write a program that plays the popular rock-paper-scissor
game. A scissor can cut a paper, a rock can knock a scissor and a paper can
wrap a rock. The program randomly generates a number 0, 1 or 2 representing
rock, paper and scissor. Use Math.random() method to get a random double value
d such that 0.0 <= d < 1.0. Thus, to generate a random integer in the
range of [min,max], we can use the expression: min + (int)(Math.random() *
(max-min) + 1) The program prompts the user to enter a number 0, 1 or 2 and
displays a message indicating whether the user or the computer wins, loses or
draws. Let the user continuously play until either the user or the computer
wins three times more than their opponent.
Question
10
Find the minimum point on the function f(x) = ( x – 2 )2 + 1 where x = [-1, 0, … , 4] using random optimization. In
this optimization technique, your program should just run through all the
different values of the input space ( i.e. the values x can have) and see which
one would give the minimum f(x).
That’s all from me. Remain positive though it is impossible. Have a nice day ^_^
Nurazreen binti Mohd Nor (182433)
SSK3100 (Group 15)