Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Confused...returning certain types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused...returning certain types" Watch "Confused...returning certain types" New topic
Author

Confused...returning certain types

Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
OK, here's the deal, I'm a huge beginner at java and have a horrible teacher who doesnt explain anything and then throws complicated assignments on us (well, for me they are anyway).
For this method, I want to return a character, and get the "non static method cannont be referenced from a static method" error. We have yet to learn about static/non static methods. I have anothe similar message in which I returned the entire object, but I don't understand how I can return certain parts of this object (like the char that represents a letter grade). I tried creating a new "grade" variable inside this method, but that didn't really seem to help. For some reason, this seems too easy, yet I'm very confused.

Here is my Code:

public char getLetterGrade(int courseIndex)
{
char grade;


if (courseIndex < 0)
{
return ' ';
}

else if (courseIndex < numberOfCourses())
{

courses.get(courseIndex);
return Grade.getLetterGrade();
}

else
{
return ' ';
}


}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
On what line are you getting the error?

If you have an instance variable defined in the class, you cannot access that variable from the main method.
Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
This line here:

It comes from another method, and I don't understand how to get around this problem. (gives the static/non static error)

return Grade.getLetterGrade();
Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
Please post the whole code so we can help. Most probably you will have to declare your getLettergrade method as static for this to work.


SCJP 1.4<br />SCWCD 1.3
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
if i'm not mistaken, the method shown is the getLetterGrade(int) method, unless its oveloaded with another method that takes no parameters. If the method is overloaded, and not static, and in this class, you can call it by simply calling getLetterGrade().

You declare a variable grade at the beginning of the method that I assume you'll want to return at the end, but you have to assign it some value first.
[ March 10, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
Ok here is ALL the code, there are 3 seperate classes.

Transcript class:
import java.util.ArrayList;
import java.util.Iterator;

public class Transcript
{
//Instance Variables
private ArrayList<Course> courses;
private String studentId;
private String studentName;


public Transcript(String id, String name)
{
//Initialize instance variables
studentId = id;
studentName = name;
courses = new ArrayList();
}

//returns the number of courses stored in the courses Array
public int numberOfCourses()
{
return courses.size();
}

//adds a course to the courses Array
public void addCourse(Course aCourse)
{

courses.add(aCourse);
}

public Course getCourse(int courseIndex)
{


if (courseIndex < 0)
{
return (null);
}
else if (courseIndex < numberOfCourses())
{


return courses.get(courseIndex);
}
else
{
return (null);
}

}

public char getLetterGrade(int courseIndex)
{
char grade;


if (courseIndex < 0)
{
return ' ';
}

else if (courseIndex < numberOfCourses())
{

courses.get(courseIndex);
return Grade.getLetterGrade();
}

else
{
return ' ';
}


}
}

Course class:
/**
Patrick Kent
3-8-06
This class takes information about a course, and allows the user to
set the grade and get the points from the grade class as well.
*/

public class Course
{
private String title;
private int creditHours;
private Grade grade;

//Allows the user to enter a course, credit worth, and letter grade
public Course(String courseTitle, int courseCredits, char letterGrade)
{
title = new String(courseTitle);
creditHours = courseCredits;
grade = new Grade(letterGrade);
}

//allows the user to reset the course title
public void setTitle(String newTitle)
{
title = newTitle;
}

//returns the course title
public String getTitle()
{
return title;
}

//allows the user to reset the amount of credit hours
public void setCreditHours(int credits)
{
creditHours = credits;
}

//returns the credit hours
public int getCreditHours()
{
return creditHours;
}

//returns the amount of points
public int getQualityPoints()
{
return grade.qualityPoints();
}


//allows the user to reset the grade
public void setGrade(char letterGrade)
{
grade.setLetterGrade(letterGrade);
}

//returns the grade
public char getGrade()
{
return grade.getLetterGrade();
}

//returns course and grade information
public String toString()
{
return "Course: " + title
+ " (" + creditHours + " credits);"
+ " grade " + getGrade();
}
}

Grade class:
public class Grade
{
private char letterGrade;

//takes the entered grade and converts it to an uppercase char
public Grade(char grade)
{
letterGrade = grade;
letterGrade = Character.toUpperCase(letterGrade);
}

//this method allows the user to change the grade entered
public void setLetterGrade(char grade)
{
letterGrade = grade;
letterGrade = Character.toUpperCase(letterGrade);
}

//returns the letter grade entered by the user
public char getLetterGrade()
{
return letterGrade;
}

//retruns a number of "points" based on the entered letter grade
public int qualityPoints()
{
int points;

if (letterGrade == 'A')
points = 4;
else if (letterGrade == 'B')
points = 3;
else if (letterGrade == 'C')
points = 2;
else if (letterGrade == 'D')
points = 1;
else
points = 0;

return points;
}


}
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Grade.getLetterGrade();

That is calling getLetterGrade as if it were a static method, it is not. Either make it static or pass an instance of Grade to getLetterGrade(). The former is probably what you need since letterGrade in Grade is not(and probably shouldnt be) static.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
 
 
subject: Confused...returning certain types
 
Similar Threads
Calling a static method. Help!
to Dave Lander,Marilyn de Queiroz
Java/JBuilder - Anyone with brains please help
static vs non static methods
Inheritance error?