每日要點(diǎn)
二維數(shù)組
int a[][] = new int[2][3];
二維數(shù)組 a 可以看成一個兩行三列的數(shù)組。
- 例子1:學(xué)生的成績
int[][] scores = new int[5][3];
for (int i = 0; i < scores.length; i++) {
for (int j = 0; j < scores[i].length; j++) {
scores[i][j] = (int) (Math.random() * 101);
}
}
- 例子2:楊輝三角:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
int[][] y = new int[10][];
for (int i = 0; i < y.length; i++) {
y[i] = new int[i + 1];
for (int j = 0; j < y[i].length; j++) {
if (j == 0 || j == i) {
y[i][j] = 1;
}
else {
y[i][j] = y[i - 1][j] + y[i - 1][j - 1];
}
System.out.print(y[i][j] + "\t");
}
System.out.println();
}
面向?qū)ο?/h2>
對象是可以接收消息的實(shí)體
特征 :
1.一切都是對象
2.對象都有屬性和行為
3.每個對象都是獨(dú)一無二的
4.對象都屬于某個類
類是對象的藍(lán)圖和模板
- 數(shù)據(jù)抽象 - 屬性
- 構(gòu)造器 - 創(chuàng)建對象需要使用構(gòu)造器
- 行為抽象 - 方法
面向?qū)ο缶幊痰牡?步 - 定義類
public class Student {
// 數(shù)據(jù)抽象 - 屬性 - 找名稱
private String name;
private int age;
// 構(gòu)造器
public Student(String n, int a) {
name = n;
age = a;
}
// 行為抽象 - 方法 - 找動詞
public void play(String gameName) {
System.out.println(name + "正在玩" + gameName + ".");
}
public void study() {
System.out.println(name + "正在學(xué)習(xí).");
}
public void watchJapaneseAV() {
if (age >= 18) {
System.out.println(name + "正在觀看島國愛情動作片.");
}
else {
System.out.println(name + "只能觀看《熊出沒》.");
}
}
}
**面向?qū)ο缶幊痰?步 - 創(chuàng)建對象 **
// new 構(gòu)造器();
Student stu = new Student("王大錘", 15);
面向?qū)ο缶幊痰?步 - 給對象發(fā)消息
stu.study();
stu.play("LOL");
stu.watchJapaneseAV();
Student stu2 = new Student("張三", 18);
stu2.study();
stu2.play("斗地主");
stu2.watchJapaneseAV();
this 關(guān)鍵字
this 代表這個對象
- 例子:
public class Dog {
private String nickname;
private boolean isLarge;
public Dog(String nickname, boolean isLarge) {
// this 代表這個對象
this.nickname = nickname;
this.isLarge = isLarge;
}
public void bark() {
System.out.println(nickname + ": 汪汪汪....");
}
public void keepTheDoor() {
if (isLarge) {
System.out.println(nickname + "正在看門.");
}
else {
System.out.println(nickname + "不能看門.");
}
}
}
主函數(shù):
Dog dog1 = new Dog("旺財", true);
dog1.bark();
dog1.keepTheDoor();
Dog dog2 = new Dog("花花", false);
dog2.bark();
dog2.keepTheDoor();
其他內(nèi)容
寫代碼的終極原則: 高內(nèi)聚 低耦合
high cohesion low coupling
1TBS - One True Bracing Style 打{}風(fēng)格
Allman - FreeBSD
如果定義類時沒有寫任何一個構(gòu)造器
那么系統(tǒng)會自動添加一個默認(rèn)的隱式構(gòu)造器(平庸構(gòu)造器)
public Clock() {
}
寫代碼低耦合 舉例:
System.out.printf("%02d:%02d:%02d\n", hour, minute, second);
這樣寫代碼就跟控制臺緊緊耦合在了一起 ,應(yīng)該這樣寫:
return String.format("%02d:%02d:%02d", hour, minute, second);
窗口類
JFrame
// 創(chuàng)建窗口對象
JFrame f = new JFrame("我的第一個窗口");
// 通過給窗口對象發(fā)消息來設(shè)置和顯示窗口
f.setSize(400, 300); // 大小
f.setLocationRelativeTo(null); // 窗口擺放
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)窗口自動關(guān)閉程序
Clock clock = new Clock();
JLabel label = new JLabel(clock.display()); // 創(chuàng)建標(biāo)簽
label.setHorizontalAlignment(JLabel.CENTER); // 設(shè)置水平對齊 - 居中
Font font = new Font("微軟雅黑", Font.BOLD, 36); // 設(shè)置字體
label.setFont(font); // 將字體添加到標(biāo)簽
f.add(label); // 將標(biāo)簽添加到窗口
new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
clock.run();
label.setText(clock.display());
}
}).start();
f.setVisible(true); // 顯示窗口
昨天作業(yè)講解
- 1.買彩票紅色球 1~33 藍(lán)色球 1~16
01 05 11 17 18 22 05
08 10 22 23 31 32 11
數(shù)字不能重復(fù)
輸入 幾 隨機(jī)選出 幾組號碼
Scanner input = new Scanner(System.in);
System.out.print("機(jī)選幾注: ");
int n =input.nextInt();
input.close();
for (int counter = 1; counter <= n; counter++) {
int[] redBalls = new int[6];
for (int i = 0; i < redBalls.length;) {
// 生成1~33的隨機(jī)數(shù)作為紅色球的號碼
int number = (int) (Math.random() * 33 + 1);
// 檢查此號碼在之前選中的號碼中有沒有出現(xiàn)過
boolean isDuplicated = false;
for (int j = 0; j < i; j++) {
if (redBalls[j] == number) {
isDuplicated = true;
break;
}
}
if (!isDuplicated) {
redBalls[i] = number;
i += 1;
}
}
bubbleSort(redBalls);
for (int x : redBalls) {
System.out.printf("%02d ", x);
}
int blueBall = (int) (Math.random() * 16 + 1);
System.out.printf("| %02d\n", blueBall);
}
}
public static void bubbleSort(int[] array) {
// 數(shù)組有N個數(shù),需要N-1次循環(huán)
boolean swapped =true;
for (int i = 1; swapped && i < array.length; i++) {
swapped = false;
for (int j = 0;j < array.length - i; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
}
}
例子
-
1.電子鐘
電子鐘類:
public class Clock {
private int hour;
private int minute;
private int second;
// 如果定義類時沒有寫任何一個構(gòu)造器
// 那么系統(tǒng)會自動添加一個默認(rèn)的隱式構(gòu)造器(平庸構(gòu)造器)
// public Clock() {
// }
public Clock() {
// 拿系統(tǒng)時間
// Java 7-
// Calendar cal = new GregorianCalendar();
// this.hour = cal.get(Calendar.HOUR_OF_DAY);
// this.minute = cal.get(Calendar.MINUTE);
// this.second = cal.get(Calendar.SECOND);
// Java 8+
LocalDateTime time = LocalDateTime.now();
this.hour = time.getHour();
this.minute = time.getMinute();
this.second = time.getSecond();
}
public Clock(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public String display() {
// 這樣寫代碼就跟控制臺緊緊耦合在了一起
// System.out.printf("%02d:%02d:%02d\n", hour, minute, second);
return String.format("%02d:%02d:%02d", hour, minute, second);
}
public void run() {
second += 1;
if (second == 60) {
second = 0;
minute += 1;
if (minute == 60) {
minute = 0;
hour += 1;
if (hour == 24) {
hour = 0;
}
}
}
}
}
主函數(shù):
Clock clock = new Clock();
System.out.println(clock.display());
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
clock.run();
System.out.println(clock.display());
}
作業(yè)
- 1.定義一個類 描述手機(jī) 品牌尺寸等 打電話 發(fā)短信等
call send short message install application App
uninstall / remove update
手機(jī)類:
public class MobilePhone {
private String brand;
private double size;
private double price;
private String phoneModel;
private String manufacturer;
private String number;
public MobilePhone(String brand, double size, double price, String phoneModel,
String manufacturer, String number) {
this.brand = brand;
this.size = size;
this.price = price;
this.phoneModel = phoneModel;
this.manufacturer = manufacturer;
this.number = number;
}
public String displayInformation() {
return String.format("手機(jī)信息: \n品牌: " + brand + "\n型號: " + phoneModel
+ "\n制造商: " + manufacturer + "\n尺寸: %.2f寸 \n價格: %.2f元 \n電話號碼: "
+ number, size,price);
}
public String call(String number) {
return String.format(this.number + " 正在給 " + number + "打電話");
}
public String sendShortMessage(String number) {
return String.format(this.number + " 正在給 " + number + "發(fā)短信");
}
public String installAPP(String app) {
return String.format(this.phoneModel + " 正在安裝 " + app);
}
public String uninstallAPP(String app) {
return String.format(this.phoneModel + " 正在卸載 " + app);
}
public String updateAPP(String app) {
return String.format(this.phoneModel + " 正在更新 " + app);
}
}
主函數(shù):
MobilePhone mobilePhone = new MobilePhone("魅族", 5.5, 2000, "魅藍(lán)Note",
"中國", "18908042994");
System.out.println(mobilePhone.displayInformation());
System.out.println(mobilePhone.call("18602132435"));
System.out.println(mobilePhone.sendShortMessage("18602132435"));
System.out.println(mobilePhone.installAPP("熊貓TV"));
System.out.println(mobilePhone.uninstallAPP("UC瀏覽器"));
System.out.println(mobilePhone.updateAPP("QQ"));
-
2.寫個程序 模擬 奧特曼打小怪獸
hp mp
攻擊行為
攻擊參數(shù) 小怪獸
奧特曼必殺技
小怪獸反擊
奧特曼類:
public class Ultraman {
private String name;
int hp;
int mp;
public Ultraman(String name, int hp, int mp) {
this.name = name;
this.hp = hp;
this.mp = mp;
}
public void attack(Monster m) {
m.hp -= 20;
System.out.println(this.getName() + "普通攻擊了" +
m.getName());
}
public void hugeAttack(Monster m) {
m.hp -= 50;
System.out.println(this.getName() + "暴擊了" +
m.getName());
}
public void magicalAttack(Monster[] mArray) {
for (Monster m : mArray) {
m.hp -= 20;
}
this.mp -= 100;
System.out.println(this.getName() + "發(fā)動大招攻擊了所有小怪獸");
}
public void status() {
System.out.printf(this.getName() + " hp:%d mp:%d\n", hp, mp);
}
public String getName() {
return this.name;
}
}
小怪獸類:
public class Monster {
private String name;
int hp;
public Monster(String name, int hp) {
this.name = name;
this.hp = hp;
}
public void attackBack(Ultraman u) {
u.hp -= 10;
System.out.println(this.getName() + "反擊了" + u.getName());
}
public String getName() {
return this.name;
}
public void status() {
System.out.printf(this.getName() + "小怪獸 hp:%d\n", hp);
}
}
主函數(shù):
Ultraman ultraman = new Ultraman("迪迦奧特曼", 500, 200);
Monster[] monsters = new Monster[3];
for (int i = 0; i < monsters.length; i++) {
monsters[i] = new Monster("小怪獸" + (i + 1) + "號", 100);
}
Scanner input = new Scanner(System.in);
while (ultraman.hp > 0) {
System.out.printf(ultraman.getName() + ",請選擇攻擊方式: \n" + "1.普通攻擊 2.發(fā)動暴擊 3.魔法攻擊\n");
int attackModel = input.nextInt();
System.out.print(ultraman.getName() + ",請選擇攻擊哪個小怪獸: ");
int monsterNum = input.nextInt();
if (monsters[monsterNum - 1].hp > 0) {
switch (attackModel) {
case 1:
ultraman.attack(monsters[monsterNum - 1]);
monsters[monsterNum - 1].attackBack(ultraman);
ultraman.status();
monsters[monsterNum - 1].status();
break;
case 2:
ultraman.hugeAttack(monsters[monsterNum - 1]);
monsters[monsterNum - 1].attackBack(ultraman);
ultraman.status();
monsters[monsterNum - 1].status();
break;
case 3:
ultraman.magicalAttack(monsters);
for (int i = 0; i < monsters.length; i++) {
monsters[i].attackBack(ultraman);
}
ultraman.status();
for (int i = 0; i < monsters.length; i++) {
monsters[i].status();
}
break;
}
}
else {
System.out.println(monsters[monsterNum - 1].getName() + "沒有hp了,已經(jīng)死亡");
}
}
input.close();
-
3.改造成面向?qū)ο?圍墻 過道 fencePrice aislePrice
過道類:
public class Aisle {
private static final double AISLE_UNIT_PRICE = 38.5;
private double radius;
public Aisle(double radius) {
this.radius = radius;
}
public double area() {
return Math.PI * this.radius * this.radius;
}
public double area(double radius) {
return Math.PI * radius * radius;
}
public double aislePrice(double r) {
return (this.area() - area(r))* AISLE_UNIT_PRICE;
}
}
圍墻類:
public class Fence {
private static final double FENCE_UNIT_PRICE = 15.5;
private double radius;
public Fence(double radius) {
this.radius = radius;
}
public double perimeter() {
return 2 * Math.PI * this.radius;
}
public double fencePrice() {
return this.perimeter() * FENCE_UNIT_PRICE;
}
}
主函數(shù):
Scanner input = new Scanner(System.in);
System.out.print("請輸入游泳池的半徑: ");
double r = input.nextDouble();
if (r > 0) {
Fence fence = new Fence(r + 3);
System.out.printf("圍墻的造價為: %.2f元\n", fence.fencePrice());
Aisle aisle = new Aisle(r + 3);
System.out.printf("過道的造價: %.2f元", aisle.aislePrice(r));
}
else {
System.out.println("游泳池的半徑應(yīng)該是一個正數(shù).");
}
input.close();