JAVA LAB3 问题1-答案与解析

在这里插入图片描述

package question1;

import java.awt.Color;

/**
 * Title: Cat.java Description: This class contains the definition of a cat
 * 
 * @author wang
 * @version 1.1
 * 
 */

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

	/**
	 * 
	 * @return String
	 */

	public String getName()
	{
		return name;
	}

	/**
	 * 
	 * @param name
	 */

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

	/**
	 * 
	 * @return String
	 */

	public String getFurType()
	{
		return furType;
	}

	/**
	 * 
	 * @param furType
	 */

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

	/**
	 * 
	 * @return boolean
	 */

	public boolean isTail()
	{
		return tail;
	}

	/**
	 * 
	 * @param tail
	 */

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

	/**
	 * 
	 * @return Color
	 */

	public Color getColour()
	{
		return colour;
	}

	/**
	 * 
	 * @param colour
	 */

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

	/**
	 * 
	 * @return int
	 */

	public int getSpeed()
	{
		return speed;
	}

	/**
	 * 
	 * @param speed
	 */

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

	/**
	 * 
	 * @param name
	 * @param furType
	 * @param tail
	 * @param colour
	 * @param speed
	 */

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

	}

	/**
	 * Description : this is a sleep method of cat
	 * 
	 * @param duration the number of minutes to sleep.
	 */

	public void sleep(int duration)
	{
		System.out.println("I am sleep for" + duration + " minutes");

	}

	/**
	 * 
	 * @param duration
	 * @param zigzag
	 * @return int
	 */
	public int run(int duration, boolean zigzag)
	{
		System.out.println("i am running" + (zigzag ? " in zigzag" : "straight") + " for " + duration + " minutes");
		int distanceRun = duration * speed;
		if (zigzag)
		{
			return distanceRun = distanceRun / 3;
		} else

			return distanceRun;

	}

}
package question1;

import java.awt.Color;

public class CatTest
{
	public static void main(String[] args)
	{
// question 1-3 the constructor to initialize all  instance variable	
		Cat myCat = new Cat("Napolean", "short", true, Color.ORANGE, 300);

		myCat.sleep(5);
		int numMeters = myCat.run(10, true);
		System.out.println("i have run " + numMeters + " meters");

//question1-2 print out the name and speed of myCat
		System.out.println(myCat.getName());
		System.out.println(myCat.getSpeed());

//question 1-4 create cat1 and cat2
		Cat cat1 = new Cat("Tom", "short", true, Color.BLACK, 500);
		Cat cat2 = new Cat("Moggy", "long", false, Color.WHITE, 400);
		System.out.println(cat1.getName());
		System.out.println(cat1.getColour());
		System.out.println(cat2.getName());
		System.out.println(cat2.getColour());
// note: the RGB of black is [r=0,g=0,b=0] the RGB of white is [r=255,g=255,b=255]

		System.out.println("i have run " + cat1.run(10, false) + " meters");
		System.out.println("i have run " + cat2.run(5, true) + " meters");

	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值