public abstract class GeometricObject {
String color = "white";
boolean filled;
public GeometricObject() {
}
public GeometricObject(String color,boolean filled) {
this.color = color;
this.filled = filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
---------------------------------------------------------------
public class Circle extends GeometricObject implements Comparable{
double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public Circle(double radius,String color,boolean filled) {
this.radius = radius;
this.color = color;
this.filled = filled;
}
public double getArea(){
return Math.PI * radius * radius;
} public double getPerimeter(){
return 2 * radius * Math.PI;
}
}
---------------------------------------------------------------
public class Rectangle extends GeometricObject{
double width;
double height; public Rectangle() {
}
public Rectangle(double width,double height){
this.width = width;
this.height = height;
}
public Rectangle(double width,double height,String color,boolean filled){
this.width = width;
this.height = height;
this.color = color;
this.filled = filled;
}
public double getArea(){
return height * width;
}
public double getPerimeter(){
return 2 * (height + width);
}
}
---------------------------------------------------------
public class Shiyan3{
public static void main(String[] args){
Circle c1=new Circle(3);
Circle c2=new Circle(4);
Rectangle r1=new Rectangle(2.3,4.5);
Rectangle r2=new Rectangle(3.1,5.1);
double result=Sum.sumArea(GeometriObject a[]);//问题出在这个地方,但是我不知道怎么修改。。求教了。
System.out.println(result);
}
}
求大神们帮帮忙,修改了一晚上都不行。小弟在此恭候大神来临!
String color = "white";
boolean filled;
public GeometricObject() {
}
public GeometricObject(String color,boolean filled) {
this.color = color;
this.filled = filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
---------------------------------------------------------------
public class Circle extends GeometricObject implements Comparable{
double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public Circle(double radius,String color,boolean filled) {
this.radius = radius;
this.color = color;
this.filled = filled;
}
public double getArea(){
return Math.PI * radius * radius;
} public double getPerimeter(){
return 2 * radius * Math.PI;
}
}
---------------------------------------------------------------
public class Rectangle extends GeometricObject{
double width;
double height; public Rectangle() {
}
public Rectangle(double width,double height){
this.width = width;
this.height = height;
}
public Rectangle(double width,double height,String color,boolean filled){
this.width = width;
this.height = height;
this.color = color;
this.filled = filled;
}
public double getArea(){
return height * width;
}
public double getPerimeter(){
return 2 * (height + width);
}
}
---------------------------------------------------------
public class Shiyan3{
public static void main(String[] args){
Circle c1=new Circle(3);
Circle c2=new Circle(4);
Rectangle r1=new Rectangle(2.3,4.5);
Rectangle r2=new Rectangle(3.1,5.1);
double result=Sum.sumArea(GeometriObject a[]);//问题出在这个地方,但是我不知道怎么修改。。求教了。
System.out.println(result);
}
}
求大神们帮帮忙,修改了一晚上都不行。小弟在此恭候大神来临!
解决方案 »
- 求助:教学用javaIDE
- Jpcap类抓包控制的问题 急~
- java里怎么取出所有的记录?
- 这个程序为何会出现两处错误提示:The constructor Date(int,int,int) is deprecated 和 The method getYear() from the type Date is de
- 学校录取学生
- ■■数据库结果的排列显示问题■■
- 请教java的数字的格式化问题
- thinking in java中线程例子求教
- 面试遇到一个数学问题...求大神解答
- java下怎么就任意的随机数?random怎么用啊?
- Java JTextarea自动换行时获取行列值
- <求助>Xstream的问题--各位牛人帮帮小妹吧!
Circle c1=new Circle();
Circle c2=new Circle();
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
public static double sumArea(GeometriObject[] a){
GeometriObject [] a={c1,c2,r1,r2};
double sumarea=c1.getArea()+c2.getArea()+r1.getArea()+r2.getArea();
return sumarea;
}
}
不好意思哈,少了一个类。。补上这个还是不行,一样的错误
改为这样:GeometriObject[] a = new GeometriObject[数组长度] ;
double result=Sum.sumArea(a);或者这样
double result=Sum.sumArea(new GeometriObject[数组长度]);
调用方法时,要传实际参数,GeometriObject a[] 只是定义了参数,你把这两个概念混淆了。楼上正解。
形参:主要用于接收,你写一个数据类型+变量名就可以。例如(int a)
实参:主要给形参传递实际参数(一个确切的值)。例如(5)
1. Circle.java 类去掉 implements Comparable
2. 修改Sum.javapublic class Sum {
public static double sumArea(GeometricObject[] a) {
double sumarea = 0.0;
for(GeometricObject obj : a) {
sumarea += obj.getArea();
}
return sumarea;
}
}
3. 修改Shiyan3.javapublic class Shiyan3 {
public static void main(String[] args) {
Circle c1 = new Circle(3);
Circle c2 = new Circle(4);
Rectangle r1 = new Rectangle(2.3, 4.5);
Rectangle r2 = new Rectangle(3.1, 5.1);
GeometricObject[] a = new GeometricObject[4];
a[0] = c1;
a[1] = c2;
a[2] = r1;
a[3] = r2;
double result = Sum.sumArea(a);
System.out.println(result);
}
}
注:建议用List传递