• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

help student with subroutine

Kako

Limp Gawd
Joined
Jun 15, 2001
Messages
371
I am stuck with a 'grade assignment' problem for school. What I wanted to do was send the numeral grade to a subroutine and have it return a letter grade, but I can not get it to work. I am getting a loss of precision error when I try to compile.

The code here is a simplified version of what I want to do. I am trying to return a character from the subroutine, which will be saved into another character. I don't know why this doesn't work and if anyone can explain the logic to me or my error, it'll help me a bunch.

import javax.swing.*;
class learnFunction3
{
public static void main(String [] args)
{
char zoom;
zoom = integer_add (5, 12);

JOptionPane.showMessageDialog(null, "The addition of 5 and 12 results in letter " +zoom);
}

static int integer_add (int x, int y)
{
int result=0;
char vroom;

result = x + y;
if (result == 12)
{
vroom = 'K';
}
return vroom;
}
}
 
The output of your integer_add() function is set as an integer by the function constructor/declaration:

Code:
static int integer_add (int x, int y)

The function is constructed expecting an integer output, but you are returning a char. Try changing the constructor/declaration to this:

Code:
static char integer_add (int x, int y)

That way, it will know to expect a char output.

I feel compelled to add that this is much more complex than it needs to be, but the forum rules prevent me from telling you how to do it.
 
Is there a specific language that you must write this in? This seems like it would be easy to do in some of the scripting languages. Ruby can do this pretty well, and easy I would guess.

couldnt you just do a simple

if numbera somemath numberb = certainvalue
then zoom = A

for each grade value?
 
zutme said:
FYI subroutines are called methods in java

They're called methods in most languages, but usually only if they are a part of an object class.
 
HaMMerHeD said:
They're called methods in most languages, but usually only if they are a part of an object class.

Most languages are not OO.
 
ameoba said:
Most languages are not OO.

67.3% of all statistics are made up on the spot.
(Not that I think you're wrong, but I would love to see the numbers.)
 
37.5% of the posts in this thread don't help Kako.
 
lol, Actually, the first post clarified everything so thanks to hammerhead. I didn't really know if I should post here with such a beginners question but I appreciate as it cleared a road block for me and let me go and finish the rest of the homework. If I have any other questions, I'll try and use the same thread instead of making a new one. Thanks alot guys.
 
Just a reminder that they're called "member functions" in C++ lingo :)
 
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html

My professor wants me to take the small individual programs that I wrote and put them in a single file where I input a choice and select what I want done. I'd like to do it with a switch statement but since similar variables are declared in different blocks, I'm getting an error where the variable is already defined. It only takes a moment to rename the variables, but I would like to know if it's possible to make the switch have private blocks not see each other. I know what I want, its just difficult to search online for it if I don't know the proper lingo.
 
You could create a method for each thing and call the right one from the switch.
This is also easier to read than a huge switch/case containing all your code.
 
Not sure about for java but in c++ you could get away with enclosing the contents of each switch case with curley brackets { ... }. This gives the variables you define within them block lifetime and they expire once outside of the block they were defined in. For more information about this you could look into scope.

Again, i'm not sure if this is a correct solution in java but it works fine in c++. Using a seperate methods/function for each case like HHunt suggested may be a little cleaner though.
 
The curly braces work in java too. I tried that last night, but I must have enclosed only one of the cases instead of all of them and so they were still accessable to each other. Thanks again.
 
I know the drill already, I'm not asking for you guys to do any homework, but just a little help to debug since it seems my syntax is off. The assignment was to get the work done in methods instead of having it done in main. The code layout seems right, but since I don't have enough experience yet, I can't really identify what the problem is yet. The whole program is located .


Code:
getMidterm();
getFinal();
calcGrade( double midterm, double Final );
void display_info( double midterm, double final, double grade, char letter );

errors:

----jGRASP exec: javac F:\Programming\Java\lesson7a.java

lesson7a.java:84: '.class' expected
calcGrade( double midterm, double Final );
^
lesson7a.java:84: ')' expected
calcGrade( double midterm, double Final );
^
lesson7a.java:85: illegal start of expression
void display_info( double midterm, double final, double grade, char letter );
^
lesson7a.java:154: '(' expected
void display info(double midterm,double final,double grade, char letter)
^
4 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
 
I'm not sure how to explain it clearly, but your methods in your main are wrong for the reason that you need to just pass in the variable/value and not the classifier (double, char).

So take the double/char part off your calcGrade and display_info and take the void off yoru display_info. The void is only used when writing a method, not calling it.

Also, on your display_info your 2nd parameter is double final, which you probably meant to pass in Final, but Java is case-sensitive so be careful.

Not sure if this is clear to you, but someone else will probably reply soon, so let me know if you understand that.

Another error you're going to get is that you need to set your variables that are getting returned, but it might be best if you just try to understand what the code is doing when it returns something or when a method gets called.
 
Ok, I removed the data types from the calling methods and that cleared up some of the errors. I think my problem lies now with what you were trying to explain in your last sentence. I'm calling a method without assigning it to a variable. When the method returns a value, where would that value be saved? For example, if I'm calling getMidterm(); to prompt the user for the midterm grade and the method returns 'midterm', where would midterm be saved? Does midterm assign it's value to getMidterm? or should a method always be assigned to a variable if it's returning a value?
 
When a method returns a value, it's up to you to store it somewhere via assignment.

[TYPE] variable_name = some_method_call();

(Technically, the return value will likely get stored ("pushed") onto the stack, but you don't need to know that.)
 
And if you don't store it anywhere, it just silently disappears.
 
Back
Top