• Post Reply Bookmark Topic Watch Topic
  • New Topic

Program runs but not exact output needed  RSS feed

 
Kevin Kang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm new to Java programming and my professor assigned this really hard assignment, and I can't seem to get the right output. I get the right results, but it doesn't match the way the output is suppose to look like. I tried a lot of ways, but I can't fix it.
The code algorithm runs like this: It takes in a file, and you assign a constraint, and then the program runs and assigns a bracket if the grade in the file corresponds to the grades from 0-100.

My output:
What bucket size would you like?
>>> 10
000 - 010 | [][]
010 - 020 |
020 - 030 | [][]
030 - 040 | [][][][][][][]
040 - 050 | [][][][][][][]
050 - 060 | [][][][][][][]
060 - 070 | [][][][][][][][][][][][][][][][][]
070 - 080 | [][][][][][][][][][][][][][]
080 - 090 | [][][][][][][][][][][][][][][][][][][][][][][][][][]
090 - 100 | [][][][][][][][][][][][][][][][][]
100 - 100 | []

How the output should look like:
What bucket size would you like?
>>> 10
100 - 91 | [][][][][][][][][][][][]
90 - 81 | [][][][][][][][][][][][][][][][][][][][][][][][][][]
80 - 71 | [][][][][][][][][][][][][][]
70 - 61 | [][][][][][][][][][][][][][][][][]
60 - 51 | [][][][][][][]
50 - 41 | [][][][]
40 - 31 | [][][][][][]
30 - 21 | [][]
20 - 11 |
10 - 1 | []
0 - 0 | []

My code:
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid you have methods too long. Your main method should ideally contain one statement.
I think you are trying to write code before you have worked out what to do. You need to write down (preferably with paper pencil and [important]eraser) what you intend to do. When you have a formula, algorithm or similar, then you have a good chance of converting it into code.
 
Rodion Gork
Ranch Hand
Posts: 47
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, you'd better have single method for loading data from file to array. then another to ask for input. etc... you see, at least your printBar is a dedicated method - it is good.

To your question - it looks worth starting with flipping the output upside down. Your main loop counts from small values to large - and you obviously need to count from large to smaller. Start changes from here.

I'm new to Java programming and my professor assigned this really hard assignment

I'm sorry to say this, but it is very easy assignment. Just compose yourself and fix your code step by step. This assignment do not contain any puzzle, nothing which requires some brain-storming. It is very routine thing - simply to read data and plot them. So keep calm and continue, good luck

Feel free to ask any specific question.
 
Kevin Kang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I tweaked some parts of my code, but I still don't get the correct output. Could I have some assistance, please?

 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your original post it looked like your buckets were just coming out in the wrong sequence. So what's the problem now?

And if I misunderstood your original problem... it helps if you spell things out for us. When I look at your code it doesn't seem to represent buckets in any obvious way (at least not obvious to me in my 30-second look), so it's really hard for an outsider (i.e. anybody but you) to point to problems with it.
 
Kevin Kang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yes the buckets were wrong, but I fixed it. It's suppose to print out "[]" for the constraint the user inputs.

My current output looks like this:

What bucket size would you like?
>>> 5
100 - 96 | [][][][][][][][]
95 - 91 | [][][][][][][][]
90 - 86 | [][][][][][][][][][][][][][][][]
85 - 81 | [][][][][]
80 - 76 | [][][][][][][][][][][][]
75 - 71 | [][]
70 - 66 | [][][][][][][][][][][]
65 - 61 | [][][][][][]
60 - 56 | [][][][][][]
55 - 51 | []
50 - 46 | [][][]
45 - 41 | [][][][]
40 - 36 |
35 - 31 | [][][][]
30 - 26 | []
25 - 21 |
20 - 16 |
15 - 11 |
10 - 6 | []
5 - 1 | []
0 - 0 | []

I think it's counting too many, but I can't figure out it. I tried decreasing and increasing the "minLimit" and "maxLimit" but the result isn't pretty.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Kang wrote:So, I tweaked some parts of my code, but I still don't get the correct output. Could I have some assistance, please?

We're trying; but nobody here is simply going to hand you the code.

My advice:

1. StopCoding (←click). You're not writing "Hello World" any more, you have a substantial task; and you can't expect to simply bash out code to solve it. Right now, you're so busy thinking about what you've written and trying to "tweak" it to get it to work that you can't see the wood for the trees.

2. In order to write a good program, you need to understand what it needs to do (Note: WhatNotHow (←click)), and in order to do that you need to understand the problem.

3. Write out, in English (or your native language), all the things that need to be done.
For example:
You know you need to read grades from a file, so one task might be 'Read grade', or 'Read grades' (or maybe both).
You also know that grades in the file are strings, but your program will probably want them as numbers, so another task might be 'Convert grade'.
You also need to get a "bucket size", so another task might be 'get Bucket Size'.
You'll then probably need to convert your bucket size to ranges, so how about 'convert Bucket Size to ranges'.
...
and so on.
Note that you're not interested (at this point) in HOW they're going to work, simply that it's something you'll need to do.
Alternatively, if you really feel you have to put them in sequence, write some pseudo-code, but write it in English, not in Java.

Once you've done that, go back to your Histogram class, and see if it bears any resemblance to what you've mapped out. I doubt it will, but that's good, because now you're designing.

Hope it helps.

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!