diff --git a/com/study/demo2/Account.java b/com/study/demo2/Account.java new file mode 100644 index 0000000..e40541e --- /dev/null +++ b/com/study/demo2/Account.java @@ -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; + } +} \ No newline at end of file diff --git a/com/study/demo2/Account_Main.java b/com/study/demo2/Account_Main.java new file mode 100644 index 0000000..3bceb06 --- /dev/null +++ b/com/study/demo2/Account_Main.java @@ -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()); + } +} \ No newline at end of file diff --git a/com/study/demo2/Animal.java b/com/study/demo2/Animal.java new file mode 100644 index 0000000..5e5df8f --- /dev/null +++ b/com/study/demo2/Animal.java @@ -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 + " 飞翔"); + } +} \ No newline at end of file diff --git a/com/study/demo2/Animal_Main.java b/com/study/demo2/Animal_Main.java new file mode 100644 index 0000000..a98c3e7 --- /dev/null +++ b/com/study/demo2/Animal_Main.java @@ -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(); + } +} \ No newline at end of file diff --git a/com/study/demo2/Rectangle.java b/com/study/demo2/Rectangle.java new file mode 100644 index 0000000..f8ed91b --- /dev/null +++ b/com/study/demo2/Rectangle.java @@ -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; + } +} diff --git a/com/study/demo2/Rectangle_Main.java b/com/study/demo2/Rectangle_Main.java new file mode 100644 index 0000000..b70c52e --- /dev/null +++ b/com/study/demo2/Rectangle_Main.java @@ -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()); + } +} \ No newline at end of file diff --git a/com/study/demo2/Student.java b/com/study/demo2/Student.java new file mode 100644 index 0000000..5c823ac --- /dev/null +++ b/com/study/demo2/Student.java @@ -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'; + } +} \ No newline at end of file diff --git a/com/study/demo2/Student_Main.java b/com/study/demo2/Student_Main.java new file mode 100644 index 0000000..1859463 --- /dev/null +++ b/com/study/demo2/Student_Main.java @@ -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()); + } +} \ No newline at end of file