Guest User

Untitled

a guest
Feb 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. test.java:5: error: illegal start of expression
  2. public int[] locations={1,2,3};
  3. ^
  4. 1 error
  5.  
  6. public class test{
  7.  
  8. public static void main(String[] args){
  9.  
  10. test dot=new test();
  11. public int[] locations={1,2,3};
  12.  
  13. dot.setLocationCells(locations);
  14.  
  15. String userGuess="2";
  16. String result = dot.checkYourself(userGuess);
  17. String testResult="failed";
  18.  
  19. if(result.equals("hit")){
  20. testResult="passed";
  21. }
  22.  
  23.  
  24. System.out.println(testResult);
  25. }
  26.  
  27. public String checkYourself(String stringGuess){
  28. int guess=Integer.parseInt(stringGuess);
  29. String result="miss";
  30. int numOfHits=0;
  31.  
  32. for(int cell:locations){
  33. if(guess==cell){
  34. result="hit";
  35. numOfHits++;
  36. break;
  37. }
  38. }
  39.  
  40. if(numOfHits==locations.length){
  41. result="kill";
  42. }
  43.  
  44. System.out.println(result);
  45. return result;
  46. }
  47.  
  48.  
  49. public void setLocationCells( int[] locations){
  50. int[] locns;
  51. locns=locations;
  52. }
  53.  
  54. }
  55.  
  56. public class Test { //Capitalized name for classes are used in Java
  57. private final ini[] locations; //key final mean that, is must be assigned before object is constructed and can not be changed later.
  58.  
  59. public Test(int[] locations) {
  60. this.locations = locations;//To access to class member, when method argument has the same name use `this` key word.
  61. }
  62.  
  63. public boolean ckeckYourSelf(int value) { //This method is accessed only from a object.
  64. for(int location : locations) {
  65. if(location == value) {
  66. return true; //When you use key word return insied of loop you exit from it. In this case you exit also from whole method.
  67. }
  68. }
  69. return false; //Method should be simple and perform one task. So you can ge more flexibility.
  70. }
  71. public static int[] locations = {1,2,3};//This is static array that is not part of object, but can be used in it.
  72.  
  73. public static void main(String[] args) { //This is declaration of public method that is not part of create object. It can be accessed from every place.
  74. Test test = new Test(Test.locations); //We declare variable test, and create new instance (obect) of class Test.
  75. String result;
  76. if(test.checkYourSelf(2)) {//We moved outsie the string
  77. result = "Hurray";
  78. } else {
  79. result = "Try agian"
  80. }
  81. System.out.println(result); //We have only one place where write is done. Easy to change in future.
  82. }
  83. }
  84.  
  85. public static int [] locations={1,2,3};
  86.  
  87. public static test dot=new test();
  88.  
  89. public static void main(String[] args){
Add Comment
Please, Sign In to add comment