Advertisement
Guest User

Untitled

a guest
Oct 19th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. //GiraffeDemo - This is a program I am working on to figure out coding in Java!
  2. //The program is going to revolve around giraffes, and will explore objects in
  3. //the form of giraffes.
  4. interface GiraffeTing {                 //I start with an interface. This is a list of methods and local variables.
  5.     void    goLeft();
  6.     void    goRight();
  7.     void    goUp();
  8.     void    goDown();
  9.     void    changeSpeed(int newValue);
  10.     int     getSpeed();
  11.     int     getX();
  12.     int     getY();
  13. }
  14.  
  15. class Giraffe implements GiraffeTing {
  16.     private int speed;                      //must contain the above methods and local variables in its source code.
  17.     private int x;                          //The interface does not include what the methods do, just their names
  18.     private int y; 
  19.     public Giraffe (int newSpeed, int newX, int newY) {
  20.         int speed = newSpeed;
  21.         int x = newX;
  22.         int y = newY;
  23.     }   //The class giraffe uses the GiraffeTing interface, which means that it
  24.                         //and variables.
  25.    
  26.     public void goLeft() {              //Some familiar methods...
  27.         x -= 1;
  28.     }
  29.     public void goRight() {
  30.         x += 1;
  31.     }
  32.     public void goUp() {
  33.         y -= 1;
  34.     }
  35.     public void goDown() {
  36.         y += 1;
  37.         }
  38.     public int getSpeed() {
  39.         return speed;
  40.         }
  41.     public void changeSpeed(int newValue) {     //Not sure what the point of this is yet... notice how the
  42.         speed = newValue;                       //local variable works though.
  43.     }
  44.     public int getX() {
  45.         return x;
  46.         }
  47.     public int getY() {
  48.         return y;
  49.         }
  50.    
  51.  
  52. }
  53.    
  54.    
  55. class GiraffeDemo {
  56.      public static void main(String[] args) {
  57.         Giraffe giraffe1 = new Giraffe(60, 13, 4);
  58.         giraffe1.changeSpeed(48);
  59.         System.out.println("Hello giraffe!");
  60.         System.out.println("Your x coordinate is " + giraffe1.getX());
  61.         System.out.println("I'm going to ask you to move 9 steps to the right.");
  62.         //for loops.l
  63.         for(int j = 0;j<9;j++) {        //This is a for loop...
  64.             giraffe1.goRight();
  65.         }
  66.         System.out.println("Your x coordinate is now " + giraffe1.getX());
  67.         System.out.println("I'm going to ask you to move 5 steps to the north-east.");
  68.         for(int j = 0;j<5;j++) {        //This is a for loop...
  69.             giraffe1.goRight();
  70.             giraffe1.goUp();
  71.         }
  72.         System.out.println("Your x and y coordinates are now " + giraffe1.getX() + "," + giraffe1.getY() + ".");
  73.         System.out.println("Now we are going to try the switch statement.");
  74.        
  75.         //switch Statement.
  76.         //This calls different lines of code depending on the value of the parameter.
  77.         int spotNumber = 1;
  78.         String spotReport;
  79.         switch (spotNumber) {
  80.             case 1:     spotReport = "You have one spot. Keep trying...";
  81.             break;
  82.             case 2:     spotReport = "You have two spots. A little better...";
  83.             break;
  84.             case 3:     spotReport = "You have three spots. Not bad...";
  85.             break;
  86.             case 4:     spotReport = "You have four spots. Pretty good.";
  87.             break;
  88.             case 5: case 6: case 7: case 8:     spotReport = "You have many spots. Wow!";
  89.             break;
  90.             default:    spotReport = "Invalid spotNumber.";
  91.             break;
  92.         }
  93.         System.out.println(spotReport);
  94.    
  95.         //This is the recommended way to do for loops, don't know why.
  96.         int[] numbers = {1,2,3,4,5,6,7,8,9,10};
  97.         for (int item : numbers) {
  98.             System.out.println("Count is: " + item);
  99.         }
  100.  
  101.         System.out.println(giraffe1.getSpeed());
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement