Question
In JAVA complete this code import java.util.*; public class Pyramid { public static String pyramid(int n){ // create a new StringBuilder object named p. It
In JAVA
complete this code
import java.util.*;
public class Pyramid {
public static String pyramid(int n){
// create a new StringBuilder object named p. It is a blank string
// at first. Feel free to print p out as you work.
StringBuilder p = new StringBuilder();
// Build the pyramid string here. Feel free to rename p.
// Use the append method to add a string to the end of p.
// eg: p.append("*");
// p is a StringBuilder object, which is not the same as a
// String object. Luckily, the StringBuilder class comes
// with a method called toString.
String result = p.toString();
return result;
}
public static String sidePyramid(int n){
StringBuilder p = new StringBuilder();
// work here
String result = p.toString();
return result;
}
public static String alternatingPyramid(int n){
StringBuilder p = new StringBuilder();
// work here
// Out of these 3 pyramids, this will be the most difficult.
// I highly recommend solving this on paper/whiteboard first.
String result = p.toString();
return result;
}
public static String camelKebab(String text){
StringBuilder result = new StringBuilder();
// work here
// use these methods to convert a character to upper/lower-case
// Character.toLowerCase(some_char)
// Character.toUpperCase(some_char)
return result.toString();
}
public static void main(String[] args){
// To see your results, call the functions and print them out here.
// eg: System.out.println(pyramid(5));
run_tests();
}
public static void run_tests(){
System.out.print("pyramid(2) - ");
if("* ** ".equals(pyramid(2))){
System.out.println("passed");
}else{
System.out.println("failed");
}
System.out.print("pyramid(5) - ");
if("* ** *** **** ***** ".equals(pyramid(5))){
System.out.println("passed");
}else{
System.out.println("failed");
}
System.out.print("sidePyramid(2) - ");
if("* ** * ".equals(sidePyramid(2))){
System.out.println("passed");
}else{
System.out.println("failed");
}
System.out.print("sidePyramid(5) - ");
if("* ** *** **** ***** **** *** ** * ".equals(sidePyramid(5))){
System.out.println("passed");
}else{
System.out.println("failed");
}
System.out.print("alternatingPyramid(2) - ");
if(" * *+* ".equals(alternatingPyramid(2))){
System.out.println("passed");
}else{
System.out.println("failed");
}
System.out.print("alternatingPyramid(5) - ");
if(" * *+* *+*+* *+*+*+* *+*+*+*+* ".equals(alternatingPyramid(5))){
System.out.println("passed");
}else{
System.out.println("failed");
}
System.out.print("camelKebab(\"magnificent flying penguins\") - ");
if("magnificent-Flying-Penguins".equals(camelKebab("magnificent flying
penguins"))){
System.out.println("passed");
}else{
System.out.println("failed");
}
}
}
Write a function String pyranid(int n ) that takes an integer rows as an argument, and returns a String that represents a pyramid that has zows height. For example, if rows is 5, printing the string that is returned should show: Exercise 2 Write a function String sidepyramid (int n) that takes the number of rows as an argument, and returns a string that represents a pyramid on its side. For example, if rows is 5 , printing the string will show: Exercise 3 Write a function string alternatingPyramid (int n) that takes a number of rows as an argument, and returns a string that represents a pyramid with alternating '' 1 ' and ' + ' symbols. For example, if rows is 6 , then printing the string will show: Hint: try attempting this without alternating at first Hint: separate the logic for spaces and asterisks. Also, highlight the pyramid using your mouse. What do you notice about the whitespace to the left and right side of the tree
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started