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 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...
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first...
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first printing a request for the int, then creating an int x, and using the Scanner to read an int from the user and put it in x, like this: int x = scan.nextInt(); ☑ Next read in two doubles d1 and d2 from the user. This will look a lot like what you did for the int, but the scanner reads doubles using scan.nextDouble();...
Can you provide java code for a program that prompts the user by asking "How many...
Can you provide java code for a program that prompts the user by asking "How many one mile races have you ran?". After the user inputs how many one mile races have they run it then prompts the user to input how many seconds it took for them to finish each race. So for example, if the user ran 6 races then the user will have to input 6 race times. Is there a way when you prompt the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT