mirror of
https://github.com/Dichgrem/Java.git
synced 2025-12-16 12:41:58 -05:00
update:demo2
This commit is contained in:
36
com/study/demo2/Account.java
Normal file
36
com/study/demo2/Account.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Account {
|
||||
private String accountId;
|
||||
private String owner;
|
||||
private double balance;
|
||||
|
||||
public Account(String accountId, String owner, double balance) {
|
||||
this.accountId = accountId;
|
||||
this.owner = owner;
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public void deposit(double amount) {
|
||||
if (amount > 0) {
|
||||
balance += amount;
|
||||
}
|
||||
}
|
||||
|
||||
public void withdraw(double amount) {
|
||||
if (amount > 0 && amount <= balance) {
|
||||
balance -= amount;
|
||||
}
|
||||
}
|
||||
|
||||
public void transfer(Account target, double amount) {
|
||||
if (amount > 0 && amount <= balance) {
|
||||
this.balance -= amount;
|
||||
target.balance += amount;
|
||||
}
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return balance;
|
||||
}
|
||||
}
|
||||
15
com/study/demo2/Account_Main.java
Normal file
15
com/study/demo2/Account_Main.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Account acc1 = new Account("A001", "张三", 1000);
|
||||
Account acc2 = new Account("A002", "李四", 500);
|
||||
|
||||
acc1.deposit(200);
|
||||
acc1.withdraw(100);
|
||||
acc1.transfer(acc2, 300);
|
||||
|
||||
System.out.println("账户1余额:" + acc1.getBalance());
|
||||
System.out.println("账户2余额:" + acc2.getBalance());
|
||||
}
|
||||
}
|
||||
44
com/study/demo2/Animal.java
Normal file
44
com/study/demo2/Animal.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public abstract class Animal {
|
||||
protected String name;
|
||||
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public abstract void makeSound();
|
||||
public abstract void move();
|
||||
}
|
||||
|
||||
class Dog extends Animal {
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeSound() {
|
||||
System.out.println(name + " 汪汪叫");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move() {
|
||||
System.out.println(name + " 跑动");
|
||||
}
|
||||
}
|
||||
|
||||
class Bird extends Animal {
|
||||
public Bird(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeSound() {
|
||||
System.out.println(name + " 叽叽喳喳");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move() {
|
||||
System.out.println(name + " 飞翔");
|
||||
}
|
||||
}
|
||||
12
com/study/demo2/Animal_Main.java
Normal file
12
com/study/demo2/Animal_Main.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Animal dog = new Dog("Buddy");
|
||||
Animal bird = new Bird("Tweety");
|
||||
dog.makeSound();
|
||||
dog.move();
|
||||
bird.makeSound();
|
||||
bird.move();
|
||||
}
|
||||
}
|
||||
37
com/study/demo2/Rectangle.java
Normal file
37
com/study/demo2/Rectangle.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Rectangle {
|
||||
private double width = 1.0;
|
||||
private double height = 1.0;
|
||||
|
||||
public Rectangle() {}
|
||||
|
||||
public Rectangle(double width, double height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public double getArea() {
|
||||
return width * height;
|
||||
}
|
||||
|
||||
public double getPerimeter() {
|
||||
return 2 * (width + height);
|
||||
}
|
||||
|
||||
public double getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setWidth(double width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public double getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(double height) {
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
18
com/study/demo2/Rectangle_Main.java
Normal file
18
com/study/demo2/Rectangle_Main.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Rectangle rect1 = new Rectangle(5.0, 3.0);
|
||||
System.out.println("面积:" + rect1.getArea());
|
||||
System.out.println("周长:" + rect1.getPerimeter());
|
||||
|
||||
Rectangle rect2 = new Rectangle();
|
||||
System.out.println("默认面积:" + rect2.getArea());
|
||||
System.out.println("默认周长:" + rect2.getPerimeter());
|
||||
|
||||
rect2.setWidth(2.5);
|
||||
rect2.setHeight(4.0);
|
||||
System.out.println("修改后面积:" + rect2.getArea());
|
||||
System.out.println("修改后周长:" + rect2.getPerimeter());
|
||||
}
|
||||
}
|
||||
40
com/study/demo2/Student.java
Normal file
40
com/study/demo2/Student.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Student {
|
||||
private String id;
|
||||
private String name;
|
||||
private int[] score = new int[10];
|
||||
|
||||
public Student(String id, String name, int[] scores) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
if (scores.length == 10) {
|
||||
this.score = scores;
|
||||
}
|
||||
}
|
||||
|
||||
public double getAverage() {
|
||||
int sum = 0;
|
||||
for (int s : score) {
|
||||
sum += s;
|
||||
}
|
||||
return (double) Math.round((sum / 10.0) * 100) / 100;
|
||||
}
|
||||
|
||||
public int getMaxScore() {
|
||||
int max = score[0];
|
||||
for (int s : score) {
|
||||
if (s > max) max = s;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public char getGrade() {
|
||||
double avg = getAverage();
|
||||
if (avg >= 90) return 'A';
|
||||
else if (avg >= 80) return 'B';
|
||||
else if (avg >= 70) return 'C';
|
||||
else if (avg >= 60) return 'D';
|
||||
else return 'E';
|
||||
}
|
||||
}
|
||||
12
com/study/demo2/Student_Main.java
Normal file
12
com/study/demo2/Student_Main.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
int[] scores = {85, 90, 78, 92, 88, 76, 95, 89, 79, 94};
|
||||
Student student = new Student("5001", "张三", scores);
|
||||
|
||||
System.out.println("平均分: " + student.getAverage());
|
||||
System.out.println("最高分: " + student.getMaxScore());
|
||||
System.out.println("等级: " + student.getGrade());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user