Skip to content

Instantly share code, notes, and snippets.

@maragh
Forked from schifano/java-warmup1.java
Created March 9, 2021 19:52
Show Gist options
  • Save maragh/0930c39abc57510879d57f557482eb2c to your computer and use it in GitHub Desktop.
Save maragh/0930c39abc57510879d57f557482eb2c to your computer and use it in GitHub Desktop.

Revisions

  1. @schifano schifano renamed this gist Jan 9, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @schifano schifano renamed this gist Jan 9, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @schifano schifano renamed this gist Jan 9, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @schifano schifano revised this gist Jan 9, 2014. 1 changed file with 17 additions and 36 deletions.
    53 changes: 17 additions & 36 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -336,39 +336,20 @@ public String endUp(String str) {
    return str.substring(0, beginUpper) + upper;
    }





































    // everyNth
    // Given a non-empty string and an int N, return the string made starting with char 0,
    // and then every Nth char of the string. So if N is 3, use char 0, 3, 6, ... and so on. N is 1 or more.
    public String everyNth(String str, int n) {
    int length = str.length();

    String nth = "";
    int i = 0;
    while (i < length) {

    nth = nth + str.substring(i, i+1);

    i=i+n;
    }

    return nth;
    }
  5. @schifano schifano revised this gist Jan 9, 2014. 1 changed file with 216 additions and 0 deletions.
    216 changes: 216 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -123,6 +123,222 @@ else if (str.length() == 2) {

    }

    // front3
    // Given a string, we'll say that the front is the first 3 chars of the string.
    // If the string length is less than 3, the front is whatever is there.
    // Return a new string which is 3 copies of the front.
    public String front3(String str) {
    String front;

    if (str.length() <=3)
    front = str;
    else {
    front = str.substring(0,3);
    }
    return front+front+front;
    }

    // backAround
    // Given a string, take the last char and return a new string with the last char added at the front and back,
    // so "cat" yields "tcatt". The original string will be length 1 or more.
    public String backAround(String str) {
    return str.charAt(str.length()-1) + str + str.charAt(str.length()-1);
    }

    // or35
    // Return true if the given non-negative number is a multiple of 3 or a multiple of 5.
    // Use the % "mod" operator.
    public boolean or35(int n) {
    if (n % 3 == 0 || n % 5 == 0)
    return true;
    return false;
    }

    // front22
    // Given a string, take the first 2 chars and return the string with the 2 chars added at both the front and back,
    // so "kitten" yields"kikittenki". If the string length is less than 2, use whatever chars are there.
    public String front22(String str) {
    if (str.length() < 2) {
    return str+str+str;
    }

    String front = str.substring(0,2);
    return front+str+front;
    }

    // startHi
    // Given a string, return true if the string starts with "hi" and false otherwise.
    public boolean startHi(String str) {
    if (str.length() < 2)
    return false;

    String start = str.substring(0,2);
    return start.equals("hi");
    }

    // icyHot
    // Given two temperatures, return true if one is less than 0 and the other is greater than 100.
    public boolean icyHot(int temp1, int temp2) {
    return ((temp1 < 0 && temp2 > 100) || (temp1 > 100 && temp2 < 0));
    }

    // in1020
    // Given 2 int values, return true if either of them is in the range 10..20 inclusive.
    public boolean in1020(int a, int b) {
    return ((a >= 10 && a <= 20) || (b >=10 && b <=20));
    }

    // hasTeen
    // We'll say that a number is "teen" if it is in the range 13..19 inclusive.
    // Given 3 int values, return true if 1 or more of them are teen.
    public boolean hasTeen(int a, int b, int c) {
    return ((a >= 13 && a <= 19) ||
    (b >= 13 && b <= 19) ||
    (c >= 13 && c <= 19));
    }

    // loneTeen
    // We'll say that a number is "teen" if it is in the range 13..19 inclusive.
    // Given 2 int values, return true if one or the other is teen, but not both.
    public boolean loneTeen(int a, int b) {
    if ((a >= 13 && a <= 19) && (b >= 13 && b <=19)) {
    return false;
    }
    return ((a >= 13 && a <= 19) || (b >= 13 && b <=19));
    }

    // delDel
    // Given a string, if the string "del" appears starting at index 1,
    // return a string where that "del" has been deleted. Otherwise, return the string unchanged.
    public String delDel(String str) {
    if (str.length() <= 3)
    return str;

    if(str.substring(1,4).equals("del")) {
    StringBuilder sb = new StringBuilder(str);
    sb.replace(1,4,"");

    return sb.toString();
    }
    return str;
    }

    // mixStart
    // Return true if the given string begins with "mix",
    // except the 'm' can be anything, so "pix", "9ix" .. all count.
    public boolean mixStart(String str) {
    if (str.length() < 3)
    return false;
    return str.substring(1,3).equals("ix");
    }

    // startOz
    // Given a string, return a string made of the first 2 chars (if present),
    // however include first char only if it is 'o' and include the second only if it is 'z', so "ozymandias" yields "oz".
    public String startOz(String str) {
    String result = "";

    if (str.length() >= 1 && str.charAt(0)=='o') {
    result = result + str.charAt(0);
    }

    if (str.length() >= 2 && str.charAt(1)=='z') {
    result = result + str.charAt(1);
    }

    return result;
    }

    // intMax
    // Given three int values, a b c, return the largest.
    public int intMax(int a, int b, int c) {
    int max = 0;

    if (a > b)
    max = a;
    else
    max = b;
    if (c > max)
    max = c;

    return max;
    }

    // close10
    // Given 2 int values, return whichever value is nearest to the value 10,
    // or return 0 in the event of a tie. Note that Math.abs(n) returns the absolute value of a number.
    public int close10(int a, int b) {
    int value1 = Math.abs(10-a);
    int value2 = Math.abs(10-b);

    if (value1 < value2)
    return a;
    else if (value2 < value1)
    return b;
    return 0;
    }

    // in3050
    // Given 2 int values, return true if they are both in the range 30..40 inclusive,
    // or they are both in the range 40..50 inclusive.
    public boolean in3050(int a, int b) {
    return (((a >= 30 && a <= 40) && (b >= 30 && b <= 40)) ||
    ((a >= 40 && a <= 50) && (b >= 40 && b <= 50)));
    }

    // max1020
    // Given 2 positive int values, return the larger value that is in the range 10..20 inclusive,
    // or return 0 if neither is in that range.
    public int max1020(int a, int b) {
    if (b > a) {
    int temp = a;
    a = b;
    b = temp;
    }

    // Knowing a is bigger, just check a first
    if (a >= 10 && a <= 20) return a;
    if (b >= 10 && b <= 20) return b;
    return 0;
    }

    // stringE
    // Return true if the given string contains between 1 and 3 'e' chars.
    public boolean stringE(String str) {
    int j = 0;
    for (int i=0; i < str.length(); i++) {
    if (str.substring(i,i+1).equals("e"))
    j++;
    }
    return (j >= 1 && j <= 3);
    }

    // lastDigit
    // Given two non-negative int values, return true if they have the same last digit,
    // such as with 27 and 57. Note that the % "mod" operator computes remainders, so 17 % 10 is 7.
    public boolean lastDigit(int a, int b) {
    return (a % 10 == b % 10);
    }

    // endUp
    // Given a string, return a new string where the last 3 chars are now in upper case.
    // If the string has less than 3 chars, uppercase whatever is there. Note that str.toUpperCase()
    // returns the uppercase version of a string.
    public String endUp(String str) {
    int beginUpper = str.length()-3;
    int length = str.length();

    if (str.length() < 3)
    return str.toUpperCase();

    String upper = str.substring(beginUpper, length).toUpperCase();

    return str.substring(0, beginUpper) + upper;
    }







  6. @schifano schifano revised this gist Jan 9, 2014. 1 changed file with 41 additions and 0 deletions.
    41 changes: 41 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -85,6 +85,47 @@ public String notString(String str) {
    return "not " + str;
    }

    // missingChar
    // Given a non-empty string and an int n, return a new string where the char at index n has been removed.
    // The value of n will be a valid index of a char in the original string
    // (i.e. n will be in the range 0..str.length()-1 inclusive).
    public String missingChar(String str, int n) {
    StringBuilder sb = new StringBuilder(str);
    return sb.deleteCharAt(n).toString();
    }

    // frontBack
    // Given a string, return a new string where the first and last chars have been exchanged.
    public String frontBack(String str) {

    String front;
    String middle;
    String back;

    if (str.length() == 0) {
    return str;
    }
    else if (str.length() == 1) {
    return str;
    }
    else if (str.length() == 2) {
    front = str.substring(0,1);
    back = str.substring(1);

    return back+front;
    }

    front = str.substring(0,1);
    middle = str.substring(1, str.length()-1);
    back = str.substring(str.length()-1);

    return back+middle+front;

    }







  7. @schifano schifano revised this gist Jan 5, 2014. No changes.
  8. @schifano schifano revised this gist Jan 5, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,6 @@ public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    // Unless the two values are the same, then return double their sum.
    public int sumDouble(int a, int b) {
    int sum = a + b;

    if (a == b) {
    sum = sum *2;
    }
    @@ -59,7 +58,6 @@ public boolean makes10(int a, int b) {
    public boolean nearHundred(int n) {
    int value1 = 100-n;
    int value2 = 200-n;

    return (Math.abs(value1) <= 10 || Math.abs(value2) <=10);
    }

  9. @schifano schifano revised this gist Jan 5, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -43,9 +43,7 @@ public int diff21(int n) {
    // We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23.
    // We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return true if we are in trouble.
    public boolean parrotTrouble(boolean talking, int hour) {

    return (talking && (hour < 7 || hour > 20));

    }

    // makes10
  10. @schifano schifano revised this gist Jan 5, 2014. No changes.
  11. @schifano schifano revised this gist Jan 5, 2014. 1 changed file with 0 additions and 9 deletions.
    9 changes: 0 additions & 9 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -19,28 +19,24 @@ public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    // Given two int values, return their sum.
    // Unless the two values are the same, then return double their sum.
    public int sumDouble(int a, int b) {

    int sum = a + b;

    if (a == b) {
    sum = sum *2;
    }
    return sum;

    }

    // diff21
    // Given an int n, return the absolute difference between n and 21,
    // except return double the absolute difference if n is over 21.
    public int diff21(int n) {

    int difference = 21 - n;

    if (n > 21) {
    difference = Math.abs(difference) * 2;
    }
    return difference;

    }

    // parrotTrouble
    @@ -55,10 +51,8 @@ public boolean parrotTrouble(boolean talking, int hour) {
    // makes10
    // Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.
    public boolean makes10(int a, int b) {

    int sum = a + b;
    return (sum == 10 || a == 10 || b == 10);

    }

    // nearHundred
    @@ -75,23 +69,20 @@ public boolean nearHundred(int n) {
    // Given 2 int values, return true if one is negative and one is positive.
    // Except if the parameter "negative" is true, then return true only if both are negative.
    public boolean posNeg(int a, int b, boolean negative) {

    if (negative && a < 0 && b < 0) {
    return true;
    }
    else if (!negative && ((a < 0 && b > 0) || (a > 0 && b < 0))) {
    return true;
    }
    return false;

    }

    // notString
    // Given a string, return a new string where "not " has been added to the front.
    // However, if the string already begins with "not", return the string unchanged.
    // Note: use .equals() to compare 2 strings.
    public String notString(String str) {

    if (str.startsWith("not")) {
    return str;
    }
  12. @schifano schifano renamed this gist Jan 5, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  13. @schifano schifano renamed this gist Jan 5, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  14. @schifano schifano revised this gist Jan 5, 2014. 1 changed file with 36 additions and 0 deletions.
    36 changes: 36 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -61,6 +61,42 @@ public boolean makes10(int a, int b) {

    }

    // nearHundred
    // Given an int n, return true if it is within 10 of 100 or 200.
    // Note: Math.abs(num) computes the absolute value of a number.
    public boolean nearHundred(int n) {
    int value1 = 100-n;
    int value2 = 200-n;

    return (Math.abs(value1) <= 10 || Math.abs(value2) <=10);
    }

    // posNeg
    // Given 2 int values, return true if one is negative and one is positive.
    // Except if the parameter "negative" is true, then return true only if both are negative.
    public boolean posNeg(int a, int b, boolean negative) {

    if (negative && a < 0 && b < 0) {
    return true;
    }
    else if (!negative && ((a < 0 && b > 0) || (a > 0 && b < 0))) {
    return true;
    }
    return false;

    }

    // notString
    // Given a string, return a new string where "not " has been added to the front.
    // However, if the string already begins with "not", return the string unchanged.
    // Note: use .equals() to compare 2 strings.
    public String notString(String str) {

    if (str.startsWith("not")) {
    return str;
    }
    return "not " + str;
    }



  15. @schifano schifano revised this gist Jan 5, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // sleepIn
    // The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation.
    // We sleep in if it * is not a weekday or we're on vacation. Return true if we sleep in.
    boolean sleepIn(boolean weekday, boolean vacation) {
    public boolean sleepIn(boolean weekday, boolean vacation) {
    if (!weekday || vacation) {
    return true;
    }
    @@ -11,14 +11,14 @@ boolean sleepIn(boolean weekday, boolean vacation) {
    // monkeyTrouble
    // We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling.
    // We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble.
    boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    return (aSmile == bSmile);
    }

    // sumDouble
    // Given two int values, return their sum.
    // Unless the two values are the same, then return double their sum.
    int sumDouble(int a, int b) {
    public int sumDouble(int a, int b) {

    int sum = a + b;

    @@ -32,7 +32,7 @@ int sumDouble(int a, int b) {
    // diff21
    // Given an int n, return the absolute difference between n and 21,
    // except return double the absolute difference if n is over 21.
    int diff21(int n) {
    public int diff21(int n) {

    int difference = 21 - n;

    @@ -46,15 +46,15 @@ int diff21(int n) {
    // parrotTrouble
    // We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23.
    // We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return true if we are in trouble.
    boolean parrotTrouble(boolean talking, int hour) {
    public boolean parrotTrouble(boolean talking, int hour) {

    return (talking && (hour < 7 || hour > 20));

    }

    // makes10
    // Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.
    boolean makes10(int a, int b) {
    public boolean makes10(int a, int b) {

    int sum = a + b;
    return (sum == 10 || a == 10 || b == 10);
  16. @schifano schifano revised this gist Jan 5, 2014. 1 changed file with 34 additions and 3 deletions.
    37 changes: 34 additions & 3 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // sleepIn
    // The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation.
    // We sleep in if it * is not a weekday or we're on vacation. Return true if we sleep in.
    public boolean sleepIn(boolean weekday, boolean vacation) {
    boolean sleepIn(boolean weekday, boolean vacation) {
    if (!weekday || vacation) {
    return true;
    }
    @@ -11,14 +11,14 @@ public boolean sleepIn(boolean weekday, boolean vacation) {
    // monkeyTrouble
    // We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling.
    // We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble.
    public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    return (aSmile == bSmile);
    }

    // sumDouble
    // Given two int values, return their sum.
    // Unless the two values are the same, then return double their sum.
    public int sumDouble(int a, int b) {
    int sumDouble(int a, int b) {

    int sum = a + b;

    @@ -29,6 +29,37 @@ public int sumDouble(int a, int b) {

    }

    // diff21
    // Given an int n, return the absolute difference between n and 21,
    // except return double the absolute difference if n is over 21.
    int diff21(int n) {

    int difference = 21 - n;

    if (n > 21) {
    difference = Math.abs(difference) * 2;
    }
    return difference;

    }

    // parrotTrouble
    // We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23.
    // We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return true if we are in trouble.
    boolean parrotTrouble(boolean talking, int hour) {

    return (talking && (hour < 7 || hour > 20));

    }

    // makes10
    // Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.
    boolean makes10(int a, int b) {

    int sum = a + b;
    return (sum == 10 || a == 10 || b == 10);

    }



  17. @schifano schifano revised this gist Jan 5, 2014. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -37,3 +37,27 @@ public int sumDouble(int a, int b) {



























  18. @schifano schifano revised this gist Jan 5, 2014. 1 changed file with 28 additions and 12 deletions.
    40 changes: 28 additions & 12 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,39 @@
    /* The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it * is not a weekday or we're on vacation. Return true if we sleep in.
    *
    * sleepIn(false, false) → true
    * sleepIn(true, false) → false
    * sleepIn(false, true) → true
    */
    // sleepIn
    // The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation.
    // We sleep in if it * is not a weekday or we're on vacation. Return true if we sleep in.
    public boolean sleepIn(boolean weekday, boolean vacation) {
    if (!weekday || vacation) {
    return true;
    }
    return false;
    }

    /* We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble.
    *
    * monkeyTrouble(true, true) → true
    * monkeyTrouble(false, false) → true
    * monkeyTrouble(true, false) → false
    */
    // monkeyTrouble
    // We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling.
    // We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble.
    public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    return (aSmile == bSmile);
    }

    // sumDouble
    // Given two int values, return their sum.
    // Unless the two values are the same, then return double their sum.
    public int sumDouble(int a, int b) {

    int sum = a + b;

    if (a == b) {
    sum = sum *2;
    }
    return sum;

    }









  19. @schifano schifano revised this gist Jan 5, 2014. No changes.
  20. @schifano schifano revised this gist Jan 5, 2014. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -9,4 +9,15 @@ public boolean sleepIn(boolean weekday, boolean vacation) {
    return true;
    }
    return false;
    }
    }

    /* We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble.
    *
    * monkeyTrouble(true, true) → true
    * monkeyTrouble(false, false) → true
    * monkeyTrouble(true, false) → false
    */
    public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    return (aSmile == bSmile);
    }

  21. @schifano schifano created this gist Jan 5, 2014.
    12 changes: 12 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    /* The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it * is not a weekday or we're on vacation. Return true if we sleep in.
    *
    * sleepIn(false, false) → true
    * sleepIn(true, false) → false
    * sleepIn(false, true) → true
    */
    public boolean sleepIn(boolean weekday, boolean vacation) {
    if (!weekday || vacation) {
    return true;
    }
    return false;
    }