• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused...returning certain types

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ' ';
}


}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}


}
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic