Compare commits

..

4 Commits

Author SHA1 Message Date
dichgrem
9285e13f89 update:demo6 2025-12-14 13:30:08 +08:00
dichgrem
43c6b58317 update:demo5 2025-12-05 14:33:25 +08:00
dichgrem
5832b4fe9d update:demo4 2025-11-28 17:09:44 +08:00
dichgrem
41e6b71bd7 style:fmt 2025-11-21 15:20:01 +08:00
63 changed files with 833 additions and 321 deletions

95
.gitignore vendored Normal file
View File

@@ -0,0 +1,95 @@
### Compiled class files
*.class
### Log files
*.log
### BlueJ files
*.ctxt
### Mobile Tools for Java (J2ME)
.mtj.tmp/
### Package Files
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
### VM crash logs
hs_err_pid*
replay_pid*
# ========================
# Maven
# ========================
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
# ========================
# Gradle
# ========================
.gradle/
build/
!gradle-wrapper.jar
!*/src/main/**/build/
!*/src/test/**/build/
### Avoid ignoring Gradle wrapper
gradle-app.setting
# ========================
# IntelliJ IDEA
# ========================
.idea/
*.iml
*.ipr
*.iws
out/
# ========================
# Eclipse
# ========================
.project
.classpath
.cproject
.settings/
bin/
tmp/
# ========================
# NetBeans
# ========================
nbproject/private/
nbbuild/
dist/
nbdist/
.nb-gradle/
# ========================
# VS Code
# ========================
.vscode/
# ========================
# OS files
# ========================
# macOS
.DS_Store
# Windows
Thumbs.db
#local
com/study/test/

View File

@@ -1,5 +1,7 @@
package com.study.demo;
import java.util.Scanner;
// I am 212306206
public class test1_type {
public static void main(String[] args) {

View File

@@ -13,4 +13,3 @@ public class test2_for {
}
}
}

View File

@@ -16,4 +16,3 @@ public class test3_1_99 {
System.out.println("1+2+...+99 的和 = " + sum);
}
}

View File

@@ -1,4 +1,5 @@
package com.study.demo;
import java.util.Scanner;
public class test5 {

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -8,6 +8,7 @@ public abstract class Animal {
}
public abstract void makeSound();
public abstract void move();
}

View File

@@ -4,7 +4,8 @@ public class Rectangle {
private double width = 1.0;
private double height = 1.0;
public Rectangle() {}
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;

View File

@@ -24,17 +24,23 @@ public class Student {
public int getMaxScore() {
int max = score[0];
for (int s : score) {
if (s > max) max = s;
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';
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';
}
}

View File

@@ -4,7 +4,6 @@ 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());

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,9 @@
package com.study.demo4;
class AlipayPayment implements PaymentProcessor {
@Override
public boolean pay(double amount) {
System.out.println("支付宝支付: " + amount);
return true;
}
}

View File

@@ -0,0 +1,11 @@
package com.study.demo4;
class BikeDeliveryCalculator implements DeliveryFeeCalculator {
@Override
public double calculateFee(double distance) {
if (distance <= 5) {
return 5.0; // 起步价
}
return 5.0 + (distance - 5) * 1.5;
}
}

View File

@@ -0,0 +1,12 @@
package com.study.demo4;
class CarDeliveryCalculator implements DeliveryFeeCalculator {
@Override
public double calculateFee(double distance) {
if (distance <= 5) {
return 10.0; // 起步价
}
return 10.0 + (distance - 5) * 2.0;
}
}

View File

@@ -0,0 +1,9 @@
package com.study.demo4;
class CreditCardPayment implements PaymentProcessor {
@Override
public boolean pay(double amount) {
System.out.println("银行卡支付: " + amount);
return true;
}
}

View File

@@ -0,0 +1,5 @@
package com.study.demo4;
interface DeliveryFeeCalculator {
double calculateFee(double distance);
}

View File

@@ -0,0 +1,26 @@
package com.study.demo4;
abstract class HomeAppliance implements SmartDevice {
protected String name;
protected boolean isOn;
public HomeAppliance(String name) {
this.name = name;
this.isOn = false;
}
@Override
public void turnOn() {
isOn = true;
}
@Override
public void turnOff() {
isOn = false;
}
@Override
public String getStatus() {
return name + " is " + (isOn ? "ON" : "OFF");
}
}

View File

@@ -0,0 +1,21 @@
package com.study.demo4;
import java.util.ArrayList;
import java.util.List;
class HomeController {
private List<SmartDevice> devices = new ArrayList<>();
public void addDevice(SmartDevice device) {
devices.add(device);
}
public String getDeviceStatus(String name) {
for (SmartDevice device : devices) {
if (device.getStatus().contains(name)) {
return device.getStatus();
}
}
return "Device not found";
}
}

View File

@@ -0,0 +1,16 @@
package com.study.demo4;
class PaymentFactory {
public static PaymentProcessor getProcessor(String type) {
switch (type.toLowerCase()) {
case "wechat":
return new WechatPayment();
case "alipay":
return new AlipayPayment();
case "card":
return new CreditCardPayment();
default:
throw new IllegalArgumentException("不支持的支付方式");
}
}
}

View File

@@ -0,0 +1,6 @@
package com.study.demo4;
interface PaymentProcessor {
boolean pay(double amount);
}

View File

@@ -0,0 +1,7 @@
package com.study.demo4;
class SmartAirConditioner extends HomeAppliance {
public SmartAirConditioner(String name) {
super(name);
}
}

View File

@@ -0,0 +1,7 @@
package com.study.demo4;
interface SmartDevice {
void turnOn();
void turnOff();
String getStatus();
}

View File

@@ -0,0 +1,7 @@
package com.study.demo4;
class SmartLight extends HomeAppliance {
public SmartLight(String name) {
super(name);
}
}

View File

@@ -0,0 +1,7 @@
package com.study.demo4;
class SmartMasher extends HomeAppliance {
public SmartMasher(String name) {
super(name);
}
}

View File

@@ -0,0 +1,31 @@
package com.study.demo4;
import java.util.ArrayList;
import java.util.List;
public class TestControl {
public static void main(String[] args) {
HomeController controller = new HomeController();
// 创建设备
SmartLight light = new SmartLight("Living Room Light");
SmartAirConditioner ac = new SmartAirConditioner("Bedroom AC");
SmartMasher masher = new SmartMasher("Kitchen Masher");
// 添加到控制器
controller.addDevice(light);
controller.addDevice(ac);
controller.addDevice(masher);
// 测试设备状态
light.turnOff();
ac.turnOff();
masher.turnOn();
// 查询状态
System.out.println(controller.getDeviceStatus("Living Room Light"));
System.out.println(controller.getDeviceStatus("Bedroom AC"));
System.out.println(controller.getDeviceStatus("Kitchen Masher"));
System.out.println(controller.getDeviceStatus("Non-existent Device"));
}
}

View File

@@ -0,0 +1,8 @@
package com.study.demo4;
public class TestPay {
public static void main(String[] args) {
PaymentProcessor processor = PaymentFactory.getProcessor("alipay");
processor.pay(666.0);
}
}

View File

@@ -0,0 +1,22 @@
package com.study.demo4;
public class TestTransport {
public static void main(String[] args) {
// 测试自行车配送
DeliveryFeeCalculator bike = new BikeDeliveryCalculator();
System.out.println("自行车配送 3km: ¥" + bike.calculateFee(3));
System.out.println("自行车配送 8km: ¥" + bike.calculateFee(8));
// 测试汽车配送
DeliveryFeeCalculator car = new CarDeliveryCalculator();
System.out.println("汽车配送 3km: ¥" + car.calculateFee(3));
System.out.println("汽车配送 8km: ¥" + car.calculateFee(8));
// 对比不同距离的费用
System.out.println("\n费用对比:");
for (int km = 1; km <= 10; km++) {
System.out.printf("%dkm: 自行车¥%.1f, 汽车¥%.1f%n",
km, bike.calculateFee(km), car.calculateFee(km));
}
}
}

View File

@@ -0,0 +1,9 @@
package com.study.demo4;
class WechatPayment implements PaymentProcessor {
@Override
public boolean pay(double amount) {
System.out.println("微信支付: " + amount);
return true;
}
}

View File

@@ -0,0 +1,40 @@
package com.study.demo5;
import java.io.*;
public class DataIO {
public void writeConfig(String path) {
try (DataOutputStream dos = new DataOutputStream(
new FileOutputStream(path))) {
dos.writeUTF("李华"); // 用户名
dos.writeInt(25); // 年龄
dos.writeBoolean(true); // 是否启用
dos.writeDouble(1234.56); // 余额
System.out.println("配置写入成功: " + path);
} catch (IOException e) {
System.err.println("写入配置失败: " + e.getMessage());
}
}
public void readConfig(String path) {
try (DataInputStream dis = new DataInputStream(
new FileInputStream(path))) {
String username = dis.readUTF();
int age = dis.readInt();
boolean enabled = dis.readBoolean();
double balance = dis.readDouble();
System.out.println("用户名: " + username);
System.out.println("年龄: " + age);
System.out.println("是否启用: " + enabled);
System.out.println("余额: " + balance);
} catch (IOException e) {
System.err.println("读取配置失败: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,20 @@
package com.study.demo5;
import java.io.*;
public class FileCopy {
public void copy(String source, String target) {
try (FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(target)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException e) {
System.err.println("源文件未找到: " + e.getMessage());
} catch (IOException e) {
System.err.println("文件复制失败: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,25 @@
package com.study.demo5;
import java.io.*;
public class FileStat {
public long countBytes(String filePath) {
long totalBytes = 0;
try (FileInputStream fis = new FileInputStream(filePath)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
totalBytes += bytesRead;
}
} catch (IOException e) {
System.err.println("文件读取失败: " + e.getMessage());
return -1;
}
return totalBytes;
}
}

17
com/study/demo5/Main.java Normal file
View File

@@ -0,0 +1,17 @@
package com.study.demo5;
public class Main {
public static void main(String[] args) {
// 7.1 测试
FileCopy fc = new FileCopy();
fc.copy("source.txt", "target.txt");
// 7.2 测试
DataIO dataIO = new DataIO();
dataIO.writeConfig("config.bin");
dataIO.readConfig("config.bin");
// 7.3 测试
FileStat fs = new FileStat();
long size = fs.countBytes("test3.txt");
System.out.println("文件大小: " + size + " 字节");
}
}

View File

@@ -0,0 +1,52 @@
package com.study.demo6;
public class SumThread {
static class PartSumTask implements Runnable {
private final int[] array;
private final int start;
private final int end;
public long result;
public PartSumTask(int[] array, int start, int end) {
this.array = array;
this.start = start;
this.end = end;
}
@Override
public void run() {
long sum = 0;
for (int i = start; i <= end; i++) {
sum += array[i];
}
result = sum;
}
}
public static void main(String[] args) throws InterruptedException {
int size = 1_000_000;
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = i;
}
PartSumTask task1 = new PartSumTask(array, 0, 333_333);
PartSumTask task2 = new PartSumTask(array, 333_334, 666_666);
PartSumTask task3 = new PartSumTask(array, 666_667, 999_999);
Thread t1 = new Thread(task1);
Thread t2 = new Thread(task2);
Thread t3 = new Thread(task3);
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
long total = task1.result + task2.result + task3.result;
System.out.println("总和为:" + total);
}
}

View File

@@ -0,0 +1,20 @@
package com.study.demo6;
public class ThreadDemo1 {
static class MyThread extends Thread {
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("当前线程:" + getName() + ",输出次数 " + i);
}
}
}
public static void main(String[] args) {
MyThread s1 = new MyThread();
MyThread s2 = new MyThread();
s1.start();
s2.start();
}
}

View File

@@ -0,0 +1,22 @@
package com.study.demo6;
public class ThreadDemo2 {
static class CountTask implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(Thread.currentThread().getName() + " 计数:" + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Thread t = new Thread(new CountTask(), "计数线程");
t.start();
}
}

View File

@@ -3,7 +3,7 @@
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
outputs = { self, nixpkgs }:
let
supportedSystems = [ "x86_64-linux" "aarch64-linux" ];
supportedSystems = [ "x86_64-linux" ];
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
pkgs = import nixpkgs { inherit self system; };
});