TASK: Write a static method called repeatString that takes as input a String parameter word followed by an int parameter num. It should return a String that would be the result of repeating word exactly num times. If num is 0 or negative, the method should output an empty string.


HINT: Don't print out anything! Your method should return a string, not print it. print the output of your method behind the scenes to check that your method returns the right string; if you add your own print statements then the output won't be correct.


HINT: Make use of String concatenation!

Answers

Answer 1

Answer:

Follows are the method definition to this question:

public static String repeatString(String word, int num)//defining a String method repeatString that takes two parameters

{

   String Val= "";//defining String variable Val

   for(int x = 0;x<num;x++)//defining for loop to calculate number of string

   {

       Val=Val+word;//hold calculated value in Val

   }

   return Val;//return Val value

}

Explanation:

Follows are the full code to this question:

import java.util.*;//import package for user input

public class Main//defining class Main

{

   public static String repeatString(String word, int num)//defining a String method repeatString that takes two parameters

{

   String Val= "";//defining String variable Val

   for(int x = 0;x<num;x++)//defining for loop to calculate number of string

   {

       Val=Val+word;//hold calculated value in Val

   }

   return Val;//return Val value

}

public static void main(String[] ar)//defining main method

{

   int num;//defining integer variable

   String word; //defining String variable

   Scanner on=new Scanner(System.in);//creating Scanner class Object

   System.out.print("Enter numeric value: ");//print message

   num=on.nextInt();//input value

   System.out.print("Enter String value: ");//print message

   word=on.next();//input value

   System.out.print(repeatString(word,num));//call method and print its return value

}

}

Output:

Enter numeric value: 3

Enter String value: data

datadatadata

Description:

Inside the main class,  a static string method "repeatString" is defined that accepts two parameters "word and num", in which one is a string and one is an integer.

Inside the method, the string variable "Val" is defined, which uses the "for" loop to calculate the number of string and store its value in the variable.

In the main class, the above two variables are defined, that uses the Scanner class object for input the value and pass into the method and print its value.


Related Questions

Josh needs to write a research report for his Civics class. Which file type will allow him to save his file?
A: DOC
B: GIF
C: JPG
D: ZIP

Answers

answer is Doc type format

Answer:

Josh would have to use a DOC as that will allow him to save his file.

Explanation:

DOC is a document file format, basically, a typed file on your computer. It's the best place for anyone to edit their document.

Other Questions
Jennifer is currently standing on a cruise ship at 29 feet above sea level. Jim is swimming at 8 feet below sea level. Jennifer is currently standing on a cruise ship at 29 feet above sea level. Jim is swimming at 8 feet below sea level. How far apart are Jim and Jennifer?How far apart are Jim and Jennifer? What other nation does Germany invite to attack the United States?chinaFranceCanadajapan Since the collapse of the Soviet Union in 1991, what has the us emerged as what country is located in the eastern part of europe Which of the following is the proper scientific notation for the number18,200?O A. .182 x 105B. 1.82 x 104O c. 18200 x 100O D. 182 x 102 Identify the bold (***) subordinate clauses as noun, adverbial, or relative.***When Olivia returned from her week-long summer vacation***, all her flowers had wilted.Joe's parents will be happy with ***whatever college he chooses to attend***.Did you know ***that Halley's Comet appears once every seventy-six years***?The Chinese conversation classes ***that I took in college*** have proven to be very beneficial.***Although you protest***, I will not change my mind.The student ***who graduated as valedictorian*** has a reputation of putting forth her best effort in all her activities. Dan steps on the brakes in his toy car which causes a braking force of 700 N to the left. a) If Dans car weighs 750 N, then what is the mass of Dans car?b) What is the acceleration of Dans car? S The people of which land were cut off from sea trade routes on the Mediterranean? O Asia Minor O Italy O Persia O Spain Select the term that matches each definition: a) A decrease in the solubility of an ionic compound as a result of the addition of a common ion. b) The mass of a salt in grams that will dissolve in 100 mL of water. c) A solution that has dissolved the maximum amount of a compound at a given temperature. Any further addition of salt will remain undissolved. d) The product of the molarities of the dissolved ions, raised to a power equal to the ion's coefficient in the balanced chemical equation. e) The maximum number of moles of a salt that will dissolve in 1 L of solution. *** Answer options for all questions: *** - Solubility - Molar Solubility - Solubility product constant - Common ion effect - Saturated Solution straws are sold in packs and boxesthere are 15 straws in each packthere are 48 straws in each boxTricia buts p packs of straws and b boxes of straws write down an expression in terms of p and b for the total number of straws bought by Tricia You are the manager of a monopoly that faces a demand curve described by P = 85 5Q. Your costs are C = 20 + 5Q. The profit-maximizing output for your firm is: BRAINLIEST AND 20 POINTS PLZ HELP ASAPA student writes an incorrect step while checking if the sum of the measures of the two remote interior angles of triangle ABC below is equal to the measure of the exterior angle.In which step did the student first make a mistake and how can it be corrected?Step 1; it should be mm + mn + mo = 180 degrees (sum of angles in a triangle)Step 1; it should be mm + mn + mo = 90 degrees (adjacent angles)Step 2; it should be mo + mp = 180 degrees (alternate exterior angles)Step 2; mp mo = 90 degrees (alternate interior angles) List the numbers in order from least to greatest.-4,8,-15,-8,4,1The numbers in order from least to greater Are Im not well with percentages lol. Can someone help me with solving it and answer Which statement best describes how the Inca developed and improved methods of agriculture? They planted crops that needed little water to grow. They used llamas to carry water to areas with little rainfall. They made terraces in the mountains to create more farmable land. They brought soil from the Amazon Rainforest to make their farmland richer. Help me please ^_^!!! A few boats were traveling upstream and downstream. A satellite recorded the movements of several boats. A motor boat traveled -3.4 miles for every hour in 0.75 hours. How much progress?Explain plsss When you mix two colors of paint in equivalent ratios, the resulting color is always the same.Use the table below to help you answer the following question.How many cups of yellow paint should you mix with 1 cup of blue paint to make the same shade of green? I will mark BRAINLIEST Which of the following does not occur in an aquatic ecosystem as a result of nutrient loading?a. acidification of the waterb. algae growth as a result of eutrophicationc. degraded quality of water for human and livestock consumptiond. improved water habitats for human recreation During November, the production department of a process operations system completed and transferred to finished goods 26,000 units that were in process at the beginning of November and 120,000 units that were started and completed in November. November's beginning inventory units were 100% complete with respect to materials and 45% complete with respect to conversion. At the end of November, 33,000 additional units were in process in the production department and were 100% complete with respect to materials and 65% complete with respect to conversion. Required:Compute the number of equivalent units with respect to materials for November using the weighted-average method.