Question

In: Computer Science

How do I code a pyramid and a row (reference shapes below) into JAVA using user...

How do I code a pyramid and a row (reference shapes below) into JAVA using user prompts (asking for different dimensions)? I am trying to make a nested loop code and keep running into these syntax issues. Will rate for proofed code!!!

Please try to leave lots of explanation.

Pyramid:

---*---

--**--

-***-

****

Row:

---***---

This is what I have so far:

public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       //start the scanner
       Scanner scanner = new Scanner(System.in);
      
       final char SIDE_SYMB = '-';
       final char MID_SYMB = '*';
      
       //Start setting values for pyramid
       String inputStr = "";
       char choice = ' ';
       int numSymbols = -1, sideWidth = -1, midWidth = -1;
      
       do {
           System.out.print("Please enter your selection");   
           System.out.print("'r' Print a row of symbols");
           System.out.print("'p' Print out a pyramid");
           System.out.print("'q' Quit program");
           inputStr = scanner.nextLine();
           if (inputStr.length() > 0) {
               choice = inputStr.charAt(0); }
           else { choice = ' '; }
          
           //Set up the switch
           switch (choice) {
          
           //Code for the row output
           case 'r':
           System.out.println("Width of the sides?");
           sideWidth = scanner.nextInt();
           System.out.println("Width of the middle?");
           midWidth = scanner.nextInt();
           scanner.nextLine();
          
           case 'p':
               System.out.println("Number of symbols on the lowest layer?");
               numSymbols = scanner.nextInt();   
               scanner.nextLine();
               // Flush junk newline symbols
               System.out.println();
               System.out.println(buildPyramid(SIDE_SYMB, MID_SYMB, numSymbols));
               break;

           case 'q':
               System.out.println("Bye");
               break;
           //Send code back to the top  
           default:
                   System.out.println("Please choose a valid option from the menu.");
                                               }
          
private static
String buildRow(char sideSymb, int sideWidth, char midSymb, int midWidth){
String result = "";


for (int i = 0; i < sideWidth; i++){
result += sideSymb; }

for (int i = 0; i < midWidth; i++) {
result += midSymb; }

return result;}

                  
                  
//Separate code for building pyramid shape                  
private static
   String buildPyramid(char sideSymb, char midSymb, int numSymbols) {
   String result = "";
   int totalSymbOneRow = numSymbols;
   for (int numStars = 1; numStars <= numSymbols; numStars += 2)   
   {int numDashed = (totalSymbOneRow - numStars) / 2;   
   String buildRow(char sideSymb, char numDashed, char midSymb, numStars);
                   result += row + "\n"; }
                   return result;
   }
}


the goal of the code is to get a diamond and a row output with a size rhat depends on user input. I am getting error codes thrown at me at fhe bottom of the code and do not know where rhey are coming from. if you input the code unto the JAVA 2019-20 platform yiu shiuld see the 2 red X logos and the one lightbulb with a red X at the bottom of the code.

Solutions

Expert Solution

import java.util.*;
public class RandomNum {
   public static void main(String[] args) {
   // TODO Auto-generated method stub
  
   //start the scanner
   Scanner scanner = new Scanner(System.in);
  
   final char SIDE_SYMB = '-';
   final char MID_SYMB = '*';
  
   //Start setting values for pyramid
   String inputStr = "";
   char choice = ' ';
   int numSymbols = -1, sideWidth = -1, midWidth = -1;
  
   do {
   System.out.print("Please enter your selection");   
   System.out.print("'r' Print a row of symbols");
   System.out.print("'p' Print out a pyramid");
   System.out.print("'q' Quit program");
   inputStr = scanner.nextLine();
   if (inputStr.length() > 0) {
   choice = inputStr.charAt(0); }
   else { choice = ' '; }
  
   //Set up the switch
   switch (choice)
   {
  
   //Code for the row output
   case 'r':
   System.out.println("Width of the sides?");
   sideWidth = scanner.nextInt();
   System.out.println("Width of the middle?");
   midWidth = scanner.nextInt();
   scanner.nextLine();
   // break;
   case 'p':
   System.out.println("Number of symbols on the lowest layer?");
   numSymbols = scanner.nextInt();   
   scanner.nextLine();
   // Flush junk newline symbols
   System.out.println();
   System.out.println(buildPyramid(SIDE_SYMB, MID_SYMB, numSymbols));
   break;

   case 'q':
   System.out.println("Bye");
   break;
   }
   //Send code back to the top
   // default:
   // System.out.println("Please choose a valid option from the menu.");
   }while(choice=='q');   
   }   
   private static String buildRow(char sideSymb, int sideWidth, char midSymb, int midWidth)
   {
   String result = "";


   for (int i = 0; i < sideWidth; i++){
   result += sideSymb; }

   for (int i = 0; i < midWidth; i++) {
   result += midSymb; }

   return result;

   }   
  
   //Separate code for building pyramid shape
   public static String buildPyramid(char sideSymb, char midSymb, int numSymbols)
   {
   String result = "";
   int totalSymbOneRow = numSymbols;
   for (int numStars = 1; numStars <= numSymbols; numStars += 2)   
   {
       int numDashed = (totalSymbOneRow - numStars) / 2;   
   String row=buildRow(sideSymb,numDashed, midSymb,numStars);//function call does not need datatypes
   result += row + "\n";
   }
   return result;
   }
  

}


Related Solutions

I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Using the provided Java program below, complete the code to do the following. You may need...
Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
How do I select every row in pandas dataframe?
How do I select every row in pandas dataframe?
C++ How do you make a 2d array with a user input. For example, the row...
C++ How do you make a 2d array with a user input. For example, the row and columns of the 2d array will be the same so the program can create a square matrix.
How can I print the sales reports grouped by the room grades using the java code...
How can I print the sales reports grouped by the room grades using the java code below: ----------------------------------------------------------------------------------------------------------------------------------- public class Book {    String bookno;    int ff;    Customer cust;       public String getBookingNumber()    {        return bookno;    }       public void BookNew(Customer c,Standard stand,Deluxe dx,Suite suite,int iStandard,int flag1,int iDelux,int flag2,int isdx,int flag3)       {        cust=c;    Fare f=new Fare();    int no,i;          if(c.type==1)    {       ...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user to type in a set of input. Below that textfield have the following controls to show string manipulations: (1) A button that will change the entire textfield’s current text to uppercase. (2) A button with its own textfield for a search value, that will tell the position the search value appears in the textfield above. (3) A button that reports the current number of...
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT