- 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?
Monday, November 26, 2012
Assignment: Teacher Contact
Assignment: Send me an email (rgriffith@kusd.lake.k12.ca.us) and answer the following questions:
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
- 1 - Installing the JDK
- 2 - Running a Java Program
- 3 - Downloading Eclipse
- 4 - Hello YouTube
- 5 - Variables
- 6 - Getting User Input
- 7 - Building a Basic Calculator
- 8 - Math Operators
- 9 - Increment Operators
- 10 - If Statement
- 11 - Logical Operators
- 12 - Switch Statement
- 13 - While Loop
- 14 - Using Multiple Classes
- 15 - Use Methods with Parameters
- 16 - Many Methods and Instances
- 17 - Constructors
- 18 - Nested if Statements
- 19 - else if Statement
- 20 - Conditional Operators
- 21 - Simple Averaging Program
- 22 - for Loops
- 23 - Compound Interest Program
- 24 - do while Loops
- 25 - Math Class Methods
- 26 - Random Number Generator
- 27 - Introduction to Arrays
- 28 - Creating an Array Table
- 29 - Summing Elements of Arrays
- 30 - Array Elements as Counters
- 31 - Enhanced for Loop
- 32 - Arrays in Methods
- 33 - Multidimensional Arrays
- 34 - Table for Multi Arrays
- 35 - Variable Length Arguments
- 36 - Time Class
- 37 - Display Regular time
- 38 - Public, Private and this
- 39 - Multiple Constructors
- 40 - Set and Get Methods
- 41 - Building Objects for Constructors
- 42 - toString
- 43 - Composition
- 44 - Enumeration
- 45 - EnumSet range
- 46 - Static
- 47 - More on Static
- 48 - final
- 49 - Inheritance
- 50 - Graphical User Interface GUI
- 51 - GUI with JFrame
- 52 - Event Handling
- 53 - ActionListner
- 54 - Event Handler Program
- 55 - Intoduction to Polymorphism
- 56 - Polymorphic Arguements
- 57 - Overriding Rules
- 58 - Abstract and Concrete Classes
- 59 - Class to Hold Objects
- 60 - Array Holding Many Objects
- 61 - Simple Polymorphic Program
- 62 - JButton
- 63 - JButton Final Program
- 64 - JCheckBox
- 65 - The Final Check Box Program
- 66 - JRadioButton
- 67 - JRadioButton Final Program
- 68 - JComboBox
- 69 - Drop Down List Program
- 70 - JList
- 71 - JList Program
- 72 - Multiple Selection List
- 73 - Moving List Items Program
- 74 - Mouse Events
- 75 - MouseListener interface
- 76 - MouseMotionListener interface
- 77 - Adapter Classes
- 78 - File Class
- 79 - Creating Files
- 80 - Writing to Files
- 81 - Reading from Files
- 82 - Exception Handling
- 83 - FlowLayout
- 84 - Drawing Graphics
- 85 - JColorChooser
- 86 - Drawing More Stuff
- 87 - Series Finale
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.
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.
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);
}
}
Subscribe to:
Posts (Atom)