Gaming Need help with Java coursework

  • Thread starter Thread starter R2DJ
  • Start date Start date
  • Views Views 2,063
  • Replies Replies 13

R2DJ

Well-Known Member
Member
Joined
Jan 30, 2008
Messages
1,898
Solutions
1
Reaction score
17
Trophies
1
Location
London
XP
875
Country
The program should allow the tutor to enter in the various marks which the students have been awarded, until the tutor enters in a mark exceeding 100. At this point the program should display a histogram. Each star represents a student who achieved a module mark in the range shown.

Code:
0 - 29ÂÂ ***ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ(3 students received a mark between 0-29)
30-39ÂÂÂÂ*****
40-69ÂÂÂÂ********
70-100ÂÂ ****

20 students in total

What I have to do is to make a program in Java following the instructions mentioned above. Whenever I add a grade, a star must be added to the corresponding group. The same 4 categories will be used.

I know what to do mostly in this (use while loops, if-else statements, printing a star for each category) but I am stuck with how to display the histogram at the end (when the user enters 100+, the program stops and then the result histogram is shown. Finally, I have to make sure the histogram looks as neat as the one above.

Thanks.
 
Do you mean you're stuck on the logic to make the histogram display or you're stuck on the code to make the Histogram look like that?
 
xakota said:
Do you mean you're stuck on the logic to make the histogram display or you're stuck on the code to make the Histogram look like that?
Well, both really. I missed the tutorial on how to do the sample triangle pyramid of the same sort (and the lecturer doesn't upload the files to the online site)
frown.gif
 
Okay, well to make it appear you're probably going to want an if statement, like(pseudo code)
if(input variable > 100)
{print histogram}
else if(input variable =70)
variable for score is between 70 100 ++
[continue with the else if branch]

To make it look like it does you're probably going to want something like this(this is just for the first line of the histogram)
final String UBERFAIL = "0-29"; //put a few spaces after '29', for some reason gbatemp won't let me put them there
System.out.println(UBERFAIL);
for(int cnt = 0; cnt < x; cnt++) //where "x" is whatever variable you use to count how many people scored between 0 and 29
System.out.print("*"); //note that this is print and not println
 
Should be something like

CODEpublic static void main(String[] args) {
ÂÂÂÂScanner scanner = new Scanner(System.in);
ÂÂÂÂint input;
ÂÂÂÂint lowest = 0;
ÂÂÂÂint low = 0;
ÂÂÂÂint medium = 0;
ÂÂÂÂint high = 0;
ÂÂÂÂdo {
ÂÂÂÂÂÂÂÂSystem.out.print("Enter a score: ");
ÂÂÂÂÂÂÂÂinput = scanner.nextInt();
ÂÂÂÂÂÂÂÂif (input < 0) {
ÂÂÂÂÂÂÂÂÂÂÂÂSystem.out.println("Score can't be negative");
ÂÂÂÂÂÂÂÂ} else if (input < 30) {
ÂÂÂÂÂÂÂÂÂÂÂÂlowest++;
ÂÂÂÂÂÂÂÂ} else if (input < 40) {
ÂÂÂÂÂÂÂÂÂÂÂÂlow++;
ÂÂÂÂÂÂÂÂ} else if (input < 70) {
ÂÂÂÂÂÂÂÂÂÂÂÂmedium++;
ÂÂÂÂÂÂÂÂ} else if (input
 
Thanks xakota and tj_cool, should try it later and give an update when Netbeans decides to be stable.
 
Ha, I wanted to just post the whole thing but I thought it would be better for you if I just posted snippets but yeah, if you want you can just copypasta tj_cool's and it'll work perfectly.
 
xakota said:
Ha, I wanted to just post the whole thing but I thought it would be better for you if I just posted snippets but yeah, if you want you can just copypasta tj_cool's and it'll work perfectly.
Yeah, that might actually have been better
unsure.gif


*Quickly removes solution*
ninja.gif
 
XD, i almost did the exact same thing but then I thought if i had someone writing my code for me there's no way i would learn anything.
 
LOL I didn't really need the whole thing but I appreciate it. Almost all of the code is similar to what I've already done. It also helped since I just moved houses and everything's everywhere right now (and I had to move on the final week of uni a.k.a coursework and exam hell)

Oh, and I can't get to load because the scanner line returns an error (red line)? It's something I haven't covered in my Java lectures.
 
You need to import it.

Code:
import java.util.Scanner;

Put that above the class header.

Yeah, my compiler adds that automatically when I type
tongue.gif
 
R2DJ said:
LOL I didn't really need the whole thing but I appreciate it. Almost all of the code is similar to what I've already done. It also helped since I just moved houses and everything's everywhere right now (and I had to move on the final week of uni a.k.a coursework and exam hell)

Oh, and I can't get to load because the scanner line returns an error (red line)? It's something I haven't covered in my Java lectures.

You need to import the Scanner class first.

CODEimport java.util.Scanner;

EDIT: Nevermind, tj_cool beat me to it
tongue.gif
 
Ah OK. I haven't really toured around Netbeans that much (thanks to its bugginess). Huge thanks! One coursework done!

One last thing: do you guys know how to reference this as part of a source? I don't know how to Harvard reference info from a forum. Thanks
smile.gif
 
R2DJ said:
Ah OK. I haven't really toured around Netbeans that much (thanks to its bugginess). Huge thanks! One coursework done!

One last thing: do you guys know how to reference this as part of a source? I don't know how to Harvard reference info from a forum. Thanks
smile.gif

you put the url of the post that you want.
if you want tj_cools' post then you click the post number. a pop-up apears. copy and paste the address

you get this
CODEhttp://gbatemp.net/t269423-need-help-with-java-coursework?view=findpost&p=3324838
 

Site & Scene News

Popular threads in this forum