jump to navigation

putting a system together August 7, 2008

Posted by crystalchangdin in coding.
add a comment

main

public class control {

 public static void main(String[] args) {
   TUI tui = new TUI();
  tui.start();
 }

}

TUI

import java.util.*;

public class TUI {
 private static final int NO_CHOICE = 0;
 private static final int PLAY_GAME = 1;
 private static final int EXIT = 2;

 private Player player1;
 private Player player2;
 private Game game;
 private Scanner in;

 public TUI()
 {
  this.player1 = new Player(“Player1″);
  this.player2 = new Player(“Player2″);
  this.game = null;
  this.in  = new Scanner(System.in);
 }
 
 public void start()
 {
  int choice = NO_CHOICE;
  while(choice != EXIT)
  {
   displayMainMenu();
   choice = readIntWithPrompt(“Enter choice”);
   executeChoice(choice);
  }
 }
 private void playGame(int numberOfSticks)
 {
  game = new Game(player1,player2,numberOfSticks);
  String s = game.toString();
  System.out.println(s);
  while(!game.gameOver())
  {
   game.play();
   reportPlay(game.previousPlayer());
  }
  reportWinner(game.winner());
 }
 private void displayMainMenu()
 {
  System.out.println();
  System.out.println(“Enter the number denoting the action to perform”);
  System.out.println(“Run game……” + PLAY_GAME);
  System.out.println(“EXIT……” + EXIT);
 }
 private void executeChoice(int choice)
 {
  System.out.println();
  if(choice == PLAY_GAME)
  {
   int numberOfSticks = readNumberOfSticks();
   playGame(numberOfSticks);
  }
  else if(choice == EXIT)
   System.out.println(“Good-Bye”);
  else
   System.out.println(choice + “is not valid.”);
 }
 private int readNumberOfSticks()
 {
  int number = -1;
  while(number <=0)
  {
   number = readIntWithPrompt(“Enter number of sticks (a positive integer):”);
  }
  return number;
 }
 private void reportWinner(Player player)
 {
  System.out.println();
  System.out.println(“Player: ” + player.name() + ” wins.”);
  System.out.println();
 }
 private int readIntWithPrompt(String prompt)
 {
  System.out.println(prompt); System.out.flush();
  while(!in.hasNextInt())
  {
   in.nextLine();
   System.out.println(prompt);System.out.flush();
  }
  int input = in.nextInt();
  in.nextLine();
  return input;
 }
 private void reportPlay(Player player)
 {
  System.out.println(“Player ” + player.name()+” takes “+player.numOfStick()+” stick(s),leaving ” + game.sticksLeft()+”.”);
 }
}

Game

public class Game {
 private static final int MAX_ON_A_TURN = 3;
 private Player player1;
 private Player player2;
 private Player nextPlayer;
 private Player previousPlayer;
 private Pile pile;
 
 public Game(Player player1,Player player2,int sticks)
 {
  assert sticks > 0 : “precondition: initial sticks > 0″;
  this.player1 = player1;
  this.player2 = player2;
  this.nextPlayer = player1;
  this.previousPlayer = null;
  this.pile = new Pile(sticks);
 }
 public int sticksLeft()
 {
  return pile.howManyLeft();
 }
 public Player nextPlayer()
 {
  return nextPlayer;
 }
 public Player previousPlayer()
 {
  return previousPlayer;
 }
 public boolean gameOver()
 {
  return pile.howManyLeft()==0;
 }
 public Player winner()
 {
  if(gameOver())
   return otherPlayer(previousPlayer);
  else
   return null;
 }
 public void play()
 {
  if(!gameOver())
  {
   nextPlayer.takeTurn(pile,MAX_ON_A_TURN);
   previousPlayer  = nextPlayer;
   nextPlayer = otherPlayer(nextPlayer);
  }
 }
 public String toString()
 {
  return “Game with players:” + player1.name() + ” and ” + player2.name();
 }

 private Player otherPlayer (Player player)
 {
  if(player == player1)
   return player2;
  else
   return player1;
 }
}

Player

import java.util.*;

public class Player {
 private String name;
 private int numberOfTakenSticks;
 private Scanner in = new Scanner(System.in);
 
 //Constructor
 public Player(String name){
  this.name = name;
  this.numberOfTakenSticks = 0;
 }
 
 //Queries
 public String name(){
  return this.name;
 }
 
 public int numOfStick(){
  return numberOfTakenSticks;
 }
 
 //Commands
 public void reachToPile(Pile pile){
  pile.remove(1);
  System.out.println(pile.howManyLeft()+”sticks left!”);
 }
 
 public void takeTurn(Pile pile,int maxOnATurn)
 {
  System.out.println(“You can take at most ” + maxOnATurn + ” sticks”);
  System.out.flush();
  
  while(!in.hasNextInt())
  {
   in.nextLine();
   System.out.println(“You can take at most ” + maxOnATurn + “sticks”);
   System.out.flush();
  }
  
  numberOfTakenSticks = in.nextInt();
  pile.remove(numberOfTakenSticks);
  
 }
}

Pile
public class Pile {
 private int numberOfLeftStick;
 
 //Constructor
 public Pile(){
  numberOfLeftStick = 10;
 }
 
 //User set up original number of sticks
 public Pile(int num){
  this.numberOfLeftStick = num;
 }
 
 //Queries
 public int howManyLeft(){
  return numberOfLeftStick;
 }
 
 //Commands
 public void remove(int num){
  assert num <= numberOfLeftStick: “precondition: take too many sticks”;
  numberOfLeftStick -= num;
 }
}

Traffic Light July 26, 2008

Posted by crystalchangdin in coding.
add a comment

main

public class TrafficLight {

 public static void main(String[] args) {
  LightTester tester = new LightTester();
  tester.runTest();
 }

}

Light

public class Light {
 public static final int RED = 0;
 public static final int GREEN = 1;
 public static final int YELLOW = 2;
 
 private int currentLight;
 
 public Light()
 {
  currentLight = RED;
 }
 
 public void change()
 {
  currentLight = (currentLight + 1) %3;
 }
 
 public void show()
 {
  showColor();
 }
 
 private void showColor()
 {
  if(currentLight == RED)
   System.out.println(“RED”);
  else if (currentLight == GREEN)
   System.out.println(“GREEN”);
  else
   System.out.println(“YELLOW”);
 }
}

LightTester

public class LightTester {
 Light light;
 
 public LightTester()
 {
  light = new Light();
 }
 
 public void runTest()
 {
  System.out.print(“current light is = “);
  light.show();
  testChange();
 }
 
 private void testChange()
 {
  System.out.println(“Change first time”);
  light.change();
  System.out.print(“current light is = “);
  light.show();
  System.out.println(“Change second time”);
  light.change();
  System.out.print(“current light is = “);
  light.show();
  System.out.println(“Change third time”);
  light.change();
  System.out.print(“current light is = “);
  light.show();
 }
}

Rectangle July 26, 2008

Posted by crystalchangdin in coding.
add a comment

main

public class showRectangle {

 public static void main(String[] args) {
   Rectangle r = new Rectangle(10,20);
  RectangleViewer rv = new RectangleViewer();
  rv.DisplayRectangle(r);
 }

}

Rectangle

public class Rectangle {
 private int width;
 private int length;
 
 
 //Constructor
 public Rectangle()
 {
  this.width = 3;
  this.length = 5;
 }
 
 public Rectangle(int w,int l)
 {
  this.width = w;
  this.length = l;
 }
 
 //Queries
 public int showWidth()
 {
  return width;
 }
 
 public int showLength()
 {
  return length;
 }
 
 public int showArea()
 {
  return width * length;
 }
 //Commands
 public void change(int w, int l)
 {
  this.width = w;
  this.length = l;
 }
}

RectangleViewer

public class RectangleViewer {
 
 public RectangleViewer(){}
 
 public void DisplayRectangle(Rectangle rectangle)
 {
  System.out.println(“Width = ” + rectangle.showWidth());
  System.out.println(“Length = ” + rectangle.showLength());
  System.out.println(“Area = ” + rectangle.showArea());
 }
}

game_pick up sticks July 26, 2008

Posted by crystalchangdin in coding.
add a comment

main

public class moveSticks {

 public static void main(String[] args) {
   // two people take turns in playing this game
   // who picks up the last stick will lose the game
  
  final int ORIGIN = 5;
  
  Pile pile = new Pile(ORIGIN);
  Player player1 = new Player(“Tony”);
  Player player2 = new Player(“Mary”);
  
  int switchIt = 1;
     do{
      if(switchIt == 1)
      {
       System.out.println(“Tony take one”);
       player1.reachToPile(pile);
       switchIt = 0;
      }
      else
      {
       System.out.println(“Mary take one”);
       player2.reachToPile(pile);
       switchIt = 1;
      }
     }while(pile.howManyLeft()>0);
         if(switchIt == 1)
         {
          System.out.println(“Mary lose!”);
         }
         else{
          System.out.println(“Tony lose!”);
         }
  
 }

}

Player

public class Player {
 private String name;
 private int numberOfTakenSticks;
 

 //Constructor
 public Player(String name){
  this.name = name;
  this.numberOfTakenSticks = 0;
 }
 //Queries
 public String name(){
  return this.name;
 }
 
 public int numOfStick(){
  return numberOfTakenSticks;
 }
 //Commands
 public void reachToPile(Pile pile){
  pile.remove(1);
  System.out.println(pile.howManyLeft()+”sticks left!”);
 }
}

Pile

public class Pile {
 private int numberOfLeftStick;
 
 //Constructor
 public Pile(){
  numberOfLeftStick = 10;
 }

 //User set up original number of sticks
 public Pile(int num){
  this.numberOfLeftStick = num;
 }
 
 //Queries
 public int howManyLeft(){
  return numberOfLeftStick;
 }
 
 //Commands
 public void remove(int num){
  numberOfLeftStick -= num;
 }
 
 
}

implement a simple program including three files – main, functional class, tester July 25, 2008

Posted by crystalchangdin in coding.
add a comment

main

public class TestClass {

  public static void main(String[] args) {
  testProcessNumber test = new testProcessNumber();
  test.start();
 }

}

functional class

public class ProcessNumber {
 private int num;
 
 ProcessNumber(){
  num = 0;
 }
 
 ProcessNumber(int i){
  num = i;
 }
 
 public int add(){
  num++;
  return num;
 }
 public int sub(){
  num–;
  return num;
 }
 public int getValue(){
  return num;
 }
 public void setValue(int i){
  num = i;
 }
 public void reset(){
  num = 0;
 }
}

tester

public class testProcessNumber {
 private ProcessNumber process_number;
 
 testProcessNumber(){
  process_number = new ProcessNumber();
 }
 
 public void start(){
  System.out.println(“testing starts:”);
  System.out.println(“Original Number is:”);
  System.out.println(process_number.getValue());
  process_number.add();
  process_number.add();
  process_number.add();
  System.out.println(“After add 3:”);
  System.out.println(process_number.getValue());
  process_number.sub();
  process_number.sub();
  System.out.println(“After subtract 2:”);
  System.out.println(process_number.getValue());
  process_number.reset();
  System.out.println(“After reset:”);
  System.out.println(process_number.getValue());
  
 }
}