佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 959|回复: 3

JAVA Programming问题..

[复制链接]
发表于 13-11-2006 10:25 PM | 显示全部楼层 |阅读模式
我没达到要求吗?要怎么改?
Define a class called Animal with the following data attributes:
Desc:array of 20 characters
Height: float
Weight: Float

Define a class Bird that inherits from Animal class above with the following attributes:
Wingspan:float

You program should be able to store up to 10 BirdRec records in an array called BirdsArray.

Your program should at least perform the below operations:
-Add new bird records into BirdsArray
-Display all Birds' records
-Display the bird details with the largest winspan
-Display the bird details with the smallest winspan

Requirement:
Your program should apply the concepts of class, array of objects and inheritance.

答案:
class Animal {
public char[] Desc = new char[20];
public float Height;
public float Weight;
public Animal() {
this(new char[1], 0.0f, 0.0f);
}
public Animal(char[] desc, float height, float weight) {
this.Desc = desc;
this.Height = height;
this.Weight = weight;
}
}

        
class Bird extends Animal {
public float wingspan;
public Bird() {
this(0.0f, new char[1], 0.0f, 0.0f);
}
public Bird(float ws) {
this(ws, new char[1], 0.0f, 0.0f);
}
public Bird(float ws, char[] desc, float height, float weight) {
super(desc, height, weight);
wingspan = ws;
}
}

public class QBird {
public Bird[] birds;
public int pointer;

public QBird() {
birds = new Bird[10];
pointer = 0;
}

public synchronized boolean addBird(Bird newBird) {
if (pointer >= birds.length) return false;
birds[pointer++] = newBird;
return true;
}

public void displayBird(Bird b) {
System.out.print("\nBird description: ");
System.out.print(b.Desc);
System.out.println("\nBird Height: " + b.Height + "\nBird Weight: " + b.Weight +
"\nBird wingspan: " + b.wingspan);
}

public void displayLargestWingspan() {
int p = 0;
for (int i = 1; i < birds.length && i < pointer; ++i) if (birds.wingspan > birds[p].wingspan) p = i;
System.out.println("Largest Wingspan - " + birds[p].wingspan + ":");
displayBird(birds[p]);
}

public void displaySmallestWingspan() {
int p = 0;
for (int i = 1; i < birds.length && i < pointer; ++i) if (birds.wingspan < birds[p].wingspan) p = i;
System.out.println("Smallest Wingspan - " + birds[p].wingspan + ":");
displayBird(birds[p]);
}



public void displayBirds() {
for (int i = 0; i <birds.length && i < pointer; ++i) displayBird(birds);
}

public static void main(String[] args) {
QBird p = new QBird();

p.addBird(new Bird(5, "Duck".toCharArray(), 10.0f, 20.0f));
p.addBird(new Bird(10, "Swan".toCharArray(),20.0f, 30.0f));
p.addBird(new Bird(80, "Phenix".toCharArray(), 70.0f, 90.0f));
p.addBird(new Bird(200, "Peacock".toCharArray(), 30.0f, 40.0f));
p.displayBirds();
p.displayLargestWingspan();
p.displaySmallestWingspan();
}
}
回复

使用道具 举报


ADVERTISEMENT

发表于 22-11-2006 04:15 PM | 显示全部楼层
你确定这是你自己写的程式吗?讲师给然后吩咐你们改还是 rejected?

因为有几个不应该存在的 errors,换言之 compilation errors.
回复

使用道具 举报

发表于 24-11-2006 01:59 AM | 显示全部楼层
看起来好像还没完成呢。。。。寻找最大的winspan和最小的winspan的流程好像不太对的感觉。
回复

使用道具 举报

发表于 24-11-2006 03:10 PM | 显示全部楼层
原帖由 黄sir 于 22-11-2006 04:15 PM 发表
你确定这是你自己写的程式吗?讲师给然后吩咐你们改还是 rejected?

因为有几个不应该存在的 errors,换言之 compilation errors.

原帖由 jllp 于 24-11-2006 01:59 AM 发表
看起来好像还没完成呢。。。。寻找最大的winspan和最小的winspan的流程好像不太对的感觉。



对啊,就是嘛,楼主怎么问了就闪?不过应该不重要了吧,due date 应该到了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT


本周最热论坛帖子本周最热论坛帖子

ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 26-8-2025 05:01 PM , Processed in 0.132962 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表