//GiraffeDemo - This is a program I am working on to figure out coding in Java!
//The program is going to revolve around giraffes, and will explore objects in
//the form of giraffes.
interface GiraffeTing { //I start with an interface. This is a list of methods and local variables.
void goLeft();
void goRight();
void goUp();
void goDown();
void changeSpeed(int newValue);
int getSpeed();
int getX();
int getY();
}
class Giraffe implements GiraffeTing {
private int speed; //must contain the above methods and local variables in its source code.
private int x; //The interface does not include what the methods do, just their names
private int y;
public Giraffe (int newSpeed, int newX, int newY) {
int speed = newSpeed;
int x = newX;
int y = newY;
} //The class giraffe uses the GiraffeTing interface, which means that it
//and variables.
public void goLeft() { //Some familiar methods...
x -= 1;
}
public void goRight() {
x += 1;
}
public void goUp() {
y -= 1;
}
public void goDown() {
y += 1;
}
public int getSpeed() {
return speed;
}
public void changeSpeed(int newValue) { //Not sure what the point of this is yet... notice how the
speed = newValue; //local variable works though.
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
class GiraffeDemo {
public static void main(String[] args) {
Giraffe giraffe1 = new Giraffe(60, 13, 4);
giraffe1.changeSpeed(48);
System.out.println("Hello giraffe!");
System.out.println("Your x coordinate is " + giraffe1.getX());
System.out.println("I'm going to ask you to move 9 steps to the right.");
//for loops.l
for(int j = 0;j<9;j++) { //This is a for loop...
giraffe1.goRight();
}
System.out.println("Your x coordinate is now " + giraffe1.getX());
System.out.println("I'm going to ask you to move 5 steps to the north-east.");
for(int j = 0;j<5;j++) { //This is a for loop...
giraffe1.goRight();
giraffe1.goUp();
}
System.out.println("Your x and y coordinates are now " + giraffe1.getX() + "," + giraffe1.getY() + ".");
System.out.println("Now we are going to try the switch statement.");
//switch Statement.
//This calls different lines of code depending on the value of the parameter.
int spotNumber = 1;
String spotReport;
switch (spotNumber) {
case 1: spotReport = "You have one spot. Keep trying...";
break;
case 2: spotReport = "You have two spots. A little better...";
break;
case 3: spotReport = "You have three spots. Not bad...";
break;
case 4: spotReport = "You have four spots. Pretty good.";
break;
case 5: case 6: case 7: case 8: spotReport = "You have many spots. Wow!";
break;
default: spotReport = "Invalid spotNumber.";
break;
}
System.out.println(spotReport);
//This is the recommended way to do for loops, don't know why.
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
System.out.println(giraffe1.getSpeed());
}
}