From 9f9cbdf1b89fdce276729776e17b96253759e220 Mon Sep 17 00:00:00 2001 From: JRNitre Date: Tue, 28 May 2024 18:37:54 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9D=A2=E5=90=91=E5=AF=B9=E8=B1=A1=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E8=AE=BE=E8=AE=A1=20=E8=AF=BE=E7=A8=8B=E5=AE=9E?= =?UTF-8?q?=E9=AA=8C=20=E4=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../untitled/file2.txt | 2 +- .../javaAssignment_20240520/function_01.java | 5 +- .../javaAssignment_20240520/function_02.java | 28 ++++--- .../src/javaAssignment_20240520/test.java | 12 +-- .../src/javaAssignment_20240528/Bank.java | 32 ++++++++ .../src/javaAssignment_20240528/Customer.java | 41 ++++++++++ .../javaAssignment_20240528/FlightSystem.java | 35 ++++++++ .../src/javaAssignment_20240528/test.java | 79 +++++++++++++++++++ .../Bus.java | 2 +- .../Charge.java | 2 +- .../Cinema.java | 2 +- .../Play.java | 2 +- .../Taxi.java | 2 +- .../test.java | 2 +- .../src/javaPackage_20240524/function.java | 18 +++++ .../src/javaPackage_20240524/test.java | 14 ++++ 16 files changed, 252 insertions(+), 26 deletions(-) create mode 100644 01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/Bank.java create mode 100644 01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/Customer.java create mode 100644 01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/FlightSystem.java create mode 100644 01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/test.java rename 01_Intellij_Java_universal_20240419/untitled/src/{javaImplementsTemplate_20240514 => javaPackage_20240514}/Bus.java (79%) rename 01_Intellij_Java_universal_20240419/untitled/src/{javaImplementsTemplate_20240514 => javaPackage_20240514}/Charge.java (57%) rename 01_Intellij_Java_universal_20240419/untitled/src/{javaImplementsTemplate_20240514 => javaPackage_20240514}/Cinema.java (85%) rename 01_Intellij_Java_universal_20240419/untitled/src/{javaImplementsTemplate_20240514 => javaPackage_20240514}/Play.java (55%) rename 01_Intellij_Java_universal_20240419/untitled/src/{javaImplementsTemplate_20240514 => javaPackage_20240514}/Taxi.java (77%) rename 01_Intellij_Java_universal_20240419/untitled/src/{javaImplementsTemplate_20240514 => javaPackage_20240514}/test.java (96%) create mode 100644 01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240524/function.java create mode 100644 01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240524/test.java diff --git a/01_Intellij_Java_universal_20240419/untitled/file2.txt b/01_Intellij_Java_universal_20240419/untitled/file2.txt index ea719de..2ef2887 100644 --- a/01_Intellij_Java_universal_20240419/untitled/file2.txt +++ b/01_Intellij_Java_universal_20240419/untitled/file2.txt @@ -1 +1 @@ -5236-499-4365-97-6843-2313-5993-7998-1455-9132-4886-8419-9757-997-5574 \ No newline at end of file +7153-767-7764-9819-6754-3453-9423-86-5411-7717-9489-5214-5481-6695-8273 \ No newline at end of file diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_01.java b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_01.java index e3b3ef1..b4d2d4e 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_01.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_01.java @@ -9,6 +9,7 @@ package javaAssignment_20240520; import java.io.*; public class function_01 { + // 私有成员: 文件名 private String fileName; // 构造函数, 接收需要创建的文件名 @@ -26,9 +27,9 @@ public class function_01 { * */ public boolean createFile(){ try { - // 实例化 file 对象创建文件 + // 通过 File 实例化一个 file 对象用于创建文件 File file = new File(fileName); - // 判断文件是否创建成功 + // 判断是否已经存在同名文件 if (file.createNewFile()){ System.out.println("File : " + this.fileName + " Create Done!"); return true; diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_02.java b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_02.java index 077ca90..53317bb 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_02.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_02.java @@ -17,9 +17,9 @@ public class function_02 { private int[] randomArray = new int[MaxLength]; // 构造函数, 接收需要创建的文件名 - public function_02(String name ){ - this.fileName = name; - } + // public function_02(String name ){ + // this.fileName = name; + // } /* * Function: createRandom @@ -32,7 +32,7 @@ public class function_02 { * Return: int[] 整型数组 * */ public int[] createRandom(int min, int max){ - // 通过 SecureRandom 实例化一个 secureRandom 对象用于生成随机数 + // 通过 SecureRandom (安全随机类) 实例化一个 secureRandom 对象用于生成随机数 SecureRandom secureRandom = new SecureRandom(); for (int i = 0; i < MaxLength; i++){ // 先生成 0 ~ max - min + 1 之间的随机数 @@ -50,16 +50,19 @@ public class function_02 { * Description: * 创建文件 * - * Input: void - * Return: boolean 文件是否创建成功 + * Input: String name 需要创建的文件名 + * Return: boolean 文件是否创建成功 * */ - public boolean createFile(){ + public boolean createFile(String name){ try { + this.fileName = name; // 通过 File 实例化 file 对象用于创建文件 File file = new File(fileName); // 判断文件是否创建成功 + // .createNewFile 方法会检查文件是否存在, 如果不存在就创建文件并返回 true + // 否则返回 false 即文件已存在 if (file.createNewFile()){ - System.out.println("File : " + this.fileName + " Create Done!"); + System.out.println("File: " + this.fileName + " Create Done!"); return true; } else { System.out.println("The file already exists!"); @@ -84,9 +87,9 @@ public class function_02 { try { // 通过 BufferedWriter 实例化 writer 对象用于向文件写入 BufferedWriter writer = new BufferedWriter(new FileWriter(this.fileName)); - // write 方法写入数据 + // .write 方法写入数据 writer.write(data); - // close 方法写入完毕关闭文件 + // .close 方法写入完毕关闭文件 writer.close(); System.out.println("Write File Done!"); return true; @@ -107,7 +110,7 @@ public class function_02 { * */ public int[] readFileToArray(){ try{ - // 通过 BufferedReader 实例化 reader 对象用于读取文件内容 + // 通过 BufferedReader 类实例化 reader 对象用于读取文件内容 BufferedReader reader = new BufferedReader(new FileReader(this.fileName)); // 读取文件中一行的内容 String line = reader.readLine(); @@ -115,7 +118,7 @@ public class function_02 { String[] strArray = line.split("-"); // 创建整型数组 int[] intArray = new int[MaxLength]; - for (int i = 0; i < 15; i++){ + for (int i = 0; i < MaxLength; i++){ // 将字符串数组转换为整型赋值给整型数组 intArray[i] = Integer.parseInt(strArray[i]); } @@ -139,6 +142,7 @@ public class function_02 { * Return: int[] 需要排序的数组 * */ public void bubbleSort(int[] array){ + // 获取输入的数组长度 int n = array.length; // 冒泡排序 diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/test.java b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/test.java index 1680f79..ee2b1ac 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/test.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/test.java @@ -42,20 +42,22 @@ public class test { * class: function_02.java * - // 实例化第二个实验内容的对象 - function_02 f_2 = new function_02("file2.txt"); + // 实例化第二个实验内容的对象 f_2 + function_02 f_2 = new function_02(); // 创建文件 - f_2.createFile(); + f_2.createFile("file2.txt"); // 接收 createRandom 返回的随机数数组 int[] array = f_2.createRandom(20, 9999); - // 实例化 data 对象用转换 + // 使用 StringBuilder 类实例化一个 data 对象用于将 + // 整型数组转换为 字符串 + 分隔符的形式 StringBuilder data = new StringBuilder(); // 将获取到的数组转换为字符串 for (int i = 0; i < array.length; i++){ + // .append 方法将输入的字符追加到字符串末尾 data.append(array[i]); // 插入分隔符 if (i != array.length - 1){ @@ -63,7 +65,7 @@ public class test { } } - // 将字符串数组写入文件中 + // .toString 方法返回 data 中的字符串并将字符串数组写入文件中 f_2.writeToFile(data.toString()); // 读取文件中内容并返回成数组 diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/Bank.java b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/Bank.java new file mode 100644 index 0000000..0cbf47f --- /dev/null +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/Bank.java @@ -0,0 +1,32 @@ +/* + * Function: Java 课程实验 五 - 第二题 类 二 + * Date: 2024 05 28 + * Code by: JRNitre + * */ + +package javaAssignment_20240528; + +import java.io.PrintStream; + +public class Bank { + // 银行名 + private String bankName; + // 银行余额 + private static int money = 0; + + // 构造函数 + public Bank (String bankName){ + this.bankName = bankName; + } + + // 获取银行当前余额 + public int getBankBalance (){ + return money; + } + + // 向银行存钱 + // static 可以实现在没有实例化对象时就调用这个方法 + public static synchronized void Funds (int funds){ + money += funds; + } +} diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/Customer.java b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/Customer.java new file mode 100644 index 0000000..e1b816b --- /dev/null +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/Customer.java @@ -0,0 +1,41 @@ +/* + * Function: Java 课程实验 五 - 第二题 类 一 + * Date: 2024 05 28 + * Code by: JRNitre + * */ + +package javaAssignment_20240528; + +public class Customer implements Runnable{ + // 用户姓名 + private String customerName; + // 存取次数 + private int customerNum; + + // 构造函数 + public Customer (String customerName){ + this.customerName = customerName; + } + + // 重写 run 方法 + @Override + public void run (){ + // 重复指定次数 + for (int i = 0; i < this.customerNum; i++){ + // 输出用户存款信息 + System.out.println("User: " + this.customerName + " Deposit 100"); + // 调用存款方法 + deposit(100); + } + } + + // 存款方法 + private synchronized void deposit (int amount){ + Bank.Funds(amount); + } + + // 设置存款次数 + public void setDepositNum(int num){ + this.customerNum = num; + } +} diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/FlightSystem.java b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/FlightSystem.java new file mode 100644 index 0000000..afbed13 --- /dev/null +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/FlightSystem.java @@ -0,0 +1,35 @@ +/* + * Function: Java 课程实验 五 - 第一题 类 + * Date: 2024 05 28 + * Code by: JRNitre + * */ + +package javaAssignment_20240528; + +public class FlightSystem implements Runnable{ + // 售票窗口名 + private String windowName; + // 剩余票数 + private int tickets = 20; + + // 构造函数 + public FlightSystem(String windowName){ + this.windowName = windowName; + } + + // 重写 run 方法 + @Override + public void run (){ + while (tickets > 0){ + Tickets(); + } + } + + // 售票方法 + private synchronized void Tickets (){ + if (this.tickets > 0){ + --this.tickets; + System.out.println("Window: " + this.windowName + " Tickets were sold successfully Now remainder" + this.tickets); + } + } +} diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/test.java b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/test.java new file mode 100644 index 0000000..8febc67 --- /dev/null +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240528/test.java @@ -0,0 +1,79 @@ +/* + * Function: Java 课程实验 五 + * Date: 2024 05 28 + * Code by: JRNitre + * */ + +package javaAssignment_20240528; + +public class test { + public static void main (String[] args){ + /* + * 用 Runnable 接口程序来模拟航班售票系统 + * 1. 实现3个售票窗口发售某次航班的20张机票 + * 2. 一个售票窗口用一个线程来表示。 + * + * Class: FlightSystem + * + + FlightSystem window_1 = new FlightSystem("Window_alpha"); + FlightSystem window_2 = new FlightSystem("Window_bravo"); + FlightSystem window_3 = new FlightSystem("Window_charlie"); + + Thread t_1 = new Thread(window_1); + Thread t_2 = new Thread(window_2); + Thread t_3 = new Thread(window_3); + + t_1.start(); + t_2.start(); + t_3.start(); + + */ + + /* ( つ•̀ω•́)つ :-D */ + + /* + * 1. 设某家银行可接受用户的存款,每进行一次存款,便可计算机出存款的总额。 + * 2. 现有两名顾客,每人分3次、每次存入100元。 + * + * Class + * 1. Bank.java + * 2. Customer.java + * + + // 通过 Bank 类实例化 b_1 对象 + Bank b_1 = new Bank("Best Nigger Bank"); + + // 通过 Customer 实例化两个用户对象 + Customer c_1 = new Customer("Todd"); + Customer c_2 = new Customer("KoDick"); + + // 调用其中的方法设置每个人的存款次数 + c_1.setDepositNum(3); + c_2.setDepositNum(3); + + // 通过 Thread 实例化两个线程类 + Thread t_1 = new Thread(c_1); + Thread t_2 = new Thread(c_2); + + // 在开始存款前输出一次银行当前的余额 + System.out.println("Now Bank Balance: " + b_1.getBankBalance()); + + // 开始执行两个线程 + t_1.start(); + t_2.start(); + + // 等待两个线程执行完毕 + try { + t_1.join(); + t_2.join(); + } catch (InterruptedException e){ + e.printStackTrace(); + } + + // 输出当前银行余额 + System.out.println("Now Bank Balance: " + b_1.getBankBalance()); + + */ + } +} diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Bus.java b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Bus.java similarity index 79% rename from 01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Bus.java rename to 01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Bus.java index 7b3bfe2..593ed04 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Bus.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Bus.java @@ -1,4 +1,4 @@ -package javaImplementsTemplate_20240514; +package javaPackage_20240514; public class Bus implements Charge{ @Override diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Charge.java b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Charge.java similarity index 57% rename from 01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Charge.java rename to 01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Charge.java index f44e41e..6b8816c 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Charge.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Charge.java @@ -1,4 +1,4 @@ -package javaImplementsTemplate_20240514; +package javaPackage_20240514; public interface Charge { public void charge(); diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Cinema.java b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Cinema.java similarity index 85% rename from 01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Cinema.java rename to 01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Cinema.java index cb12695..ae315f5 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Cinema.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Cinema.java @@ -1,4 +1,4 @@ -package javaImplementsTemplate_20240514; +package javaPackage_20240514; public class Cinema implements Charge, Play{ diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Play.java b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Play.java similarity index 55% rename from 01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Play.java rename to 01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Play.java index 1bd6467..003ff2a 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Play.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Play.java @@ -1,4 +1,4 @@ -package javaImplementsTemplate_20240514; +package javaPackage_20240514; public interface Play { public void play(); diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Taxi.java b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Taxi.java similarity index 77% rename from 01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Taxi.java rename to 01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Taxi.java index 07ceb97..f9060be 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/Taxi.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/Taxi.java @@ -1,4 +1,4 @@ -package javaImplementsTemplate_20240514; +package javaPackage_20240514; public class Taxi implements Play{ diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/test.java b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/test.java similarity index 96% rename from 01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/test.java rename to 01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/test.java index 1a0cdfc..41ad0e5 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaImplementsTemplate_20240514/test.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240514/test.java @@ -4,7 +4,7 @@ * Code by: JRNitre * */ -package javaImplementsTemplate_20240514; +package javaPackage_20240514; public class test { diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240524/function.java b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240524/function.java new file mode 100644 index 0000000..402715f --- /dev/null +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240524/function.java @@ -0,0 +1,18 @@ +package javaPackage_20240524; + +import java.security.SecureRandom; + +public class function { + private int ArrayMaxLength; + + public int[] createRandomArray(int num, int min, int max){ + this.ArrayMaxLength = num; + int[] array = new int[this.ArrayMaxLength]; + SecureRandom secureRandom = new SecureRandom(); + for (int i = 0; i < this.ArrayMaxLength; i++){ + array[i] = min + secureRandom.nextInt(max - min + 1); + } + return array; + } + +} diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240524/test.java b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240524/test.java new file mode 100644 index 0000000..776aa33 --- /dev/null +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaPackage_20240524/test.java @@ -0,0 +1,14 @@ +package javaPackage_20240524; + +public class test { + public static void main (String[] args){ + /* + * 1. 产生 15 个 20~9999 之间的随机整数 + * 2. 利用 BufferedWriter 类将其写入文件 file2.txt 中 + * 3. 读取该文件中的数据并升序排序 + */ + function f_1 = new function(); + + f_1.createRandomArray(15, 20, 9999); + } +}