1 / 17

Applications in Java

Applications in Java. Towson University 2013. *Ref: http://chortle.ccsu.edu/java5/index.html. The Guessing Game. The Guessing Game. Summary: The game involves a ‘game’ object and three ‘player’ objects.

dom
Download Presentation

Applications in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Applications in Java Towson University 2013. *Ref: http://chortle.ccsu.edu/java5/index.html

  2. The Guessing Game

  3. The Guessing Game • Summary: • The game involves a ‘game’ object and three ‘player’ objects. • The game generates a random number between 0 and 9, and the three player objects try to guess it. • Classes: GuessGame.class, Player.class, GameLauncher.class • The Logic: • The GameLauncher class is where the application starts; it has the main() method. • In the main() method, a GuessGame object is created, and its startGame() method is called. • The GuessGame object’s startGame() method is where the entire game plays out. It creates three players, then “thinks” of a random number. • It then asks each player to guess, checks the results, and either prints out information about the winning player(s) or ask them to guess again.

  4. Classes GameLauncher main(String[] args) GuessGame p1 p2 p3 startGame() Player number guess() The number this player guesses Instance variables for the three players Make a GuessGame object and tells it to startGame Method for making a guess

  5. GuessGame class 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."); } // end of if/esle } // end of while loop } // end of StartGame() } // end of class GuessGame

  6. Player and GameLauncher public class Player { int number = 0; public void guess() { number = (int) (Math.random()*10); System.out.println("I'm guessing " + number); } // end of guess() } // end of class Player • public class GameLauncher { • public static void main(String[] args) { • GuessGame game = new GuessGame(); • game.startGame(); • } // end of main() • } // end of class GameLauncher

  7. Numbers and Statics

  8. Math class • Math class doesn’t have any instance variables. • If you try to make an instance of class Math: Math mathObject = new Math();  You’ll get error! • Methods in the Math class don’t use any instance variable values. (known as a kind of utility method) • Because the methods are ‘static’, you don’t need to have an instance of Math. • All you need is the Math class. int x = Math.round(42.2); int y = Math.min(56, 12); int z = Math.abs(-343);

  9. Regular (non-static) vs. static methods • The keyword static lets a method run without any instance of the class. • A static method means “behavior not dependent on an instance variable, so no instance/object is required. Just the class.” • Static method • public static int min(int a, int b) { • // returns the lesser of a and b • } // end of min() • Regular (non-static) method • public class Song { • String title; • public Song(String t) { • title = t; • } // end of Song() • public void play() { • SongPlayer player = new SoundPlayer(); • player.playSound(title); • } // end of paly() • } // end of class Song Song title play() Math min() max() abs() random() … Math.min(42,36); s1 = new Song(); s2 = new Song(); s1.play(); s2.play();

  10. Calling a method • Static mehtod  using a class name Math.min(88, 77); Math.max(88, 77); Math.round(88.77); Math.abs(-88); Math.random()*10; • Non-static method  using a reference variable name Song s1 = new Song(); s1.play();

  11. Using non-static variables from inside a static method • The compile thinks, “I don’t know which object’s instance variable you’re talking about!” • public class Duck { • private int size; • public static void main(String[] args) { • System.out.println("Size of duck is " + size); • } // end of main() • public void setSize(int s) { • size = s; • } // end of setSize() • public int getSize() { • return size; • } // end of getSize() • } // end of class Duck 1 error found: Error: non-static variable size cannot be referenced from a static context

  12. Using non-static methods from inside a static method • The compile thinks, “I don’t know which object’s instance variable you’re talking about!” • public class Duck { • private int size; • public static void main(String[] args) { • System.out.println("Size of duck is " + getSize()); • } // end of main() • public void setSize(int s) { • size = s; • } // end of setSize() • public int getSize() { • return size; • } // end of getSize() • } // end of class Duck 1 error found: Error: non-static method getSize() cannot be referenced from a static context

  13. Static variable • Value is the same for All instances of the class • class Duck2 { • int duckCount; • public Duck2() { • duckCount++; • } // end of Duck2() • } // end of class Duck2 The constructor Duck2() always set duckCount to 1 each time a Duck2 was made after resetting to 0. • public class Duck3 { • private int size; • private static int duckCount = 0; • public Duck3() { • duckCount++; • } // end of Duck3() • public void setSize(int s) { • size = s; • } • public int getSize() { • return size; • } • } // end of class Duck3 duckCount keep incrementing each time the Duck3() constructor runs, because duckCount is static and won’t be reset to 0.

  14. Static variable vs. instance variable • A Duck object doesn’t keep its own copy of duckCount (static). • Duck objects all share a single copy of it. • Each Duck object has its own size variable, but only one copy of the duckCount in the class Duck size static duckCount getSize() setSize() Size : 22 duckCount : 4 Size : 20 duckCount : 4 Duck object Duck object Size : 8 duckCount : 4 Size : 12 duckCount : 4 Duck object Duck object

  15. static final variables: constants • A variable marked final means that – once initialized – it can never change. • The value of the static final variable will stay the same as long as the class is loaded. • Math.PI public static final double PI=3.141592653689793; • Naming convention:  Constant variable names should be in all caps! • Any code can access it. • No need for an instance of class Math • PI doesn’t change

  16. Wrapping a primitive

  17. Wrapper class for every primitive type • Wrapper class for every primitive type is in the java.lang package • public class Num { • public void doNums() { • ArrayList<Integer> listOfNumbers = new ArrayList<Integer>(); • listOfNumbers.add(3); • int num = listOfNumbers.get(0); • } // end of doNums() • } // end of class Num Compiler does all the wrapping for you from number 3 (int) to Integer. Compiler automatically unwraps the Integer object to int so you can assign the int value directly to a primitive.

More Related