Java Strings Part Two- 16 Problems with Solutions

Problem-1 Given a string, return a string where for every char in the original, there are two chars. Example doubleChar(“The”) → “TThhee” doubleChar(“AAbb”) → “AAAAbbbb” doubleChar(“Hi-There”) → “HHii–TThheerree” Solution public String doubleChar(String str) { String result=””; for (int i=0;i<str.length();i++){ result+=str.substring(i,i+1)+str.substring(i,i+1); } return result; } Problem-2 Return the number of times that the string “hi” appears … [Read more…]

Java Logic Part Two- 9 Problems with Solutions

Problem-1 We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return true if it is possible to make the goal by choosing from the given bricks. Example makeBricks(3, 1, 8) → true makeBricks(3, 1, 9) … [Read more…]

Java Logic Part One -30 Problems with Solutions

Problem-1 When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return true if the party with the given … [Read more…]

Java Basics Part Two -16 Problems with Solutions

Problem-1 Given a string and a non-negative int n, return a larger string that is n copies of the original string. Example: stringTimes(“Hi”, 2) → “HiHi” stringTimes(“Hi”, 3) → “HiHiHi” stringTimes(“Hi”, 1) → “Hi” Solution public String stringTimes(String str, int n) { String y=””; // empty string to store the result for (int i=0;i<n; i++){ … [Read more…]

Java Basics Part One- 28 Problems with Solutions

 Problem -1 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. Example: sleepIn(false, false) → true sleepIn(true, false) → false sleepIn(false, true) → true … [Read more…]