Question

Debug the following java code so that it will have given out put at the bottom....

Debug the following java code so that it will have given out put at the bottom.  Make sure to fix all syntax and logical errors.

import java.util.Scanner;

public class Graphs
{
   // draws 5 histograms
   public void drawHistograms()

      Scanner input = new Scanner( System.in );
      
      int number1 = 0; // first number
      int number2 = 0; // second number
      int number3 = 0; // third number
      int number4 = 0; // fourth number
      int number5 = 0; // fifth number

      int inputNumber; // number entered by user
      int value = 0; // number of stars to print
      counter = 1; // counter for current number

      while ( int counter <= 5 )
      {
         System.out.print( "Enter number: " );
         inputNumber = input.nextInt();

         // define appropriate num if input is between 1-30
         if ( inputNumber >= 1 && inputNumber <= 30 )
         {
            switch ( inputNumber )
            {
               case 1:
                  number1 = inputNumber;
                  break; // done processing case

               case 2:
                  number2 = inputNumber;
                  break; // done processing case

               case 3:
                  number3 = inputNumber;

               4:
                  number4 = inputNumber;
                  break; // done processing case

               case 5:
                  number5 = inputNumber;
                  break; // done processing case

            counter++
         } // end if
         else
            System.out.println(
               "Invalid Input\nNumber should be between 1 and 30" );
      } // end while

      // print histograms
      for ( counter = 1, counter >= 5, counter++ )
      {
         switch ( counter == 1 )
         {
            case 1:
               value = number1;
               break; // done processing case

            case 2:
               value = number2;
               break; // done processing case

            case 3:
               value = number3;
               break; // done processing case

            case 4:
               value = number4;
               break; // done processing case

            case 5:
               value = number5;
               break; // done processing case
         }

         for ( int j = 1; int j <= value; int j++ )
            System.out.print( "*" );
         
         System.out.println();         
      } // end for loop
   } // end method drawHistograms
} // end class Graphs

Sample output

--------------------Configuration: <Default>--------------------
Enter number: 5
Enter number: 2
Enter number: 8
Enter number: 4
Enter number: 6
*****
**
********
****
******

Process completed.

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

//Program is well tested and producing the same output as above. The above code is buggy and not feasible. So i wrote the //code from scratch to fulfill the need.

class Main
{
public static void drawHistograms(){

//Defining Scanner class to take input
Scanner sc = new Scanner(System.in);

//Defining Variables
int array[]=new int[10]; //increase the array size according to need
int i,j,k,number,counter=0;
char c ='*';
//Loop to recurcively take 5 inputs for making histogram. Change the value in loop to increase number of inputs

while(counter<5){
System.out.print("Enter Number: ");
number = sc.nextInt();
array[counter] = number;
counter = counter+1;
}

//making histogram
  
for(i =0; i<array.length; i++){
  
for(j=0;j<array[i];j++){
  
System.out.print(c);
  
}
System.out.println("");
}
  }

//main Function
public static void main (String[] args) {

//calling method

drawHistograms();
}
}

Add a comment
Know the answer?
Add Answer to:
Debug the following java code so that it will have given out put at the bottom....
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
  • LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR...

    LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR CODE: import java.util.Scanner; public class CalculatorProject { public static void main (String[] args) { Scanner yurr = new Scanner(System.in);    String mode; double i; String choice; String answer = "Y"; while (answer.equals("Y")) { System.out.println("Enter the calculator mode: Standard/Scientific?"); mode = yurr.next(); if(mode.equals("Scientific")) { System.out.print("Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x,...

  • IN JAVA: Write a program that displays a menu as shown in the sample run. You...

    IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...

  • In the following code, it gets hung up at    cout << "Number1 * Number2 =...

    In the following code, it gets hung up at    cout << "Number1 * Number2 = " << number1 * number2 << endl; giving an error of "no math for operator<<" what am i doing wrong? Thank you #include <iostream> #include <cctype> #include <cstdlib> using namespace std; class Rational //class for rational numbers (1/2, 5/9, ect..) {    public:        Rational(int numerator, int denominator);        Rational(int numberator);        Rational(); //default        friend istream& operator >>(istream& ins,...

  • 1. What is the output of each of the following code segments if the inputs are...

    1. What is the output of each of the following code segments if the inputs are as follows: 5 0 15 -5 2 Scanner input = new Scanner(System.in); int sum = 0; for(int i = 0; i < 5; i++){ System.out.print( “Enter a number”); int number = input.nextInt(); if(number <= 0) break; sum += number; } System.out.println(sum); Second Segment: Scanner input = new Scanner(System.in); int sum = 0; for(int i = 0; i < 5; i++){ System.out.print( “Enter a number”);...

  • Java Im doing a binary search on an array and need to allow as many queries...

    Java Im doing a binary search on an array and need to allow as many queries as possible and cannot figure out how to. The output should read something like this. Enter a number. 3 3 is a prime number. Enter another number. 4 4 is not a prime number. Enter another number. 8 Current file not large enough for 8. Enter another number. -1 Bye. My code so far looks like this. public static void main(String[] args)    {...

  • Hi. This is a prototype of Java. The following Java program was developed for prototyping a...

    Hi. This is a prototype of Java. The following Java program was developed for prototyping a mini calculator. Run the program and provide your feedback as the user/project sponsor. (Save the code as MiniCalculator.java; compile the file: javac MiniCalculator.java; and then run the program: java MiniCalculator). HINTs: May give feedback to the data type and operations handled by the program, the way the program to get numbers and operators, the way the calculation results are output, the termination of the...

  • I need the following java code commented import java.util.Scanner; public class Main { public static void...

    I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent...

    Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent debugging * problems. Solve one at a time, uncommenting the next * one only after the previous problem is working correctly. */ public class FindTheErrors { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); /* * Problem 1 Debugging * * This problem is to read in your first name, * last name, and current year and display them in *...

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