Sunday, August 4, 2013

Simple Dot Com Tester Program

Program:

SimpleDotComTester.java

package chap05;

public class SimpleDotComTester {
    public static void main(String[] args)
    {
        SimpleDotCom dot = new SimpleDotCom();
        int[] locations = {2, 3, 4};
        dot.setLocationCells(locations);
        String userGuess = "2";
        String result = dot.checkYourself(userGuess);
    }
}

SimpleDotCom.java

package chap05;

public class SimpleDotCom {
    int[] locationCells;
    int numOfHits = 0;
   
    public void setLocationCells(int[] locs)
    {
        locationCells = locs;
    }
   
    public String checkYourself(String stringGuess) {
        int guess = Integer.parseInt(stringGuess);
        String result = "miss";
        for (int cell: locationCells)
        {
            if (guess == cell) {
                result = "hit";
                numOfHits++;
                break;
            }
        }
        if (numOfHits == locationCells.length)
        {
            result = "kill";
        }
        System.out.println(result);
        return result;
    }
}


O/P:

hit

Good Dog Test Drive Program

Program:

package chap04;

public class GoodDogTestDrive {
public static void main(String[] args)
{
    GoodDog one = new GoodDog();
    one.setSize(70);
    GoodDog two = new GoodDog();
    two.setSize(8);
    System.out.println("Dog one: " + one.getSize());
    System.out.println("Dog two: " + two.getSize());
    one.bark();
    two.bark();
}
}

class GoodDog
{
private int size;
public int getSize() { return size;}
public void setSize(int s) {
size = s;
}

void bark() {
if (size > 60) {
System.out.println("Wooof! Wooof!");
} else if (size > 60) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}

}

O/P:

Dog one: 70
Dog two: 8
Wooof! Wooof!

Yip! Yip!

Poor Dog Test Drive Program:

Program:

package chap04;

public class PoorDogTestDrive {
public static void main(String[] args)
    {
        PoorDog one = new PoorDog();
        System.out.println("Dog size is " + one.getSize());
        System.out.println("Dog name is " + one.getName());
    }
}

class PoorDog
{
    private int size;
    private String name;
    public int getSize() { return size;}
    public String getName() { return name;}

}

O/P:

Dog size is 0

Dog name is null

Dog Program

Program:

package chap03;

public class Dog {
String name;
   public static void main(String[] args) {
       Dog dog1 = new Dog();
       dog1.bark();
       dog1.name = "Bart";
     
       Dog[] myDogs = new Dog[3];
       myDogs[0] = new Dog();
       myDogs[1] = new Dog();
       myDogs[2] = dog1;
     
       myDogs[0].name = "Fred";
       myDogs[1].name = "Marge";
     
       System.out.print("last don't name is ");
       System.out.println(myDogs[2].name);
     
       int x = 0;
       while (x < myDogs.length) {
           myDogs[x].bark();
           x = x+1;
       }
   }
   public void bark() {
       System.out.println(name + " says Ruff!");
   }
 
   public void eat() { }
 
   public void chaseCat() { }

}

O/P:

null says Ruff!
last don't name is Bart
Fred says Ruff!
Marge says Ruff!

Bart says Ruff!

Player Program

Game Launcher Program:

package chap02;

public class GameLauncher {
public static void main (String[] args) {
        GuessGame game = new GuessGame();
        game.startGame();
    }


}

Guess Game Program:

package chap02;

public class GuessGame {
Player p1;
  Player p2;
  Player p3;
 
  public void startGame() {
      p1 = new Player();
      p2 = new Player();
      p3 = new Player();
      int guessp1 = 0;
      int guessp2 = 0;
      int guessp3 = 0;
      boolean p1isRight = false;
      boolean p2isRight = false;
      boolean p3isRight = false;
      int targetNumber = (int) (Math.random() * 10);
      System.out.println("I'm thinking of a number between 0 and 9...");
      while(true) {
          System.out.println("Number to guess is " + targetNumber);
         
          p1.guess();
          p2.guess();
          p3.guess();
         
          guessp1 = p1.number;
          System.out.println("Player one guessed " + guessp1);
          guessp2 = p2.number;
          System.out.println("Player two guessed " + guessp2);
          guessp3 = p3.number;
          System.out.println("Player three guessed " + guessp3);
         
          if (guessp1 == targetNumber) {
              p1isRight = true;
          }
          if (guessp2 == targetNumber) {
              p2isRight = true;
          }
          if (guessp3 == targetNumber) {
              p3isRight = true;
          }
         
          if (p1isRight || p2isRight || p3isRight)
          {
              System.out.println("We have a winner!");
              System.out.println("Player one got it right? " + p1isRight);
              System.out.println("Player two got it right? " + p2isRight);
              System.out.println("Player three got it right? " + p3isRight);
              System.out.println("Game is over");
              break;
          }
          else
          {
              System.out.println("Players will have to try again.");
          }
      }
  }
}

Player Program:

package chap02;

public class Player {
int number = 0;
   public void guess()
   {
       number = (int) (Math.random() * 10);
       System.out.println("I'm guessing " + number);
   }
}

O/P:

Run GameLauncher.java program

I'm thinking of a number between 0 and 9...
Number to guess is 3
I'm guessing 9
I'm guessing 9
I'm guessing 7
Player one guessed 9
Player two guessed 9
Player three guessed 7
Players will have to try again.
Number to guess is 3
I'm guessing 9
I'm guessing 1
I'm guessing 7
Player one guessed 9
Player two guessed 1
Player three guessed 7
Players will have to try again.
Number to guess is 3
I'm guessing 8
I'm guessing 7
I'm guessing 1
Player one guessed 8
Player two guessed 7
Player three guessed 1
Players will have to try again.
Number to guess is 3
I'm guessing 1
I'm guessing 4
I'm guessing 1
Player one guessed 1
Player two guessed 4
Player three guessed 1
Players will have to try again.
Number to guess is 3
I'm guessing 2
I'm guessing 3
I'm guessing 3
Player one guessed 2
Player two guessed 3
Player three guessed 3
We have a winner!
Player one got it right? false
Player two got it right? true
Player three got it right? true
Game is over

Every time you run this program, the random number gets generated,

I'm thinking of a number between 0 and 9...
Number to guess is 0
I'm guessing 7
I'm guessing 2
I'm guessing 0
Player one guessed 7
Player two guessed 2
Player three guessed 0
We have a winner!
Player one got it right? false
Player two got it right? false
Player three got it right? true
Game is over




Phrase O Matic Program

Program:

package chap01;

public class PhraseOMatic {
public static void main(String[] args) {
        String[] wordListOne = {"24/7", "multi-Tier", "30,000 foot", "B-to-B",
                "win-win", "front-end", "web-based", "pervasive", "smart", "six-sigma",
                "critical-path", "dynamic"};
        String[] wordListTwo = {"empowered", "sticky", "value-added", "oriented", "centric",
                "distributed", "clustered", "branded", "outside-the-box", "positioned", "networked",
                "focused", "leveraged", "aligned", "targeted", "shared", "cooperative", "accelerated"};
        String[] wordListThree = {"process", "tipping-point", "solution", "architecture", "core competency",
                "strategy", "mindshare", "portal", "space", "vision", "paradigm", "mission"};
        int oneLength = wordListOne.length;
        int twoLength = wordListTwo.length;
        int threeLength = wordListThree.length;
        int rand1 = (int) (Math.random() * oneLength);
        int rand2 = (int) (Math.random() * twoLength);
        int rand3 = (int) (Math.random() * threeLength);
       
        String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];
        System.out.println("What we need is a " + phrase);
    }

}

O/P:

What we need is a six-sigma positioned portal
What we need is a six-sigma accelerated vision
What we need is a 24/7 focused process
What we need is a multi-Tier branded space
What we need is a dynamic clustered mission


On running the program again and again, the output keeps changing based on the random number that is generated on every run of the program.


Beer Song Program

Program:

package chap01;

public class BeerSong {
public static void main(String[] args) {
       int beerNum = 99;
       String word = "bottles";
       while (beerNum > 0)
       {
           if (beerNum == 1)
           {
               word = "bottle";
           }
           System.out.println(beerNum + " " + word + " of beer on the wall");
           System.out.println(beerNum + " " + word + " of beer");
           System.out.println("Take one down.");
           System.out.println("Pass it around.");
           beerNum = beerNum - 1;
           if (beerNum > 0)
           {
               System.out.println(beerNum +  " " + word + " of beer on the wall");
           }
           else
           {
               System.out.println("No more bottles of beer on the wall");
           }
       }
   }
}

O/P:

99 bottles of beer on the wall
99 bottles of beer
Take one down.
Pass it around.
98 bottles of beer on the wall
98 bottles of beer on the wall
98 bottles of beer
Take one down.
Pass it around.
97 bottles of beer on the wall
97 bottles of beer on the wall
97 bottles of beer
Take one down.
Pass it around.
96 bottles of beer on the wall
96 bottles of beer on the wall
96 bottles of beer
Take one down.
Pass it around.
95 bottles of beer on the wall
95 bottles of beer on the wall
95 bottles of beer
Take one down.
Pass it around.
94 bottles of beer on the wall
94 bottles of beer on the wall
94 bottles of beer
Take one down.
Pass it around.
93 bottles of beer on the wall
93 bottles of beer on the wall
93 bottles of beer
Take one down.
Pass it around.
92 bottles of beer on the wall
92 bottles of beer on the wall
92 bottles of beer
Take one down.
Pass it around.
91 bottles of beer on the wall
91 bottles of beer on the wall
91 bottles of beer
Take one down.
Pass it around.
90 bottles of beer on the wall
90 bottles of beer on the wall
90 bottles of beer
Take one down.
Pass it around.
89 bottles of beer on the wall
89 bottles of beer on the wall
89 bottles of beer
Take one down.
Pass it around.
88 bottles of beer on the wall
88 bottles of beer on the wall
88 bottles of beer
Take one down.
Pass it around.
87 bottles of beer on the wall
87 bottles of beer on the wall
87 bottles of beer
Take one down.
Pass it around.
86 bottles of beer on the wall
86 bottles of beer on the wall
86 bottles of beer
Take one down.
Pass it around.
85 bottles of beer on the wall
85 bottles of beer on the wall
85 bottles of beer
Take one down.
Pass it around.
84 bottles of beer on the wall
84 bottles of beer on the wall
84 bottles of beer
Take one down.
Pass it around.
83 bottles of beer on the wall
83 bottles of beer on the wall
83 bottles of beer
Take one down.
Pass it around.
82 bottles of beer on the wall
82 bottles of beer on the wall
82 bottles of beer
Take one down.
Pass it around.
81 bottles of beer on the wall
81 bottles of beer on the wall
81 bottles of beer
Take one down.
Pass it around.
80 bottles of beer on the wall
80 bottles of beer on the wall
80 bottles of beer
Take one down.
Pass it around.
79 bottles of beer on the wall
79 bottles of beer on the wall
79 bottles of beer
Take one down.
Pass it around.
78 bottles of beer on the wall
78 bottles of beer on the wall
78 bottles of beer
Take one down.
Pass it around.
77 bottles of beer on the wall
77 bottles of beer on the wall
77 bottles of beer
Take one down.
Pass it around.
76 bottles of beer on the wall
76 bottles of beer on the wall
76 bottles of beer
Take one down.
Pass it around.
75 bottles of beer on the wall
75 bottles of beer on the wall
75 bottles of beer
Take one down.
Pass it around.
74 bottles of beer on the wall
74 bottles of beer on the wall
74 bottles of beer
Take one down.
Pass it around.
73 bottles of beer on the wall
73 bottles of beer on the wall
73 bottles of beer
Take one down.
Pass it around.
72 bottles of beer on the wall
72 bottles of beer on the wall
72 bottles of beer
Take one down.
Pass it around.
71 bottles of beer on the wall
71 bottles of beer on the wall
71 bottles of beer
Take one down.
Pass it around.
70 bottles of beer on the wall
70 bottles of beer on the wall
70 bottles of beer
Take one down.
Pass it around.
69 bottles of beer on the wall
69 bottles of beer on the wall
69 bottles of beer
Take one down.
Pass it around.
68 bottles of beer on the wall
68 bottles of beer on the wall
68 bottles of beer
Take one down.
Pass it around.
67 bottles of beer on the wall
67 bottles of beer on the wall
67 bottles of beer
Take one down.
Pass it around.
66 bottles of beer on the wall
66 bottles of beer on the wall
66 bottles of beer
Take one down.
Pass it around.
65 bottles of beer on the wall
65 bottles of beer on the wall
65 bottles of beer
Take one down.
Pass it around.
64 bottles of beer on the wall
64 bottles of beer on the wall
64 bottles of beer
Take one down.
Pass it around.
63 bottles of beer on the wall
63 bottles of beer on the wall
63 bottles of beer
Take one down.
Pass it around.
62 bottles of beer on the wall
62 bottles of beer on the wall
62 bottles of beer
Take one down.
Pass it around.
61 bottles of beer on the wall
61 bottles of beer on the wall
61 bottles of beer
Take one down.
Pass it around.
60 bottles of beer on the wall
60 bottles of beer on the wall
60 bottles of beer
Take one down.
Pass it around.
59 bottles of beer on the wall
59 bottles of beer on the wall
59 bottles of beer
Take one down.
Pass it around.
58 bottles of beer on the wall
58 bottles of beer on the wall
58 bottles of beer
Take one down.
Pass it around.
57 bottles of beer on the wall
57 bottles of beer on the wall
57 bottles of beer
Take one down.
Pass it around.
56 bottles of beer on the wall
56 bottles of beer on the wall
56 bottles of beer
Take one down.
Pass it around.
55 bottles of beer on the wall
55 bottles of beer on the wall
55 bottles of beer
Take one down.
Pass it around.
54 bottles of beer on the wall
54 bottles of beer on the wall
54 bottles of beer
Take one down.
Pass it around.
53 bottles of beer on the wall
53 bottles of beer on the wall
53 bottles of beer
Take one down.
Pass it around.
52 bottles of beer on the wall
52 bottles of beer on the wall
52 bottles of beer
Take one down.
Pass it around.
51 bottles of beer on the wall
51 bottles of beer on the wall
51 bottles of beer
Take one down.
Pass it around.
50 bottles of beer on the wall
50 bottles of beer on the wall
50 bottles of beer
Take one down.
Pass it around.
49 bottles of beer on the wall
49 bottles of beer on the wall
49 bottles of beer
Take one down.
Pass it around.
48 bottles of beer on the wall
48 bottles of beer on the wall
48 bottles of beer
Take one down.
Pass it around.
47 bottles of beer on the wall
47 bottles of beer on the wall
47 bottles of beer
Take one down.
Pass it around.
46 bottles of beer on the wall
46 bottles of beer on the wall
46 bottles of beer
Take one down.
Pass it around.
45 bottles of beer on the wall
45 bottles of beer on the wall
45 bottles of beer
Take one down.
Pass it around.
44 bottles of beer on the wall
44 bottles of beer on the wall
44 bottles of beer
Take one down.
Pass it around.
43 bottles of beer on the wall
43 bottles of beer on the wall
43 bottles of beer
Take one down.
Pass it around.
42 bottles of beer on the wall
42 bottles of beer on the wall
42 bottles of beer
Take one down.
Pass it around.
41 bottles of beer on the wall
41 bottles of beer on the wall
41 bottles of beer
Take one down.
Pass it around.
40 bottles of beer on the wall
40 bottles of beer on the wall
40 bottles of beer
Take one down.
Pass it around.
39 bottles of beer on the wall
39 bottles of beer on the wall
39 bottles of beer
Take one down.
Pass it around.
38 bottles of beer on the wall
38 bottles of beer on the wall
38 bottles of beer
Take one down.
Pass it around.
37 bottles of beer on the wall
37 bottles of beer on the wall
37 bottles of beer
Take one down.
Pass it around.
36 bottles of beer on the wall
36 bottles of beer on the wall
36 bottles of beer
Take one down.
Pass it around.
35 bottles of beer on the wall
35 bottles of beer on the wall
35 bottles of beer
Take one down.
Pass it around.
34 bottles of beer on the wall
34 bottles of beer on the wall
34 bottles of beer
Take one down.
Pass it around.
33 bottles of beer on the wall
33 bottles of beer on the wall
33 bottles of beer
Take one down.
Pass it around.
32 bottles of beer on the wall
32 bottles of beer on the wall
32 bottles of beer
Take one down.
Pass it around.
31 bottles of beer on the wall
31 bottles of beer on the wall
31 bottles of beer
Take one down.
Pass it around.
30 bottles of beer on the wall
30 bottles of beer on the wall
30 bottles of beer
Take one down.
Pass it around.
29 bottles of beer on the wall
29 bottles of beer on the wall
29 bottles of beer
Take one down.
Pass it around.
28 bottles of beer on the wall
28 bottles of beer on the wall
28 bottles of beer
Take one down.
Pass it around.
27 bottles of beer on the wall
27 bottles of beer on the wall
27 bottles of beer
Take one down.
Pass it around.
26 bottles of beer on the wall
26 bottles of beer on the wall
26 bottles of beer
Take one down.
Pass it around.
25 bottles of beer on the wall
25 bottles of beer on the wall
25 bottles of beer
Take one down.
Pass it around.
24 bottles of beer on the wall
24 bottles of beer on the wall
24 bottles of beer
Take one down.
Pass it around.
23 bottles of beer on the wall
23 bottles of beer on the wall
23 bottles of beer
Take one down.
Pass it around.
22 bottles of beer on the wall
22 bottles of beer on the wall
22 bottles of beer
Take one down.
Pass it around.
21 bottles of beer on the wall
21 bottles of beer on the wall
21 bottles of beer
Take one down.
Pass it around.
20 bottles of beer on the wall
20 bottles of beer on the wall
20 bottles of beer
Take one down.
Pass it around.
19 bottles of beer on the wall
19 bottles of beer on the wall
19 bottles of beer
Take one down.
Pass it around.
18 bottles of beer on the wall
18 bottles of beer on the wall
18 bottles of beer
Take one down.
Pass it around.
17 bottles of beer on the wall
17 bottles of beer on the wall
17 bottles of beer
Take one down.
Pass it around.
16 bottles of beer on the wall
16 bottles of beer on the wall
16 bottles of beer
Take one down.
Pass it around.
15 bottles of beer on the wall
15 bottles of beer on the wall
15 bottles of beer
Take one down.
Pass it around.
14 bottles of beer on the wall
14 bottles of beer on the wall
14 bottles of beer
Take one down.
Pass it around.
13 bottles of beer on the wall
13 bottles of beer on the wall
13 bottles of beer
Take one down.
Pass it around.
12 bottles of beer on the wall
12 bottles of beer on the wall
12 bottles of beer
Take one down.
Pass it around.
11 bottles of beer on the wall
11 bottles of beer on the wall
11 bottles of beer
Take one down.
Pass it around.
10 bottles of beer on the wall
10 bottles of beer on the wall
10 bottles of beer
Take one down.
Pass it around.
9 bottles of beer on the wall
9 bottles of beer on the wall
9 bottles of beer
Take one down.
Pass it around.
8 bottles of beer on the wall
8 bottles of beer on the wall
8 bottles of beer
Take one down.
Pass it around.
7 bottles of beer on the wall
7 bottles of beer on the wall
7 bottles of beer
Take one down.
Pass it around.
6 bottles of beer on the wall
6 bottles of beer on the wall
6 bottles of beer
Take one down.
Pass it around.
5 bottles of beer on the wall
5 bottles of beer on the wall
5 bottles of beer
Take one down.
Pass it around.
4 bottles of beer on the wall
4 bottles of beer on the wall
4 bottles of beer
Take one down.
Pass it around.
3 bottles of beer on the wall
3 bottles of beer on the wall
3 bottles of beer
Take one down.
Pass it around.
2 bottles of beer on the wall
2 bottles of beer on the wall
2 bottles of beer
Take one down.
Pass it around.
1 bottles of beer on the wall
1 bottle of beer on the wall
1 bottle of beer
Take one down.
Pass it around.

No more bottles of beer on the wall