本文主要是介绍JAVA lab3 第一题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 minuteif (zigzag) {/** When in zigzag, distance is 1/3 of what it would have been if the cat was* going straight.*/return distanceRun / 3;} elsereturn distanceRun;}}
这篇关于JAVA lab3 第一题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!