package question1;import java.awt.Color;/**
* Title: Cat.java Description: This class contains the definition of a cat
*
* @author wang
* @version 1.1
*
*/publicclassCat{private String name, furType;privateboolean tail;private Color colour;privateint speed;/**
*
* @return String
*/public String getName(){return name;}/**
*
* @param name
*/publicvoidsetName(String name){this.name = name;}/**
*
* @return String
*/public String getFurType(){return furType;}/**
*
* @param furType
*/publicvoidsetFurType(String furType){this.furType = furType;}/**
*
* @return boolean
*/publicbooleanisTail(){return tail;}/**
*
* @param tail
*/publicvoidsetTail(boolean tail){this.tail = tail;}/**
*
* @return Color
*/public Color getColour(){return colour;}/**
*
* @param colour
*/publicvoidsetColour(Color colour){this.colour = colour;}/**
*
* @return int
*/publicintgetSpeed(){return speed;}/**
*
* @param speed
*/publicvoidsetSpeed(int speed){this.speed = speed;}/**
*
* @param name
* @param furType
* @param tail
* @param colour
* @param speed
*/publicCat(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.
*/publicvoidsleep(int duration){
System.out.println("I am sleep for"+ duration +" minutes");}/**
*
* @param duration
* @param zigzag
* @return int
*/publicintrun(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;}elsereturn distanceRun;}}
package question1;import java.awt.Color;publicclassCatTest{publicstaticvoidmain(String[] args){// question 1-3 the constructor to initialize all instance variable
Cat myCat =newCat("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 =newCat("Tom","short",true, Color.BLACK,500);
Cat cat2 =newCat("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");}}