Monday, November 26, 2012

Assignment: Teacher Contact

Assignment: Send me an email (rgriffith@kusd.lake.k12.ca.us) and answer the following questions:
  • What would you need to see for you to call this Java Class "successful" at the end of the year?
  • What would you like to get out of it? 
  • What are you willing to put into it?
Thanks!

Warm-Up: Getting Back to Java!

What I would like you to do as a warm-up activity is to comment, format and improve this game.  Be creative:

import java.util.Scanner;
public class OOPExample1 {
public static void main(String[] args) {
CPU cpu1 = new CPU();
Scanner sc1 = new Scanner(System.in);
boolean validmove = false;
String userMove = "";
while(!validmove){
System.out.println("Input your move (rock, paper or scissors): ");
userMove = sc1.nextLine();
userMove = userMove.toLowerCase();
if(userMove.equals("rock") || userMove.equals("paper")
||userMove.equals("scissors")){
validmove = true;
}
}
String CPUMove = cpu1.getCPUMove();
cpu1.comparemoves(CPUMove, userMove);
}
}
class CPU{
public void comparemoves(String CPUMove, String userMove){
if(userMove.equals("rock")){
if(CPUMove.equals("rock")){
outputmoves("rock", "Tie!");
}
if(CPUMove.equals("paper")){
outputmoves("paper", "I win!");
}
if(CPUMove.equals("scissors")){
outputmoves("scissors", "You win!");
}
}
if(userMove.equals("paper")){
if(CPUMove.equals("rock")){
outputmoves("rock", "You win!");
}
if(CPUMove.equals("paper")){
outputmoves("paper", "Tie!");
}
if(CPUMove.equals("scissors")){
outputmoves("scissors", "I win!");
}
}
if(userMove.equals("scissors")){
if(CPUMove.equals("rock")){
outputmoves("rock", "I win!");
}
if(CPUMove.equals("paper")){
outputmoves("paper", "You win!");
}
if(CPUMove.equals("Scissors")){
outputmoves("scissors", "Tie!");
}
}
}
public void outputmoves(String cpuout, String status){
String output = "I played ".concat(cpuout).concat(".").concat(status);
System.out.println(output);
}
public String getCPUMove(){
int choice = (int) Math.floor(Math.random()*3);
String rpsmove = "";
switch(choice){
case 0: rpsmove = "rock"; break;
case 1: rpsmove = "paper"; break;
default: rpsmove = "scissors"; break;
}
return rpsmove;
}
}

The New Boston video tutorials for Java

http://thenewboston.org/list.php?cat=31

Java

87 Videos

Monday, November 5, 2012

Beginning GridWorld

This week we will begin working through the GridWorld Case Study in the Java Methods book (pp.43-78).  I have also created a packet (GridWorld AP Computer Science Case Study Student Manual) to use during this process.

To begin with, we'll need to download the source code (see previous post), unzip the file, and drag the GridWorldCode folder to the main Java folder.  Then we'll add the GridWorld.jar file to the BlueJ library.  Once that is complete we will be able to view and run the BugRunner program.

Read 3.1 Prologue: Consider how huge a computer program needs to be to do something like have a soldier run through a battlefield.  Not only is the battlefield an object... and the soldier is an object... and the soldiers head is an object... and his eye is an object (if it has its' own properties)... but going deep enough, the pupil in the soldiers eye could be an object that dilates or contracts.  As projects grow like this you will need to learn techniques to keep things organized.

Read 3.2 Case Study: GridWorld: The text describes the difference of class and objects again.  And in the section where they describe the difference between the visual representation of objects and the coded objects themselves, you can think of programs like Minecraft where you can apply a different "skin" to an object.  The program works exactly the same, but the skin is different.  This is also the section where they have you read through Part 1 of the Student Manual (which I am providing you).

This section also discusses the important function of CRC cards for classes.  In this example you can see two different classes (Predator and Habitat), the class "responsibilities" (generally what it does), and "collaborators" (other classes it depends on).

We will also be opening the BugRunner program and playing with the display.

Sunday, November 4, 2012

Bring Both Books This Week

Hey, folks.  Let's go ahead and bring both Java books this week -- the BlueJ book and the big Methods book.  We're going to explore some more of the clock program and begin looking at the Grid World project.

As we approach the GridWorld project we can find the necessary files at the CollegeBoard web site.

Thursday, November 1, 2012

Coin Toss Example

While this isn't the way we would probably have approached the coin toss project, this one is interesting because it tallies up the number of heads and tails we get out of 100 coin flips.  This code involves using a counter, a Math.random number generator (0 or 1 is the result), a counter for the results of each of the sides of the coin, and a "do while" loop.  Study the code and see if you can figure out what the unfamiliar code pieces are doing.

class Toss {
        public final int HEADS = 0;
        static int countH = 0;
        static int countT = 0;
        static int counter = 0;
        private static int face;

        public static void flip() {
                face = (int) (Math.random() * 2);
        }

        public String toString() {
                String faceName;
                counter++;
                if (face == HEADS) {
                        faceName = "Heads";
                        countH++;
                } else {
                        faceName = "Tails";
                        countT++;
                }
                return faceName;
        }

        public static void main(String[] args) {
                System.out.println("Outcomes:");
                do {
                        flip();
                        System.out.println(new Toss().toString());
                } while (counter < 100);
                System.out.println("Number of Tails: " + countT);
                System.out.println("Number of Heads: " + countH);
        }
}