简体   繁体   中英

Trying to Debug Java Code

I guess I'm not getting arrays because somehow I managed yet again to break a perfectly good program.

It was working earlier but then I did something and it stopped working. I would like it back to the way it was.

Code in main:

public class Lab14d

 {

  public static void main( String args[] )
{

    int r = 5;
    int e = 3;
    int k = 9;

    double[]g ={100,90,85,72.5,95.6};
    double[]c ={50.0,100.0,80.0};
    double[]a ={93.4,-90.0,90.0};
    double[]d ={1,2,3,4,5,6,7,8,9};

    Grades w = new Grades();


    w.SortArray(g);
    w.getSum(g);
    w.getAverage(r);
    System.out.println(w);

    w.SortArray(c);
    w.getSum(c);
    w.getAverage(e);
    System.out.println(w);

    w.SortArray(a);
    w.getSum(a);
    w.getAverage(e);
    System.out.println(w);

    w.SortArray(d);
    w.getSum(d);
    w.getAverage(k);
    System.out.println(w);


}
}     

code in other file

public class Grades

  {

    int x = 0;
    public Grades()
    {
     x = 0;
     }
public static SortArray(double[] array)
{
    Arrays.sort(array);
    for (int i = 0; i < array.length; i++)
    {
        return "Grade " + i + " :: " + "   " + array[i];
    }

}
public double getSum(double[] array )
{
    double sum=0.0;
    for(int spot = 0; spot <= array.length; spot++)
    {
        sum = sum + array[spot];
    }

    return sum;
}

public double getAverage(int x)
{
    double average=0.0;
    average = getSum()/2;
    return average;
}
public String toString()
{
    return "Average = " + getAverage();
}
 }

out put i want

grade 0 ::  100.00
grade 1 ::   90.00
grade 2 ::   85.00
grade 3 ::   72.50
grade 4 ::   95.60

average = 88.62


grade 0 ::   50.00
grade 1 ::  100.00
grade 2 ::   80.00

average = 76.67


grade 0 ::   93.40
grade 1 ::  -90.00
grade 2 ::   90.00

average = 31.13


grade 0 ::    1.00
grade 1 ::    2.00
grade 2 ::    3.00
grade 3 ::    4.00
grade 4 ::    5.00
grade 5 ::    6.00
grade 6 ::    7.00
grade 7 ::    8.00
grade 8 ::    9.00

average = 5.00

I appreciate any help given.

your line of code average = getSum()/2; should be giving a compile time error ,we cannot see that method in the file.Same is the case with return "Average = " + getAverage();

The main problem is you have defined your methods to accept parameters,but in these places you are calling them without those parameters.

I really doubt how this code is supposed to be working, public static SortArray(double[] array) no RETURN type specified!

your methods just returning the result, but you are not assigning it to any variable or not printing what you get as result!! instead of that you are trying to print the object itself! the return statement will not print to the console.. it will just pass the result to where it is called/ to where we assigned it to .. my suggesion is:

1: change the returning statemnts to print statements(in all methods if you wish to do there itself)

return "Grade " + i + " :: " + "   " + array[i];

to

System.out.println("Grade " + i + " :: " + "   " + array[i]);

I think it is better to print from there itself, because in the above statement you need to print the result from the loop itself, it is better than storing then passing and printing them anyway ...

Your code is full of problems, you are returning wrong values, have unused variables (x), no return type specified etc. Below is the corrected version. I replaced double[] with Double[] (objects instead of primitives) such that I can sort them using Arrays.Sort in descending order (as you asked). Arrays.Sort with only work like this with objects and not primitives. The code produces the output you want.

Main class:

public class Lab14d {
    public static void main(String[] args) {
        Double[] g = { 100.00d, 90.00d, 85.00d, 72.5, 95.6 };
        Double[] c = { 50.0, 100.0, 80.0 };
        Double[] a = { 93.4, -90.0, 90.0 };
        Double[] d = { 1.00d, 2.00d, 3.00d, 4.00d, 5.00d, 6.00d, 7.00d, 8.00d, 9.00d };

        Grades.SortArray(g);
        System.out.println("Average = " + String.format("%.2f", Grades.GetAverage(g)));
        System.out.println();

        Grades.SortArray(c);
        System.out.println("Average = " + String.format("%.2f", Grades.GetAverage(c)));
        System.out.println();

        Grades.SortArray(a);
        System.out.println("Average = " + String.format("%.2f", Grades.GetAverage(a)));
        System.out.println();

        Grades.SortArray(d);
        System.out.println("Average = " +String.format("%.2f", Grades.GetAverage(d)));
        System.out.println();
    }
}

Grades class:

import java.util.Arrays;
import java.util.Collections;

public class Grades {

    public static void SortArray(Double[] array) {
        Arrays.sort(array, Collections.reverseOrder());
        for (int i = 0; i < array.length; i++) {
            System.out.println("Grade " + i + " :: " + array[i]);
        }
    }

    private static double GetSum(Double[] array) {
        double sum = 0.00d;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        return sum;
    }

    public static double GetAverage(Double[] array) {
        double total = GetSum(array);
        return total / array.length;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM