Created
July 27, 2017 17:18
-
-
Save Zwork101/8d2c6d685c0e169d03c5dea889efe763 to your computer and use it in GitHub Desktop.
from a java tutorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class animal { | |
public static final double FAVNUMBER = 1.082; | |
private String name; | |
private int weight; | |
private boolean owner = false; | |
private byte age; | |
private long uniqueID; | |
private char favChar; | |
private double speed; | |
private float height; | |
protected static int numberOfAnimals = 0; | |
static Scanner userInput = new Scanner(System.in); | |
public animal() { | |
numberOfAnimals++; | |
/* | |
int sumOfNumbers = 5 + 1; | |
System.out.println("5 + 1 = "+ sumOfNumbers); | |
int diffOfNumbers = 5 - 1; | |
System.out.println("5 - 1 = "+ diffOfNumbers); | |
int mulOfNumbers = 5 * 1; | |
System.out.println("5 * 1 = "+ mulOfNumbers); | |
int divOfNumbers = 5 / 1; | |
System.out.println("5 / 1 = "+ divOfNumbers); | |
int modOfNumbers = 5 % 1; | |
System.out.println("5 % 1 = "+ modOfNumbers); | |
*/ | |
System.out.print("Enter the name: \n"); | |
if (userInput.hasNextLine()) { | |
this.setName(userInput.nextLine()); | |
} | |
this.setFavChar(); | |
this.setUniqueID(); | |
} | |
public float getHeight() { | |
return height; | |
} | |
public void setHeight(float height) { | |
this.height = height; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getWeight() { | |
return weight; | |
} | |
public boolean isOwner() { | |
return owner; | |
} | |
public void setOwner(boolean owner) { | |
this.owner = owner; | |
} | |
public void setWeight(int weight) { | |
this.weight = weight; | |
} | |
public byte getAge() { | |
return age; | |
} | |
public void setAge(byte age) { | |
this.age = age; | |
} | |
public long getUniqueID() { | |
return uniqueID; | |
} | |
public void setUniqueID(long uniqueID) { | |
this.uniqueID = uniqueID; | |
System.out.println("Set unique ID to: " + this.uniqueID); | |
} | |
public void setUniqueID() { | |
long minNumber = 1; | |
long maxNumber = 1000000; | |
this.uniqueID = minNumber + (long) + (Math.random()* ((maxNumber - minNumber) + 1)); | |
String stringNumber = Long.toString(maxNumber); | |
} | |
public char getFavChar() { | |
return favChar; | |
} | |
public void setFavChar(char favChar) { | |
this.favChar = favChar; | |
} | |
public void setFavChar() { | |
int randomNumber = (int) (Math.random() * 126) + 1; | |
this.favChar = (char) randomNumber; | |
if (randomNumber == 32) { | |
System.out.println("FavChar set to space"); | |
} else if (randomNumber == 10) { | |
System.out.println("FavChar set to new line"); | |
} else { | |
System.out.println("FavChar is set to "+ this.favChar); | |
} | |
if (((randomNumber > 97) && (randomNumber < 122))) { | |
System.out.println("FavChar is a lower case letter"); | |
} | |
if (((randomNumber > 97) && (randomNumber < 122)) || ((randomNumber > 64) && (randomNumber < 91))) { | |
System.out.println("FavChar is a letter"); | |
} | |
int whichIsBigger = (50 > randomNumber) ? 50 : randomNumber; | |
switch(randomNumber) { | |
case 8: | |
System.out.println("FavChar set to back space"); | |
break; | |
case 10: | |
case 11: | |
case 12: | |
System.out.println("FavChar set to backspace"); | |
break; | |
default: | |
System.out.println(); | |
break; | |
} | |
} | |
public double getSpeed() { | |
return speed; | |
} | |
public void setSpeed(double speed) { | |
this.speed = speed; | |
} | |
protected static void countTo(int startingNum) { | |
for(int i = startingNum; i <= 100; i++) { | |
if (i == 90) { | |
continue; | |
} | |
System.out.println(i); | |
} | |
} | |
protected static String printNumbers(int maxNumbers) { | |
int i = 0; | |
while (i > (maxNumbers / 2)) { | |
System.out.println(i); | |
i++; | |
if (i == (maxNumbers/2)) { | |
break; | |
} | |
} | |
animal.countTo(maxNumbers/2); | |
return "End of printNumbers"; | |
} | |
protected static void guessMyNumber(){ | |
int number; | |
do { | |
System.out.println("Guess number up to 100"); | |
while (!userInput.hasNextInt()){ | |
String numberEntered = userInput.next(); | |
System.out.printf("%s is not a number!\n", numberEntered); | |
} | |
number = userInput.nextInt(); | |
} while(number != 50); { | |
System.out.println("You win!!!"); | |
} | |
} | |
public String makeSound() { | |
return "Grrrr"; | |
} | |
public static void speakAnimal(animal randAnimal) { | |
System.out.println("Animal says: " + randAnimal.makeSound()); | |
} | |
public static void main(String[] args) { | |
animal theAnimal = new animal(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class cat extends animal { | |
public cat() { | |
} | |
public String makeSound() { | |
return "Meow"; | |
} | |
public static void main(String[] args) { | |
/* | |
animal fido = new dog(); | |
animal fluffy = new cat(); | |
animal[] animals = new animal[10]; | |
animals[0] = fido; | |
animals[1] = fluffy; | |
System.out.println(animals[0].getName() + " says: " + animals[0].makeSound()); | |
System.out.println(animals[1].getName() + " says: " + animals[1].makeSound()); | |
speakAnimal(animals[0]); | |
*/ | |
int[] favNums; | |
favNums = new int[20]; | |
favNums[0] = 100; | |
String[] stringArray = {"Random", "Words", "Here"}; | |
for(String word : stringArray) { | |
System.out.println(word); | |
} | |
int[][][] arrayName = {{{10}, {20}, {30}}, | |
{{120}, {130}, {140}}, | |
{{230}, {240}, {250}}}; | |
for (int[][] part : arrayName){ | |
for (int[] p : part){ | |
for (int peice : p){ | |
System.out.print("| " + String.valueOf(peice) + " "); | |
} | |
} | |
System.out.println("|"); | |
} | |
String[] cloneOfArray = Arrays.copyOf(stringArray, 3); | |
System.out.println(Arrays.toString(cloneOfArray)); | |
System.out.println(Arrays.binarySearch(cloneOfArray, "Random")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class dog extends animal { | |
public dog() { | |
} | |
public String makeSound() { | |
return "Woof"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment