1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Readme [11.4K]
3 years ago
11

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!
Computers and Technology
1 answer:
leva [86]3 years ago
6 0

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.

You might be interested in
Calculate the resistance of an unknown resistor in a circuit with 30 volts and 6 amps. The formula is R = V/I.
arsen [322]
The answer is 5 ohms
7 0
2 years ago
If you are interested in working for a specific company, what type of job site should you look at for opening?
Ainat [17]

Answer:

if you are finding to be a pilot you can find it at jinnah airport

Explanation:

3 0
3 years ago
ANSWER ASAP, I'LL MARK THE BRAINLIEST
Travka [436]

Answer:

Push to make switch

Explanation:

The sound is created after you push it. Pushing it is the input and the sound is the output

4 0
2 years ago
Consider the following method, remDups, which is intended to remove duplicate consecutive elements from nums, an ArrayList of in
MAXImum [283]

Answer:

B. {1, 2, 2, 3, 3, 4, 5}

Explanation:

Given

The above code segment

Required

Determine which list does not work

The list that didn't work is B.\ \{1, 2, 2, 3, 3, 4, 5\}

Considering options (A) to (E), we notice that only list B has consecutive duplicate numbers i.e. 2,2 and 3,3

All other list do not have consecutive duplicate numbers

Option B can be represented as:

nums[0] = 1

nums[1] = 2

nums[2] = 2

nums[3] = 3

nums[4] = 3

nums[5] = 4

nums[6] = 5

if (nums.get(j).equals(nums.get(j + 1)))

The above if condition checks for duplicate numbers.

In (B), when the elements at index 1 and 2 (i.e. 2 and 2) are compared, one of the 2's is removed and the Arraylist becomes:

nums[0] = 1

nums[1] = 2

nums[2] = 3

nums[3] = 3

nums[4] = 4

nums[5] = 5

The next comparison is: index 3 and 4. Meaning that comparison of index 2 and 3 has been skipped.

<em>This is so because of the way the if statement is constructed.</em>

3 0
3 years ago
In cell E13, create a formula using the AVERAGE function
lukranit [14]
USE SOCRACTIC IT WOULD REALLY HELP WITH QUESTIONS LIKE THIS
3 0
3 years ago
Read 2 more answers
Other questions:
  • "what is the name of the ipsec configuration file"
    9·1 answer
  • What are the requirements of a data dictionary ?
    7·2 answers
  • What does the "configure dhcp options for proxy dhcp" option do?
    13·1 answer
  • Click _______ to view each individual record of a mail merge document.
    5·2 answers
  • Write a class named RetailItem that holds data about an item in a retail store. The class should store the following data in att
    15·1 answer
  • A popular encryption method used to protect data that travel over a wireless network is ____
    12·1 answer
  • Differences between electromechanical era and electronic era in point.<br>PLZ HELP​
    6·1 answer
  • Write a couple of paragraph on 'limited government and its important'?​
    7·1 answer
  • Please as soon as possible
    13·1 answer
  • Who is tim berners-lee
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!