• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Totally Confused

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK what i need to do is read an infile and output to an outfile. I need to use a value return method which I have correct. What I'm confused on is using a void method aswell. The void method has to calculate the average of yards and output in the outfile in the main method. Heres what I have for code alot of it is for testing but I'm confused beyond belief.. need some clues as what to do.



The program works like its supposed to like this but I NEED to use the void method.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way to do this is to create an array "int yards[]= new int[7];"
yards[1] through yards[6] would take the place of game_1 through game_6 in your program. You would pass the single argument yards to CalculateRating().
You would also pass yards to method CalculateAverage() which would store the average in yards[0]. You could then use yards[0] in your main program where you now use average_yards. If you call CalculateAverage() before you call CalculateRating(), you would not need the redundant average calculation in CalculateRating() as the average would already be in yards[0].
 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I've been trying all weekend to get the program to work with arrays but I haven't really learned them yet so It's not going to well. I figured out that i need to use a reference variable for the void method. Something like:

CalculateAverage(IntClass average).

Is this correct? I'm still totally lost on this void method listed in the above post.
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say "a void method" I am assuming that you mean a method that returns nothing (void). Is that right?

So you would have a method defined like this:

That is, a method that takes 6 int parameters and returns nothing. Am I with you so far?

Currently your CalculateRating() method takes 6 ints and returns a char. You must be confused because you know you need that char to be returned because it represents the player's rating. Right? If you make the method return nothing (void) then how will the player_rating value be passed back?

My first thought is to restructure the program so that the player_rating variable has a broader scope. Here's what I mean. Right now your player_rating variable is defined in your main() method. That means that you can only reference it within your main method. In other words, it is only "in scope" in your main method. Any code outside of main() doesn't know anything about it.

You could restructure your program something like this:

Now the player_rating variable has been defined, not inside a method, but right at the class level so that it can be accessed by any method.

Also, I made a method called processFile() that does what you are doing in your main method now. In the main method you could instantiate a Stats object instead of doing all this work in a static main() method.

Does that make any sense?
 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where I'm lost is the void method called CalculateAverage. Well first off here is another copy of my code:



I switched a couple things around in the CalculateRating return method so that just the average is carried over. Now with my CalculateAverage method I'm getting a compile error saying that it found void and requires int when I try to bring the method into method main.I can not just quite grasp the void method here for some reason. Do i need to declare something as a reference variable in the void method CalculateAverage? Seems my CalculateAverage method is all screwed up.
Confused :/
Shawn
[ November 22, 2004: Message edited by: Shawn Houston ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be able to say

int x = someMethod();

someMethod() has to return a value -- i.e., it has to be declared as returning int, not void, and it has to say "return Z" at the end, where Z is some value.

Changing calculateAverage() to declare that it returns "int" instead of "void", and adding "return average" at the end, will get you over this particular hump.
 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can do that but the problem being is that the assignment must use a void method for CalculateAverage.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume that you mean this is a homework assignment. Would you mind posting the exact specification from your teacher (or better yet a link to the teacher's web page)? This might help clear up some of the confusion here. I'll also be glad to chip in with my own two cents.

Layne
 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well theres not actually a link to the website. It's all password secure and everything so I'll copy and paste whats supposed to happen.

Write an algorithm for a Java program that reads a football player�s name and his total yards for 6 games. The program should then compute the average yards for each player. After you have calculated the player�s average, assign an appropriate rating to each player. The rating scale is as follows � (based on the average yards):

More then 100 - A
75 � 100 � B
50 � 74 � C
Less then 50 � D


Your program must use the following methods:

1. A void method, CalculateAverage, to determine the average yards for each player. Use a loop to read and sum the six total yard values. This method DOES NOT output the average yards. The value MUST be returned and output in void main.

2. A value-returning method, CalculateRating, to determine and return the appropriate rating for each player. This method DOES NOT output the rating. It MUST be done in void main.


Your program must also output the team average after you output all the player�s average yards.


Test your program on the following data. Read the data form a file and output the results to a file. DO NOT use any global variables. Use the appropriate parameters to pass values to and from the methods.


Ward 100 154 98 65 35 115
Burris 89 65 87 104 115 132
Harris 54 21 55 87 100 54
Bettis 3 5 8 54 15 25
Staley 87 54 98 105 104 156
Randle 54 87 54 98 65 105
Maddox 156 104 0 0 0 0
Roth 0 0 254 278 254 189
Holmes 104 87 98 114 105 147

Sample Output: The output should be of the following form. Fill in the last two columns and the last line showing the team average.




Player Game1 Game2 Game3 Game4 Game5 Game6 Average Rating
Ward 100 154 98 65 35 115
Burris 89 65 87 104 115 132
Harris 54 21 55 87 100 54
Bettis 3 5 8 54 15 25
Staley 87 54 98 105 104 156
Randle 54 87 54 98 65 105
Maddox 156 104 0 0 0 0
Roth 0 0 254 278 254 189
Holmes 104 87 98 114 105 147

Team Average =
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. A void method, CalculateAverage, to determine the average yards for each player. Use a loop to read and sum the six total yard values. This method DOES NOT output the average yards. The value MUST be returned and output in void main.


Well, I understand your confusion. This statement is very contradictory. The problem is that a void method cannot return a value as stated above. You should go to your teacher to clarify. Perhaps your teacher is trying to get you to understand pass-by-reference, which is the only thing I can think of.

Perhaps you are on the right track with yoru earlier comment:

I figured out that i need to use a reference variable for the void method. Something like:

CalculateAverage(IntClass average).

Is this correct? I'm still totally lost on this void method listed in the above post.


However, I would suggest that you use the Integer class that is already available in the Java API. It will make your life a little easier (at least once you learn how to use it).

HTH

Layne
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic