1.Convert the following pseudo code into Java code using nested for loops. set x
ID: 3553542 • Letter: 1
Question
1.Convert the following pseudo code into Java code using nested for loops.
set x to 2
while x <= 3
set y to 1
while y <= 3
print x
increment y
newline
increment x
2.
Assume you have made the declarations:
double speed;
scanner stdIn=new Scanner(System.in);
Write a Statement that causes the program to read a floating point number entered on thekeyboard into the variable called speed.
3.Assume you have defined char variable called code,which might be assigned one the the three values 'p','f',or 'i'.Write a Java switch statement that prints "pass" if code == 'p',"fail" if code == 'f", Or "incomplete' if code == 'i'.If the value code is anything else, the statement should print "data error".
4.
int x=5;
int y =8;
int z;
while (y <20)
{
z=x+y;
x=y;
y=z;
System.out.println(y);
}
=
13
21
(Results i got)
4.1
Convert the while loop in to a do loop.with exacly the same output.
4.2
Also convert the while loop in to a for loop with the same output.
Show both
5.
Assume the existenece of java code that already includes these two statements:
Scanner stdIn = new Scanner(System.in);
Then, Implement an input-validation do loop that prompts the user to enter 'y' or 'n' and assigns the entered value to entry.If the entered value is not either 'y' or 'n',the code should repeat the prompt and the input assignment an indefinite number of times until the input is either 'y' or 'n'.
//Sorry if there are any typos in advance
Explanation / Answer
1)
for(int x = 2; x<=3; x++)
{
for(int y=1; y<=3; y++)
System.out.println(x);
}
2)
speed = stdln.nextFloat();
3)
switch(code)
{
case('p'):
System.out.println("pass");
break;
case('f'):
System.out.println("fail");
break;
case('i'):
System.out.println("incomplete");
break;
default:
System.out.println("data error");
}
4.1)
int x=5;
int y =8;
int z;
do
{
z=x+y;
x=y;
y=z;
System.out.println(y);
}while (y <20);
4.2)
int x=5;
int z;
for (int y=8; y <20; y=z)
{
z=x+y;
x=y;
y=z;
System.out.println(y);
}
5)
String entry = "";
while(entry=="")
{
System.out.println("Please enter 'y' or 'n'...");
String input = stdIn.next();
if(input.equalsIgnoreCase("y") || input.equalsIgnoreCase("n"))
entry = input;
}
Let me know if I've missed something :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.