Skip to content

Instantly share code, notes, and snippets.

@rooksoto
Created August 27, 2016 17:54
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rooksoto/7645f1a05a7e0fe5614607ed42fee992 to your computer and use it in GitHub Desktop.
https://repl.it/CrZr/85 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("colexxcode"));
//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 boardElement1 = "#";
String boardElement2 = " ";
int currentElement = 1;
int size = 8;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (currentElement == 1) {
System.out.print(boardElement1);
currentElement = 2;
} else if (currentElement == 2) {
System.out.print(boardElement2);
currentElement = 1;
}
}
if (currentElement == 1) {
currentElement = 2;
} else if (currentElement == 2) {
currentElement = 1;
}
System.out.print("\n");
}
}
//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)
@jgarcia162
Copy link

Question 9:
What happens when we input "trythiscop"? How can we fix that?
Question 10:
While this works it's very inefficient. We want to stay away from nested loops as best as we can. Try some if/else statements, or a switch case?

##Things to keep in mind:
Your code is very neat and concise for the most part. Just remember to:
-avoid nested loops
-watch out for edge cases. These errors are very small but can cause big breaks. consider all possible inputs and don't forget those pesky out of bounds exceptions. Check that your program never looks for an index that may not be there.
-avoid unnecessary variables. In problem 13 you didn't need those boolean variables or the toppingsAmount variable. Where you set these boolean variables to true we could have just added the 33 to our running total.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment