EBU4201(2021/22) Lab3 Q1

 附:第(i)问中需要进行修改的源代码

import java.awt.*;

/**
 * Title      : CatTest.java
 * Description: This class contains the test class for Cat.
 * Copyright  : Copyright (c) 2006 - 2022
 * @author  Laurissa Tokarchuk
 * @version 1.0
 * @author  Paula Fonseca
 * @version 1.4
 */
public class CatTest {
    public static void main(String[] args) {
        Cat myCat = new Cat();
 myCat.name = "Napoleon";
        myCat.furType = "short";
        myCat.tail = true;
        myCat.colour = Color.ORANGE;
        myCat.speed = 300; // in metres per minute
        
 myCat.sleep(5);
        int numMetres = myCat.run(10, true);
 System.out.println("I have run " + numMetres + " metres."); 
    }
}

 java程序

代码1:Cat.java

//EBU4201(2021/22) lab3- question1- Cat.java
package Q1;
import java.awt.Color;

public class Cat {
    private String name;
    private int speed;
    private Color colour;
    private boolean tail;
    private String furType;

    public Cat(String name,int speed,Color colour,boolean tail,String furType){
        this.name = name;
        this.speed = speed;
        this.colour = colour;
        this.tail = tail;
        this.furType = furType;
    }

    public String  getName() {
        return name;
    }
    public void setName(String name){
        this.name = name;
    }

    public int getSpeed(){
        return speed ;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public  Color getColour(){
        return colour;
    }
    public void setColour(Color colour){
        this.colour = colour;
    }

    public boolean isTail() {
        return tail;
    }
    public void setTail(boolean tail){
        this.tail = tail;
    }

    public  String getFurType(){
        return furType;
    }
    public void setFurType(String furType){
        this.furType = furType;
    }

    public void sleep(int time){
        //System.out.println("I am sleep for "+time+" minutes");
        System.out.println(" sleeps "+time+" minutes");
    }
    public int run(int time,boolean zigzag){
        int distanceRun = time*speed;
        if(zigzag){distanceRun = distanceRun/3;}
        //System.out.print(" runs "+distanceRun+" meters in "+(zigzag?"a zigzag ":"straight ")+"for ");
        System.out.print(" runs "+time+" minutes in "+(zigzag?"a zigzag ":"straight ")+"and runs ");
        return distanceRun;
    }
}

代码2:CatTest.java

//EBU4201(2021/22) lab3- question1- CatTest.java
package Q1;
import java.awt.Color;

public class CatTest {
    public static void main(String[] args) {

        System.out.println(" ");
        Cat myCat = new Cat("Napoleon", 300,Color.ORANGE,true, "short");
//ii assign values in CatTest and print out the name and speed
        System.out.println("in question(ii): ");
        System.out.println("    name: "+myCat.getName());
        System.out.println("    speed: "+myCat.getSpeed());//print out the name and speed of the cat
//iii rewrite in new constructor
        System.out.println("in question(iii): ");
    //class sleep in Cat.java,the cat sleeps 5 min, print out the time of sleeping
        System.out.print("    "+myCat.getName());
        myCat.sleep(5);
    //class run in Cat.java,10 min and zigzag,print out distance of running
        System.out.print("    "+myCat.getName());
        int numMeters = myCat.run(10,true);
        System.out.println(numMeters+" meters");
//iv set two objects and print out name、colour and distance
        System.out.println("in question(iv): ");
    //create two Cat objects cat1 and cat2
        Cat cat1 = new Cat("Tom",500, Color.BLACK,true,"short");
        Cat cat2 = new Cat("Moggy",400,Color.WHITE,false,"long");
    //Cat1 Tom runs 10 minutes in straight and runs 5000 meters
        System.out.println("    "+cat1.getName()+"‘s colour is "+cat1.getColour());
        System.out.print("    "+cat1.getName());
        System.out.println(cat1.run(10,false)+" meters");
    //Moggy runs 5 minutes in a zigzag and runs 666 meters
        System.out.println("    "+cat2.getName()+"‘s colour is "+cat2.getColour());
        System.out.print("    "+cat2.getName());
        System.out.println(cat2.run(5,true)+" meters");
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值