JAVA语言程序设计两道练习题。谢谢!

static void main(String[] args) {Circle c = new Circle(5);Rectangle r = new Rectangle(3,4);Pillar p1 = new Pillar(c,6);Pillar p2 = new Pillar(r,6);System.out.println("圆的体积:"+p1.getVolume()+"\u77e9形的体积:"+p2.getVolume());}}第一题Person接口import java....
JAVA语言程序设计两道练习题。谢谢!
第一题
Person接口
import java.util.Date;public interface Person { public void setData(String sID, String speciality, String name, String sex, Date birthday); public String getData(); }Student实现类
import java.util.Date;public class Student implements Person { private String sID; private String speciality; private String name; private String sex; private Date birthday; @Override public void setData(String sID, String speciality, String name, String sex, Date birthday) { this.sID = sID; this.speciality = speciality; this.name = name; this.sex = sex; this.birthday = birthday; } @Override public String getData() { return "Student [sID=" + sID + ", speciality=" + speciality + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + "]"; } }2013-04-16
第一题有问题:1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。
问题是:你说要创建一个人(接口),然后里面有方法对人的属性进行赋值?这怎么可能呢,接口是没有成员变量(属性)的,怎么能赋值?接口里只能有常量。

第二题可以答一下:
package pillar;
public class Pillar { private Geometry buttom;
private double height;
public Pillar() {
// TODO Auto-generated constructor stub
}
public Pillar(Geometry button,double height){
this.buttom = button;
this.height = height;
}
public double getVolume(){
return this.buttom.getArea()*height;
}
public Geometry getButtom() {
return buttom;
}
public void setButtom(Geometry buttom) {
this.buttom = buttom;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}

}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public interface Geometry { double getArea();
}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class Circle implements Geometry { private double r;
public Circle() {
// TODO Auto-generated constructor stub
}

public Circle(double r) {
this.r = r;
}

public double getArea() { return Math.PI*r*r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}

}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class Rectangle implements Geometry { private double width;
private double height;

public Rectangle() {
// TODO Auto-generated constructor stub
}

public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

public double getArea() { return this.width*this.height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}

}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class TestPillar {
/** * @param args
*/
public static void main(String[] args) {
Circle c = new Circle(5);
Rectangle r = new Rectangle(3,4);
Pillar p1 = new Pillar(c,6);
Pillar p2 = new Pillar(r,6);
System.out.println("圆的体积:"+p1.getVolume()+"\t矩形的体积:"+p2.getVolume());
}

}2013-04-16
java书上都有的2013-04-16
mengvlog 阅读 8 次 更新于 2025-07-20 04:38:15 我来答关注问题0
  • public interface Person { public void setData(String sID, String speciality, String name, String sex, Date birthday); public String getData(); }Student实现类import java.util.Date;public class Student implements Person { private String sID; private String speciality; pr...

  • false 2 3-1答:一个java程序只能有一个主类是public的,辅助类不能是public的,以下我认为有两处错误一,public static void main(string[]args)[]与args间少一个空格,二,把System.out.println("Am I wrong?");后的}放到最后。保存名字为主类的名字,必须一样包括大小写。3-4答:2,5错...

  •  康运浩VD 您好!我想要《Java语言程序设计基础篇》第六版的练习题和编程题答案,谢谢了.

    2. Some measures___to protect wildlife resources.A. are taking B. are taken C. are being taken D. being taken 解此题时首先要注意到词组take measures to do sth.,由于题干中把宾语measures提前,应该使用被动语态;句子的意思是“正在采取一些措施去保护野生资源”。在表达现在进行时的被动...

  •  chenyu398 几道JAVA语言程序设计题

    1-4:has-a的关系、contains-a的关系、is-a的关系;“班级”与“学生”是has-a的关系;“学生”与“大学生”是is-a的关系。1-5:正确,把两个看做是类,“清华大学”包含了“大学”的全部成员,“大学”能接受的信息,“清华大学”一样能接受到。1-6:过程:面向对象的分析、面向对象的设计...

  •  百度网友a3889a4 高一会考很简单的JAVA语言编程,求大神帮忙!!!给经验。。一共10题,求帮助

    先给你个第七题吧 我以前做过的 public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.err.println("输入");int y = sc.nextInt();f(y/2+1);}private static void f(int x){int f1 =1,f2=1,i=3;if(x==1)System.out.print(f1);if(x==2)...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部