Question

In: Computer Science

Given a string and a non-negative int n, we'll say that the front of the string...

Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;


frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"

Must work the following problem using a while loop or do while.

Solutions

Expert Solution

StringTimesTest.java


public class StringTimesTest {

  
   public static void main(String[] args) {
      
System.out.println(frontTimes("Chocolate", 2) );
System.out.println(frontTimes("Chocolate", 3) );
System.out.println(frontTimes("Abc", 3) );
   }
   public static String stringTimes(String s, int n){
       String returnString = "";
       int i=0;
       while(i<n){
           returnString = returnString + s;
           i++;
       }
       return returnString;
   }
   public static String frontTimes(String s, int n){
       if(s.length()<=3){
           return stringTimes(s, n);
       }
       else{
           return stringTimes(s.substring(0,3), n);
       }
   }

}

Output:

ChoCho
ChoChoCho
AbcAbcAbc


Related Solutions

Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); //...
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); // converts a hexadecimal number (represented as a STRING) to decimal string dectobin(int); // converts a decimal number to binary (represted as a STRING) string dectohex(int); // converts a decimal number to hexadecimal (represted as a STRING) //the addbin and addhex functions work on UNsigned numbers string addbin(string, string); // adds two binary numbers together (represented as STRINGS) string addhex(string, string); // adds two hexadecimal...
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined...
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined by 0 ! 5 1, n! = 1·2·3· · ·n if n $ 1. For example, 4! = 1·2·3·4 = 24. Write a program that prompts the user to enter a nonnegative integer and outputs the factorial of the number. Allow the user to repeat the program. Example: If the user enters a 3 then your program should perform answer = 3 * 2...
string getStyle() ; int getNumOfBedRooms() ; int getNumOfBathRooms(); int getNumOfCarsGarage(); int getYearBuilt(); int getFinishedSquareFootage() ; double...
string getStyle() ; int getNumOfBedRooms() ; int getNumOfBathRooms(); int getNumOfCarsGarage(); int getYearBuilt(); int getFinishedSquareFootage() ; double getPrice(double p) ; double getTax(double t) ; void print() ; houseType(); houseType(string s, int numOfBeds, int numOfBaths, int numOfCars, int yBuilt, int squareFootage, double p, double t); private: string style; int numOfBedrooms; int numOfBathrooms; int numOfCarsGarage; int yearBuilt; int finishedSquareFootage; double price; double tax; }; Questions a.Write the definition of the member function set so that private members are set accordingy to the parameters....
prove or disprove .if n is a non negative integer, then 5 divides 2 ⋅ 4^n...
prove or disprove .if n is a non negative integer, then 5 divides 2 ⋅ 4^n + 3⋅9^n.
binarySearchLengths(String[] inArray, String search, int start, int end) This method will take in a array of...
binarySearchLengths(String[] inArray, String search, int start, int end) This method will take in a array of strings that have been sorted in order of increasing length, for ties in the string lengths they will be in alphabetical order. The method also takes in a search string and range of indexes (start, end). The method will return a String formatted with the path of search ranges followed by a decision (true or false), see the examples below. Example 1: binarySearchLengths({"a","aaa","aaaaa"},"bb",0,2) would...
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt() = 0;     virtual void fight(Monster&);     string getName() const; }; class GiantMonster : public Monster {     protected:         int height;          public:         GiantMonster(string, int, int);         virtual void trample(); }; class Dinosaur : public GiantMonster {     public:     Dinosaur(string, int, int);     void hunt();     void roar(); }; class Kraken : protected GiantMonster {     public:     Kraken(string, int, int);     virtual void hunt();     void sinkShip(); }; Indicate if the code snippets below are...
This is JAVA Return the centered average of an array of ints, which we'll say is...
This is JAVA Return the centered average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array has a length of 3 or more. 1 public int centeredAverage(int[] nums) 2 { 3...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 ×...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 × ... × (n − 1) × n A. Write a function that takes an input value n and returns its factorial result n!. Ensure that your function returns an error statement if the input n is either a negative or a non-integer value. You cannot use the prod() or factorial() functions. The Euler number e is calculated as the sum of the infinite series...
int main() {    footBallPlayerType bigGiants[MAX];    int numberOfPlayers;    int choice;    string name;   ...
int main() {    footBallPlayerType bigGiants[MAX];    int numberOfPlayers;    int choice;    string name;    int playerNum;    int numOfTouchDowns;    int numOfcatches;    int numOfPassingYards;    int numOfReceivingYards;    int numOfRushingYards;    int ret;    int num = 0;    ifstream inFile;    ofstream outFile;    ret = openFile(inFile);    if (ret)        getData(inFile, bigGiants, numberOfPlayers);    else        return 1;    /// replace with the proper call to getData    do    {       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT