Copy this program, run it, and try to answer the green questions.
If you want to read a bit more about binary, try this : http://ibcomp.fis.edu/numbers/numbers.html
public class LoopPractice
{
public static void main(String[] args)
{ new LoopPractice(); }
public LoopPractice()
{
int choice ;
do
{ choice = inputInt("Choose 1-9 (0 to quit):");
if (choice == 1) first();
if (choice == 2) second();
if (choice == 3) third();
if (choice == 4) fourth();
if (choice == 5) fifth();
if (choice == 6) sixth();
if (choice == 7) seventh();
if (choice == 8) eighth();
if (choice == 9) ninth();
} while (choice > 0);
}
public void first()
{
for (int x = 0; x > 10; x = x + 1)
{ output(x); }
// Explain why this prints nothing.
// Fix it so that it prints 0,1,2,3,4...,9
// Change it to print 0, 10, 20, 30, ... 100
// What is a different command that means the same as x = x + 1 ?
}
public void second()
{
for (int x = 10; x > 0; x = x + 1000000)
{ output(x); }
// Why does this print so many numbers?
// Why does it stop at 2147000010 ?
}
public void third()
{ double t = 0;
for (double x = 0; x < 1.001; x = x + 0.001)
{ output(x); t = t+x; }
output(t);
// Why does this print such strange numbers?
// Change it to ADD UP the numbers 0.01 + 0.02 + 0.03 + ... + 0.99 + 1.00
// The correct answer is 50.5
// Your answer might be off by a tiny amount.
// Change it to add up 0.001 + 0.002 + 0.003 + .... + 0.999 + 1.000
// The correct answer is 500.500
// Without running the program, predict the answer for:
// 0.0000001 + 0.0000002 + 0.0000003 + .... + 0.9999999 + 1.00000000
// Then change the program and run it to check your answer
}
public void fourth()
{ double max = inputDouble("Max value for counting:");
double t = 0;
for (double x = 2; x <= max; x = x + 1)
{
t = t + 1.0/x;
}
output(t);
// Why does it say 1.0 instead of simply 1 ?
// What is the total when max = 10 ?
// What is the total when max = 100?
// What is the total when max = 1000000?
// Find the biggest MAX you can add up in under 1 minute.
// Do you think the total will every be larger than 100 ?
}
public void fifth()
{
double t = 0;
for (int x = 1; x <= 10; x = x + 1)
{
double p = Math.pow(x,2);
t = t + p;
output(x + " \t " + p + " \t " + t);
}
// Explain what Math.pow(x,2) means.
// Change this to add up the following numbers:
// 1 + 8 + 27 + 64 + ... + 729 + 1000
// Change this to add up all the SQUARE-ROOTS of the numbers 1,2,...10
// The correct answer is : 22.468....
// Explain the purpose of " \t "
}
public void sixth()
{ int num = inputInt("Type a whole number (integer):");
for (int x = 1; x <= num/2 ; x = x + 1)
{
int r = num % x;
output(num + " mod " + x + " = " + r);
}
// Explain what (num % x) means.
// Run the program and type the number 20.
// Explain how you can use this loop to see the FACTORS of a number.
// Add an IF command so the program ONLY prints numbers
// which are factors (divide evenly) of num.
}
public void seventh()
{ int t = 0;
int num = 1;
int count = inputInt("How many numbers do you want to add up?");
for (int x = 0; x < count; x = x + 1)
{
output(num);
t = t + num;
num = num * 2;
}
output("Total = " + t);
// Run the loop and type in 10.
// What is the total?
// Run again and type in 15.
// What is the total.
// PREDICT the total for 20 powers of 2, then run the program and check.
}
public void eighth()
{
int biggest = 1000;
int num = (int)(Math.random()*biggest);
output("I have a secret number under " + biggest);
for (int x = 0; x < 10; x = x + 1)
{
output("You have " + (10-x) + " more guesses");
int guess = inputInt("Guess the number:");
if (guess == num)
{ output("Good Guess!");
return;
}
else if (guess > num)
{ output("Too big"); }
else
{ output("Too small"); }
}
output("You used up 10 guesses - you lose.");
output("The number was : " + num);
// Play this game a couple times. Try to guess the number in 20 tries.
// You might need a pencil and paper. Were you successful?
// It is theoretically possible to ALWAYS WIN this game if you are careful.
// Change the program so you only get 5 guesses.
// Then it should choose a smaller number. What is the BIGGEST number
// that is fair so you can always win in 5 guesses? If you are not sure,
// start with 2 guesses. Can you guess any number between 1 and 10,
// or would you need more guesses? What about 3 guesses?9
}
public void ninth()
{
int x = 1 + 2 + 32 + 64;
output(x);
// This shows how to add up POWERS OF 2 to get 99 (decimal).
// It is always possible to get any decimal number by
// adding up just powers of 2. For example:
// int y = 512 + 256 + 128 + 64 + 32 + 4 + 2 + 1 ===> 999
// Find a way to write each of the following numbers
// by adding up powers of 2 :
// 50 =
// 123 =
// 444 =
}
//===========================================================
// IBIO Standard Input and Output
// These methods must be copied into your program(s).
//===========================================================
static void output(String info)
{ System.out.println(info); }
static void output(char info)
{ System.out.println(info); }
static void output(byte info)
{ System.out.println(info); }
static void output(int info)
{ System.out.println(info); }
static void output(long info)
{ System.out.println(info); }
static void output(double info)
{ System.out.println(info); }
static void output(boolean info)
{ System.out.println(info); }
static String input(String prompt)
{ String inputLine = "";
System.out.print(prompt);
try
{inputLine = (new java.io.BufferedReader(
new java.io.InputStreamReader(System.in))).readLine();
}
catch (Exception e)
{ String err = e.toString();
System.out.println(err);
inputLine = "";
}
return inputLine;
}
static String inputString(String prompt)
{ return input(prompt); }
static String input()
{ return input(""); }
static int inputInt()
{ return inputInt(""); }
static double inputDouble()
{ return inputDouble(""); }
static char inputChar(String prompt)
{ char result=(char)0;
try{result=input(prompt).charAt(0);}
catch (Exception e){result = (char)0;}
return result;
}
static byte inputByte(String prompt)
{ byte result=0;
try{result=Byte.valueOf(input(prompt).trim()).byteValue();}
catch (Exception e){result = 0;}
return result;
}
static int inputInt(String prompt)
{ int result=0;
try{result=Integer.valueOf(
input(prompt).trim()).intValue();}
catch (Exception e){result = 0;}
return result;
}
static long inputLong(String prompt)
{ long result=0;
try{result=Long.valueOf(input(prompt).trim()).longValue();}
catch (Exception e){result = 0;}
return result;
}
static double inputDouble(String prompt)
{ double result=0;
try{result=Double.valueOf(
input(prompt).trim()).doubleValue();}
catch (Exception e){result = 0;}
return result;
}
static boolean inputBoolean(String prompt)
{ boolean result=false;
try{result=Boolean.valueOf(
input(prompt).trim()).booleanValue();}
catch (Exception e){result = false;}
return result;
}
//=========== end IBIO ===========================================//
}