编写一个java小程序,各位大神,很急啊,拜托了,
编写Pig类继承Animal编写Dog类继承Animal创建6个元素的数组,分别存放2个Pig、2个Dog和2个Cat对象将数组元素按重量排序,然后打印各对象的字符串表示...
编写Pig类继承Animal
编写Dog类继承Animal
创建6个元素的数组,分别存放2个Pig、2个Dog和2个Cat对象
将数组元素按重量排序,然后打印各对象的字符串表示(toString) 展开
编写Dog类继承Animal
创建6个元素的数组,分别存放2个Pig、2个Dog和2个Cat对象
将数组元素按重量排序,然后打印各对象的字符串表示(toString) 展开
4个回答
展开全部
public class Animal { //基类
char var; //品种
private String Aniname; //动物名字
private int weight; //动物体重
public Animal(){} //以下重载了构造函数
public Animal(char var,String Aniname,int weight){
this.var = var;
this.Aniname = Aniname;
this.weight = weight;
}
public Animal(String Aniname,int weight){
this.Aniname = Aniname;
this.weight = weight;
}
public int GetWeight(){ //分别返回体重,动物名字和品种
return weight;
}
public String Getname(){
return Aniname;
}
public char Getvar(){
return var;
}
}
class Dog extends Animal{ //狗类, 继承基类
public Dog(String Aniname, int weight) { //以下全使用super关键字调用父类方法
super(Aniname, weight);
super.var = '狗';
}
public int GetWeight(){
return super.GetWeight();
}
public String Aniname(){
return super.Getname();
}
}
class Pig extends Animal{ //猪类,和狗类基本一样,就品种不同
public Pig(String Aniname,int weight){
super(Aniname,weight);
super.var = '猪';
}
public int GetWeight(){
return super.GetWeight();
}
public String Aniname(){
return super.Getname();
}
}
public static void main(String[] args){
Animal[] ani = new Animal[6]; //创建对象数组
Animal temp = new Animal(); //缓存
ani[0] = new Dog("旺财",30); //初始化
ani[1] = new Pig("啊市",150);
ani[2] = new Animal('猫',"喵喵",20);
ani[3] = new Pig("啊广",100);
ani[4] = new Animal('猫',"啊贵",21);
ani[5] = new Dog("旺旺",45);
for(int i = 0; i < ani.length; i++){ //排序,用的是冒泡排序法
for(int j = 0; j < i; j++){
if(ani[j].GetWeight() > ani[i].GetWeight()){
temp = ani[j];
ani[j] = ani[i];
ani[i] = temp;
}
}
}
for(int i = 0; i < ani.length; i++){ //打印结果
System.out.print("品种:" + ani[i].Getvar() + " ");
System.out.print("名字:" + ani[i].Getname() + " ");
System.out.print("体重:" + ani[i].GetWeight());
System.out.println();
}
}
不太明白你的意思,我把cat放在了Animal类里面, 然后继承出1个dog类和一个pig类 你自己看着改一下吧
char var; //品种
private String Aniname; //动物名字
private int weight; //动物体重
public Animal(){} //以下重载了构造函数
public Animal(char var,String Aniname,int weight){
this.var = var;
this.Aniname = Aniname;
this.weight = weight;
}
public Animal(String Aniname,int weight){
this.Aniname = Aniname;
this.weight = weight;
}
public int GetWeight(){ //分别返回体重,动物名字和品种
return weight;
}
public String Getname(){
return Aniname;
}
public char Getvar(){
return var;
}
}
class Dog extends Animal{ //狗类, 继承基类
public Dog(String Aniname, int weight) { //以下全使用super关键字调用父类方法
super(Aniname, weight);
super.var = '狗';
}
public int GetWeight(){
return super.GetWeight();
}
public String Aniname(){
return super.Getname();
}
}
class Pig extends Animal{ //猪类,和狗类基本一样,就品种不同
public Pig(String Aniname,int weight){
super(Aniname,weight);
super.var = '猪';
}
public int GetWeight(){
return super.GetWeight();
}
public String Aniname(){
return super.Getname();
}
}
public static void main(String[] args){
Animal[] ani = new Animal[6]; //创建对象数组
Animal temp = new Animal(); //缓存
ani[0] = new Dog("旺财",30); //初始化
ani[1] = new Pig("啊市",150);
ani[2] = new Animal('猫',"喵喵",20);
ani[3] = new Pig("啊广",100);
ani[4] = new Animal('猫',"啊贵",21);
ani[5] = new Dog("旺旺",45);
for(int i = 0; i < ani.length; i++){ //排序,用的是冒泡排序法
for(int j = 0; j < i; j++){
if(ani[j].GetWeight() > ani[i].GetWeight()){
temp = ani[j];
ani[j] = ani[i];
ani[i] = temp;
}
}
}
for(int i = 0; i < ani.length; i++){ //打印结果
System.out.print("品种:" + ani[i].Getvar() + " ");
System.out.print("名字:" + ani[i].Getname() + " ");
System.out.print("体重:" + ani[i].GetWeight());
System.out.println();
}
}
不太明白你的意思,我把cat放在了Animal类里面, 然后继承出1个dog类和一个pig类 你自己看着改一下吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我也没弄明白你的问题,下面代码是按个人理解写的,你看看不符题意的自己再改一下吧
接口
public interface Animal {
public int getWeight();
}
下面是三个实现类,Animal的实现类
class Dog implements Animal {
private int weight;
public Dog(int weight) {
this.weight = weight;
}
public int getWeight() {
return this.weight;
}
public String toString() {
return "The dog weight:" + this.weight;
}
}
class Pig implements Animal {
private int weight;
public Pig(int weight) {
this.weight = weight;
}
public int getWeight() {
return this.weight;
}
public String toString() {
return "The pig weight:" + this.weight;
}
}
class Cat implements Animal {
private int weight;
public Cat(int weight) {
this.weight = weight;
}
public int getWeight() {
return this.weight;
}
public String toString() {
return "The cat weight:" + this.weight;
}
}
调用方法
public static void main(String[] args) {
Animal animal[] = { new Dog(12), new Dog(16), new Pig(120),
new Pig(160), new Cat(5), new Cat(3) };
//排序
for (int i = 0; i < animal.length-1; i++) {
for (int j = i+1; j < animal.length; j++) {
if(animal[i].getWeight()>animal[j].getWeight()){
Animal temp = animal[i];
animal[i] = animal[j];
animal[j] = temp;
}
}
}
//输出
for (int i = 0; i < animal.length; i++) {
System.out.println(animal[i]);
}
}
接口
public interface Animal {
public int getWeight();
}
下面是三个实现类,Animal的实现类
class Dog implements Animal {
private int weight;
public Dog(int weight) {
this.weight = weight;
}
public int getWeight() {
return this.weight;
}
public String toString() {
return "The dog weight:" + this.weight;
}
}
class Pig implements Animal {
private int weight;
public Pig(int weight) {
this.weight = weight;
}
public int getWeight() {
return this.weight;
}
public String toString() {
return "The pig weight:" + this.weight;
}
}
class Cat implements Animal {
private int weight;
public Cat(int weight) {
this.weight = weight;
}
public int getWeight() {
return this.weight;
}
public String toString() {
return "The cat weight:" + this.weight;
}
}
调用方法
public static void main(String[] args) {
Animal animal[] = { new Dog(12), new Dog(16), new Pig(120),
new Pig(160), new Cat(5), new Cat(3) };
//排序
for (int i = 0; i < animal.length-1; i++) {
for (int j = i+1; j < animal.length; j++) {
if(animal[i].getWeight()>animal[j].getWeight()){
Animal temp = animal[i];
animal[i] = animal[j];
animal[j] = temp;
}
}
}
//输出
for (int i = 0; i < animal.length; i++) {
System.out.println(animal[i]);
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-07-10
展开全部
数组元素按重量排序?是什么意思,是不是对象有一个重量的属性,还是什么,求楼主说明白点。。打印各对象的字符串表示又是啥意思(对象toString打出来的东西没什么意义,除非在类中重写toString方法)?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
额~~这个嘛,很简单。网上一搜,一大把呀~~
你专业是怎么学的呀?。。。。。。交作业的时候,才写呀?
你专业是怎么学的呀?。。。。。。交作业的时候,才写呀?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询