954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with a program

Hello i'm having a problem with a program. Let me tell you first what program should do.

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.
0-29 *** (3 students received a mark between 0-29)
30-39 *****
40-69 ********
70-100 ****
20 students in total.
 As the tutor enters each mark, a counter should count the number of student’s marks which have been entered.
 Use the same 4 category ranges shown here.
 Make sure the display is neatly formatted as above.

this is the problem to be solved.

package cw23rdattempt;

import java.util.Scanner;

/**
 *
 * @author Boss
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int counter = 1;
        int marks = 0;
        
        
        while( counter <= 20)
        {
        Scanner input = new Scanner(System.in);
        System.out.println("enter student marks");
        marks = input.nextInt();

        if(marks <=29)
            {
            System.out.println("0-29 *");
            }
        if(marks >= 30 && marks <=39)
            {
            System.out.println("30-39 *");
            }
        if(marks >= 40 && marks <=69)
            {
            System.out.println("40-69 *");
            }
        if(marks >= 70 && marks <=100)
            {
            System.out.println("70-100 *");
            }
        if(marks >100)
            {
            System.out.println("invalid mark \nmarks can not exceed 100");
            }
        counter++;
        }//end while
    }//end class
    }//end main


it sort of works, but it print answer separately for every entry, and i want to print it in the format as above in description.
could you please help me out?. ps i'm very new to programing so dont get harsh on me ;p

infinitus
Junior Poster in Training
80 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

what you are doing is printing out a single line, depending on what the last mark the user input is, you will have to store all the marks the user has input (maybe into an array?) and then loop through to find out how many are <29 etc etc printing out however many in terms of stars

hanvyj
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 17
Solved Threads: 20
 

What you have looks close to what you need. Just some things are a bit out of order.
1. Move line 22 to come before line 20.
2. You need more variables to keep track of your data:
- 1 variable to store the total number of marks entered. I think you are calling that counter.
- 1 variable to store the individual mark entered by the user. I think you are calling that marks.
- 1 variable to store the number of marks between 0-29. Let's call that one low.
- 1 variable to store the number of marks between 30-39. Call that one midLow.
- 1 variable to store the number of marks between 40-69. Call that one midHigh.
- 1 variable to store the number of marks between 70-100. Call that one high.
3. Initialize all these number variables to 0.
4. Inside your while loop, you will read the input from the user like you are already doing. Then you will test the input to see which number variable should be incremented. You will do this with some if/else statements. After that you will end the while loop.
5. After the while loop finishes, you will have 4 more loops to print the results. Before each of these loops you will print the beginning of the line, like "0-29" for example. Then inside each loop, you will print the appropriate number of stars.
6. Finally, after the 4 loops mentioned above, you will print the final number of marks entered by the tutor.

Try to re-write your program based on these instructions. If you get stuck, ask another question.

kramerd
Posting Pro in Training
403 posts since Sep 2010
Reputation Points: 49
Solved Threads: 73
 

i'm a bit lost here " Then you will test the input to see which number variable should be incremented " what exactly should i do ?, also how do i print appropriate number of starts when each star represent one student.. i know that if i want to print 3 * i would do

System.out.println("***")


but this is sort of final printout and its not modified when the tutor enters different marks. do you get what i mean :P ?

infinitus
Junior Poster in Training
80 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Do you know how to write an if/else statement?
Do you know how to write a for loop?

You need to understand both of those things.

kramerd
Posting Pro in Training
403 posts since Sep 2010
Reputation Points: 49
Solved Threads: 73
 

Unless I'm missing something, there's an easier to do it. (I've got a cold today, so I might have missed something)

Make an array grades[0..maxgrade]. As you get a score, increment grades[score]. Now you have data organized so you can use it.

Simplest way (conceptually) to use it is:
iterate the array. For each element of grades, print that many of *. Add formatting, ie, line breaks and line headers, at appropriate spots.

A more organized method would be to create a method which returns an appropriate set of Strings, nicely formatted, and you just print those. Hide your work. That method's black box might get pretty intricate, depending on how much you want to build into it, but simplest would be to again, iterate the array and build Strings.

A tempting method (the first one the C coder in me reached for) would be to make an array scores[0..maxgrade/10] and for each score entered, increment score/10. This makes my processing much easier. Why don't I want to do it?
(this question directed at the original poster, please allow them a little time to answer!)

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

jon.kiparsky, your solution does not fit the assignment.

kramerd
Posting Pro in Training
403 posts since Sep 2010
Reputation Points: 49
Solved Threads: 73
 

Hrm?
Teacher enters grades, program displays histogram of input. Was there something else?

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

There are only 4 categories of grades. You suggested creating an array[0..maxgrade]. I'm not even sure the OP knows how to deal with arrays. Since the assignment is only concerned with 4 categories, I'm guessing the teacher hasn't gone over arrays yet.

kramerd
Posting Pro in Training
403 posts since Sep 2010
Reputation Points: 49
Solved Threads: 73
 
I'm guessing the teacher hasn't gone over arrays yet.

Well, no time like the present. They're not that hard.
You agree, though, that this does solve the problem that was asked for, right?

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

No, it doesn't. The assignment asks for 4 categories of grades, not 100.

kramerd
Posting Pro in Training
403 posts since Sep 2010
Reputation Points: 49
Solved Threads: 73
 

Read the rest of the post.

Get the data into an array.
Iterate the array of grades, printing line headers and line breaks where appropriate.

So:

for (m from 1 to 100)
{
for (int i from 1 to grades[m]) print "*"
if m is at a break point (30, 60, etc) print a linefeed and line header
}


or, if you'd rather:

print a header, ie "0-29: "
(for i from 1 to first boundary)
{ myTempVariable +=grades[i];
}
print myTempVariable asterixen,
print newline, header for next line, lather rinse repeat.

More clear now?
Best,
-jpk

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

since i'm quite new to programming i dont really understand what you just did, also i haven't learned arrays yet.

ok so what i did so far.. =/

i have declared varables

int counter = 1;
        int marks = 0;
        int low = 0;
        int midlow = 0;
        int midhigh = 0;
        int high = 0;

then i have moved the scanner above while statement

Scanner input = new Scanner(System.in);
        System.out.println("enter student marks");

        while( counter <= 5)
        {        
        marks = input.nextInt();

and now what i tried is to make an if/else statement to see if specific condition was met

if(marks <=29)
            {
            low = (int)marks;
            }
            else if(marks >= 30 && marks <= 39)
            {
            midlow = (int)marks;
            }
            else if(marks >= 40 && marks <= 69)
            {
            midhigh = (int)marks;
            }
            else if(marks >= 70 && marks <= 100)
            {
            high = (int)marks;
            }
            else
            {
            System.out.println("invalid mark \nmarks can not exceed 100");
            }

if i write a code like

high = (int)marks;


is this correct in terms of, if a specific condition was met ie. the marks entered was 80, it stores this value in variable named "high" ??

i have a feeling that either i've made it worst then it was, or actually i haven't done anything useful ...

infinitus
Junior Poster in Training
80 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Your if/else conditions are all correct - very good! But what you need to do inside each condition is just increment (add 1 to) the correct variable. For example,

else if(marks >= 30 && marks <= 39)
{
  midlow++;  // or midlow = midlow + 1;
}
kramerd
Posting Pro in Training
403 posts since Sep 2010
Reputation Points: 49
Solved Threads: 73
 
high = (int)marks;


This line takes the value stored in "marks", attempts to cast it as an int, and stores the result of that cast as "high". If it's within an if() block, it'll do that if the condition of the if statement evaluated to "true". So, in your case, if marks holds 80, this line would fire, and high would equal 80.
If you don't get a numberFormatException at the input stage, your input value will come as an int, so I don't know that you need the cast there, but it won't do any harm.
Since you're new to this, I won't give you any grief about not checking your input, but try running this and entering something like "e" for one of the grades. This is why we have exception handling, which you'll come to presently.


Now, what does your loop do? This is a good chance to practice reading code.
Pretend you're the computer, and run through your loop, with the values 27, 56, 45, 78, 100, 101, 71. Just use a pencil and paper - make sure there's an eraser on your pencil.
What are the values in low, midlow, mighigh, and high when you're done?

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

As for arrays, they're a handy way to deal with data like this, which is inherently numerically ordered.
Briefly, an array is a collection of variables referred to by the array name and an index number: if grades[] is an array of ints, then grades[76] is a single int. The index must be an integer, but this can be a literal int (ie, 76) or a variable (n, which maybe equals 76, or equals something else) or anything else that evaluates to an int. This means that you now have data which you can manipulate systematically, instead of in an ad hoc fashion.
so, instead of
if (someInt == 1)

{ do something to variable1 }

else if (someInt == 2)

{ do something to variable2 }
else if (someInt == 3)

{ do something to variable3 }


you have

for (int i = 0; i < [length of array]; i++)
{
do something to array[i];
}

The advantages of this should be pretty obvious. There are some limitations to arrays as well, but for an application like this, they're indispensible.
For example, you'll notice that in your solution to the problem, you destroy the original data on the way in. There's no way to re-analyze anything without re-entering everything. If you use an array, you preserve the data (number of students receiving a given mark), so later on you could generate a similar histogram for letter grades or to move the boundaries to see if patterns show up.

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

@ jon.kiparsky
1st. its quite hard to pretend that i'm computer :P
2nd. i can now see why these arrays are handy... less code, and as you said it preserves the data for later use. i will have a look at arrays soon maybe in couple of days, until i'll get confident with the easier stuff before

also i think i sort of cracked it ....

package cw23rdattempt;

import java.util.Scanner;
import java.util.*;
/**
 *
 * @author Boss
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int counter = 1;
        int marks = 0;
        int low = 0;
        int midlow = 0;
        int midhigh = 0;
        int high = 0;

        Scanner input = new Scanner(System.in);
        System.out.println("enter student marks");

        while( counter <= 20)
        {        
        marks = input.nextInt();

        if(marks <=29){
            low++;
            }else if(marks >= 30 && marks <= 39){
            midlow++;
            }else if(marks >= 40 && marks <= 69){
            midhigh++;
            }else if(marks >= 70 && marks <= 100){
            high++;
            }else{
            System.out.println("invalid mark \nmarks can not exceed 100");
            }
        counter++;
        }//end while
        System.out.print("0-29 ");        
            if(low == 1){
                System.out.println("*");
            } else if (low == 2) {
                System.out.println("**");
            } else if (low == 3) {
                System.out.println("***");
            } else if (low == 4) {
                System.out.println("****");
            } else if (low == 5) {
                System.out.println("*****");
            }else if (low == 6) {
                System.out.println("******");
            } else if (low == 7) {
                System.out.println("*******");
            } else if (low == 8) {
                System.out.println("********");
            } else if (low == 9) {
                System.out.println("*********");
            }else if (low == 10) {
                System.out.println("**********");
            } else if (low == 11) {
                System.out.println("***********");
            } else if (low == 12) {
                System.out.println("************");
            } else if (low == 13) {
                System.out.println("*************");
            }else if (low == 14) {
                System.out.println("**************");
            } else if (low == 15) {
                System.out.println("***************");
            } else if (low == 16) {
                System.out.println("****************");
            } else if (low == 17) {
                System.out.println("*****************");
            }else if (low == 18) {
                System.out.println("******************");
            } else if (low == 19) {
                System.out.println("*******************");
            } else if (low == 20) {
                System.out.println("********************");
            } //end if(low)
        System.out.print("30-39 ");            
            if(midlow == 1){
                System.out.println("*");
            } else if (midlow == 2) {
                System.out.println("**");
            } else if (midlow == 3) {
                System.out.println("***");
            } else if (midlow == 4) {
                System.out.println("****");
            } else if (midlow == 5) {
                System.out.println("*****");
            }else if (midlow == 6) {
                System.out.println("******");
            } else if (midlow == 7) {
                System.out.println("*******");
            } else if (midlow == 8) {
                System.out.println("********");
            } else if (midlow == 9) {
                System.out.println("*********");
            }else if (midlow == 10) {
                System.out.println("**********");
            } else if (midlow == 11) {
                System.out.println("***********");
            } else if (midlow == 12) {
                System.out.println("************");
            } else if (midlow == 13) {
                System.out.println("*************");
            }else if (midlow == 14) {
                System.out.println("**************");
            } else if (midlow == 15) {
                System.out.println("***************");
            } else if (midlow == 16) {
                System.out.println("****************");
            } else if (midlow == 17) {
                System.out.println("*****************");
            }else if (midlow == 18) {
                System.out.println("******************");
            } else if (midlow == 19) {
                System.out.println("*******************");
            } else if (midlow == 20) {
                System.out.println("********************");
            }//end if(midlow)
        System.out.print("40-69 ");
            if(midhigh == 1){
                System.out.println("*");
            } else if (midhigh == 2) {
                System.out.println("**");
            } else if (midhigh == 3) {
                System.out.println("***");
            } else if (midhigh == 4) {
                System.out.println("****");
            } else if (midhigh == 5) {
                System.out.println("*****");
            }else if (midhigh == 6) {
                System.out.println("******");
            } else if (midhigh == 7) {
                System.out.println("*******");
            } else if (midhigh == 8) {
                System.out.println("********");
            } else if (midhigh == 9) {
                System.out.println("*********");
            }else if (midhigh == 10) {
                System.out.println("**********");
            } else if (midhigh == 11) {
                System.out.println("***********");
            } else if (midhigh == 12) {
                System.out.println("************");
            } else if (midhigh == 13) {
                System.out.println("*************");
            }else if (midhigh == 14) {
                System.out.println("**************");
            } else if (midhigh == 15) {
                System.out.println("***************");
            } else if (midhigh == 16) {
                System.out.println("****************");
            } else if (midhigh == 17) {
                System.out.println("*****************");
            }else if (midhigh == 18) {
                System.out.println("******************");
            } else if (midhigh == 19) {
                System.out.println("*******************");
            } else if (midhigh == 20) {
                System.out.println("********************");
            }//end if(midhigh)
        System.out.print("70-100 ");
            if(high == 1){
                System.out.println("*");
            } else if (high == 2) {
                System.out.println("**");
            } else if (high == 3) {
                System.out.println("***");  
            } else if (high == 4) {
                System.out.println("****");
            } else if (high == 5) {
                System.out.println("*****");
            }else if (high == 6) {
                System.out.println("******");
            } else if (high == 7) {
                System.out.println("*******");
            } else if (high == 8) {
                System.out.println("********");
            } else if (high == 9) {
                System.out.println("*********");
            }else if (high == 10) {
                System.out.println("**********");
            } else if (high == 11) {
                System.out.println("***********");
            } else if (high == 12) {
                System.out.println("************");
            } else if (high == 13) {
                System.out.println("*************");
            }else if (high == 14) {
                System.out.println("**************");
            } else if (high == 15) {
                System.out.println("***************");
            } else if (high == 16) {
                System.out.println("****************");
            } else if (high == 17) {
                System.out.println("*****************");
            }else if (high == 18) {
                System.out.println("******************");
            } else if (high == 19) {
                System.out.println("*******************");
            } else if (high == 20) {
                System.out.println("********************");
            }// end if(high)
    }//end class
    }//end main
infinitus
Junior Poster in Training
80 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Oh, dear.
Do you know about for loops yet? They're a specialized case of the while loop. You need them badly here.

A for loop takes this while loop:

int i = 0;
while (i<n)
{
  // do some stuff
  
  ++ i;
}


and rewrites it as

for (int i = 0; i <n; i++)
{
  // do some stuff
}

In both cases, i is initialized to zero, then as long as i is less than n (which has some value, in this example we don't know where it came from) the body of the loop will be executed and then i will be incremented.


One of the things you can do with this is to do a thing n number of times. For example, you could print a single "*"... without a line break... Are you seeing what I'm getting at?

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 
its quite hard to pretend that i'm computer

You need to be able to execute code in your head, and you learn to do that by executing it on paper.
Go back to that loop and try it. Take a piece of paper, and write down the name of each variable. What is its value when you enter the loop? If you haven't assigned it to anything, only declared it, what is its value? If you don't know, how would you find out?
Okay, now you know the initial states. Then, walk through the loop with the values I gave you, and when you set a value, write it down. Probably you already know what you'll see when you do this, but do it anyway, you need the practice.
Anytime a piece of code isn't doing what you want it to do, this is your first resort. The computer is doing what you've told it to do, so go figure out what you told it to do. You might be surprised.

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

no i havent got to the for loop yet, but i'll read about it tomorrow perhaps and then i'll try to modify my program :). and yes i can see what you mean...but since i don't know YET how to do it i'll leave this code for now as it is :)
also i'll work through the code on paper tomorrow since i'm done for today and its time for me ;>, so have a good night and anyway thanks for all help very appreciate it

infinitus
Junior Poster in Training
80 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You