Need help with Java coursework

Discussion in 'Computer Games and General Discussion' started by R2DJ, Dec 13, 2010.

  1. R2DJ
    OP

    R2DJ GBAtemp Advanced Maniac

    Member
    1,900
    15
    Jan 30, 2008
    London
    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.
     
  2. xakota

    xakota GBAtemp Fan

    Member
    340
    2
    Mar 18, 2010
    United States
    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?
     
  3. R2DJ
    OP

    R2DJ GBAtemp Advanced Maniac

    Member
    1,900
    15
    Jan 30, 2008
    London
    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) [​IMG]
     
  4. xakota

    xakota GBAtemp Fan

    Member
    340
    2
    Mar 18, 2010
    United States
    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
     
  5. tj_cool

    tj_cool Site dev

    Supervisor
    9,989
    -1
    Jan 7, 2009
    Belgium
    This planet
    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
     
  6. R2DJ
    OP

    R2DJ GBAtemp Advanced Maniac

    Member
    1,900
    15
    Jan 30, 2008
    London
    Thanks xakota and tj_cool, should try it later and give an update when Netbeans decides to be stable.
     
  7. xakota

    xakota GBAtemp Fan

    Member
    340
    2
    Mar 18, 2010
    United States
    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.
     
  8. tj_cool

    tj_cool Site dev

    Supervisor
    9,989
    -1
    Jan 7, 2009
    Belgium
    This planet
    Yeah, that might actually have been better [​IMG]

    *Quickly removes solution* [​IMG]
     
  9. xakota

    xakota GBAtemp Fan

    Member
    340
    2
    Mar 18, 2010
    United States
    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.
     
  10. R2DJ
    OP

    R2DJ GBAtemp Advanced Maniac

    Member
    1,900
    15
    Jan 30, 2008
    London
    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.
     
  11. tj_cool

    tj_cool Site dev

    Supervisor
    9,989
    -1
    Jan 7, 2009
    Belgium
    This planet
    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 [​IMG]
     
  12. Y05h1

    Y05h1 GBAtemp Regular

    Member
    157
    0
    Nov 23, 2008
    Finland
    EDIT: Nevermind, tj_cool beat me to it [​IMG]
     
  13. R2DJ
    OP

    R2DJ GBAtemp Advanced Maniac

    Member
    1,900
    15
    Jan 30, 2008
    London
    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 [​IMG]
     
  14. Dter ic

    Dter ic ~

    Member
    1,757
    45
    Feb 19, 2009