Question

Write a static method called printWithSpaces that takes a String as its parameter and prints the...

Write a static method called printWithSpaces that takes a String as its parameter and prints the characters of the string separated by spaces. For example:

> Methods.printWithSpaces("method")
m e t h o d

You should have a single space after the last character. This method should not return a value. That is similar to this code for printing the string vertically

public static void printVertical(String s) {

for (int i = 0; i < s.length(); i++) {

char c = s.charAt(i);

System.out.println(c);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Methods.java
public class Methods {
    public static void printWithSpaces(String s){
        for(int i = 0;i<s.length();i++){
            System.out.print(s.charAt(i)+" ");
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a static method called printWithSpaces that takes a String as its parameter and prints the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a static method called encodeString that takes a string as input and prints to the...

    Write a static method called encodeString that takes a string as input and prints to the console a word where each letter is advanced by one value. In addition, your method should return the encoded String. You may make use of the method provided below to help you. public static char encode(char x) { int current = x; current++; return (char)current; } This method takes a character and returns an "encoded" character. In other words, encode('a') will return 'b'. Your...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...

  • JAVA Write a method that accepts a String as an argument. The method should use recursion...

    JAVA Write a method that accepts a String as an argument. The method should use recursion to display each individual character in the String. Then, modify the method you just wrote so it displays the String backwards. The following code does not correctly display the String backwards. It merely moves the first character of the String to the end: public static void displayCharacter(String s)    {        if(s.length() == 0)            return;        else       ...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus";...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println ()

  • Debug following methods(longestRun(finds how many times a character got repeated), findLastP(finds last p in a string),...

    Debug following methods(longestRun(finds how many times a character got repeated), findLastP(finds last p in a string), findFirstP(finds first p in a string)) in java language. public class simpleLoops { /** * @param args */ public static void main(String[] args) {    System.out.println(longestRun("aabbbccd"));    System.out.println("Expected 3");    System.out.println(longestRun("aaa"));    System.out.println("Expected 3");    System.out.println(longestRun("aabbbb"));    System.out.println("Expected 4");          int count = countP("Mississippi"); System.out.println(count);    int result = findLastP("Mississippi"); System.out.println(result); result = findFirstP("stop"); System.out.println(result); result = findFirstP("xxxyyyzzz"); System.out.println(result); } /** *...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Given the following code: public static void foo3(String s) { if (s.length() >0) { System.out.print(s.charAt(s.length() -1));...

    Given the following code: public static void foo3(String s) { if (s.length() >0) { System.out.print(s.charAt(s.length() -1)); foo3(s.substring(0, s.length() -1)); } } What is the output of: foo3(“”); 2, You coded the following in the file Test.java : System.out.println( foo(5)); //more code here public static int foo(int n) //line 9 { if (n = = 0)    return 1; else    System.out.println(n* foo(n-1) ); }                                    //line 15 At compile time, you get the following error: Text.java: 15: missing return statement }                                ...

  • write a static method named greet that takes a String that represents a name and prints...

    write a static method named greet that takes a String that represents a name and prints a personalized greeting to StdDraw. So far i have in the main method... 4 public class TakeHomeAssignment 5 { 6 public static void main(String[] args) 7 { 8 StdDraw.setScale(0,500); 9 //drawEye(250,250,50); 10 String greet(Aaron); 11 // drawStickFigure(250,250); and for the method; //public static String greet( name) 31 //{ 32 33 34 //StdDraw.text(250,250,"Hello" +name); 35 36 // } Im getting errors saying its an illegal...

  • Write a recursive function (static method) called removeCopies that takes a String as a parameter and...

    Write a recursive function (static method) called removeCopies that takes a String as a parameter and replaces any copies of the same character with a single occurrence of the character. Your function must be recursive (no loops). For example: removeCopies(“abccd”) => “abcd” removeCopies(“xxxxxxx”) => “x” removeCopies(“xxxyyzaaxxx”) => “xyzax”

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT