This lab deals with loops, and the details are covered in the What you should know section.
Download the L05.zip file and extract the files to your cs125 folder in your home directory. You will use these files to complete this week's lab exercises.
Examine the Java program Fibonacci.java that is intended to compute the first n Fibonacci numbers where the user enters n. (If you don't know what a Fibonacci number is, don't worry. They are defined in the comments of Fibonacci.java). The java program is not correct but can be corrected with exactly two minor changes. Make these changes and submit the program Fibonacci.java in the box below.
NOTE: You do not need to make the function work for n <= 1.
Write a program (called ExitLoop.java)
that will ask the user to enter a word. If that word is
"quit" or
"q" then the program will stop. If that word is anything else then the
program will ask the user to enter a word again.
The program should be case insensitive; in other
words it should stop if the user enters "QUIT", or "Quit", or "qUit", or any other
variation of the word quit using upper and lower case letters.
Hint: You shouldn't have to use all 16 variations of "quit": there is a String method (or two) that will help you.
In this question you will write a program
(called XBoard.java)
to display a red X on a Board. Your program should read in the size of a square board and then place red pegs on the main diagonal and back diagonal. Use for loops. An example is given below.
Trace the following code (without Dr. Java) assuming the user enters the following integer values for number. For each of the following inputs, determine the corresponding output from the program.
You must do a separate trace for each input.
Code to Trace
import java.util.Scanner;
public class TracingL5
{
public static void main(String[] args)
{
int number = 0;
int update = 0;
int count = 0;
Scanner in = new Scanner(System.in);
number = in.nextInt();
update = number;
for (int i=2; i<=number; i++)
{
while (update > 0 && update % i == 0)
{
System.out.print(i + " ");
update = update / i;
count++;
}
}
System.out.println("");
if (count == 1)
{
System.out.println("This is a special number.");
}
}
}
Trace for input 1:
Trace for input 8:
Trace for input 30:
Trace for input 37:
Trace for input 150:
Trace for input 999:
Under what conditions will the message "This is a special number." be displayed? Write your answer below.
If you request solutions for this lab you will be sent an email that contains your answers as well as the expected answers. The email will be sent out to all interested students once the deadline for completing the exercises has passed for all students.