每日要点
修改器和访问器
修改器 - 属性的setter方法
public void setXxx(T t) {
this.t = t;
}
访问器 - 属性的getter方法
public T getXxx() {
return t;
}
toString()
将对象转换成字符串输出
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
文档注释
用来描述自己定义的类、变量、方法等,例如:
/**
* 平面上的点
* @author Kygo
* @since 0.1
*/
昨天作业修正
-
1.定义一个类 描述手机 品牌尺寸等 打电话 发短信等call send short message install application Appuninstall / remove update
手机类:
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 + "的价值" + price + "元的" + size +
"英寸的" + brand + "手机");
}
public void call(String tel) {
System.out.println("正在呼叫" + tel + "...");
}
public void sendShortMessage(String[] tels, String content) {
for (String tel : tels) {
System.out.println("向" + tel + "发送信息: " + content);
}
}
public void installApp(String appName) {
if (isSmart && numberOfApps < installedApps.length) {
System.out.println("正在安装" + appName);
installedApps[numberOfApps] = appName;
numberOfApps += 1;
}
else {
System.out.println("不能安装应用程序.");
}
}
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("没有这个应用程序.");
}
else {
System.out.println("不能卸载应用程序.");
}
}
public void runApp(String appName) {
for (int i = 0; i < numberOfApps; i++) {
if (installedApps[i].equals(appName)) {
System.out.println("启动" + appName);
return ;
}
}
System.out.println("没有对应应用程序");
}
}
测试类:
MobilePhone phone1 = new MobilePhone("iphone 5S", 4, 2000, true);
phone1.setOwner("张三");
System.out.println(phone1.getOwner());
phone1.show();
phone1.installApp("捕鱼达人");
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("王室战争");
-
2.写个程序 模拟 奥特曼打小怪兽hp mp攻击行为攻击参数 小怪兽奥特曼必杀技小怪兽反击
奥特曼类:
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;
}
}
测试类:
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.改造成面向对象 围墙 过道 **
圆类:
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;
}
}
测试类:
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("请输入游泳池的半径: ");
double r = input.nextDouble();
Circle small = new Circle(r);
Circle big = new Circle(r + 3);
System.out.printf("围墙的造价为: ¥%.2f元\n",
big.perimeter() * FENCE_UNIT_PRICE);
System.out.printf("过道的造价为: ¥%.2f元\n",
(big.area() - small.area()) * AISLE_UNIT_PRICE);
input.close();
}
练习
- 1.定义一个类描述矩形
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.定义一个类描述平面 点 可以移动修改点 能计算这个点到另外一个点的距离
点类:
/**
* 平面上的点
* @author Kygo
* @since 0.1
*/
// 定义一个类描述平面 点 可以移动修改点 能计算这个点到另外一个点的距离
public class Point {
private double x; // 横坐标
private double y; // 纵坐标
/**
* 构造器
* @param x 横坐标
* @param y 纵坐标
*/
public Point(double x, double y) {
this.x = x;
this.y = y;
// this.moveTo(x, y);
}
/**
* 移动到
* @param newX 新的横坐标
* @param newY 新的纵坐标
*/
// 移动到
public void moveTo(double newX, double newY) {
x = newX;
y = newY;
}
/**
* 移动了
* @param dx 横坐标的增量
* @param dy 纵坐标的增量
*/
// 移动了
public void moveBy(double dx, double dy) {
x += dx;
y += dy;
}
/**
* 计算到另一个点的距离
* @param other 另一个点
* @return 两个点的距离
*/
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);
}
// 将对象转换成字符串输出
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
- 2.定义一条线段
// 文档注释
/**
* 平面上的线条
* @author Kygo
* @since 0.2
*/
//定义一条线段
public class Line {
private Point start;
private Point end;
/**
* 构造器
* @param start 线段的起点
* @param end 线段的终点
*/
public Line(Point start, Point end) {
this.start = start;
this.end = end;
}
/**
* 获取线段的长度
* @return 线段的长度
*/
public double length() {
return start.distanceTo(end);
}
}
作业
-
1.写一个类 描述 数学上的分数 提供分数的加减乘除的运算 化简
分数类:
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";
}
}
}