1,105,546 Community Members

Grade ArrayList

Member Avatar
zacant
Newbie Poster
6 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

My code:

public ArrayList<Grade> grades;
grades = new ArrayList<Grade>();
public Grade getLowestGrade()
      {
        Grade lowestGrade = grades.get(0);
            for (int i = 1; i < grades.size(); i++) 
         {
            Grade g = grades.get(i);
            if(s.getGrade() < lowestGrade.getGrade())

               lowestGrade = g;

            }
                return lowestYet;
         }   

This code is in my Student class using the grades ArrayList.
In my tester I have:

Student getLowestGrade = new Student();
getLowestGrade.getLowestGrade();

My question is what am I missing to get this to print the lowestGrade out to my tester.

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

a print statement? what is it you are having trouble with?
also: since you compare instances of Grade, you may want to choose compareTo instead of the < operator.

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

The Java compiler is your friend. Don't be afraid to try and compile your code. There's no point trying to get it right before compiling. Just chuck it into the compiler and see what errors it throws up. That costs you nothing.
At first you;ll get a lot of errors, but start with the first, fix them one at a time, re-compiling after each one.

But first...
Go back to your very first Java program (Hello World?). Remenber the method called main? Every program needs one. That's where you can: create new grades list, add some data to it, call your getLowest method, and print the result.

Have a look at that, if/when you get stuck come back here and post the whole of your code - it's hard to help you if we have to guess what's in the bits you didn't post!

Member Avatar
zacant
Newbie Poster
6 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I tried the compareTo but couldn't quite get the code right.
I'm not sure how to use it when I have s.getScore.
string1.compareTo(string2) < 0

So how could I do

if(s.getGrade.compareTo(lowestGrade.getGrade))

error states: cannot find symbol.

I did try the print command which helped. Now I know something else is wrong else where.
I also don't want to put all my code in because it is homework and I want to try.
If I post and have a lot wrong then I'm not the one doing it. So the print helped me
see that something is wrong with my ArrayList.

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

I understand why you don't want to post all your code, but there is some more info neded. In particular, what is the return type for the getGrade() method? The name implies that it returns a Grade object (in which case you need to get compareTo working), but the context suggests that it's unlikely to be a Grade object. If, for example, it just returns an int or a char then you can compare with a simple <

(s.getGrade.compareTo(lowestGrade.getGrade)
Unless getGrade is also a variable this syntax is wrong. If, as the name suggests, it's a method then you need a (...) for the parameter list, even if it's empty ... eg s.getGrade()

Member Avatar
zacant
Newbie Poster
6 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

The getGrade depending on a findGrade method.

public Grade getGrade(Score aGrade)
      {
         findGrade(aGrade);      
         for (int i = 0; i < grades.size(); i++)
         {
            if (grades.get(i).equals (aGrade))
            {
               return aGrade;
            }
         }
         return null;
      } 

The findGrade method is:

private Grade findGrade(Grade aGrade)
      {
        Grade NOTFOUND = grades.get(-1);        
            Grade g = grades.get(0);       
            for (int i = -1; i <= grades.size(); i++)
         {
            if (s.getGrade().equals(aGrade))
            {
               return g;
            }
         }
         return NOTFOUND;
      }

The seem to all depend on each other and I am just trying to get on part correct so maybe I can build my confidence to work on the rest. I am really to just get the grade I get.

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

So far you posted getGrade (no parameter list) and getGrade() (empty parameter list), yet the method signature requires a parameter of type Score, so neither of those is right. And what's the difference between a Score and a Grade? Line 6 implies that a Score can be equal to a Grade, but without the class definitions who knows?

Grade NOTFOUND = grades.get(-1); will never assign a value to NOTFOUND, and none of the rest of findGrade will ever be executed because it will always throw an exception first (see the JavaDoc for ArrayList's get(int) method). Similarly getGrade will also stop executing and throw the exception as soon as it calls findGrade. (And why call findGrade anyway when you just ignore the value it returns). From which I guess you haven't been able to execute any of this code yet.

It seems to me that you are trying to code this in pieces that are too big. You will have fewer problems if you write one method at a time, write some trivial discardable code to test that method, and get the method working before starting on the next method. If you try to write too much in one go then you get buried in errors when you finally try to compuile and run, and its really hard to see where to start fixing things.

Member Avatar
zacant
Newbie Poster
6 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

The reason I set it to -1 is because of her instructions.
findGrade - private method given a parameter representing the grade's date, returns the ArrayList index of a grade. (Use in deleteGrade and getGrade). Return constant NOTFOUND if not found, NOTFOUND is set to -1;

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

In that case, it's time to stop and read the instructions properly:

Instruction: NOTFOUND is set to -1;
Your code: NOTFOUND = grades.get(-1);

Instruction: findGrade - private method given a parameter representing the grade's date
Your code: findGrade(Grade aGrade)

Instruction: returns the ArrayList index of a grade
Your code: Grade g .... return g;

So that's three examples of not following the instructions just from that tiny excerpt.

For your own sake I would start again with a clean sheet of paper and re-read the instructions until you understand every single word in them. Until then your code is doomed to go off in random directions.
Please don't think I'm being negative or critical; I'm just giving the best advice I can, even if it's uncomfortable.

Member Avatar
zacant
Newbie Poster
6 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

No thank you. I have reread the instructions over and over that I was starting to see things. You have helped me alot. I first went through the instructions and broke it down and when things went wrong I started to stray. So I don't take it the wrong way.

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

OK. Don't worry. Keep posting your problems and we will try to help.

Member Avatar
zacant
Newbie Poster
6 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I took your advice and it worked. I was able to step away from the compiler and look at the instructions. I wrote out what each part was asking and then start to compile from scratch. With what I ran into problems with and learned about I was able to write the code with minor errors that I could fix. Thanks again.

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Excellent! Glad we could help.
Perhaps you can mark this thread "solved" now. If you run into problems with your next assignment you can start a new thread.
J

Question Answered as of 1 Year Ago by JamesCherrill and stultuske
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article