问题是:你说要创建一个人(接口),然后里面有方法对人的属性进行赋值?这怎么可能呢,接口是没有成员变量(属性)的,怎么能赋值?接口里只能有常量。
第二题可以答一下:
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())
}
}
3, 文件名:Three.javapublic class Three {
public static void main(String[] args) {
Student stu = new Student("Zhang San", true, (short)12)
System.out.println("Student name: " + stu.name)
System.out.println("Student is a male?: " + stu.sex)
System.out.println("Student's age: " + stu.age)
stu.work()
stu.study()
Teacher teacher = new Teacher()
teacher.learnMoney()
}
}
abstract class Person{//抽象类Person
protected String name
protected boolean sex
protected short age
protected abstract void work()//work抽象方法
}
interface Learnmoney{//Learnmoney接口
public void learnMoney()
}
interface Study{//Study接口
public void study()
}
class Student extends Person implements Study{//Student类
public void work() {
System.out.println("学生的工作是努力学习")
}
public Student(String name, boolean sex, short age){
super.name = name
super.sex = sex
super.age = age
}
public void study() {
System.out.println("学生正在学习")
}
}
class Teacher extends Person implements Learnmoney{
public void work() {
System.out.println("教师的工作是教书育人")
}
public void learnMoney() {
System.out.println("教师正在赚钱")
}
}
class Docotor extends Person implements Learnmoney{
public void work() {
System.out.println("医生的职责是救死扶伤")
}
public void learnMoney() {
System.out.println("医生正在赚钱")
}
}
------------------------------------
4文件名:Four.java
public class Four {
public static void main(String[] args) {
Rectangle r = new Rectangle(3, 4)
System.out.println("Area is : " + r.area())
System.out.println("Circle is: " + r.circle())
}
}
class Rectangle{
private double width
private double height
public Rectangle(double width, double height){
this.width = width
this.height = height
}
public double circle(){//求周长
return (width + height) * 2
}
public double area(){//求面积
return width * height
}
}
--------------------
5Five.java
public class Five {
public static void main(String[] args) {
AImpl a = new AImpl()
a.paint()
}
}
interface A {
public int method1(int x)
public int method2(int x, int y)
}
class AImpl implements A{
public int method1(int x) {
return (int)Math.pow(x, 5)
}
public int method2(int x, int y) {
return x >y? x: y
}
public void paint(){
int result1 = method1(2)
int result2 = method2(2, 8)
System.out.println("method1(2) = " + result1)
System.out.println("method2(2, 8) = " + result2)
}
}
-----------------------------测试
method1(2) = 32
method2(2, 8) = 8
你的题有很多错误,我给你改了一下。1.设变量i和j的定义如下,试分别计算下列表达式的值:
int i=1double d=1.0
1题 35/4 [8]
2题 46%9+4*4-2 [15]
3题 45+43%5*(23*3%2)[48]
4题 45+45*50%i-- [45]
5题 45+45*50%(i--) [45]
6题 1.5*3+(++d) [6.5]
7题 1.5*3+d++ [5.5]
8题 i+=3/i+3[7]
程序阅读题
1给定如下代码,写出程序运行结果
class Example{
public static void main(String arges[]){
int i=0
do{
System.out.println("doing it for i is:"+i)
}while(--i>0)
System.out.println("finish")
}
}
结果如下:
doing it for i is:0
finish
2 阅读程序段写出执行结果
for(int k=1k<=5k++){
if(k>4)break
System.out.println("k="+k)
}
结果:
k=1
k=2
k=3
k=4
3试写出下列程序段循环的运行结果
int i=1
while(i<10)
if(i++%2==0)
System.out.println(i)
结果:
3
5
7
9
*** 作题
求1!+2!+...+10!
public static void main(String arges[]){
long sum = 0
for(int i = 1i <= 10i++) {
long s = 1
for(int j = 1j <= ij++) {
s *= j
}
sum += s
}
System.out.println("sum = " + sum)
}
求100之内的所有“完数”。完数是指等于它的因子和的数。例如:6=1+2+3,6=1*2*3,则6是一个完数
public class wanshu{
public static void main(String[] args) {
for(int i = 1i <= 100i++) {
if(fun(i)) {
System.out.println(i)
}
}
}
public static boolean fun(int num) {
int sum = 0
for(int i = 1i <numi++) {
if(num % i == 0) {
sum += i
}
}
return num == sum
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)