Question

Hi, I can't figure out how to make spaces between the characters of "Davy Crockett." I...

Hi,

I can't figure out how to make spaces between the characters of "Davy Crockett." I can get the output as "Davy Crockett" but not with the added spaces. Could you give me some suggestions and feedback please? Thank you.

public class Practice3 {

public static void main (String [] args) {

System.out.println(pentagonArea(3.14));

System.out.println("Davy Crockett");

}

public static double pentagonArea(double a) {

return (Math.sqrt(5*(5 + 2 * Math.sqrt(a)) ) * a *a)/4;

}

public static String addSpaces(String s) {

String str=" ";

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

str=str+s.charAt(i)+ " ";

}

return str.trim();

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

public class Practice3 { // Here declaring the class name as Practice3
public static void main (String [] args) { // Start of main()  
System.out.println(pentagonArea(3.14)); // Here calling the pentagonArea() method
System.out.println(addSpaces("Davy Crockett"));
// Here calling the addSpaces() method in the print statement

}// End of main()  
public static double pentagonArea(double a) { // Definition of the method pentagonArea()
return (Math.sqrt(5*(5 + 2 * Math.sqrt(a)) ) * a *a)/4;
//Here returning value of pentagonArea to the main()
}
public static String addSpaces(String s) { // Definition of the method addSpaces()
String str=" ";
for (int i = 0; i<s.length(); i++) {
str=str+s.charAt(i)+ " "; //Here make spaces between the characters of String s
}
return str.trim(); //Here returning value of str to the main()
}
} // End of class Practice3

Output:

%2 Administrator: Windows Command Processor F:\java〉javac Practic e3-Java F:\java〉java Practic e3 16.11072766097994 Davy Cr ocke t C ro c k e t t F O C t F:\java〉

Add a comment
Know the answer?
Add Answer to:
Hi, I can't figure out how to make spaces between the characters of "Davy Crockett." I...
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
  • 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(""); ]...

  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • 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 =...

  • 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 ()

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

  • Question 8 Identify the COMPILER ERRORS and EXCEPTIONS in this program. This program is saved in...

    Question 8 Identify the COMPILER ERRORS and EXCEPTIONS in this program. This program is saved in a file named MyProgram.java. public class MYProgram { public static void main( String [] args ) { int [] arrayOfIntegers = { 15, 20, 30 50, 60 }; for( int i = 0; i <= arrayOfIntegers.length(); i++ ) { System.out.println( "Hello" + i * 2 ) int i = 100 * arrayOfIntegers[ i ]; doSomething( i ); } } Public static void DoSomething( )...

  • 1 Problem Description Instructions. You are provided one skeleton program named LCS.java . The source files...

    1 Problem Description Instructions. You are provided one skeleton program named LCS.java . The source files are available on Canvas in a folder named HW6 . Please modify the skeleton code to solve the following tasks. • Task 1 (100 pts). Implement the lcs length() function as discussed in Lecture 11. • Note: You should not return the double-array b and c as in the pseu- docode. Instead, return the length of the longest common subsequence. • Hint: To get...

  • Bet you can't figure this out Create a Recursive Method to test whether a partial string...

    Bet you can't figure this out Create a Recursive Method to test whether a partial string is a subset of a full string. If the partial string is a subset of the full string return true. Otherwise return false.Create a Recursive Method to test whether a partial string is a subset of a full string. If the partial string is a subset of the full string return true. Otherwise return false. Here is the Main and Recursive below: (Note only...

  • Simplifying radicals

    The program is in java. The problems are it will not simplify any kind of odd number, and it will simplify some incorrectly. For example, if you input 17, it will print nothing out. If you input 200, it will print sqrt(200) when the answer is 10 * sqrt(2). I just can't seem to figure out why.import java.util.Scanner;public class Radicals{ public static void main(String[] args) {  Scanner in = new Scanner(System.in);  System.out.println("Please enter the radical to simplify: sqrt(x)");  System.out.print("x: ");  double rad = in.nextDouble();  double simp =...

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
ADVERTISEMENT