Recursion, Graphics, Random and While Quiz: review exercises - some answers

Tracing: what will these print:

1. what prints when I call whileRoller(25)? -- > You should win
 

2. What is returned by the call countby3(8)? --> 15

3. Draw what this will print when I call printSomething(500,500). --> draw a lollipop

public static void printSomething (int x, int y)
{

WorldImage shape1 = AImage.makeCircle(x/10,Color.blue, Mode.filled);
WorldImage shape2 = AImage.makeRectangle(x/10,y/5,Color.red, Mode.filled);
WorldImage shape3 = AImage.makeRectangle(x,y,Color.yellow, Mode.filled);
shape3 = shape3.place(shape1,x/2,y/5);
shape3 = shape3.place(shape2,x/2,y/5*2);
shape3.show();
}

Coding Exercises: 

1. Recursion: (from book) Write a method called writeNums that takes an integer as a parameter and prints that many integers starting with 1 in sequential order, separated by commas. See sample runs:

public class WriteNums
{

public static void main()
{
printNums(5);
System.out.println();
printNums(10);
System.out.println();
}
public static void printNums(int x)
{
if (x < 2)
{
System.out.print("1");
}
else
{
printNums(x-1);
System.out.print(", " + x);
}
}
}

2. While & Random: Write a program that keeps asking a user if they want to roll the dice. It stops when the user says no. As it rolls the dice, it prints the dice throw. It prints a total of all the dice when the user is done throwing dice. See sample run:


/**
* Write a description of class rollthroughaboard here.
**/
import java.util.Scanner;
import java.util.Random;
public class rollthroughaboard
{
public static void main()
{
Scanner ask=new Scanner(System.in); // create a scanner terminal window
Random randomGen = new Random(); // create an object that knows how to give you a random number
int total = 0; // start a total bucket
System.out.println("Do you want to throw the dice?"); // set up before the first loop
String answer=ask.next(); // take in the answer
while(answer.equals("yes")) // test the answer - first time it comes from above, after that, the answer comes from below
{
int roll = randomGen.nextInt(6)+1; // get a random number 0-5 and add 1
total = total + roll; // accumulate in a loop
System.out.println("you threw a " + roll);
System.out.println("Do you want to throw the dice?");
answer=ask.next(); // take in a new answer so maybe the loop will stop
}

System.out.println("Your total is " + total); // print the total
}
}

3. Make a method that takes in two doubles and returns true if the two variables are equal to each other. It is considered equal if the absolute difference between the two variables is less than .001.

public static boolean matcher(double x, double y)
{
return Math.abs(x-y) < .001;
}


4. Graphics & while --- New answer coming. Sorry this is still in another language, but the pattern will be the same
Draw a row of boxes, stopping when the x value is .9

(Make your boxes RectangleSprites of length .09 and width .09 and then move over .1 x every time.)

import fang2.core.*;
import fang2.sprites.*;
import java.awt.*;
import java.awt.geom.*;

public class dsfs extends GameLoop
{
@Override
public void startGame()
{
double x = .1;
while (x <= .9)
{ Sprite shape = new RectangleSprite(.09,.09);
shape.setLocation(x,.1);
canvas.addSprite(shape);
x = x + .1;
}
}
}