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

No comments:

Post a Comment