0

完了する必要がある 6 つのタスクがありますが、それらを表示する方法がわかりません。必要な各タスクを計算するためのすべてのコードがありますが、それらをテスター クラスに適用するとなると行き詰まります。以下は、元のクラスの私のコードです。これらは、実行する必要があるタスクです。

  1. 表に示すように、元のデータセットを表示します。
  2. スーパー マーケット チェーンの平均利益。
  3. 利益率が最も高い都市。
  4. 利益が平均以上のすべての都市のリスト。
  5. 都市とその利益は、利益の降順でリストされています。
  6. 各スーパーマーケットの業績を示す横グラフを作成する

この数日間これをやっていて、まだ何もできていないので助けてください...私は愚かに違いない助けてください

package supermarkets;

public class Supermarkets 
{

    private String[] city = {"Miami", "Sunrise", "Hollywood",
        "Tallahassee", "Jacksonville", "Orlando", "Gainesville", "Pensacola",
        "Ocala", "Sebring"};

    private double[] profit = {10200000, 14600000, 17000000, 6000000,
        21600000, 9100000, 8000000, 12500000, 2000000, 4500000};

    public Supermarkets(String[] c, double[] p)
    {
        //array for the city
        city = new String[c.length];
        for (int i = 0; i < c.length; i++)
            city[i] = c[i];


        //array for the profits
        profit = new double[p.length];
        for(int i = 0; i < p.length; i++)
            profit[i] = p[i];
    }


    //sums up the profits from the cities
    public double sumArray()
    {
        double sum = 0;

        for (int i = 0; i < profit.length; i++)
            sum = sum + profit [i];

        return sum;
    }

    //calculates the average of the profits from the cities
    public double average()
    {
        return sumArray() / profit.length;
    }


    //shows highest profit
    double findHighestProfit() 
    {
        double highest = profit[0];

        for(int i = 1; i < profit.length; i++)
        {
            if ( profit [i] > highest )
                highest = profit [i];      
        }
        return highest;
    }


    //gives the above average profit
    public String aboveAvarage()
    {
        String s = "";
        double avg = average();
        for (int i = 0; i < profit.length; i++)
            if (profit [i] > avg)
                s = s + city[i] + "  " + profit[i] + "\n";
        return s;
    }


    //creates a graph showing city and profits
    public String makeGraph()
    {
        String s = "";
        for (int i = 0; i < profit.length; i++)
        {
            s = s + city[i] + " ";
            int x = (int) Math.floor( profit[i] );

            for(int j = 1; j <=x; j++)
                s = s + "*";
            s = s + "\n";
        }
        return s;
    }


    //resets profits position from least to greatest
    public int findPosition(int startScanFrom)
    {
        int position = startScanFrom;

        for (int i = startScanFrom + 1; i < profit.length; i++)
            if (profit[i] < profit[position])
                position = i;

        return position;
    }

    //swaps values for city and profits
    public void swap(int i, int j)
    {
        // Swap the profits
        double temp = profit[i];
        profit[i] = profit[j];
        profit[j] = temp;

        // Swap the cities
        String str = city[i];
        city[i] = city[j];
        city[j] = str;
    }
}
4

1 に答える 1

0

あなたはテスト/ドライバー プログラムを提供しなかったので、多くの仮定を立てる必要がありました。

  1. 2 つの配列をループして値を出力するメソッドを作成するだけです。

  2. 平均利益を見つける方法は問題ないようです。

  3. 最高の利益を見つける方法はありますが、都市を表示する必要があります。収益が最も高い都市を見つけるメソッドを追加しました。

    //shows city with highest profit
    String findHighestProfitCity() 
    {
        double highest = profit[0];
        String c = city[0];
    
        for(int i = 1; i < profit.length; i++)
        {
            if ( profit [i] > highest ) {
                highest = profit [i]; 
                c = city[i];
            }
        }
        return c;
    }
    
  4. これはかなり簡単な方法です。平均を計算したら、都市/利益の配列をループして、利益が平均よりも高い都市を表示します。

  5. 2 つの配列に編成されたデータでこれを行う最も簡単な方法は、利益配列をソートし、利益に必要なたびに都市配列でスワップを行うカスタム ソート ルーチンを作成することです。

  6. 都市と利益の配列に入力したテスト データは大きすぎて、グラフで表示できません。メソッドは、利益の各値にアスタリスクmakeGraph()を描画しようとします。x各値から 5 つのゼロを切り取った後でも、それでも画面に収まりませんでした。その方法を修正してx/10、利益ごとにアスタリスクのみを描画するようにしました。

    for(int j = 1; j <= x/10; j++)
        s = s + "*";
    

これは、出発点として使用できるテスト/ドライバー プログラムです。

package supermarkets;

public class SupermarketDriver {

/**
 * @param args
 */
public static void main(String[] args) {

    String[] cities = {"Miami", "Sunrise", "Hollywood",
            "Tallahassee", "Jacksonville", "Orlando", "Gainesville", "Pensacola",
            "Ocala", "Sebring"};

    double[] profits = {102, 146, 170, 60, 216, 91, 80, 125, 20, 45};


    Supermarkets sm = new Supermarkets(cities, profits);
    System.out.println(sm.makeGraph());
    System.out.println("Average profit: " + sm.average());
    System.out.println();
    System.out.println("Highest profit city: " + sm.findHighestProfitCity() + ", " + sm.findHighestProfit());

    }

}
于 2012-07-15T14:35:58.117 に答える