JAVA代码(面向对象程序设计)

}

this.y = y; this.r = r; }

public double GetPerimeter() { double perimeter = 0; perimeter = 2 * r * 3.14; return perimeter; }

public double GetArea() { double area = 0; area = 3.14 * r * r; return area; }

public boolean Inside(double x1, double y1) { boolean b; double d; d = (x1 - this.x) * (x1 - this.x) + (y1 - this.y) * (y1 - this.y); b = (d > r * r) ? false : true; return b; }

Rect(矩形):

//define a class for Rectangles public class Rect extends Shape{

double x1,y1,x2,y2; // left lower angle and right upper angle point define the location double width,length;

public Rect (double x1, double y1, double x2, double y2) { this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; length = this.x2-this.x1; width = this.y2-this.y1; //没有分情况讨论,默认右上角的点的坐标大于左下角的坐标 }

public double GetPerimeter() { double perimeter=0; perimeter = (length+width)*2;

return perimeter; }

public double GetArea() { double area=0; area=length*width; return area; }

public Rect MergeRect(Rect r1) { double u1,v1,u2,v2; u1 = (r1.x1this.x2) ? r1.x2 : this.x2; v2 = (r1.y2>this.y2) ? r1.y2 : this.y2; Rect r2=new Rect(u1,v1,u2,v2); return r2; }

public boolean Inside(double x1, double y1) { boolean b=(x1>=this.x1)&&(x1<=this.x2)&&(y1>=this.y1)&&(y1<=this.y2); return b; } }

Test(测试类):

public class RectCircleTest {

public static void main(String[] args) { Rect r1=null, r2=null; Circle o1=null,o2=null; //调用构造函数先创建两个矩形和两个圆形对象 r1=new Rect(1,1,10,10); r2=new Rect(5,5,20,20); o1=new Circle(3,3,1); o2=new Circle(5,5,2);

int end=r1.compareArea(r2); end=r1.compareArea(o1); end=o2.comparePeri(r1);

} }

4.订单

Goods(商品类):

public class Goods { String num; String name; float price; Goods(String num,String name,float price){ this.num=num; this.name=name; this.price=price; } public String toString(){ String s; s=num+\ 单价:\元 \ return s; } }

订单类(Order):

public class Order { //String no; double TotalPrice; OrderItem item1,item2,item3; Order(OrderItem item1,OrderItem item2,OrderItem item3){ this.item1=item1; this.item2=item2; this.item3=item3; TotalPrice(); } private double TotalPrice(){ TotalPrice=item1.price+item2.price+item3.price; return TotalPrice; } void show(){ String s=\ s=item1.toString()+\总价格为:\元\

System.out.println(s); } }

订单项类(OrderItem):

public class OrderItem { Goods g; int num; double price; OrderItem(Goods g, int num){ this.g=g; this.num=num; price(); } private double price(){ price=g.price*num; if(price>=100) price-=20; return price; } public String toString(){ String s; s=g.toString()+\ 共\件\总价为:\元\ return s; } }

用户类(User):

public class User { String name; Order o;

User(String name, Order o) { this.name=name; this.o=o;

} import java.util.Scanner; 购买(MakeOrder):

public class MakeOrder { public static void main(String[] args) { // TODO Auto-generated method stub Goods good1=new Goods(\果果来啦 南充特产 脆脆香提子\

Goods good2=new Goods(\果果来啦 温馨早餐 包子和甜粥\

Goods good3=new Goods(\松鼠之家 智慧家庭 红魔方冰箱

联系客服:779662525#qq.com(#替换为@)