2.5(杨.丹尼尔梁英文第11版P537:*13.12)(几何对象的面积求和)写一个方法,将数组中所有几何对象的面积求和。
方法签名是:
公共静态双求和区域(几何对象【】a)
编写一个测试程序,创建一个包含四个对象(两个圆和两个矩形)的数组,并使用sumArea方法计算它们的总面积。
package six;
import java.util.Scanner;
public class jihe {
public static void main(String[] args) {
class Circle{
public double radius,k;
public Circle(double radius,double k){
this.radius=radius;
this.k=k;
}
public double getRadius(){
return radius;
}
public double getK(){
return k;
}
public double Area() {
if(k==0) {
return Math.PI*radius*radius;}
else {
return radius*k;
}
}
}
Scanner scanner=new Scanner(System.in);
double[] a=new double[8];
int i,j;
double s=0;
for(i=0;i<8;i++){
a[i]=scanner.nextDouble();
}
for(j=0,i=0;j<4&&i<8;j++,i+=2) {
Circle circlei=new Circle(a[i],a[i+1]);
s+=circlei.Area();
}
System.out.println("总面积为:"+s);
}
}
2.6(由丹尼尔梁英文第11版P536:*13.11)(八边形类)编写一个名为八边形的类,它扩展了地理对象,实现了可比较和可克隆接口。
假设八边形的八条边长度相等。面积可以用下面的公式计算:
面积=(2+4/22)×边×边
写一个测试程序,创建一个八边形对象,边值为5,并显示它的面积和周长。使用clone方法创建一个新对象,并使用compareTo方法比较两个对象。
package six;
import java.util.Scanner;
interface Comparable{
int compare(EightAngle eightAngle);
}
interface Kelong{
void clone(GeometricObject geometricObject);
}
abstract class GeometricObject{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;public GeometricObject() {
dateCreated = new java.util.Date();
}public GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}public String getColor() {
return color;
}public void setColor(String color) {
this.color = color;
}public boolean isFilled() {
return filled;
}public void setFilled(boolean filled) {
this.filled = filled;
}public java.util.Date getDateCreated() {
return dateCreated;
}public String toString() {
return "color: " + color + " and filled: " + filled;
}
abstract double getArea();
abstract double getPerimeter();
}
class EightAngle extends GeometricObject{
public double side;
public EightAngle(double side) {
this.side=side;
}
public double getArea() {
return (2+4.0/22)*side*side;
}
public double getPermire() {
return 8*side;
}
public int compare(EightAngle other) {
if(this.getArea()>other.getArea()) {
return 1;
} else if(this.getArea()<other.getArea()) {
return -1;
} else{
return 0;
}
}
public void clone(EightAngle other){
other.side=this.side;
}
}
public class eightbian {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
double a=scanner.nextDouble();
double b=scanner.nextDouble();
EightAngle eightAngle1=new EightAngle(a);
EightAngle eightAngle2=new EightAngle(b);
int r=eightAngle1.compare(eightAngle2);
if(r>0) {
System.out.println("eightAngle1面积更大");
}
else if(r==0) {
System.out.println("两个面积一样大");
}
else {
System.out.println("eightAngle2面积更大");
}
System.out.println("面积1为:"+eightAngle1.getArea());
System.out.println("周长1为:"+eightAngle1.getArea());
System.out.println("面积2为:"+eightAngle2.getArea());
System.out.println("周长2为:"+eightAngle2.getArea());
eightAngle1.clone(eightAngle2);
System.out.println("将eightAngle1克隆给eightAngle2,此时eightAngle2的面积和周长分别为:");
System.out.println("面积2为:"+eightAngle2.getArea());
System.out.println("周长2为:"+eightAngle2.getArea());
System.out.println("将eightAngle1克隆给eightAngle2,此时eightAngle1的面积和周长分别为:");
System.out.println("面积2为:"+eightAngle1.getArea());
System.out.println("周长2为:"+eightAngle1.getArea());
}
}
其实克隆就是把要克隆的数据赋给被克隆的数据即可
设计一个Triangle类继承自GeometricObject类
设计一个名为Triangle的类来扩展GeometricObject类。该类包括:
三个名为side1、side2和side3的double数据域表示这个三角形的三条边,它们的默认值是1.0。
一个无参构造方法创建默认的三角形。
一个能创建带制定side1、side2和side3的三角形的构造方法。
所有三个数据域的访问器方法。
一个名为getArea()的方法返回这个三角形的面积。
一个名为getPerimeter()的方法返回这个三角形的周长。
一个名为toString()的方法返回这个三角形的字符串描述,返回的信息包括三角形的颜色、是否填充和三条边的边长。
package six;
import java.util.Scanner;
//GeometricObject类
public abstract class GeometricObject{
public static void main(String[] args) {
class Triangle extends GeometricObject{
public double side1,side2,side3;
public Triangle(){
this.side1=1.0;
this.side2=1.0;
this.side3=1.0;
}
public Triangle(double newSide1,double newSide2,double newSide3){
this.side1=newSide1;
this.side2=newSide2;
this.side3=newSide3;
}
public void setSide1(double newSide1){
this.side1=newSide1;
}
public double getSide1(){
return side1;
}
public void setSide2(double newSide2){
this.side2=newSide2;
}
public double getSide2(){
return side2;
}
public void setSide3(double newSide3){
this.side3=newSide3;
}
public double getSide3(){
return side3;
}
public double getArea(){
double l=(side1+side2+side3)/2;
return Math.sqrt(l*(l-side1)*(l-side2)*(l-side3));
}
public double getPerimeter(){
return side1+side2+side3;
}
public String toString(){
return "color: "+getColor()+" and filled: "+isFilled()+"\n"+"Triangle: side1="+side1+" side2="+side2+" side3="+side3;
}
}
Scanner input = new Scanner(System.in);
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
Triangle triangle = new Triangle(side1, side2, side3);
String color = input.next();
triangle.setColor(color);
boolean filled = input.nextBoolean();
triangle.setFilled(filled);
System.out.println("The area is " + triangle.getArea());
System.out.println("The perimeter is " + triangle.getPerimeter());
System.out.println(triangle);
}private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject() {
dateCreated = new java.util.Date();
}
public GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "color: " + color + " and filled: " + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}