Programming exercise noob - method not working, please help

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 847 Views - Last Post: 19 September 2011 - 10:33 PM Rate Topic: -----

#1 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Programming exercise noob - method not working, please help

Posted 18 September 2011 - 06:43 PM

I'm writing a toy program that messes around with grades.

I got everything working until I decided it was time to start writing methods.

I just need help with the method I wrote below.

It works fine, but the aCount++, bCount++, etc, are not working. At the end of the program their values remain 0, although everything else works.


public static void gradeLetter(float grade, StringBuffer gradeList, int aCount, int bCount, int cCount, int dCount, int fCount)
	{
	    if (grade > 100)
	    {
	    	System.out.println("Error! Please enter only grades up to 100. Restart the program to continue.");
	    	System.exit(0);
	    }	    
	    if (grade >= 90 && grade <= 100)
	    {		    	
	    	gradeList.append(g + ": A\n");
	        aCount++;
	        return;
	    }
	    if (grade >= 80 && grade <= 89)
	    {
	    	gradeList.append(g + ": B\n");
	    	bCount++;
	    	return;
	    }
	    if (grade >= 70 && grade <= 79)
	    {
	    	gradeList.append(g + ": C\n");	
	    	cCount++;
	    	return;
	    }
	    if (grade >= 60 && grade <= 69)
	    {
	    	gradeList.append(g + ": D\n");
	    	dCount++;
	    	return;
	    }
	    if (grade >= 0 && grade <= 59)
	    {
	    	gradeList.append(g + ": F \n");	
	    	fCount++;
	    	return;
	    }
	}



And here's the entire code, just in case.

package assignement;

import java.util.*;

public class Program3 
{   	
	public static void main(String[] args)
	{				
		StringBuffer gradeList = new StringBuffer (""); //list of grades empty for now
		int count = 2; //keeps track of the amount of grades the user has entered
	    float sum = 0; //the sum of all grades to calculate the average
	    float next1; //variable used in the do-white loop
	    
	    int aCount = 0; //these are used to keep track of the amount of
	    int bCount = 0; //each letter grade. It is used to calculate the
	    int cCount = 0; //percentage of each. 
	    int dCount = 0;
	    int fCount = 0;
	    
	    System.out.println("Enter at least 2 grades. Enter a negative number to signal finish: ");
	    Scanner keyboard = new Scanner(System.in);
	    float grade = keyboard.nextFloat();
	    float next = keyboard.nextFloat();
	    
	    float max = grade; //at this point, min and max are assigned the value of grade
	    float min = grade;
	   
	    gradeLetter(grade, gradeList, aCount, bCount, cCount, dCount, fCount);
	    
	    gradeLetter(next, gradeList, aCount, bCount, cCount, dCount, fCount);
	    
	    sum = grade + next;  
	      
	    if (next > max)
	    {
	    	max = next;
	    }
	    if (next < min)
	    {
	    	min = next;
	    }
	     
	    do
	    {	 		    
	    	next1 = keyboard.nextFloat();
	    	
		    if (next1 < 0)
		    {
		    	break; //ends loop when user enters a negative number
		    }
		    
		    if (next1 > max)
		    {
		    	max = next1;
		    }
		    if (next1 < min)
		    {
		    	min = next1;
		    }
		    
		    gradeLetter(next1, gradeList, aCount, bCount, cCount, dCount, fCount);
		    
		    sum = sum + next1; //updates value of sum to include new value of next1
		    		    
	    	count ++; //if loop was successful, count gets raised
	    	
	    } while (next1 >= 0);
	    
	    float aPrc = (aCount / count) * 100;
	    float bPrc = (bCount / count) * 100;
	    float cPrc = (cCount / count) * 100;
	    float dPrc = (dCount / count) * 100;
	    float fPrc = (fCount / count) * 100;
	    
	    float average = (sum / count);
	    
	    System.out.println("You have entered " + count + " grades.");
	    System.out.println("Average: " + Math.round(average));
	    System.out.println("The highest score is " + max);
	    System.out.println("The lowest score is " + min);
	    System.out.println("The letter grades for each grade are: \n" + gradeList);
	    System.out.println("The percentage of of each letter grade category are:");
	    System.out.println("A: " + Math.round(aPrc) + "%");
	    System.out.println("B: " + Math.round(bPrc) + "%");
	    System.out.println("C: " + Math.round(cPrc) + "%");
	    System.out.println("D: " + Math.round(dPrc) + "%");
	    System.out.println("F: " + Math.round(fPrc) + "%");		    
	}
	
	public static void gradeLetter(float g, StringBuffer gradeList, int aCount, int bCount, int cCount, int dCount, int fCount)
	{
	    if (g > 100)
	    {
	    	System.out.println("Error! Please enter only grades up to 100. Restart the program to continue.");
	    	System.exit(0);
	    }	    
	    if (g >= 90 && g <= 100)
	    {		    	
	    	gradeList.append(g + ": A\n");
	        aCount++;
	        return;
	    }
	    if (g >= 80 && g <= 89)
	    {
	    	gradeList.append(g + ": B\n");
	    	bCount++;
	    	return;
	    }
	    if (g >= 70 && g <= 79)
	    {
	    	gradeList.append(g + ": C\n");	
	    	cCount++;
	    	return;
	    }
	    if (g >= 60 && g <= 69)
	    {
	    	gradeList.append(g + ": D\n");
	    	dCount++;
	    	return;
	    }
	    if (g >= 0 && g <= 59)
	    {
	    	gradeList.append(g + ": F \n");	
	    	fCount++;
	    	return;
	    }
	}
}



Here's the output I'm getting:

Enter at least 2 grades. Enter a negative number to signal finish:
96
95
81
56
74
23
-1
You have entered 6 grades.
Average: 71
The highest score is 96.0
The lowest score is 23.0
The letter grades for each grade are:
96.0: A
95.0: A
81.0: B
56.0: F
74.0: C
23.0: F

The percentage of of each letter grade category are:
A: 0%
B: 0%
C: 0%
D: 0%
F: 0%

^^^^^^^^^^^^^ These should not be 0%. That's how the aCount++'s are not working.

This post has been edited by carnivroar: 18 September 2011 - 06:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Programming exercise noob - method not working, please help

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 06:53 PM

You can't process float, and double values, that way

        if (grade >= 90 && grade <= 100)
        {   
          ...            
        }
        if (grade >= 80 && grade <= 89)


what if grade == 89.5 ?

also as acount, bcount, coutn... and count are integer

(aCount / count) will always be 0 if aCount < count

int 799 / 800 == 0
Was This Post Helpful? 0
  • +
  • -

#3 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 07:16 PM

View Postpbl, on 18 September 2011 - 06:53 PM, said:

You can't process float, and double values, that way

        if (grade >= 90 && grade <= 100)
        {   
          ...            
        }
        if (grade >= 80 && grade <= 89)


what if grade == 89.5 ?

also as acount, bcount, coutn... and count are integer

(aCount / count) will always be 0 if aCount < count

int 799 / 800 == 0


if grade is a float, it works fine.

edit: never mind, I see what a you mean, that's easy to fix. nice catch.

it's just the ++ inside the method that's not working.

	    float aPercentage = (aCount / count) * 100;
	    float bPercentage = (bCount / count) * 100;
	    float cPercentage = (cCount / count) * 100;
	    float dPercentage = (dCount / count) * 100;
	    float fPercentage = (fCount / count) * 100;



a/b/c/d/f-Count = float
count = float

I don't see why this calculation is not working

Unless the ++ thing doesn't work for float values. But that can't be, because it works for count as seen in the output:

"You have entered 3.0 grades."

It must be a problem inside the method.

Any suggestions? :gun_bandana:

This post has been edited by carnivroar: 18 September 2011 - 07:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 07:31 PM

make count or aCount, bCount, cCount... float
Was This Post Helpful? 0
  • +
  • -

#5 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 07:52 PM

View Postpbl, on 18 September 2011 - 07:31 PM, said:

make count or aCount, bCount, cCount... float

They are both floats, that is not the problem.

This is where the problem is:

	    if (grade >= 90 && grade <= 100)
	    {		
	    	gradeList.append(grade + ": A\n");
	    	aCount++;
	        return;



I found that aCount++ works, but it doesn't get updated. The value stays 1.0 no matter how many times the do-while loop uses this method.

Why is aCount not being updated?

Do I have to put the initial definition

float aCount = 0;

somewhere else so it doesn't interfere? I can't find anywhere else to put it.

This is what's happening:

Enter at least 2 grades:
99
1.0
99
1.0
99
1.0
99
1.0
99
1.0
99
1.0

This is what has got to happen:

Enter at least 2 grades:
99
1.0
99
2.0
99
2.0
99
3.0
99
4.0
99
5.0

Where the float numbers record the amount of A grades that the user inputs.

This post has been edited by carnivroar: 18 September 2011 - 08:01 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 08:10 PM

View Postcarnivroar, on 18 September 2011 - 10:52 PM, said:

This is where the problem is:

	    if (grade >= 90 && grade <= 100)
	    {		
	    	gradeList.append(grade + ": A\n");
	    	aCount++;
	        return;



I found that aCount++ works, but it doesn't get updated. The value stays 1.0 no matter how many times the do-while loop uses this method.

Does not make sense :) ... repost your updated code
Was This Post Helpful? 0
  • +
  • -

#7 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 08:15 PM

View Postpbl, on 18 September 2011 - 08:10 PM, said:

View Postcarnivroar, on 18 September 2011 - 10:52 PM, said:

This is where the problem is:

	    if (grade >= 90 && grade <= 100)
	    {		
	    	gradeList.append(grade + ": A\n");
	    	aCount++;
	        return;



I found that aCount++ works, but it doesn't get updated. The value stays 1.0 no matter how many times the do-while loop uses this method.

Does not make sense :) ... repost your updated code

I didn't change anything except I added a print statement in the method which shows that aCount is not being updated by the aCount++.

private static void gradeLetter(float grade, StringBuffer gradeList, float aCount, float bCount, float cCount, float dCount, float fCount) 
	{
	    //...    
	    if (grade >= 90 && grade <= 100)
	    {		
	    	gradeList.append(grade + ": A\n");
	    	aCount++;
                System.out.println(aCount) //this shows me that no matter how many times the loop goes through this method, aCount is still 1.0 despite the aCount++
	    	return;
	    }
	    //other similarif statements	
	}


Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7630
  • View blog
  • Posts: 12,866
  • Joined: 19-March 11

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 08:19 PM

Of course they get updated. But since they're local variables, their updated values don't matter because they're lost as soon as you exit the method.

When you pass a parameter to a method, you're passing a value that lives on the stack. Whatever you do to that value only matters in the course of that method.
If that value points to an object on the heap, then whatever you do to that object persists, of course, but ints are primitives: they don't live on the heap.

Passing a parameter to a method is like writing a value down on a piece of paper and handing it to someone. Now, imagine two scenarios.

First, you write down your age on a piece of paper. I cross it out and write something else. You say "okay, whatever". That's passing an int. You're still the same age, right? Of course you are.

Now suppose you write down the password you use on your bank's website, and I take it and I access your account and transfer all of your money to my account. You don't say "okay, whatever" because I just used the information you gave me to change something in the world, and it stays changed. That's passing an object.

This post has been edited by jon.kiparsky: 18 September 2011 - 08:20 PM

Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 08:27 PM

Wait a minute :) Am I completly drunk ? I really missed that one

    int i = 10:
    System.out.println(i);     // will print 10

    method(i);
    System.out.println(i);     // will still print 10

....
    void method(int x) {
       x = x + 100;
       System.out.println(x);   // will print 110
    }



You just CANNOT change the value of a basic data type variable passed to a method inside that method (yes will be true during the life of the variable) but back to the caller, the variable stays the same. Basic data type are passed by value... which means a copy is sent to the the method. Changing it into the method does NOT change its original value

*Edited: fixed the code tags

This post has been edited by pbl: 18 September 2011 - 08:28 PM

Was This Post Helpful? 1
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7630
  • View blog
  • Posts: 12,866
  • Joined: 19-March 11

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 08:29 PM


public static void gradeLetter(float grade, StringBuffer gradeList, int aCount, int bCount, int cCount, int dCount, int fCount)
{}
	




public class Program3 
{   	
	public static void main(String[] args)
	{				
	
	    
	    int aCount = 0; //these are used to keep track of the amount of
	    int bCount = 0; //each letter grade. It is used to calculate the
	    int cCount = 0; //percentage of each. 
	    int dCount = 0;
	    int fCount = 0;   




Top tip: if you can declare the variable in two separate places, that's a sure-fire sign that it's a different variable in each of those places, and what you do to one won't affect the other.
This is good, if you use i as your loop counter variable all over the place. You can do this:
for(int i = 0; i<limit; i++)
{}


as many times as you like, and no i will never interfere with any other one.
However, in your case it sems to be causing some bother.
Was This Post Helpful? 0
  • +
  • -

#11 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 08:35 PM

View Postjon.kiparsky, on 18 September 2011 - 08:19 PM, said:

Of course they get updated. But since they're local variables, their updated values don't matter because they're lost as soon as you exit the method.


Got you, makes sense.

Is there any way to fix that?

I did this but I'm still getting the same problem

    public class Global {
		public static final float aCount = 0;
		public static final float bCount = 0;
		public static final float cCount = 0;
		public static final float dCount = 0;
		public static final float fCount = 0;
    }



then substituted Global.aCount whereever I had to.

This post has been edited by carnivroar: 18 September 2011 - 08:36 PM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 08:40 PM

By definition final variables cannot be changed
Why would you make them final if you plan to use them as counter ?
Was This Post Helpful? 0
  • +
  • -

#13 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 08:55 PM

View Postpbl, on 18 September 2011 - 08:40 PM, said:

By definition final variables cannot be changed
Why would you make them final if you plan to use them as counter ?


Because if I make them just "public float" i get this error message

Cannot make a static reference to the non-static field Global.bCount

I miss Python... :death:
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7630
  • View blog
  • Posts: 12,866
  • Joined: 19-March 11

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 09:01 PM

There are a few fixes you could use here. The simplest would be to declare them as fields of the class Program3:

	public class Program3
	{   
           private int aCount; 
           private int bCount; 
           etc...

	    public static void main(String[] args)
	    {   ...   


This way, they're in scope throughout the existence of an instance of Program3, and they'll persist as long as that object does. That means you can refer to them throughout your "Program3" object.

There are a few other ways you could do it - pass an array of the ints back and forth, for example, would be one way. (since an array is an object, you don't even need to return it)

Or you could probably get more creative, but those two will be fine for now.
Was This Post Helpful? 0
  • +
  • -

#15 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Programming exercise noob - method not working, please help

Posted 18 September 2011 - 09:21 PM

View Postjon.kiparsky, on 18 September 2011 - 09:01 PM, said:

There are a few fixes you could use here. The simplest would be to declare them as fields of the class Program3:

	public class Program3
	{   
           private int aCount; 
           private int bCount; 
           etc...

	    public static void main(String[] args)
	    {   ...   


This way, they're in scope throughout the existence of an instance of Program3, and they'll persist as long as that object does. That means you can refer to them throughout your "Program3" object.

There are a few other ways you could do it - pass an array of the ints back and forth, for example, would be one way. (since an array is an object, you don't even need to return it)

Or you could probably get more creative, but those two will be fine for now.


Nothing of the sort seems to work.

I guess I will have to rewrite the entire program.

I had it working perfectly fine without methods.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2