Felipe Monegro

Greenhorn

Posts: 3

posted 9 years ago

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]);

}

}

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]);

}

}