试图理解java中的“驱动程序”程序

时间:2016-04-03 05:51:18

标签: java arrays

我收到了这个作业,不知道该怎么办。

我完成了以下任务:询问5个数组输入,5个数据输入的平均值,并从最高到最低排序。

但我不明白(toString)要求做什么,它怎么能返回一个包含数组升序和同时平均值的“字符串”数据呢?

目前我的程序正常运行,但我认为我在运算符类(平均值)中使用toString方法做错了。

运算符类,驱动程序类,输出窗口:http://imgur.com/a/rj5NJ

作业:http://imgur.com/QC9LTrG

运算符类(平均值)

    import java.util.Scanner;
    public class average
{
    private int data[] = new int[5];
    private double mean = 0;
    Scanner keyboard = new Scanner(System.in);
    int i = 1;
    int index = 0;
    average()
    {

        for (int num : data)
        {
            System.out.print("Enter score for student " + i + " : ");
            i++;
            data[index] = keyboard.nextInt();
            index++;
        }

        toString();
    }
    public void calculateMean()
    {
        double total = 0;

        for (int num : data)
        {
            total = total + num;
        }

        mean = total / data.length;
        System.out.println("Average score is :" + mean);
    }

    public String toString()
    {
        selectionSort();
        System.out.print("Data after selection sort (lowest to highest): ");
        index = 0;
        for (int num : data)
        {
            System.out.print(data[index] + " ");
            index++;
        }

        System.out.println("");

        calculateMean();

        String result = null;
        return result;
    }

    public void selectionSort()
    { 

        java.util.Arrays.sort(data);

    }
}

驱动程序类(avgdr)

public class avgdr
{
    public static void main(String[] args)
    {
        average e1 = new average();
    }
}

提前致谢。

4 个答案:

答案 0 :(得分:1)

正如它所说,在赋值中,你的构造函数应该从用户那里获取输入,并且已经计算了平均值。

计算均值后,toString方法应该只是答案,即它应该打印对象状态。 [数组中的所有值和平均值。]

public String toString() {
    return "Data after selection sort (lowest to highest): "
            + Arrays.toString(data) + "\nAverage score is: " + mean;
}

因此应该从构造函数中调用所有计算和方法。和toString方法一样,您可以从构造函数或驱动程序类调用。 通常的做法是从驱动程序类调用它,如下所示 - 驱动程序类

public class avgdr {
    public static void main(String[] args) {
        average e1 = new average();
        System.out.println(e1);
    }
}

在自动调用System.out.println(e1);时,将调用该类的toString方法,并在控制台上显示返回的字符串。

,构造函数看起来像这样 -

average() {
    for (int num : data) {
        System.out.print("Enter score for student " + i + " : ");
        i++;
        data[index] = keyboard.nextInt();
        index++;
    }
    selectionSort();
    calculateMean();
}

答案 1 :(得分:0)

你的toString需要返回String,它包含两个数组的升序和平均值 为此,您需要在您的班级中使用两个实例变量 1.数组:int data [] = new int [5]
2.意思是:双重均值
在从用户调用您的selectionSort和calculateMean方法之后,在调用toString方法之后不再进行进一步计算只需返回您想要的字符串

import java.util.Scanner;
public class average {
private int data[] = new int[5];
private double mean = 0;
Scanner keyboard = new Scanner(System.in);
int i = 1;
int index = 0;
average()
{

    for (int num : data)
    {
        System.out.print("Enter score for student " + i + " : ");
        i++;
        data[index] = keyboard.nextInt();
        index++;
    }
    selectionSort();
    calculateMean();
    toString();
}
public void calculateMean()
{
    double total = 0;

    for (int num : data)
    {
        total = total + num;
    }

    mean = total / data.length;
    System.out.println("Average score is :" + mean);
}

public String toString()
{
    StringBuilder builder = new StringBuilder();
    builder.append("Sorted Data is: ");
    for (int num : data)
    {
        builder.append(data[index] + " ");
        index++;
    }
    builder.append(", Mean is: ").append(mean);
    return builder.toString();
}

public void selectionSort()
{ 
    java.util.Arrays.sort(data);

}

}

答案 2 :(得分:0)

    for (int num : data)
    {
        System.out.print("Enter score for student " + i + " : ");
        i++;
        data[index] = keyboard.nextInt();
        index++;
    }

    toString(); //calling toString Method Here.
}

现在您在构造函数

中调用此方法

平均()     {

toString()
  

执行流程: - 在构造函数内部,您正在调用selectionSort();方法,该方法在内部调用两个函数A)selectionSort();和B)main()。在average e1 = new average();内,您创建了Constuctor 平均的实例。调用构造函数所以,执行将像toString()> calculateMean()> selectionSort() this.analyzer = new Analyzer({ if( Dolby.checkDDPlus() === true ){ url: 'http://example.com/samples_Dolby.mp4', } else { url: 'http://example.com/samples_Dolby.mp3', } onPlay: function() { button.classList.add('pause'); button.classList.remove('play'); }, onPause: function() { button.classList.remove('pause'); button.classList.add('play'); }, onEnd: function() { button.classList.remove('pause'); button.classList.add('play'); } });

答案 3 :(得分:0)

事实证明,您似乎并不是想从代码中的任何位置调用toString ()。只需按照降序和String返回包含所有元素的mean即可。此外,在这种情况下使用DecoratorScanner从用户获取输入的方式稍微不正确恕我直言。首选用于此目的的缓冲流,并让它们进行字节读取。

以下是修改后的代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

import java.util.Arrays;

public class Average {

    private static final int TOTAL_SCORES = 5;

    private int [] data;
    private double mean;

    private BufferedReader inputStream;

    public Average () {
        data = new int [ TOTAL_SCORES ];
        try {
            inputStream = new BufferedReader ( 
                        new InputStreamReader ( System.in ) );
            for ( int i = 0; i < data.length; ++i ) {
                System.out.print ( "Enter Score Number " + ( i + 1 ) + ": " );
                data [ i ] = Integer.parseInt ( inputStream.readLine () );
            }
        } catch ( IOException ioex ) {
            ioex.printStackTrace ();
        }

        selectionSort ();
        calculateMean ();
    }

    private void selectionSort () {
        for ( int i = 0; i < ( data.length - 1 ); ++i ) {
            int max = i;
            for ( int j = ( i + 1 ); j < data.length; ++j ) {
                if ( data [ max ] < data [ j ] ) {
                    max = j;
                }
            }
            if ( max != i ) {
                int temp = data [ max ];
                data [ max ] = data [ i ];
                data [ i ] = temp;
            }
        }
    }

    private void calculateMean () {
        double result = 0.0;
        for ( int number : data ) {
            result += number;
        }
        result /= data.length;

        mean = result;
    }

    @Override
    public String toString () {
        StringBuilder sb = new StringBuilder ();
        sb.append ( "Elements: \n" );
        for ( int number : data ) {
            sb.append ( number + "\n" );
        }
        sb.append ( "Mean: " + mean );

        return sb.toString ();
    }    

    private void display ( int [] data ) {
        System.out.println ( "Data in Array is: " );
        for ( int number : data ) {
            System.out.println ( number );
        }
    }

    public static void main ( String [] args ) {
        System.out.println ( new Average () );
    }
}