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?如何合計ArrayList中所有對象的總成本?
Most Expensive Boat: Color= red Length= 42 Num of Sails= Cost= $48,000.00
如何獲得ArrayList以最高的成本搜索船?
我還沒有真正理解如何在ArrayList中獲得任何東西(除了只在一行代碼中重複打印),而沒有變量存在於測試類本身中。這是我使用的所有三個類的代碼。希望我能更好地瞭解你們如何獲得和使用任何東西(如計算,最高成本,低成本,最便宜等)。
船是繼承(超級) SailBoat和PowerBoat延伸船(子類) 清單(測試類)
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());
}
}
}
遍歷列表。總計成本。還有其他問題嗎? – 2013-03-21 01:13:01
(數組不過是一張紙,你可以把它寫下來,它「沒有」) – 2013-03-21 01:15:05
你遇到的問題是'船'沒有價格的概念,當它應該... – MadProgrammer 2013-03-21 01:17:55