Skip to content

Instantly share code, notes, and snippets.

@rooksoto
Created August 27, 2016 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rooksoto/a0e5f99a06d678f9b66bad7f71149286 to your computer and use it in GitHub Desktop.
Save rooksoto/a0e5f99a06d678f9b66bad7f71149286 to your computer and use it in GitHub Desktop.
https://repl.it/CrZr/69 created by rooksoto
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Q1. add-function Call
add(1, 2);
//Q2. age-calculator Call
calculateAge(1987, 2016);
//Q3. exes-and-ohs Call
System.out.println(XO("ooxx")); //Passed function call to SOUT in order to log result to console
//Q4. endsly Call
System.out.println(endsly("Test string here!")); //Again, used SOUT to print result
//Q5. chessboard Call
chessboard();
//Q6. scanner-hungry-hippos Call
Scanner scanner = new Scanner (System.in);
System.out.println("Please enter a valid food: ");
String hippoFood = scanner.next();
System.out.println(hungryHippos(hippoFood));
//Q7. string-elide Call
elide("Hello, world!");
//Q8. triangle Call
triangle();
//Q9. count-code Call
System.out.println(countCode("codexxcode"));
//Q10. count-the-vowels Call
countTheVowels("longer string with more vowels");
//Q11. cut-a-string-at-character Call
subStrAfterChars("this is another test", 'o');
//Q12. twelve-days Call
twelveDays();
//Q13. scanner-ice-cream-start-up Call
scannerIceCream();
}
//Q1. add-function Function
public static void add(int addOperand1, int addOperand2) {
System.out.println(addOperand1 + addOperand2);
}
//Q2. age-calculator Function
public static void calculateAge(int birthYear, int currentYear) {
int probableAge = (currentYear - birthYear);
System.out.println("You are either " + (probableAge -1) + " or " + (probableAge));
}
//Q3. exes-and-ohs Function
public static boolean XO (String evalXO) {
evalXO = evalXO.toLowerCase();
int numOfX = 0;
int numOfO = 0;
for (int i = 0; i < evalXO.length(); i++) {
if (evalXO.charAt(i) == 'x') {
numOfX++;
} else if (evalXO.charAt(i) == 'o') {
numOfO++;
}
}
if (numOfX == numOfO) {
return true;
} else {
return false;
}
}
//Q4. endsly Function
public static boolean endsly (String endslyTestString) {
if (endslyTestString.endsWith("ly")) {
return true;
} else {
return false;
}
}
//Q5. chessboard Function
public static void chessboard () {
String boardElements = " # # # #\n# # # # \n";
for (int i = 0; i < 4; i++) {
System.out.print(boardElements);
}
}
//Q6. scanner-hungry-hippos Function
public static String hungryHippos (String food) {
food = food.toLowerCase();
if (food.startsWith("h")) {
return "Yum!";
} else {
return "Yuck...";
}
}
//Q7. string-elide Function
public static void elide (String elideString) {
if (elideString.length() <= 7) {
System.out.println(elideString);
} else {
System.out.println(elideString.substring(0, 3) + "..." + elideString.charAt(elideString.length() - 1));
}
}
//Q8. triangle Function
public static void triangle () {
String triangleBlock = "#";
for (int i = 0; i < 7; i++) {
System.out.println(triangleBlock);
triangleBlock += "#";
}
}
//Q9. count-code Function
public static int countCode (String codeString) {
codeString = codeString.toLowerCase();
int codeMatch = 0;
for (int i = 0; i < codeString.length() ; i++) {
if (codeString.charAt(i) == ('c')) {
if (codeString.charAt(i + 1) == ('o')) {
if (codeString.charAt(i + 3) == ('e')) {
codeMatch++;
}
}
}
}
return codeMatch;
}
//Q10. count-the-vowels Function
public static void countTheVowels(String vowelString) {
int numVowels = 0;
String allVowels = "aeiou";
for (int i = 0; i < vowelString.length(); i++) {
for (int j = 0; j < allVowels.length(); j++) {
if (vowelString.charAt(i) == allVowels.charAt(j)) {
numVowels++;
}
}
}
System.out.println(numVowels);
}
//Q11. cut-a-string-at-character Function
public static void subStrAfterChars (String cutString, char cutChar) {
int indexBegin = 0;
for (int i = 0; i < cutString.length(); i++) {
if (cutString.charAt(i) == cutChar) {
indexBegin = i + 1;
break;
}
}
System.out.println(cutString.substring(indexBegin));
}
//Q12. twelve-days Function
public static void twelveDays () {
String day = "";
String giftPrint = "";
String firstLinePt1 = "On the ";
String firstLinePt2 = " day of Christmas\n";
String secondLine = "My true love sent to me: \n";
String giftsDay1 = "a Partridge in a Pear Tree.\n";
String giftsDay2 = "Two Turtle Doves\nand ";
String giftsDay3 = "Three French Hens,\n";
String giftsDay4 = "Four Calling Birds,\n";
String giftsDay5 = "Five Gold Rings,\n";
String giftsDay6 = "Six Geese a-Laying,\n";
String giftsDay7 = "Seven Swans a-Swimming,\n";
String giftsDay8 = "Eight Maids a-Milking,\n";
String giftsDay9 = "Nine Ladies Dancing,\n";
String giftsDay10 = "Ten Lords a-Leaping,\n";
String giftsDay11 = "Eleven Pipers Piping,\n";
String giftsDay12 = "Twelve Drummers Drumming,\n";
String giftsRunningTotal = "";
System.out.print("\n");
for (int i = 0; i <= 11 ; i++) {
switch (i) {
case 0:
day = "first";
giftPrint = giftsDay1;
break;
case 1:
day = "second";
giftPrint = giftsDay2;
break;
case 2:
day = "third";
giftPrint = giftsDay3;
break;
case 3:
day = "fourth";
giftPrint = giftsDay4;
break;
case 4:
day = "fifth";
giftPrint = giftsDay5;
break;
case 5:
day = "sixth";
giftPrint = giftsDay6;
break;
case 6:
day = "seventh";
giftPrint = giftsDay7;
break;
case 7:
day = "eighth";
giftPrint = giftsDay8;
break;
case 8:
day = "ninth";
giftPrint = giftsDay9;
break;
case 9:
day = "tenth";
giftPrint = giftsDay10;
break;
case 10:
day = "eleventh";
giftPrint = giftsDay11;
break;
case 11:
day = "twelfth";
giftPrint = giftsDay12;
break;
}
System.out.print(firstLinePt1 + day + firstLinePt2 + secondLine + giftPrint + giftsRunningTotal + "\n");
giftsRunningTotal = giftPrint + giftsRunningTotal;
}
}
//Q13. scanner-ice-cream-start-up Function
public static void scannerIceCream() {
Scanner iceCreamScanner = new Scanner(System.in);
int waitTime = (int) (Math.random() * 60 + 1);
String flavor;
String choiceWalnuts = "";
String choiceSprinkles = "";
String choiceSyrup = "";
boolean hasSprinkles = false;
boolean hasWalnuts = false;
boolean hasSyrup = false;
int toppingsAmount = 0;
float runningTotal = 233;
System.out.println("\n");
System.out.println("Hello! I'm so happy to take your order.");
System.out.println("What is your name?");
String customerName = iceCreamScanner.next();
System.out.println("OK " + customerName + ". What flavor of ice-cream would you like?");
flavor = iceCreamScanner.next();
System.out.println(flavor + ", great choice!");
System.out.println("Now, let's go over some add-ons.");
while ((!choiceWalnuts.equalsIgnoreCase("yes")) && (!choiceWalnuts.equalsIgnoreCase("no"))) {
System.out.println("Would you like to add walnuts?");
System.out.println("Please type \"Yes\" or \"No\".");
choiceWalnuts = iceCreamScanner.next();
if (choiceWalnuts.equalsIgnoreCase("yes")) {
hasWalnuts = true;
runningTotal += 33;
} else if (choiceWalnuts.equalsIgnoreCase("no")) {
hasWalnuts = false;
} else {
System.out.println("That is not a valid choice.");
}
}
while ((!choiceSprinkles.equalsIgnoreCase("yes")) && (!choiceSprinkles.equalsIgnoreCase("no"))) {
System.out.println("Would you like to add sprinkles?");
System.out.println("Please type \"Yes\" or \"No\".");
choiceSprinkles = iceCreamScanner.next();
if (choiceSprinkles.equalsIgnoreCase("yes")) {
hasSprinkles = true;
runningTotal += 33;
} else if (choiceSprinkles.equalsIgnoreCase("no")) {
hasSprinkles = false;
} else {
System.out.println("That is not a valid choice.");
}
}
while ((!choiceSyrup.equalsIgnoreCase("yes")) && (!choiceSyrup.equalsIgnoreCase("no"))) {
System.out.println("Would you like to add chocolate syrup?");
System.out.println("Please type \"Yes\" or \"No\".");
choiceSyrup = iceCreamScanner.next();
if (choiceSyrup.equalsIgnoreCase("yes")) {
hasSyrup = true;
runningTotal += 33;
} else if (choiceSyrup.equalsIgnoreCase("no")) {
hasSyrup = false;
} else {
System.out.println("That is not a valid choice.");
}
}
if (hasWalnuts == true) {
toppingsAmount++;
}
if (hasSyrup) {
toppingsAmount++;
}
if (hasSprinkles) {
toppingsAmount++;
}
System.out.println("Okay! A " + flavor + " ice cream with " + toppingsAmount + " toppings. Your total is $" + (runningTotal / 100) + " and your ice cream will arrive in " + waitTime + " minutes.");
}
}
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
>>> 3
You are either 28 or 29
true
false
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
Please enter a valid food:
t
Yuck...
Hel...!
#
##
###
####
#####
######
#######
2
8
ther test
On the first day of Christmas
My true love sent to me:
a Partridge in a Pear Tree.
On the second day of Christmas
My true love sent to me:
Two Turtle Doves
and a Partridge in a Pear Tree.
On the third day of Christmas
My true love sent to me:
Three French Hens,
Two Turtle Doves
and a Partridge in a Pear Tree.
On the fourth day of Christmas
My true love sent to me:
Four Calling Birds,
Three French Hens,
Two Turtle Doves
and a Partridge in a Pear Tree.
On the fifth day of Christmas
My true love sent to me:
Five Gold Rings,
Four Calling Birds,
Three French Hens,
Two Turtle Doves
and a Partridge in a Pear Tree.
On the sixth day of Christmas
My true love sent to me:
Six Geese a-Laying,
Five Gold Rings,
Four Calling Birds,
Three French Hens,
Two Turtle Doves
and a Partridge in a Pear Tree.
On the seventh day of Christmas
My true love sent to me:
Seven Swans a-Swimming,
Six Geese a-Laying,
Five Gold Rings,
Four Calling Birds,
Three French Hens,
Two Turtle Doves
and a Partridge in a Pear Tree.
On the eighth day of Christmas
My true love sent to me:
Eight Maids a-Milking,
Seven Swans a-Swimming,
Six Geese a-Laying,
Five Gold Rings,
Four Calling Birds,
Three French Hens,
Two Turtle Doves
and a Partridge in a Pear Tree.
On the ninth day of Christmas
My true love sent to me:
Nine Ladies Dancing,
Eight Maids a-Milking,
Seven Swans a-Swimming,
Six Geese a-Laying,
Five Gold Rings,
Four Calling Birds,
Three French Hens,
Two Turtle Doves
and a Partridge in a Pear Tree.
On the tenth day of Christmas
My true love sent to me:
Ten Lords a-Leaping,
Nine Ladies Dancing,
Eight Maids a-Milking,
Seven Swans a-Swimming,
Six Geese a-Laying,
Five Gold Rings,
Four Calling Birds,
Three French Hens,
Two Turtle Doves
and a Partridge in a Pear Tree.
On the eleventh day of Christmas
My true love sent to me:
Eleven Pipers Piping,
Ten Lords a-Leaping,
Nine Ladies Dancing,
Eight Maids a-Milking,
Seven Swans a-Swimming,
Six Geese a-Laying,
Five Gold Rings,
Four Calling Birds,
Three French Hens,
Two Turtle Doves
and a Partridge in a Pear Tree.
On the twelfth day of Christmas
My true love sent to me:
Twelve Drummers Drumming,
Eleven Pipers Piping,
Ten Lords a-Leaping,
Nine Ladies Dancing,
Eight Maids a-Milking,
Seven Swans a-Swimming,
Six Geese a-Laying,
Five Gold Rings,
Four Calling Birds,
Three French Hens,
Two Turtle Doves
and a Partridge in a Pear Tree.
Hello! I'm so happy to take your order.
What is your name?
t
OK t. What flavor of ice-cream would you like?
van
van, great choice!
Now, let's go over some add-ons.
Would you like to add walnuts?
Please type "Yes" or "No".
no
Would you like to add sprinkles?
Please type "Yes" or "No".
no
Would you like to add chocolate syrup?
Please type "Yes" or "No".
no
Okay! A van ice cream with 0 toppings. Your total is $2.33 and your ice cream will arrive in 26 minutes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment