-1

I am trying to make a histogram with inputting a value and rounding it. The rounded value should print out the number of asterisks.

I did the following code and inputted a value but, the output is coming out as nothing.

public class Histogram
{
    public static void main(String args[]) 
    {
      histogram obj = new histogram();
      obj.histogram(13.5);
    }
}

class histogram
{
    public void histogram(double num)
    {
        int roundNum = (int)(num);
        if (roundNum == 14)
        {
            System.out.println("**************" + num);
        }
        if (roundNum == 3)
        {
            System.out.println("***" + num);
        }
        if (roundNum == 16)
        {
            System.out.println("****************" + num);
        }
        if (roundNum == 0)
        {
            System.out.println("" + num);
        }
        if (roundNum == 1)
        {
            System.out.println("*" + num);
        }
    }
}
  • The output seems correct for the code as written. Please explain what you are expecting the output to be, and why. – Hovercraft Full Of Eels Jan 14 '23 at 20:57
  • The values I am using are: 13.5, 16.1, 3.2, 0.0, and 0.6 – Abdullah Malik Jan 14 '23 at 20:57
  • ************** 13.5 *** 3.2 **************** 16.1 0.0 * .6 – Abdullah Malik Jan 14 '23 at 20:58
  • 1
    With input of 13.5, will any of those if tests return true? And if not, *again*, why would you expect any output? – Hovercraft Full Of Eels Jan 14 '23 at 20:59
  • It is going to assess the rounded values and print the asterisks in the line with the number assigned as a double. – Abdullah Malik Jan 14 '23 at 21:00
  • Again, if the input is 13.5, then roundNum should be 13, right? And if roundNum is 13, then all the if tests will be false, and none of the code in the if blocks will run. Again, *why* are you expecting to see any output with this code? – Hovercraft Full Of Eels Jan 14 '23 at 21:02
  • I'm trying to get you to think about your code. If you do this well, you will see the error in your logic and hopefully be able to fix your error. – Hovercraft Full Of Eels Jan 14 '23 at 21:02
  • Keep in mind that casting a `double` to an `int` doesn't round _per se_. It simply drops the decimal value. So `13.x` cast to an `int` will always be `13`, never `14`. I suppose you could say the cast always rounds towards zero. – Slaw Jan 14 '23 at 21:12
  • Does this answer your question? [I have a double value = 1.6 i want to round it up like 2.in java](https://stackoverflow.com/questions/35706438/i-have-a-double-value-1-6-i-want-to-round-it-up-like-2-in-java) – Nysand Jan 14 '23 at 21:15
  • Thank you, I able to find a solution without using the rounding. – Abdullah Malik Jan 14 '23 at 21:17
  • I added 0.5 and set and if statement equal to 14 and set the amount of asterisks. – Abdullah Malik Jan 14 '23 at 21:18
  • Yes that is the obvious choice since your if statement will print result at 14 only in that first if statement. But if you want to round your doubles use the method I put in the answer. – Nysand Jan 14 '23 at 21:21
  • I am doing it individually by value so the histogram will form one by one. It gave me a an error when I replaced my double to int conversion. – Abdullah Malik Jan 14 '23 at 21:33

2 Answers2

0

In Java, typecasting to primitive type int from primitive type double can be thought of as simply removing the decimal part.

For example;

System.out.println((int) 13.1); // prints 13
System.out.println((int) 13.5); // prints 13
System.out.println((int) 13.9); // prints 13

So, when you call obj.histogram(13.5); with the function parameter num being 13.5, the operation int roundNum = (int)(num); is the same as int roundNum = (int)(13.5);, and assigns 13 to the roundNum variable.

Since no if statements handle this case (roundNum being 13), no output is generated.

On another note, hardcoding a lot of if statements for checking the same variable over and over again can usually lead to unnecessarily complex, inefficient and hard-to-read code. Can you think of a better way to print "*" characters for the histogram, by using the roundNum variable? (Hint: try experimenting with for loops)

-1

Change your int roundNum = (int)(num); to int rounded = Math.round((float)num); it should give you the desired output.

Nysand
  • 102
  • 8