• sone07
    了解作者
  • Java
    开发工具
  • 1KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 1
    下载次数
  • 2017-12-07 11:27
    上传日期
创建java中main函数,构造函数.适合java新手学习
java.rar
  • Dog.java
    3.4KB
  • DogTest.java
    1.4KB
内容介绍
import java.awt.*; /** * Title : Dog.java * Description: This class contains the definition of a dog. * Copyright : Copyright (c) 2006-2017 * @author Laurissa Tokarchuk * @version 1.0 * @author Paula Fonseca * @version 1.2 * @author Lejin Bai * @version 1.4 */ public class Dog { // Declaration of instance variables. private String name, furType; private boolean tail; private Color colour; private int speed; private int age; /** This is the constructor for the Counter. */ public Dog(){ } /** This is the constructor for the Counter. * It initialised the variables. * @param name The name of the dog. * @param furType The type of the fur of the dog. * @param tail The tail of the dog. * @param colour The colour of the dog. * @param speed The speed of the dog. */ public Dog(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; } /** This is the sleep method for the dog. It dictates the number of * minutes the dog 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 dog to run. The distance (in a straight line) * the dog runs is dependent on how long the dog 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 dog was going straight. */ return distanceRun/3; } else return distanceRun; } /* These are setters, they change the value of the corresponding variable */ public void setName(String name) { this.name = name; } public void setSpeed(int speed) { this.speed = speed; } public void setColour(Color colour) { this.colour = colour; } public void setTail(boolean tail) { this.tail = tail; } public void setFurType(boolean tail) { this.furType = furType; } public void setAge(int age) { this.age = age; } /* These are getters, they can visit the value of the corresponding variable */ public String getName() { return name; } public int getSpeed() { return speed; } public Color getColour() { return colour; } public boolean getTail() { return tail; } public String getFurType() { return furType; } public int getAge() { return age; } }
评论
    相关推荐