Misato   , Jillian , Patrick, Tao  , Rafael  , Iris  , Garrett, Yibo ,  
Keiland  , Nicholas, John   , Jiang, Daniel R, Taylor, Bihan  , Elise, 
Katherine, Daniel C, Ryan   , Noah , Kefei   , 

Stage Two (Game Engine)

Stage Two (Wed Development) 

Homework 09 

Stage Three is the Final Project, Homework 10 next week.


(a) Stage Two for the Game Engine

If you have a Game Engine you need to test it. 

Stage One (example) posted last night. 

In Stage Two you need some other game than what you did in Stage One. 

import java.awt.*;
import java.awt.event.*;

public class Game implements World {
  Circle circle;
  public Game() {
    this.circle = new Circle(100, 100, 30, new Color(1.0f, 1.0f, 0.0f));
  }
  public void teh() {
  
  }
  public void meh(MouseEvent e) {

  }
  public void keh(KeyEvent e) {
    // System.out.println( e ); 
    int code = e.getKeyCode(); 
    if (code == 37) { // left 
      this.circle.move("left"); 
    } else if (code == 38) { // up 
      this.circle.move("up"); 
    } else if (code == 39) { // right
      this.circle.move("right"); 
    } else if (code == 40) { // down
      this.circle.move("down"); 
    } else {
      
    }
  }
  public void draw(Graphics g) {
    this.circle.draw(g); 
  }
  public static void main(String[] args) {
    BigBang b = new BigBang(new Game()); 
    b.start(5000, 400);
  }
  public boolean hasEnded() {
    return false; 
  } 
  public void sayBye() { 
  
  } 
}


This was relying on this kind of Circle:



import java.awt.*; 

public class Circle {
  
  int x, y, radius, vx = -1, vy = -1;

  public void move(String how) {
    if (how.equals("up")) { 
      this.y -= 5;
    } else if (how.equals("right")) {
      this.x += 5; 
    } else if (how.equals("down")) {
      this.y += 5; 
    } else if (how.equals("left")) {
      this.x -= 5; 
    } else {
      
    }
  }
  
  Color c;
  public Circle(int x, int y, int radius, Color c) {
    this.x = x;
    this.y = y;
    this.radius = radius; 
    this.c = c;
  }
  public void draw(Graphics g) {
    g.setColor(this.c); 
    g.fillOval(this.x - this.radius, 
               this.y - this.radius, 
               2 * this.radius, 
               2 * this.radius); 
    g.setColor(Color.BLACK); 
    g.drawOval(this.x - this.radius, 
               this.y - this.radius, 
               2 * this.radius, 
               2 * this.radius); 
  }
}

Now we add momentum. 

import java.awt.*;
import java.awt.event.*;

public class Game implements World {
  Circle circle;
  public Game() {
    this.circle = new Circle(100, 100, 30, new Color(1.0f, 1.0f, 0.0f));
  }
  public void teh() {
    this.circle.move(); 
  }
  public void meh(MouseEvent e) {

  }
  public void keh(KeyEvent e) {
    int code = e.getKeyCode(); 
    if (code == 37) { // left 
      this.circle.vx -= 1; 
    } else if (code == 38) { // up 
      this.circle.vy -= 1; 
    } else if (code == 39) { // right
      this.circle.vx += 1; 
    } else if (code == 40) { // down
      this.circle.vy += 1; 
    } else {
      
    }
  }
  public void draw(Graphics g) {
    this.circle.draw(g); 
  }
  public static void main(String[] args) {
    BigBang b = new BigBang(new Game()); 
    b.start(50, 400);
  }
  public boolean hasEnded() {
    return false; 
  } 
  public void sayBye() { 
  
  } 
}

Now about the assignment:

import java.util.*; 

public class Mary {
  public static void main(String[] args) {
    String phrase = "Mary had a little lamb. Its fleece was white as snow.";
    Scanner in = new Scanner( phrase ); 
    while (in.hasNext()) {
      String token = in.next(); 
      System.out.println( token ); 
    }
  }
}

This runs like this: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Mary
Mary
had
a
little
lamb.
Its
fleece
was
white
as
snow.


Now I want to use a Stack:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Stack<String> a 
Static Error: Undefined class 'Stack'
> import java.util.Stack; // auto-import
Stack<String> a
> a
null
> a = new Stack<String>()
[]
> a
[]
> a.add("Mary")
true
> a
[Mary]
> a.add("had")
true
> a.add("a")
true
> a.add("little")
true
> a.add("lamb.")
true
> a
[Mary, had, a, little, lamb.]
> a.get(0)
"Mary"
> a.get(1)
"had"
> a.get(2)
"a"
> a.get(3)
"little"
> a
[Mary, had, a, little, lamb.]


I look in the book a little and I find out this:

> a.get(2)
"a"
> a.get(3)
"little"
> a
[Mary, had, a, little, lamb.]
> a
[Mary, had, a, little, lamb.]
> a.push("Its")
"Its"
> a.push("fleece")
"fleece"
> a.push("was")
"was"
> a.push("white")
"white"
> a.push("as")
"as"
> a.push("snow.")
"snow."
> a
[Mary, had, a, little, lamb., Its, fleece, was, white, as, snow.]
> a.pop()
"snow."
> a.pop()
"as"
> a.pop()
"white"
> a
[Mary, had, a, little, lamb., Its, fleece, was]
> a.pop()
"was"
> a.size()
7
> a.pop()
"fleece"
> a.size()
6


Now I think I can write 50% of the program. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Mary
snow.aswhitewasfleeceItslamb.littleahadMary


--

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Mary
 snow. as white was fleece Its lamb. little a had Mary


import java.util.*; 

public class Mary {
  public static void main(String[] args) {
    Stack<String> stack = new Stack<String>(); 
    String phrase = "Mary had a little lamb. Its fleece was white as snow.";
    Scanner in = new Scanner( phrase ); 
    while (in.hasNext()) {
      String token = in.next(); 
      // System.out.println( token ); 
      stack.push( token );
    }
    String reverse = ""; 
    while (stack.size() > 0) {
      reverse = reverse + " " + stack.pop();       
    }
    System.out.println( reverse ); 
  }
}

So that now is closer to what you need to produce. 

--