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

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

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

}
 
Marshal
Posts: 79938
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    lowercase baba
    Posts: 13091
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Marshal
    Posts: 79938
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79938
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome. We are only too pleased to help.
     
    Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic