java語言程式設計 第十版(基礎篇)9..1-2
阿新 • • 發佈:2018-12-18
9.1
public class J9_1 { public static void main(String[] args) { // TODO Auto-generated method stub Rectangle a = new Rectangle(3.5,35.9); System.out.println("width:"+a.width+" height "+a.height+" Area: "+a.getArea()+" Perimeter: "+a.getPerimeter()); Rectangle b = new Rectangle(4,40); System.out.println(" width1 "+b.width+" height1 "+b.height+"Area1:"+b.getArea()+" Perimeter1 "+b.getPerimeter()); } } class Rectangle{ double width=1; double height=1; Rectangle(){ } Rectangle(double newWidth,double newHeight){ width= newWidth; height= newHeight; } double getArea() { return width*height; } double getPerimeter() { return width*2+height*2; } }
9.2
public class J9_2 { public static void main(String []args) { Stock a = new Stock("ORCL","Oracle Corporation"); a.previousClosingPrice=34.5; a.currentPrice=34.35; System.out.printf("The change percent is %5.2f%%\n",a.getChangePercent() * 100); } } class Stock{ String symbol; String name; double previousClosingPrice; double currentPrice; Stock(String newSymbol,String newName){ symbol = newSymbol; name = newName; } double getChangePercent() { double changePercent = (currentPrice - previousClosingPrice) / previousClosingPrice; return changePercent; } }