Need help with two Java coding problems. The first one is: Using nested for loop
ID: 3860718 • Letter: N
Question
Need help with two Java coding problems.
The first one is:
Using nested for loops, write the code that produces the following output:
|********|
**|****|**
****||****
**|****|**
|********|
The second problem i have is:
Write a method called revString that accepts a string phrase as a parameter. Your method will reverse the order of the characters within the phrase. Also, your method should add up how many spaces are in the string. As output your method will print out the new reversed String and display how many spaces are in the String passed in. Some examples are below.
Method Call Output
revString("Hello"); olleH
There are 0 spaces in this phrase.
revString("Hello there"); ereht olleH
There are 1 spaces in this phrase.
Also this is a basic class so please basic language would be prefered, thank you!
Explanation / Answer
Both parts of the code are done under 2 functions
Please contact if you need any further help
CODE
package Chegg;
public class Pattern1 {
public static void main(String[] args) {
print_pattern();
reverse_string("Hello there");
}
public static void print_pattern() {
for (int i = 1; i <= 5; i++) {
if (i == 1 || i == 5) {
System.out.print("|");
for (int j = 0; j < 8; j++)
System.out.print("*");
System.out.println("|");
} else if (i == 2 || i == 4) {
for (int j = 0; j < 2; j++)
System.out.print("*");
System.out.print("|");
for (int j = 0; j < 4; j++)
System.out.print("*");
System.out.print("|");
for (int j = 0; j < 2; j++)
System.out.print("*");
System.out.println();
} else {
for (int j = 0; j < 4; j++)
System.out.print("*");
System.out.print("||");
for (int j = 0; j < 4; j++)
System.out.print("*");
System.out.println();
}
}
}
public static void reverse_string(String s) {
int count = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (" ".equals(s.charAt(i)+""))
count++;
System.out.print(s.charAt(i));
}
System.out.println(" There are " + count + " spaces in this phrase.");
}
}
Sample output
|********|
**|****|**
****||****
**|****|**
|********|
ereht olleH
There are 1 spaces in this phrase.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.