A request has been made for an ASCII Text Generator. :)
http://patorjk.com/software/taag/#p=display&f=Doh&t=Griff
Thursday, September 12, 2013
Flow Chart and Code: Coin Toss
I would like for you to create the following:
Part 1: Random Coin Toss
- Create a Class called CoinToss
- Add appropriate comments, titles and attributions
- Display a title "text graphic" when the program is executed
- Generate a random number (using either of the methods that I showed you)
- Display whether the result was a Heads or a Tails
- Loop for 1000 "flips"
- Count the number of Heads
- Count the number of Tails
- Display the total number of Heads and Tails
- Ask the user if they want heads or tails (maybe a 1 or a 2) right now
- Randomly select Heads or Tails
- Tell the user whether they win or lose
- Optional: Count wins & losses and loop until user quits (i.e. enters a 0)
//import statements
import java.util.*;
public class FlipCoin
{
public static void main(String [] args)
{
//declare variables
double flip;
int count = 0;
int countHeads = 0;
int countTails = 0;
//Loop
for(int x = 0; x <= 10;x = x++)
{
x = x + 1;
//flip
flip = Math.random();
//if statement
if( flip <= .5)
{
flip = countHeads;
countHeads = countHeads + 1;
}
else
{
flip = countTails;
countTails = countTails + 1;
}
count = count +1;
}//End Loop
}//End Main
}//End Class
For Loops
class ForDemo {
public static void main(String args[]) {
int count;
for(count = 1; count < 25; count = count+1)
System.out.println("This is count: " + count);
System.out.println("Done!");
}
}//Reverse Order class ForDemo {
public static void main(String args[]) {
int count;
for(count = 99; count > 0; count = count-1)
System.out.println("This is count: " + count);
System.out.println("Done!");
}
}
Wednesday, September 11, 2013
Number Guessing Game: Revisited
Today we made some changes to the Number Guessing Game:
// ======================
// Number Guessing Game
// Version 1.1
// By Robert Griffith
// ======================
import java.util.Scanner;
public class NumberGuessingGame2 {
public static void main(String[] args)
{
int secretNumber;
int guess;
int tries;
int maxtries;
secretNumber = (int) (Math.random() * 999 + 1);
//System.out.println("Secret number is " + secretNumber); // to be removed later
Scanner keyboard = new Scanner(System.in);
tries=0;
maxtries=10;
do {
tries=tries+1;
System.out.print("Enter guess #" + tries + ": ");
guess = keyboard.nextInt();
System.out.println("Your guess is " + guess);
if(tries > maxtries)
{
System.out.println("You have exceeded the maximum number of tries. The secret number was " + secretNumber + ". Sorry!");
System.exit(0);
}
if(guess == secretNumber)
{
System.out.println("Your guess is correct! It took you " + tries + " tries. Congratulations!");
}
if (guess < secretNumber)
{
System.out.println("Your guess is smaller than the secret number.");
}
if (guess > secretNumber)
{
System.out.println("Your guess is larger than the secret number.");
}
} while (guess != secretNumber);
}
}
// ======================
// Number Guessing Game
// Version 1.1
// By Robert Griffith
// ======================
import java.util.Scanner;
public class NumberGuessingGame2 {
public static void main(String[] args)
{
int secretNumber;
int guess;
int tries;
int maxtries;
secretNumber = (int) (Math.random() * 999 + 1);
//System.out.println("Secret number is " + secretNumber); // to be removed later
Scanner keyboard = new Scanner(System.in);
tries=0;
maxtries=10;
do {
tries=tries+1;
System.out.print("Enter guess #" + tries + ": ");
guess = keyboard.nextInt();
System.out.println("Your guess is " + guess);
if(tries > maxtries)
{
System.out.println("You have exceeded the maximum number of tries. The secret number was " + secretNumber + ". Sorry!");
System.exit(0);
}
if(guess == secretNumber)
{
System.out.println("Your guess is correct! It took you " + tries + " tries. Congratulations!");
}
if (guess < secretNumber)
{
System.out.println("Your guess is smaller than the secret number.");
}
if (guess > secretNumber)
{
System.out.println("Your guess is larger than the secret number.");
}
} while (guess != secretNumber);
}
}
Tuesday, September 10, 2013
JAVA Case Study #4: Number Guessing Game
This case study involves a random number generator -- but in this case we will be using a "Math" method for the random number. We will work through it in steps:
CASE STUDY #4: A Number Guessing Game -- Building a Program Step By Step
Step One: Let's try a different method for finding a random number -- this time from 1 to 1000.
CASE STUDY #4: A Number Guessing Game -- Building a Program Step By Step
Step One: Let's try a different method for finding a random number -- this time from 1 to 1000.
import java.util.Scanner;Suggestions for Continuing to Upgrade Your Code
public class NumberGuessingGame {
public static void main(String[] args)
{
int secretNumber;
secretNumber = (int) (Math.random() * 999 + 1);
System.out.println("Secret number is " + secretNumber); // to be removed later
Scanner keyboard = new Scanner(System.in);
int guess;
do {
System.out.print("Enter a guess: ");
guess = keyboard.nextInt();
System.out.println("Your guess is " + guess);
if(guess == secretNumber)
{
System.out.println("Your guess is correct! Congratulations!");
}
if (guess < secretNumber)
{
System.out.println("Your guess is smaller than the secret number.");
}
if (guess > secretNumber)
{
System.out.println("Your guess is larger than the secret number.");
}
} while (guess != secretNumber);
}
}
- Add comments and labels [obviously]
- Create a little title text graphic to display at the beginning of your program.
- Display "Guess #" in front of [or behind] each "Enter a guess" prompt.
- Limit the number of guesses a user gets -- and you might even have it display something like "Guess 3 of 10".
Monday, September 9, 2013
JAVA Case Study #3: Magic Cue Ball
For this case study, we will need to have a random number generator -- simply put, a way to randomly pick a "No" or a "Yes" (a 0 or a 1). Here's a sample code which generates a random number:
import java.util.Random;
class RandomNumber {
public static void main(String args[]) {
Random myRandom = new Random();
int randomNumber;
randomNumber = myRandom.nextInt(10) + 1;
System.out.println(randomNumber);
}
}
CASE STUDY #3: Magic Cue Ball
For case number 3 we have "The Magic CueBall" -- okay, so it's ripped off... but it works... sort of:
import java.util.Scanner;
import java.util.Random;
class MagicCueBall {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
Random myRandom = new Random();
int randomNumber;
System.out.print("Type a Yes or No question: ");
myScanner.nextLine();
randomNumber = myRandom.nextInt(10) + 1;
if (randomNumber > 5) {
System.out.println("Yes. Isn’t it obvious?");
} else {
System.out.println("No, and don’t ask again.");
}
}
}
Let's do some playing with the code again:
- Again, begin by adding comments/labels to the code.
- What Java utils are we bringing in this time?
- Before the "if" statement, randomNumber can be what possible numbers? Why is there a +1 at the end of the line?
- Add some variation to the possible responses:
- 4 different "Yes" answers
- 4 different "No" answers
- 2 different "Ask Again" answers
- At the beginning of the program (before asking the user to type a yes or no question), add a title text graphic. For example:
- *********************************
- * Magic Cue Ball 1.0 ~ by Your Name *
- *********************************
- What else could we add to make this application more usable?
Friday, September 6, 2013
JAVA Case Study #1: Movie Tickets
CASE STUDY #1: Movie Tickets
Let's look at this code for a movie ticket program:
Let's look at this code for a movie ticket program:
import java.util.Scanner;
class TicketPrice {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int age;
double price = 0.00;
System.out.print("How old are you? ");
age = myScanner.nextInt();
if (age >= 12 && age < 65) {
price = 9.25;
}
if (age < 12 || age >= 65) {
price = 5.25;
}
System.out.print("Please pay $");
System.out.print(price);
System.out.print(". ");
System.out.println("Enjoy the show!");
}
}
- First of all, what is the name of this Class?
- What are we bringing into the Class from Java that is already built for us?
- What does "Scanner" do?
- What does "double" mean in Java?
- What variables are we using in this code?
- What could you substitute for, "if (age <12 | | age >= 65) {"
- What are some of the things that are missing in this code?
- What would the ticket price be for an 8 year old? A 14 year old? A 66 year old?
- Draw a flow chart indicating the flow of this program.
- Begin by formatting the source code to be more readable -- add line breaks, indent (Tab) some of the lines, etc. (See the next case study for an idea of how to format it)
- Add some comments to the code. Remember that every Class needs a title, author(s), and version (number or dates) and maybe some comments describing what a formula or function does.
Subscribe to:
Posts (Atom)

