2012-04-25 21 views
0

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

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

を助けてください、私は愚かでなければなりません... IVEは何もすることができたhaventはまだ最後の数日のためにこれをやってされているので助けてください
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; 
    } 
} 
+1

System.out.println(sumArray())だけでなく、 ?他のすべての場合も同様です。あなたはここで何がびっくりしていますか? テスターをもっとうまく動作させる方法がわからない場合 - (テスターメイン) スーパーマーケットsm =新しいSuperMarket(); System.out.println(sm.sumArray()); – djechlin

答えて

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ゼロを切断した後、それはまだ私の画面に収まりませんでしたので、私は唯一の各利益ここ

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

テスト/ドライバプログラムだためx/10アスタリスクを描画するために、その方法をmodifedことあなたは出発点として使うことができます。

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

    } 

} 
関連する問題