Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- // Main method contains code for questions 6, 8, 12 and 13
- class Main {
- //Question 1 add-function
- public static int add(int a, int b) {
- return a + b;
- }
- // Question 2 age-calculator
- public static String CalculateAge(int a, int b){
- int firstAge = b - a - 1;
- int secondAge = b - a;
- String Answer = "You are either " + firstAge + " or " + secondAge;
- return Answer;
- }
- // Question 3 exes-and-ohs
- public static boolean sameXAndO(String XO){
- int numOfX = 0;
- int numOfO = 0;
- boolean sameNumOfXAndO;
- for (int i = 0; i < XO.length(); i++){
- switch(Character.toLowerCase(XO.charAt(i))){
- case 'x':
- ++numOfX;
- break;
- case 'o':
- ++numOfO;
- break;
- default:
- }
- }
- if (numOfX == numOfO) {
- sameNumOfXAndO = true;
- } else {
- sameNumOfXAndO = false;
- }
- return sameNumOfXAndO;
- }
- // Question 4 endsly
- public static boolean endsly(String a){
- boolean endsInLy;
- if(a.charAt(a.length() - 2) == 'l' && a.charAt(a.length() - 1) == 'y'){
- endsInLy = true;
- } else {
- endsInLy = false;
- }
- return endsInLy;
- }
- // Question 5 chessboard
- public static void chessboard(int size){
- StringBuilder startsWithSpace = new StringBuilder();
- StringBuilder startsWithPound = new StringBuilder();
- String chessBoard = null;
- for(int i = 1; i<=(size/2); i++){
- startsWithPound.append("# ");
- }
- for (int i = 1; i<=(size/2); i++){
- startsWithSpace.append(" #");
- }
- if (size%2 == 1){
- startsWithPound = startsWithPound.append("#");
- startsWithSpace = startsWithSpace.append(" ");
- }
- for (int i = 1; i<=(size); i++){
- if (i==1){
- chessBoard = startsWithSpace.toString();
- } else if (i%2 ==0){
- chessBoard = chessBoard + "\n" + startsWithPound.toString();
- } else if(i%2==1){
- chessBoard = chessBoard + "\n" +startsWithSpace.toString();
- }
- }
- System.out.println(chessBoard);
- }
- // Question 7 string-elide
- public static void elide(String aString){
- String newString = null;
- if (aString.length() <= 6){
- System.out.println(aString);
- } else if(aString.length() > 6){
- System.out.println(aString.substring(0, 3) + "...");
- }
- }
- // Question 9 Count Code
- public static void countCode(String text){
- int numberOfWordCode = 0;
- for (int i = 0; i < text.length(); i++){
- if (text.charAt(i) != 'c'){
- continue;
- } else if (text.charAt(i) == 'c'){
- if(text.charAt(i+1) == 'o'){
- if(text.charAt(i+3) == 'e'){
- ++numberOfWordCode;
- }
- }
- }
- }
- System.out.println(numberOfWordCode);
- }
- // Question 10
- public static int countVowels(String text){
- int numOfVowels = 0;
- for (int i = 0; i < text.length(); i++){
- switch(text.charAt(i)){
- case 'a':
- case 'e':
- case 'i':
- case 'o':
- case 'u':
- ++numOfVowels;
- break;
- default:
- }
- }
- return numOfVowels;
- }
- // Question 11 cut-a-string-at-character
- public static String cutStringAtCharacter(String someText, char aChar){
- String leftoverString = null;
- for (int i = 0; i < someText.length(); i++){
- if (someText.charAt(i) == aChar){
- leftoverString = someText.substring((i+1), (someText.length()-1));
- break;
- }
- }
- return leftoverString;
- }
- // ↓ Main method contains code for questions 6, 8, 12 and 13
- public static void main(String[] args) {
- String elideString = "Thanks for checking my homework!";
- int chessBoardDimensions = 7;
- /* I left some answers calling the function through a system.out.println, and others just by calling the function itself because I wasn't sure which method is preferred and/or more robust, so it's somewhat arbitrary */
- /* Question 1 */ System.out.println(add(4,5));
- /* Question 2 */ System.out.println(CalculateAge(1990, 2016));
- /* Question 3*/ System.out.println(sameXAndO("XOOX"));
- /* Question 4*/ System.out.println(endsly("endlessly"));
- /* Question 5*/ chessboard(chessBoardDimensions);
- /* Question 7*/ elide(elideString);
- /* Question 9*/ countCode("kskscocededdecozenfgfgcode");
- /* Question 10*/ System.out.println(countVowels("there are 11 vowels in this sentence"));
- /* Question 11*/ System.out.println(cutStringAtCharacter("I wonder what will print?", 'a'));
- // Question 6 Scanner-hungry-hippos
- System.out.println("Please enter a food:");
- String UserInput = readInput();
- String Answer = null;
- char firstLetter = Character.toLowerCase(UserInput.charAt(0));
- if (firstLetter == 'h'){
- Answer = "Yum!";
- } else {
- Answer = "Yuck!";
- }
- System.out.println(Answer);
- // Question 8 triangle
- StringBuilder poundTriangle = new StringBuilder();
- for (int i = 1; i <=7; i++){
- poundTriangle.append("#");
- System.out.println(poundTriangle);
- }
- // Question 12 twelve-days
- for (int i = 12; i > 0; i--){
- switch(i){
- case 12:
- System.out.println("12 lords a leaping");
- case 11:
- System.out.println("11 ladies dancing");
- case 10:
- System.out.println("10 pipers pipping");
- }
- }
- for (int i = 1; i <= 12; i++){
- String dayNumber;
- if (i == 1) {
- dayNumber = Integer.toString(i) + "st";
- } else if (i == 2) {
- dayNumber = Integer.toString(i) + "nd";
- } else if (i == 3) {
- dayNumber = Integer.toString(i) + "rd";
- } else {
- dayNumber = Integer.toString(i) + "th";
- }
- String repeatingLyric = "On the " + dayNumber + " day of christmas my true love gave to me";
- System.out.println(repeatingLyric);
- switch (i){
- case 1:
- System.out.println("a Partridge in a pear tree");
- continue;
- }
- switch (i){
- case 12:
- System.out.println("12 Bells ringing");
- case 11:
- System.out.println("11 ladies dancing");
- case 10:
- System.out.println("10 Pipers piping");
- case 9:
- System.out.println("9 Drummers drumming");
- case 8:
- System.out.println("8 Maids a milking");
- case 7:
- System.out.println("7 Swans a swimming");
- case 6:
- System.out.println("6 Geese a laying");
- case 5:
- System.out.println("5 Gold rings");
- case 4:
- System.out.println("4 Colly birds");
- case 3:
- System.out.println("3 French hens");
- case 2:
- System.out.println("2 Turtle doves");
- case 1:
- System.out.println("and a Partridge in a pear tree");
- }
- }
- // Question 13 scanner-ice-cream-start-up, continued on line 307
- int numOfToppings = 0;
- double iceCreamCost = 2.33;
- Random random = new Random();
- String deliveryTime = Integer.toString(random.nextInt(50));
- // I found the above code on stack overflow for finding a random number. I understand how it works, as it is creating a new variable in the Random class and assigns it a new class constructor. Then I created a string variable that takes the value of a randomly generated integer found by using the nextInt() method on the random variable created in the line before it, and turns that value into a string. Hope this makes sense, still trying to learn the java lingo.
- Scanner scanner = new Scanner(System.in);
- System.out.println("Hello! Welcome to Kwik Kustards, the Uber for Ice Cream! Before we start, may I please know your name?");
- String userName = scanner.nextLine();
- System.out.println("Thank you " + userName + ", let's get started. Which flavor of ice cream would you like? We literally have everything so don't worry, we probably have what you're looking for :)");
- String iceCreamFlavor = scanner.nextLine();
- System.out.println("Great! You chose " + iceCreamFlavor + ". Now, for the toppings. Please respond with yes or no. Would you like rainbow sprinkles?");
- String topping1 = scanner.nextLine();
- System.out.println("Fantastic. would you like chocolate chips?");
- String topping2 = scanner.nextLine();
- System.out.println("Alright, finally, would you like cookie dough?");
- String topping3 = scanner.nextLine();
- if (topping1.equalsIgnoreCase("yes")){
- ++numOfToppings;
- }
- if (topping2.equalsIgnoreCase("yes")){
- ++numOfToppings;
- }
- if (topping3.equalsIgnoreCase("yes")){
- ++numOfToppings;
- }
- String finalCost = Double.toString(totalCostofIceCream(numOfToppings));
- System.out.println("Great, you have ordered " + iceCreamFlavor + " ice cream with " + Integer.toString(numOfToppings) + " toppings. Your total is $" + finalCost + ", and your ice cream will arrive in about " + deliveryTime + " minutes");
- }
- public static String readInput(){
- Scanner scanner = new Scanner(System.in);
- return scanner.next();
- }
- // Question 13 Continued
- public static double totalCostofIceCream(int toppings){
- double base = 2.33;
- double costOfToppings = toppings * 0.33;
- double totalCost = base + costOfToppings;
- return totalCost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement