-1

I am working on a basic program (console).

The program should allow the user to enter in various marks, until the mark entered exceeds 100. At this point the program should display a histogram. Each star represents a student who achieved a module mark in the range shown.

This is an example of the output.

0 - 29 xxx

30 - 39 xxxxx

40 - 69 xxxxxxx

70 - 100 xxxx

20 students in total.

As the user enters each mark, there ought to be a counter that increases in value and print the total number of marks entered.

I want to make sure that the program is as efficient as possible but also understandable

Code

Working with @Dici on collabedit I have an amazing answer to my question:

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    int[] ranges  =  { 0,29,39,69,100 };
    int[] inRange = new int[ranges.length - 1];

    int mark;
    do {
        System.out.println("Enter Mark:");
        mark = sc.nextInt();           
        for (int j=1 ; j<ranges.length ; j++)
           if (ranges[j-1] <= mark && mark <= ranges[j]) {
               inRange[j-1]++;
               break;
           }
    } while (mark <= 100);

    System.out.println(Arrays.toString(inRange));

    String s = "The number of students that have scored between %d and %d is: ";
    int k = 0;
    for (int i=0 ; i<ranges.length - 1 ; i++) {
      System.out.print(String.format(s,ranges[i] + k,ranges[i + 1]));   
        for (int r = 0; r<inRange[i] ; r++)
            System.out.print("*");
        System.out.println();
        k = 1;
    }

    sc.close();

Thank you again for your amazing help!

7
  • Ranges of what? Do you want to break the possible grade range into "buckets" (e.g. 0 - 29, 30 - 59, etc.) and keep a running count of the grades in each bucket?
    – APerson
    Nov 18, 2014 at 17:19
  • 2
    You need to be much more specific. We aren't mind readers, concise and targeted questions are best. Are you asking about ranges for the marks for the student? Nov 18, 2014 at 17:22
  • Can you use Java 8 ?
    – Dici
    Nov 18, 2014 at 17:27
  • @Dici I am using the Java 8 JDK Nov 18, 2014 at 17:30
  • Your code doesn’t make the slightest sense. Neither does your request to put ranges into it. Maybe you try to fix you code regarding whatever it is supposed to do without these mysterious ranges. Once it does something useful you may come back to the idea of putting ranges to it.
    – Holger
    Nov 18, 2014 at 17:43

3 Answers 3

1

As we did it together on Collabedit, here is a working code for your question :

public class Test {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int[] ranges  =  { 0,29,39,69,100 };
        int[] inRange = new int[ranges.length - 1];

        int mark;
        do {
            System.out.println("Enter Mark:");
            mark = sc.nextInt();           
            for (int j=1 ; j<ranges.length ; j++)
               if (ranges[j-1] <= mark && mark <= ranges[j]) {
                   inRange[j-1]++;
                   break;
               }
        } while (mark <= 100);

        System.out.println(Arrays.toString(inRange));

        String s = "The number of students that have scored between %d and %d is : ";
        int k = 0;
        for (int i=0 ; i<ranges.length - 1 ; i++) {
          System.out.print(String.format(s,ranges[i] + k,ranges[i + 1]));   
            for (int r = 0; r<inRange[i] ; r++)
                System.out.print("*");
            System.out.println();
            k = 1;
        }

        sc.close();
    }   
}
9
  • I'm sorry, I can't quite understand this. Is this correct, this a list and through the list you've defined a range of 0 to 100 on the second line. Then you've created both a maximum and minimum range value that can be altered only within the code. I'm not quite understanding the i +29* on your fourth line. Could you explain the fifth line to me please? Nov 18, 2014 at 17:43
  • Let's assume you have the input array [3,25,17,88]. What you want is to know how many items of this array are in each range you defined, is that right ?
    – Dici
    Nov 18, 2014 at 17:45
  • Yeah that is right, so you would use the above code to sort the array into the appropriate range? Nov 18, 2014 at 17:49
  • Each element of the list is a Predicate with a test method. test(i) returns true iff min <= i <= max. I guess you see how it can be useful
    – Dici
    Nov 18, 2014 at 17:55
  • Thank you for explaining it to me, so if I wanted to store values ad have the list sort them can I use the scanner outside of the list? Nov 18, 2014 at 18:00
0

Okay. Now that makes sense. If you have a number

  int mark = 3;

You can use an if statement

 if(mark <= 10){

      //do this

 }else if(mark <= 20){

     //do this

  }

You can also use a switch statement, however personal preference leads me to almost always use if statements.

If you want "buckets" you could use

  ArrayList<String> marksForStudent = new ArrayList<>();

the documentation for that is here.

https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

Or, alternatively and probably what you want is a hashmap. With the student's name or other unique identifier as the key and then a collection to contain their marks.

  HashMap<String, ArrayList<String>> gradesForStudents = new HashMap<>();


  gradesForStudents.put("Marc",Arrays.asList("99","98","100"));

Something like that.

0

Quick suggestion: do not alter marksInsp inside the loop if that's your loop index.

Paulinho made the excellent suggestion of using the modulo operator. I'll extend that here to a map, since you didn't specify the upper mark limit:

public static void main(String[] args) {
    int currentMark, currentBucket;
    final int BUCKET_WIDTH = 30;
    Map<Integer, Integer> markRanges = new HashMap<Integer, Integer>();

    for(int i = 0; i < 101; i++) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter Marks: ");
        currentMark = input.nextInt();
        currentBucket = currentMark % BUCKET_WIDTH;
        if(markRanges.containsKey(currentBucket)) {

            // There's already an entry for this bucket
            markRanges.put(currentBucket, markRanges.get(currentBucket) + 1);
        } else {

            // If there isn't, make a new entry
            markRanges.put(currentBucket, 1);
        }

        // Print out grade buckets
        for(Integer j : markRanges.keySet) {
            System.out.println(String.format("There are %d students with scores between %d and %d", markRanges.get(j), j, j.intValue() + BUCKET_WIDTH - 1));
        }
    }
} 
2
  • At the end of your code it says that the variable is already defined in method main(java.lang.String[]) Nov 18, 2014 at 17:36
  • @NiralMehta yeah; fixed.
    – APerson
    Nov 19, 2014 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.