Java學(xué)習(xí)筆記 - 第008天

每日要點(diǎn)

修改器和訪問器

修改器 - 屬性的setter方法

    public void setXxx(T t) {
        this.t = t;
    }

訪問器 - 屬性的getter方法

    public T getXxx() {
        return t;
    }
toString()

將對(duì)象轉(zhuǎn)換成字符串輸出

@Override
public String toString() {
    // TODO Auto-generated method stub
    return super.toString();
}

文檔注釋
用來(lái)描述自己定義的類、變量、方法等,例如:

/**
 * 平面上的點(diǎn)
 * @author Kygo
 * @since 0.1
 */

昨天作業(yè)修正

  • 1.定義一個(gè)類 描述手機(jī) 品牌尺寸等 打電話 發(fā)短信等call send short message install application Appuninstall / remove update
    手機(jī)類:
  public class MobilePhone {
    private String brand;
    private String owner;
    private double size;
    private double price;
    private boolean isSmart;
    private String[] installedApps;
    private int numberOfApps;
    
    public MobilePhone(String brand, double size, double price, boolean isSmart) {
        this.brand = brand;
        this.size = size;
        this.price = price;
        this.isSmart = isSmart;
        if (this.isSmart) {
            this.installedApps = new String[100];
        }
    }

    // 修改器 - 屬性的setter方法
    public void setOwner(String owner) {
        this.owner = owner;
    }
    
    // 訪問器 - 屬性的getter方法
    public String getOwner() {
        return owner;
    }
    
    public void show() {
        System.out.println(owner + "的價(jià)值" + price + "元的" + size +
                "英寸的" + brand + "手機(jī)");
    }
    
    public void call(String tel) {
        System.out.println("正在呼叫" + tel + "...");
    }
    
    public void sendShortMessage(String[] tels, String content) {
        for (String tel : tels) {
            System.out.println("向" + tel + "發(fā)送信息: " + content);
        }
    }
    
    public void installApp(String appName) {
        if (isSmart && numberOfApps < installedApps.length) {
            System.out.println("正在安裝" + appName);
            installedApps[numberOfApps] = appName;
            numberOfApps += 1;
        }
        else {
            System.out.println("不能安裝應(yīng)用程序.");
        }
    }
    
    public void uninstallApp(String appName) {
        if (isSmart) {
            for (int i = 0; i < numberOfApps; i++) {
                if (installedApps[i].equals(appName)) {
                    System.out.println("正在卸載" + appName);
                    for (int j = i; j < numberOfApps - 1; j++) {
                        installedApps[j] = installedApps[j + 1];
                    }
                    numberOfApps -= 1;
                    return;
                }
            }
            System.out.println("沒有這個(gè)應(yīng)用程序.");
        }
        else {
            System.out.println("不能卸載應(yīng)用程序.");
        }
    }
    
    public void runApp(String appName) {
        for (int i = 0; i < numberOfApps; i++) {
            if (installedApps[i].equals(appName)) {
                System.out.println("啟動(dòng)" + appName);
                return ;
            }
        }
        System.out.println("沒有對(duì)應(yīng)應(yīng)用程序");
    }
}

測(cè)試類:

        MobilePhone phone1 = new MobilePhone("iphone 5S", 4, 2000, true);
        phone1.setOwner("張三");
        System.out.println(phone1.getOwner());
        phone1.show();
        phone1.installApp("捕魚達(dá)人");
        phone1.installApp("微信");
        phone1.runApp("微信");
        phone1.runApp("歡樂斗地主");
        phone1.call("110");
        phone1.sendShortMessage(new String[] {"13213213", "1231242"},"晚上約嗎");
        
        MobilePhone phone2 = new MobilePhone("山寨", 3, 200, false);
        phone2.setOwner("王大錘");
        phone2.show();
        phone2.installApp("王室戰(zhàn)爭(zhēng)");
  • 2.寫個(gè)程序 模擬 奧特曼打小怪獸hp mp攻擊行為攻擊參數(shù) 小怪獸奧特曼必殺技小怪獸反擊
    奧特曼類:
public class Ultraman {
    private String name;
    private int hp;
    private int mp;
    
    public Ultraman(String name, int hp, int mp) {
        this.name = name;
        this.setHp(hp);
        this.mp = mp;
    }
    
    public String info() {
        return String.format("%s奧特曼 - 生命值: %d, 魔法值: %d",
                name, getHp(), mp);
    }

    public void attack(Monster m) {
        int injury = (int) (Math.random() * 11 + 10);
        m.setHp(m.getHp() - injury);
    }
    
    public void hugeAttack(Monster m) {
        if (mp >= 40) {
            int injury = m.getHp() / 4 * 3 > 50 ? m.getHp() / 4 * 3 : 50;
            m.setHp(m.getHp() - injury);
            mp -= 40;
        }
    }
    
    public void magicalAttack(Monster[] mArray) {
        if (mp >= 15) {
            for (Monster m : mArray) {
                if (m.getHp() > 0) {
                    int injury = (int) (Math.random() * 11 + 5);
                    m.setHp(m.getHp() - injury);
                }
            }
            mp -= 15;
        }
    }

    public void resumeMp() {
        mp += 4;
    }
    
    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp > 0 ? hp : 0;
    }

    public int getMp() {
        return mp;
    }
}

小怪獸類:

public class Monster {
    private String name;
    private int hp;
    
    public Monster(String name, int hp) {
        this.name = name;
        this.setHp(hp);
    }

    public String info() {
        return String.format("%s小怪獸 - 生命值: %d", name, getHp());
    }
    
    public void attack(Ultraman u) {
        int injury = (int) (Math.random() * 6 + 5);
        u.setHp(u.getHp() - injury);
    }

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp < 0 ? 0 : hp;
    }
}

測(cè)試類:

    public static Monster selectOne(Monster[] mArray) {
        Monster temp = null;
        do {
            int randomIndex = (int) (Math.random() * mArray.length);
            temp = mArray[randomIndex];
        } while (temp.getHp() == 0);
        return temp;
    }
    
    public static void showMonsterInfo(Monster[] mArray) {
        for (Monster monster : mArray) {
            System.out.println(monster.info()); 
        }
    }
    
    public static boolean isAllDead(Monster[] mArray) {
        for (Monster monster : mArray) {
            if (monster.getHp() > 0) {
                return false;
            }
        }
        return true;
    }
    
    public static void main(String[] args) {
        Ultraman u = new Ultraman("迪迦", 250, 150);
        System.out.println(u.info());
        Monster m1 = new Monster("哥斯拉", 100);
        Monster m2 = new Monster("暴龍", 120);
        Monster m3 = new Monster("金剛", 140);
        Monster m4 = new Monster("異形", 150);
        Monster[] mArray = {m1, m2, m3, m4};
        showMonsterInfo(mArray);
        int round = 1;
        do {
            System.out.println("======第" + round + "回合======");
            int random = (int) (Math.random() * 10 + 1);
            Monster m = selectOne(mArray);
            if (random <= 7) {
                useNormalAttack(u, m);
            }
            else if (random <= 9) {
                if (u.getMp() >= 15) {
                    System.out.println("奧特曼使用了魔法攻擊.");
                    u.magicalAttack(mArray);
                    for (Monster monster : mArray) {
                        if (monster.getHp() > 0) {
                            monster.attack(u);
                        }
                    } 
                }
                else {
                    useNormalAttack(u, m);
                }
            }
            else {
                if (u.getMp() >= 40) {
                    System.out.println("奧特曼使用了究極必殺技.");
                    u.hugeAttack(m);
                    if (m.getHp() > 0) {
                        m.attack(u);
                    } 
                }
                else {
                    useNormalAttack(u, m);
                }
            }
            if (u.getHp() > 0) {
                u.resumeMp();
            }
            System.out.println(u.info());
            showMonsterInfo(mArray);
            round += 1;
        } while (u.getHp() > 0 && !isAllDead(mArray));
        if (u.getHp() > 0) {
            System.out.println("奧特曼勝利!");
        }
        else {
            System.out.println("小怪獸勝利!");
        }
    }

    public static void useNormalAttack(Ultraman u, Monster m) {
        System.out.println("奧特曼使用了普通攻擊.");
        u.attack(m);
        if (m.getHp() > 0) {
            m.attack(u);
        }
    }
  • **3.改造成面向?qū)ο?圍墻 過(guò)道 **
    圓類:
public class Circle {

    private double radius;
    
    public Circle(double radius) {
        this.radius = radius;
    }

    public double perimeter() {
        return 2 * Math.PI * radius;
    }
    
    public double area() {
        return Math.PI * radius * radius;
    }
}

測(cè)試類:

    private static final double AISLE_UNIT_PRICE = 38.5;
    private static final double FENCE_UNIT_PRICE = 15.5;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("請(qǐng)輸入游泳池的半徑: ");
        double r = input.nextDouble();
        Circle small = new Circle(r);
        Circle big = new Circle(r + 3);
        System.out.printf("圍墻的造價(jià)為: ¥%.2f元\n",
                big.perimeter() * FENCE_UNIT_PRICE);
        System.out.printf("過(guò)道的造價(jià)為: ¥%.2f元\n",
                (big.area() - small.area()) * AISLE_UNIT_PRICE);
        input.close();
    }

練習(xí)

  • 1.定義一個(gè)類描述矩形
public class Rectangle {
    private double height;
    private double width;
    
    public Rectangle(double height, double width) {
        this.height = height;
        this.width = width;
    }

    public double perimeter() {
        return 2 * (height + width);
    }
    
    public double area() {
        return height * width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }
}

例題

  • 1.定義一個(gè)類描述平面 點(diǎn) 可以移動(dòng)修改點(diǎn) 能計(jì)算這個(gè)點(diǎn)到另外一個(gè)點(diǎn)的距離
    點(diǎn)類:
/**
 * 平面上的點(diǎn)
 * @author Kygo
 * @since 0.1
 */
// 定義一個(gè)類描述平面  點(diǎn) 可以移動(dòng)修改點(diǎn) 能計(jì)算這個(gè)點(diǎn)到另外一個(gè)點(diǎn)的距離
public class Point {
    private double x;  // 橫坐標(biāo)
    private double y;  // 縱坐標(biāo)
    
    /**
     * 構(gòu)造器
     * @param x 橫坐標(biāo)
     * @param y 縱坐標(biāo)
     */
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
        // this.moveTo(x, y);
    }
    
    /**
     * 移動(dòng)到
     * @param newX 新的橫坐標(biāo)
     * @param newY 新的縱坐標(biāo)
     */
    // 移動(dòng)到
    public void moveTo(double newX, double newY) {
        x = newX;
        y = newY;
    }
    
    /**
     * 移動(dòng)了
     * @param dx 橫坐標(biāo)的增量
     * @param dy 縱坐標(biāo)的增量
     */
    // 移動(dòng)了
    public void moveBy(double dx, double dy) {
        x += dx;
        y += dy;
    }
    
    /**
     * 計(jì)算到另一個(gè)點(diǎn)的距離
     * @param other 另一個(gè)點(diǎn)
     * @return 兩個(gè)點(diǎn)的距離
     */
    public double distanceTo(Point other) {
        // this到other的距離
        double dx = this.x - other.x;
        double dy = this.y - other.y;
        return Math.sqrt(dx * dx + dy * dy);
    }
    
    // 將對(duì)象轉(zhuǎn)換成字符串輸出
    @Override
    public String toString() {
        return "(" + x + "," + y + ")";
    }
}
  • 2.定義一條線段
// 文檔注釋
/**
 * 平面上的線條
 * @author Kygo
 * @since 0.2
 */
//定義一條線段
public class Line {
    private Point start;
    private Point end;
    
    /**
     * 構(gòu)造器
     * @param start 線段的起點(diǎn)
     * @param end 線段的終點(diǎn)
     */
    public Line(Point start, Point end) {
        this.start = start;
        this.end = end;
    }

    /**
     * 獲取線段的長(zhǎng)度
     * @return 線段的長(zhǎng)度
     */
    public double length() {
        return start.distanceTo(end);
    }   
}

作業(yè)

  • 1.寫一個(gè)類 描述 數(shù)學(xué)上的分?jǐn)?shù) 提供分?jǐn)?shù)的加減乘除的運(yùn)算 化簡(jiǎn)
    分?jǐn)?shù)類:
public class Fraction {
    private int numerator;  // 分子
    private int denominator;  // 分母
    
    public Fraction(int numerator, int denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
    }

    public Fraction add(Fraction f) {
        Fraction result = new Fraction(0, 0);
        result.numerator = (this.numerator * f.denominator) + 
                (f.numerator * this.denominator);
        result.denominator = this.denominator * f.denominator;
        result.reduction();
        return result; 
    }
    
    public Fraction subtract(Fraction f) {
        Fraction result = new Fraction(0, 0);
        result.numerator = (this.numerator * f.denominator) - 
                (f.numerator * this.denominator);
        result.denominator = this.denominator * f.denominator;
        result.reduction();
        return result;
    }
    
    public Fraction multiply(Fraction f) {
        Fraction result = new Fraction(0, 0);
        result.denominator = this.denominator * f.denominator;
        result.numerator = this.numerator * f.numerator;
        result.reduction();
        return result;
    }
    
    public Fraction divide(Fraction f) {
        Fraction result = new Fraction(0, 0);
        result.denominator = this.denominator * f.numerator;
        result.numerator = this.numerator * f.denominator;
        result.reduction();
        return result;
    }
    
    public void reduction() {
        int min = denominator > Math.abs(numerator) ? Math.abs(numerator) : denominator;
        for (int i = min; i >= 2; i--) {
            if (numerator % i == 0 && denominator % i == 0) {
                numerator /= i;
                denominator /= i;
                return ;
            }
        }
    }
    
    @Override
    public String toString() {
        if (numerator != 0) {
            return numerator + "/" + denominator;
        }
        else {
            return "0";
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,406評(píng)論 6 538
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,034評(píng)論 3 423
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,413評(píng)論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,449評(píng)論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,165評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,559評(píng)論 1 325
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,606評(píng)論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,781評(píng)論 0 289
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,327評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,084評(píng)論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,278評(píng)論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,849評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,495評(píng)論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,927評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,172評(píng)論 1 291
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,010評(píng)論 3 396
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,241評(píng)論 2 375

推薦閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 31,726評(píng)論 18 399
  • 每日要點(diǎn) 面向?qū)ο蟮乃拇笾е?抽象 - 定義一個(gè)類的過(guò)程就是一個(gè)抽象的過(guò)程(數(shù)據(jù)抽象、行為抽象),通過(guò)抽象我們可以...
    迷茫o閱讀 397評(píng)論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,807評(píng)論 18 139
  • 2017.04.09 孫愈顏奇跡感恩日志 一、奇跡 ...
    幸福花開奇跡匯閱讀 159評(píng)論 0 1
  • 就算你我世界至此互不相干 我也要讓你看到我的優(yōu)秀
    SwanOdette閱讀 158評(píng)論 0 0