Java programming questions 1. Answer the following questions (assume that variab
ID: 3770230 • Letter: J
Question
Java programming questions
1. Answer the following questions (assume that variables are already defined):
(1) Write an if statement to satisfy the condition: if the value of variable named letter is less
than ‘z’, variable x increases by 1.
(2) Write an if-else statement to satisfy the conditions: if the value of variable named letter is
less than ‘z’, increases variable x by 1; otherwise, increases variable y by 1.
(3) Write an if-else statement to satisfy the conditions: if the expression x < y <= z, use
System.out.println() to display the value of y; otherwise display the values of y and z.
(4) Write an if-else statement to satisfy the conditions: if the expression x <= z, use
System.out.println() to display the values of x and z; otherwise if x > y, then display the
values of x and y.
2. Answer the following questions:
(1) Given the following while loop:
int number = 2;
boolean done = false;
while (done != true) {
number += 2;
if (number = 64)
done = true;
}
How many times is the loop executed?
(2) What are 3 criteria of the loop above?
(3) Insert an output statement to display the value of number each time the loop is executed.
Also, modify the code so it displays how many times the loop is executed.
3. Modify the following code segments using the specified type of loop to achieve the same
purpose. Assume all variables are declared and initialized.
(1) Modify the following code using a do-while loop
response = sc.nextInt();
count = 0;
while (response >= 0 && response <= 99) {
System.out.prinln(“response is ” + response);
response = sc.nextInt();
count++;
}
(2) Challenge problem: Modify the code above using a for loop.
(3) Modify the following code using a while loop
intData = sc.nextInt();
do {
System.out.println(" " + intData);
intData = sc.nextInt();
} while (intData >= 10);
(4) Modify the following code using a for loop
sum = 0;
count = 50;
while (count <= 1299) {
sum += count++;
}
4. Use a for loop to calculate n!. Ask the user to enter an integer in the range 0 – 25, then display
the result of the calculation. (Hint: 5! = 5 x 4 x 3 x 2 x 1).
Explanation / Answer
1.
(1) if(letter<'z')
x++;
(2)
if(letter<'z')
x++;
else
y++;
(3)
if(x<y && y<=z)
System.out.println(y);
else
System.out.println(y+" "+z);
(4)
if(x<=z)
System.out.println(x+","+z);
else if(x>y)
System.out.println(x+", "+y);
2.
(1) infinate
(2)a. loop is execute till done not equal to true
b. if numner becomes 64 the done is true
c. for ever iteration number is incremented by 1
(3)
int number = 2;
int i=0;
boolean done = false;
while (done != true) {
number += 2;
System.out.println(number);
if (number = 64)
done = true;
i++;
}
System.out.println(i);
3.
(1)
response = sc.nextInt();
count = 0;
do {
System.out.prinln(“response is ” + response);
response = sc.nextInt();
count++;
}while (response >= 0 && response <= 99);
(2)
response = sc.nextInt();
count = 0;
for(response=0;response<=99;resonse++)
{
System.out.prinln(“response is ” + response);
response = sc.nextInt();
count++;
}
(3)
intData = sc.nextInt();
while (intData >= 10)
{
System.out.println(" " + intData);
intData = sc.nextInt();
}
(4)
sum = 0;
count = 50;
for(count=0;count<= 1299;count++)
{
sum += count;
}
4.
Scanner scan = new Scanner(System.in);
int n=scan.nextInt();
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
System.out.println(f);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.