0
Print all boats
Color= blue Length= 22 Engine size= 60 Price= $12,800.00
Color= white Length= 18 Num of Sails= 1 Price= $20,000.00
Color= red Length= 42 Num of Sails= 3 Price= $48,000.00
Color= yellow Length= 35 Engine size= 80 Price= $17,100.00
Color= red Length= 50 Engine size= 120 Price= $22,400.00
Color= blue Length= 33 Num of Sails= 2 Price= $37,000.00
Color= white Length= 14 Engine size= 10 Price= $9,400.00

Total price of all boats is $166,700.00. 

ArrayList で totalCost を取得するにはどうすればよいですか?

Most Expensive Boat: Color= red Length= 42 Num of Sails= Cost= $48,000.00 

コストが最も高いボートを ArrayList で検索するにはどうすればよいですか?

ArrayList で何かを取得する方法をよく理解していません (コードの 1 行で何度も何度も出力する以外に) テスト クラス自体に変数が存在しない場合。私が使用している 3 つのクラスすべてのコードを次に示します。うまくいけば、計算、最高コスト、低コスト、最も安いなど、配列内の何かを取得して使用する方法について、皆さんがよりよく理解できるようになることを願っています.

Boat は継承 (スーパー クラス) SailBoat と PowerBoat は Boat (サブクラス) Inventory (テスト クラス) を拡張します。

public class Boat
{
//attributes
String color; //holds boat color
int lengthBoat; //contains boat length

//default constructor
public Boat()
{
    color = "white";
    lengthBoat = 0;
}
//parameterized constructor
public Boat(String _color, int _lengthBoat)
{
    setColor(_color);
    setLengthBoat(_lengthBoat);
}
//mutator method for color
public boolean setColor(String _color)
{
    if(_color == "white" || _color == "red" || _color == "yellow" || _color == "blue")
    {
        color = _color;
        return true;
    }
    else 
    {
        System.out.println("ERROR - The color must be one of these following choices, \"white\", \"red\", \"yellow\",  or \"blue\".");
        return false;
    }
}
//accessor method for color
public String getColor()
{
    return color;
}
//mutator method for lengthBoat
public boolean setLengthBoat(int _lengthBoat)
{
    if(_lengthBoat >= 0 && _lengthBoat <= 50)
    {
        lengthBoat = _lengthBoat;
        return true;
    }
    else
    {
        System.out.println("ERROR - The length must be between 0 and 50 inclusively.");
        return false;
    }
}
//accessor method for lengthBoat
public int getLengthBoat()
{
    return lengthBoat;
}
//toString method for printing results
public String toString()
{
    return "Color= " + getColor() + " Length= " + getLengthBoat();
}
}

帆船クラス

import java.text.NumberFormat;

 public class SailBoat extends Boat
 {
//attribute
int numOfSails; //holds the number of Sails

//default constructor
public SailBoat()
{
    numOfSails = 1;
}
//parameterized constructor
public SailBoat(String _color, int _lengthBoat, int _numOfSails)
{
    super(_color, _lengthBoat);//taken from the parent and inherited to be recognized in this class's constructor
    setNumOfSails(_numOfSails);
}

//mutator method for numOfSails
public boolean setNumOfSails(int _numOfSails)
{
    if(_numOfSails >= 1 && _numOfSails <= 4)
    {
        numOfSails = _numOfSails;
        return true;
    }
    else 
    {
        System.out.println("ERROR - The number of sails must be between 1 and 4 inclusively.");
        return false;
    }
}
//accessor method for numOfSails
public int getNumOfSails()
{
    return numOfSails;
}
//calculates the price of the SailBoat as follows:
public int calcPrice()
{
    return lengthBoat * 1000 + getNumOfSails() * 2000;
}
//toString method
public String toString()
{
    NumberFormat nf = NumberFormat.getCurrencyInstance();//use to make the numbers as currency with $ automatically added
    nf.setMinimumFractionDigits(2);//decimal is moved over 2 places
    nf.setMaximumFractionDigits(2);//decimal is moved over 2 places
    return super.toString() + " Num of Sails= " + getNumOfSails() + " Price= " + nf.format(calcPrice());
}
//get the total Cost of all boats printed in arraylist
public int getTotalCost()
{
    int totalCost = 0;
    totalCost += calcPrice();//as calcPrice() is printed, it is therefore added/updated to totalCost
    return totalCost;
}
}

パワーボートクラス

import java.text.NumberFormat;

public class PowerBoat extends Boat
{
//attributes
int sizeOfEngine; //holds the engine size

//default constructor
public PowerBoat()
{
    sizeOfEngine = 5;
}
//parameterized constructor
public PowerBoat(String _color, int _lengthBoat, int _sizeOfEngine)
{
    super(_color, _lengthBoat);
    setSizeOfEngine(_sizeOfEngine);
}
//mutator method for sizeOfEngine
public boolean setSizeOfEngine(int _sizeOfEngine)
{
    if(_sizeOfEngine >= 1 && _sizeOfEngine <= 350)
    {
        sizeOfEngine = _sizeOfEngine;
        return true;
    }
    else
    {
        System.out.println("ERROR - The size of Engine must be between 1 to 350 inclusively.");
        return false;
    }
}
//accessor for sizeOfEngine
public int getSizeOfEngine()
{
    return sizeOfEngine;
}
//caculates the price of the PowerBoat as follows:
public int calcPrice()
{
    return 5000 + lengthBoat * 300 + getSizeOfEngine() * 20;
}
//toString method for printing the results
public String toString()
{
    NumberFormat nf = NumberFormat.getCurrencyInstance(); //formats all the numbers a currency
    nf.setMinimumFractionDigits(2); //sets the decimal place as a currency
    nf.setMaximumFractionDigits(2); //sets the decimal place as a currency
    return super.toString() + " Engine size= " + getSizeOfEngine() + " Price= " + nf.format(calcPrice());
}
//get the total Cost of all boats printed in arraylist
public int getTotalCost()
{
    int totalCost = 0;
    totalCost += calcPrice();//as calcPrice() is printed, it is therefore added/updated to totalCost
    return totalCost;
}
}

在庫

import java.util.ArrayList;

public class Inventory
{
  public static void main(String [] args)
  {
  //boat objects
     Boat pb1 = new PowerBoat("blue", 22, 60);
     Boat sb1 = new SailBoat("white", 18, 1);
     Boat sb2 = new SailBoat("red", 42, 3);
     Boat pb2 = new PowerBoat("yellow",35, 80);
     Boat pb3 = new PowerBoat("red", 50, 120);
     Boat sb3 = new SailBoat("blue", 33, 2);
     Boat pb4 = new PowerBoat("white", 14, 10);

     ArrayList<Boat> AL = new ArrayList<Boat>();
  //add boat objects to arraylist
     AL.add(pb1);
     AL.add(sb1);
     AL.add(sb2);
     AL.add(pb2);
     AL.add(pb3);
     AL.add(sb3);
     AL.add(pb4);

  //print all boat objects
    System.out.println("Print all boats");
     for(Boat anyBoat : AL)
     {
        System.out.println(anyBoat.toString());
        //System.out.println("Total price of  all boats is " + anyBoat.getTotalCost());
     }
  }
 }
4

2 に答える 2

1
int sumCost = 0;
for(Boat b : AL)
{
  sumCost += b.calcPrice();
}
return sumCost;
于 2013-03-21T01:32:56.727 に答える
0

「ArrayListでtotalCostを取得するにはどうすればよいですか?」
そして、「 ArrayList
に最もコストの高いボートを検索させるにはどうすればよいですか?」
答え:

 int max = 0;
 int totalcost = 0;
 Boat mostExpensiveBoat = null;
 for (Boat anyBoat : AL)
 {
    if (anyBoat instanceof SailBoat) {
        totalcost += anyBoat.calcPrice();
        if (anyBoat.calcPrice() > max) {
            max = anyBoat.calcPrice();
            mostExpensiveBoat = anyBoat;
        }
    }
 }     
 // if not null print out most expensive boat here

注:最大価格が同じになる可能性のあるボートに対応する場合は、配列リストを使用して最も高価なボートを保管してください。

于 2013-03-21T01:34:10.990 に答える