JAVA lab3 第一题

这篇博客展示了Cat类的定义和测试用例。Cat类包含了猫的属性如名字、毛发类型、尾巴状态、颜色和速度,并提供了相应的方法进行设置和获取。CatTest类用于测试Cat类的功能,包括设置猫的属性并打印,创建不同特性的猫实例,以及模拟猫的睡眠和奔跑行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
CatTest类

// package dome;

import java.awt.*;

/**
 * Title : CatTest.java Description: This class contains the test class for Cat.
 * Copyright : Copyright (c) 2006 ‐ 2021
 * 
 * @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();
        //第二问(2)
        myCat.setName("Napoleon");
        myCat.setSpeed(300);
        System.out.println(myCat.getName()+myCat.getSpeed());
        // myCat.furType = "short";
        // myCat.setTail(true);
        // myCat.setColour(Color.ORANGE);
        // myCat.speed = 300; // in metres per minute
        
        //第三问(3)
        Cat Temp_cat=new Cat("xiaoming","black", true, Color.ORANGE, 300);
        
        
        //第四问(4)
        Cat cat1=new Cat("Tom","short",true,Color.BLACK,500);
        Cat cat2=new Cat("Moggy","long",false,Color.WHITE,400);
        System.out.println("the"+cat1.getName()+"and"+cat1.getfurType()+"of cat1 and call cat1 to run in a straight line for 10 minutes.");
        System.out.println("the"+cat2.getName()+"and"+cat2.getfurType()+"of cat2 and call cat2 to run in a straight line for 5 minutes.");
        //
        myCat.sleep(5);
        int numMetres = myCat.run(10, true);
        System.out.println("I have run " + numMetres + " metres.");
    }
}

Cat类

// package dome;

import java.awt.*;

import javax.swing.plaf.basic.BasicBorders.SplitPaneBorder;

/**
 * Title : Cat.java Description: This class contains the definition of a cat.
 * Copyright : Copyright (c) 2006‐2021
 * 
 * @author Laurissa Tokarchuk
 * @version 1.0
 * @author Paula Fonseca
 * @version 1.4
 */
public class Cat {
    // Declaration of instance variables.
    private String name, furType;
    private boolean tail;
    private Color colour;
    private int speed;

    /**
     * This is the getters and setters;
     * 
     */
    public Cat(){

    }

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






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


    /**
     * 
     */
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    /**
     * 
     * @return
    */
    public String getfurType(){
        return furType;
    }

    public void setfurType(String furType){
        this.furType=furType;
    }
    
    //

    public boolean isTail() {
        return tail;
    }

    public void setTail(boolean tail) {
        this.tail = tail;
    }
    
    //
    //
    public Color getColour() {
        return colour;
    }


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

    //

    public int getSpeed() {
        return speed;
    }

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

    /**
     * This is the sleep method for the cat. It dictates the number of minutes the
     * cat sleeps.
     * 
     * @param duration The number of minutes to sleep.
     */
    public void sleep(int duration) {
        System.out.println("I am sleeping for " + duration + " minutes.");
    }

    

    /**
     * This method allows the cat to run. The distance (in a straight line) the cat
     * runs is dependent on how long the cat runs and whether or not it is running
     * in a zigzag.
     * 
     * @param duration The number of minutes to run.
     * @param zigzag   Whether to run in a zigzag pattern.
     * @return int Number of metres ran.
     */
    public int run(int duration, boolean zigzag) {
        System.out.println("I am running " + (zigzag ? "in a zigzag" : "straight") + " for " + duration + " minutes.");
        int distanceRun = duration * speed; // assuming speed is metres per minute
        if (zigzag) {
            /*
             * When in zigzag, distance is 1/3 of what it would have been if the cat was
             * going straight.
             */
            return distanceRun / 3;
        } else
            return distanceRun;
    }

  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值