📄 wallpaper.java
字号:
/*
* Wallpaper.java E.L. 2001-05-17
*/
class Wallpaper {
private static final double limit = 0.02; // limit for one more width
private String name; // for identification purposes
private double price; // price per roll
private double lengthPerRoll; // meter
private double widthPerRoll; // meter
public Wallpaper(String initName, double initPrice, double initLengthPerRoll,
double initWidthPerRoll) {
name = initName;
price = initPrice;
lengthPerRoll = initLengthPerRoll;
widthPerRoll = initWidthPerRoll;
}
public String getName() {
return name;
}
public double getPricePerRoll() {
return price;
}
public double getLengthPerRoll() {
return lengthPerRoll;
}
public double getWidthPerRoll() {
return widthPerRoll;
}
/*
* This method calculates the number of rolls needed to paper a surface.
*/
public int getNoOfRolls(Surface aSurface) {
double lengthSurface = aSurface.getLength();
double heightSurface = aSurface.getWidth();
/* calculate the number of heights */
int noOfHeights = (int) (lengthSurface / widthPerRoll);
double remnant = lengthSurface % widthPerRoll;
if (remnant >= limit) noOfHeights++;
/* calculate the number of rolls */
int noOfRolls;
int noOfHeightsPerRoll = (int) (lengthPerRoll / heightSurface);
if (noOfHeightsPerRoll > 0) {
noOfRolls = noOfHeights / noOfHeightsPerRoll;
remnant = noOfHeights % noOfHeightsPerRoll;
if (remnant >= limit) noOfRolls++;
} else { // the roll is shorter than one height (rarely!)
double totalNoOfMeters = noOfHeights * heightSurface;
noOfRolls = (int) (totalNoOfMeters / lengthPerRoll);
if (totalNoOfMeters % lengthPerRoll >= limit) noOfRolls++;
}
return noOfRolls;
}
public double getTotalPrice(Surface aSurface) {
return getNoOfRolls(aSurface) * price;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -