Click to See Complete Forum and Search --> : I'm about to snap! (or) What in God's name am I doing wrong (JAVA)


WhiskeyBravo
06-08-2001, 12:24 AM
Hey guys, I'm trying to pick up java from some computer based training. Sadly enough, I have been trying to get a clean compile here for 2 days.

I'm really not looking for someone to write the code out for me, but I've obviously made a simple mistake here. Can someone tell me what I'm doing wrong please?

class Grade {
public static void main(String[] args) {
if (args.length == 0)
{
System.out.println("Usage: java GradePoints Grade");
System.exit(1);
}

char grade = args[0].charAt(0);
int points = gradePoints(points);
System.out.println(grade + " = " + points + " points");


int gradePoints(char grade)
{
switch (grade){
case 'A':
points = "4";
break;
case 'B':
points = "3";
break;
case 'C':
points = "2";
break;
case 'D':
points = "1";
break;
case 'F':
points = "0";
break;
default:
points = "0";
}
}
}
}


Thanks
Doug

[ 08 June 2001: Message edited by: WhiskeyBravo ]

TheLinuxDuck
06-08-2001, 01:49 AM
Hey Doug!

There are a few issues with the code, so I'll just list them, qool?

-- import the java.io class for the system out call
-- Pull the gradePoints function from out of the main function, and put it inside the class definition.
-- Since the class is named 'Grade', the file needs to be called 'Grade.java' (the code has GradePoints in the usage display.)
-- In the call to gradePoints:

int points = gradePoints(points);

: it's passing points in, which is an int. It should be passing in grade (since it is a char)
-- In the gradePoints function, the code is trying to assign a value to a variable that has not been defined. Instead of assigning a value and breaking for each case, simply return the value of points.

After fixing those things, the code compiles and runs fine. Let me know if you want me to post it. (^= I don't think I'll need to, though.

kmj
06-08-2001, 08:53 AM
TLD: you shouldn't need to import System.io for System.out.println, that should be done for you.

TheLinuxDuck
06-08-2001, 08:54 AM
Originally posted by kmj:
<STRONG>TLD: you shouldn't need to import System.io for System.out.println, that should be done for you.</STRONG>

Every example I've seen so far has done so, but you most certainly could be right.. I just have always done so... (^=

WhiskeyBravo
06-08-2001, 10:44 AM
thanks for the replies so far. I appreciate it. I'm still a little confused though. Let me see if I've got this straight:

int points = gradePoints(points);
this is the line that calls the method "gradePoints" I was under the assumption that (points) was the value that I want to retun from the method. But this is actually the argument that I am passing &lt;B&gt;into&lt;/B&gt; the method?

I thought that my
public int gradePoints(char grade)
was where I was passing the variable into the method.
should I not be passing any arguments, and just have gradePoints()?

Tks.

Dru Lee Parsec
06-08-2001, 12:14 PM
int points = gradePoints(points);
this is the line that calls the method "gradePoints" I was under the assumption that (points) was the value that I want to retun from the method. But this is actually the argument that I am passing &lt;B&gt;into&lt;/B&gt; the method?

I thought that my
public int gradePoints(char grade)
was where I was passing the variable into the method.
should I not be passing any arguments, and just have gradePoints()?



No, you're correct. The notationfor a method is this:

[access specifier] [static option] return-type method-name ([parameter list]){
[expresions]
}

So this is a proper method declaration:

public int addThese(int a, int b) {
return (a+b);
}


The ints "a" and "b" are passed into the method and the result of teh addition is passed back via the return statement. So you're absolutly correct there.

Where the confusion might have occured is here: If you're passing an instance of a class into the method that instance is always passed by reference (that means it uses pointers) so you could modify something n the method and have it "passed back" via the argument list.

Give me a few minutes and I'll post and example.

BTW, make your main class (the one that the file is named after, in this case your Grade class) public.

Dru Lee Parsec
06-08-2001, 12:20 PM
Here ya go, an example of passing a variable by reference:


public class TestThis
{
public static void main(String args[])
{
NameClass a = new NameClass("KMJ");
System.out.println("a is " + a);
dostuff(a);
System.out.println("a is " + a);
}

// In Java an instance of a class is ALWAYS passed by reference,
// so any modification we make to our instance of "x" inside this
// method will effect the value of "x" outside of the method.
private static void dostuff(NameClass x){
x.setName("LinuxDuck");
}
}

class NameClass {
private String name;

public NameClass(String newName){
name = newName;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

// This is a cool function that makes the class
// print the correct thing inside System.out.println("");
// statements
public String toString(){
return name;
}
}

Dru Lee Parsec
06-08-2001, 12:29 PM
OK, this works:

public class Grade {

public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java GradePoints Grade");
System.exit(1);
}
char grade = args[0].charAt(0);
int points = gradePoints(grade);
System.out.println(grade + " = " + points + " points");
}

private static int gradePoints(char grade) {
int temp;
switch (grade) {
case 'A':
temp = 4;
break;
case 'B':
temp = 3;
break;
case 'C':
temp = 2;
break;
case 'D':
temp = 1;
break;
case 'F':
temp = 0;
break;
default:
temp = 0;
}

return temp;
}

}




Here's the changes I made:

1. The big one was that your method was declared inside the main method. I moved it outside of the main method. It may have just been a bracket typing mistake.

2. I made the gradePoints use a new int temp instead of trying to re-use points.

3. gradePoints is now static because it's being called from main which is also static.

4. instead of temp = "3" i use temp = 3; because temp is an int, not a string.

You were close, you just had 4 things which were confusing you. When you have several unrelated problems it's tough to try to separate out which one to work on first. Each one hides the others so to speak.

WhiskeyBravo
06-08-2001, 01:40 PM
FINALLY!!! A CLEAN COMPILE!!!

Thanks everyone. After TLD's comments, I really had a better understanding of what was actually going on. I made lots of little changes and I really thought I had it, but alas no.

Then it was Dru's tip that really sealed the deal for me. trying to re-use the variable 'points' was really tripping me up. after I switched to a temporary variable, everything works.

Thank you very much everyone. Your suggestions really helped me understand. Maybe there is some hope for me not to be doomed to code PL/I and CLISTs for the rest of my life. ;)