wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Topic: Question/Problem w/ my Program....I'm I missing something here in my syntax?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Topic: Question/Problem w/ my Program....I Watch "Topic: Question/Problem w/ my Program....I New topic
Author

Topic: Question/Problem w/ my Program....I'm I missing something here in my syntax??

Felipe Monegro
Greenhorn

Joined: Feb 27, 2008
Posts: 3
Hi, I'm new in these forums, and I'm studying Java this semester. I would like to program in Java in the future, I find it to be a very useful and powerful language . Learning Java is like trying to learn another language and sometimes I don't understand what is going on..??

Right now I'm writing a program in which I have to sort a lis of student by grades, I am getting some errors(the yellow ones) and I'm clueless of what I am missing here....Please, if somebody could take at look at my program/syntax I would really appreciate it....thank you This what I have so far:

//This is my program:


import java.util.ArrayList;
public class Student {

private String first, last;
private ArrayList<Double> gradeList = new ArrayList<Double>();

/**
*
* @param first Student first name
* @param last Student last name
*/
public class SortMethods {

public void selectionSort(int[] list, int last){
for (int end = last; end > 0; end--)
{
int what = list[0];
int where = 0;
for(int n = 1; n <= end; n++){
if(list[n] > what){
what = list[n];
where = n;
}
}
int temp = list[end];
list[end] = list[where];
list[where] = temp;
}
}

public void selectionSort(String[] list, int last){
for (int end = last; end > 0; end--)
{
String what = list[0];
int where = 0;
for(int n = 1; n <= end; n++){
if(list[n].compareTo(what) > 0){
what = list[n];
where = n;
}
}
String temp = list[end];
list[end] = list[where];
list[where] = temp;
}
}

public void selectionSort(Comparable[] list, int last){
for (int end = last; end > 0; end--)
{
Comparable what = list[0];
int where = 0;
for(int n = 1; n <= end; n++){
if(list[n].compareTo(what) > 0){
what = list[n];
where = n;
}
}
Comparable temp = list[end];
list[end] = list[where];
list[where] = temp;
}
}

}
public Student(String first, String last){
this.first = first;
this.last = last;
}

public String getFirst(){
return first;
}

public String getLast(){
return last;
}


public void addGrade(double grade){
gradeList.add(grade);
}

/**
*
* @return student's average, or -1 if no grades entered
*/
public double getAverage(){
if (gradeList.isEmpty()) return -1;
double sum = 0;
for (int n = 0; n < gradeList.size(); n++)
sum += gradeList.get(n);
return sum/gradeList.size();
}

public String toString(){
double average = getAverage();
if (average < 0) return first + " " + last + " : no grades entered";
else return first + " " + last + " : average = " + average;


}
}


//This below is my Tester and this also has an error that I have not been //able to figure out:

public class Tester {

public static void main(String[] args) {

Student[] list = new Student[5];
list[0] = new Student("George", "Washington");
list[1] = new Student("King", "George III");
list[2] = new Student("Albert", "Einstein");
list[3] = new Student("Julius", "Caesar");
list[4] = new Student("Donald", "Duck");

list[0].addGrade(88);
list[0].addGrade(93);
list[1].addGrade(75);
list[3].addGrade(98);
list[3].addGrade(99);
list[3].addGrade(96);
for(int n = 0; n < 500; n++)
list[4].addGrade(87);

SortMethods.selectionSort(list, 4);

for(int n = 0; n < 5; n++)
System.out.println(list[n]);
}

}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Welcome to the Ranch.

Please use code tags round your posted code; it is difficult to read otherwise.

Have you got each of your classes in a file with the same name as the class and a .java extension? Your SortMethods class ought to be in a file called SortMethods.java, etc. You appear to have 3 classes, so you would need 3 files. I am not sure, but I suspect you have not got your { and } paired correctly; please check that. Is your SortMethods class supposed to be an inner class in the Student class? Ought your selectionSort() method not to be static?
Suggestion: In the Deitel books they have //end something after every }; it would be a good idea to try that to make it easier to find your way round the code.

You are passing a Student[] to the selectionSort() method, which doesn't take Student[] as a parameter. You would have to:
  • change it to take Object[] as a parameter,
  • cast each individual item to (Comparable), not casting the whole array,
  • and make the Student class implement the Comparable<Student> interface.
  • It would be better to find about the Comparator interface and pass a StudentComparator as an argument for sorting.

    BTW: You don't need a "last" parameter if you are sorting the entire array. Use the array.length parameter, which might be better as array.length - 1 if you are iterating through the array backwards.

    If those suggestions don't help, please post again with code tags and each class in its own code block, and also copy and paste the error messages.
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11444
        
      16

    Welcome to the Ranch!!!

    a couple of tips...

    1) When asking questions like "i'm getting compiler errors", it GREATLY helps if you post the exact text of the errors.

    2) Please use code tags. you can use those little "instant UBB" buttons to drop them into your message, then paste your code between - it will preserve the formatting and make your code easier to read.

    3) I would suggest writing less code before you try to compile it. Some folks would recommend writing no more than ONE line before you recompile and re-test. That way you know exactly what line caused the error. You seem to have about 80 lines of code written and you're just trying to solve the syntax errors now. That is a surefire way to come to grief.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39478
        
      28
    Maybe making Student implement the Comparable<Student> interface will make the other two changes I suggested about Object[] and casting unnecessary. I haven't tried it. Fred Rosenberger is right about not compiling lots of code.
    Felipe Monegro
    Greenhorn

    Joined: Feb 27, 2008
    Posts: 3
    Wow guys thank you! I followed very carefully all the instructions by you guys and I was able to fix most of the problems I had. A major problem my code had was that I needed to put the SortMethod, and the Student method each in their own class, and the other one was that I did not put these classes in a package.... :roll: Then I added some more lines of code in the Student class to sort the students by average.... it took a while but Finally I was able to figure it out.


    Guys Thank You very much, I'd like to learn more and more Java it is a very powerful and helpful program, in the future I would like to be able to help others like you guys do, if I get into java programming in the future. I'll keep in my those rules about inserting codes and other syuff. Thank you again, take care. Now I have to rest my brain a little bit
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39478
        
      28
    You're welcome. We are only too pleased to help.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Topic: Question/Problem w/ my Program....I'm I missing something here in my syntax??