面向对象程序设计 课程实验 五
This commit is contained in:
parent
0fc16ef8ca
commit
9f9cbdf1b8
@ -1 +1 @@
|
|||||||
5236-499-4365-97-6843-2313-5993-7998-1455-9132-4886-8419-9757-997-5574
|
7153-767-7764-9819-6754-3453-9423-86-5411-7717-9489-5214-5481-6695-8273
|
@ -9,6 +9,7 @@ package javaAssignment_20240520;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class function_01 {
|
public class function_01 {
|
||||||
|
// 私有成员: 文件名
|
||||||
private String fileName;
|
private String fileName;
|
||||||
|
|
||||||
// 构造函数, 接收需要创建的文件名
|
// 构造函数, 接收需要创建的文件名
|
||||||
@ -26,9 +27,9 @@ public class function_01 {
|
|||||||
* */
|
* */
|
||||||
public boolean createFile(){
|
public boolean createFile(){
|
||||||
try {
|
try {
|
||||||
// 实例化 file 对象创建文件
|
// 通过 File 实例化一个 file 对象用于创建文件
|
||||||
File file = new File(fileName);
|
File file = new File(fileName);
|
||||||
// 判断文件是否创建成功
|
// 判断是否已经存在同名文件
|
||||||
if (file.createNewFile()){
|
if (file.createNewFile()){
|
||||||
System.out.println("File : " + this.fileName + " Create Done!");
|
System.out.println("File : " + this.fileName + " Create Done!");
|
||||||
return true;
|
return true;
|
||||||
|
@ -17,9 +17,9 @@ public class function_02 {
|
|||||||
private int[] randomArray = new int[MaxLength];
|
private int[] randomArray = new int[MaxLength];
|
||||||
|
|
||||||
// 构造函数, 接收需要创建的文件名
|
// 构造函数, 接收需要创建的文件名
|
||||||
public function_02(String name ){
|
// public function_02(String name ){
|
||||||
this.fileName = name;
|
// this.fileName = name;
|
||||||
}
|
// }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Function: createRandom
|
* Function: createRandom
|
||||||
@ -32,7 +32,7 @@ public class function_02 {
|
|||||||
* Return: int[] 整型数组
|
* Return: int[] 整型数组
|
||||||
* */
|
* */
|
||||||
public int[] createRandom(int min, int max){
|
public int[] createRandom(int min, int max){
|
||||||
// 通过 SecureRandom 实例化一个 secureRandom 对象用于生成随机数
|
// 通过 SecureRandom (安全随机类) 实例化一个 secureRandom 对象用于生成随机数
|
||||||
SecureRandom secureRandom = new SecureRandom();
|
SecureRandom secureRandom = new SecureRandom();
|
||||||
for (int i = 0; i < MaxLength; i++){
|
for (int i = 0; i < MaxLength; i++){
|
||||||
// 先生成 0 ~ max - min + 1 之间的随机数
|
// 先生成 0 ~ max - min + 1 之间的随机数
|
||||||
@ -50,16 +50,19 @@ public class function_02 {
|
|||||||
* Description:
|
* Description:
|
||||||
* 创建文件
|
* 创建文件
|
||||||
*
|
*
|
||||||
* Input: void
|
* Input: String name 需要创建的文件名
|
||||||
* Return: boolean 文件是否创建成功
|
* Return: boolean 文件是否创建成功
|
||||||
* */
|
* */
|
||||||
public boolean createFile(){
|
public boolean createFile(String name){
|
||||||
try {
|
try {
|
||||||
|
this.fileName = name;
|
||||||
// 通过 File 实例化 file 对象用于创建文件
|
// 通过 File 实例化 file 对象用于创建文件
|
||||||
File file = new File(fileName);
|
File file = new File(fileName);
|
||||||
// 判断文件是否创建成功
|
// 判断文件是否创建成功
|
||||||
|
// .createNewFile 方法会检查文件是否存在, 如果不存在就创建文件并返回 true
|
||||||
|
// 否则返回 false 即文件已存在
|
||||||
if (file.createNewFile()){
|
if (file.createNewFile()){
|
||||||
System.out.println("File : " + this.fileName + " Create Done!");
|
System.out.println("File: " + this.fileName + " Create Done!");
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
System.out.println("The file already exists!");
|
System.out.println("The file already exists!");
|
||||||
@ -84,9 +87,9 @@ public class function_02 {
|
|||||||
try {
|
try {
|
||||||
// 通过 BufferedWriter 实例化 writer 对象用于向文件写入
|
// 通过 BufferedWriter 实例化 writer 对象用于向文件写入
|
||||||
BufferedWriter writer = new BufferedWriter(new FileWriter(this.fileName));
|
BufferedWriter writer = new BufferedWriter(new FileWriter(this.fileName));
|
||||||
// write 方法写入数据
|
// .write 方法写入数据
|
||||||
writer.write(data);
|
writer.write(data);
|
||||||
// close 方法写入完毕关闭文件
|
// .close 方法写入完毕关闭文件
|
||||||
writer.close();
|
writer.close();
|
||||||
System.out.println("Write File Done!");
|
System.out.println("Write File Done!");
|
||||||
return true;
|
return true;
|
||||||
@ -107,7 +110,7 @@ public class function_02 {
|
|||||||
* */
|
* */
|
||||||
public int[] readFileToArray(){
|
public int[] readFileToArray(){
|
||||||
try{
|
try{
|
||||||
// 通过 BufferedReader 实例化 reader 对象用于读取文件内容
|
// 通过 BufferedReader 类实例化 reader 对象用于读取文件内容
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(this.fileName));
|
BufferedReader reader = new BufferedReader(new FileReader(this.fileName));
|
||||||
// 读取文件中一行的内容
|
// 读取文件中一行的内容
|
||||||
String line = reader.readLine();
|
String line = reader.readLine();
|
||||||
@ -115,7 +118,7 @@ public class function_02 {
|
|||||||
String[] strArray = line.split("-");
|
String[] strArray = line.split("-");
|
||||||
// 创建整型数组
|
// 创建整型数组
|
||||||
int[] intArray = new int[MaxLength];
|
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]);
|
intArray[i] = Integer.parseInt(strArray[i]);
|
||||||
}
|
}
|
||||||
@ -139,6 +142,7 @@ public class function_02 {
|
|||||||
* Return: int[] 需要排序的数组
|
* Return: int[] 需要排序的数组
|
||||||
* */
|
* */
|
||||||
public void bubbleSort(int[] array){
|
public void bubbleSort(int[] array){
|
||||||
|
// 获取输入的数组长度
|
||||||
int n = array.length;
|
int n = array.length;
|
||||||
|
|
||||||
// 冒泡排序
|
// 冒泡排序
|
||||||
|
@ -42,20 +42,22 @@ public class test {
|
|||||||
* class: function_02.java
|
* class: function_02.java
|
||||||
*
|
*
|
||||||
|
|
||||||
// 实例化第二个实验内容的对象
|
// 实例化第二个实验内容的对象 f_2
|
||||||
function_02 f_2 = new function_02("file2.txt");
|
function_02 f_2 = new function_02();
|
||||||
|
|
||||||
// 创建文件
|
// 创建文件
|
||||||
f_2.createFile();
|
f_2.createFile("file2.txt");
|
||||||
|
|
||||||
// 接收 createRandom 返回的随机数数组
|
// 接收 createRandom 返回的随机数数组
|
||||||
int[] array = f_2.createRandom(20, 9999);
|
int[] array = f_2.createRandom(20, 9999);
|
||||||
|
|
||||||
// 实例化 data 对象用转换
|
// 使用 StringBuilder 类实例化一个 data 对象用于将
|
||||||
|
// 整型数组转换为 字符串 + 分隔符的形式
|
||||||
StringBuilder data = new StringBuilder();
|
StringBuilder data = new StringBuilder();
|
||||||
|
|
||||||
// 将获取到的数组转换为字符串
|
// 将获取到的数组转换为字符串
|
||||||
for (int i = 0; i < array.length; i++){
|
for (int i = 0; i < array.length; i++){
|
||||||
|
// .append 方法将输入的字符追加到字符串末尾
|
||||||
data.append(array[i]);
|
data.append(array[i]);
|
||||||
// 插入分隔符
|
// 插入分隔符
|
||||||
if (i != array.length - 1){
|
if (i != array.length - 1){
|
||||||
@ -63,7 +65,7 @@ public class test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将字符串数组写入文件中
|
// .toString 方法返回 data 中的字符串并将字符串数组写入文件中
|
||||||
f_2.writeToFile(data.toString());
|
f_2.writeToFile(data.toString());
|
||||||
|
|
||||||
// 读取文件中内容并返回成数组
|
// 读取文件中内容并返回成数组
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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());
|
||||||
|
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package javaImplementsTemplate_20240514;
|
package javaPackage_20240514;
|
||||||
|
|
||||||
public class Bus implements Charge{
|
public class Bus implements Charge{
|
||||||
@Override
|
@Override
|
@ -1,4 +1,4 @@
|
|||||||
package javaImplementsTemplate_20240514;
|
package javaPackage_20240514;
|
||||||
|
|
||||||
public interface Charge {
|
public interface Charge {
|
||||||
public void charge();
|
public void charge();
|
@ -1,4 +1,4 @@
|
|||||||
package javaImplementsTemplate_20240514;
|
package javaPackage_20240514;
|
||||||
|
|
||||||
public class Cinema implements Charge, Play{
|
public class Cinema implements Charge, Play{
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package javaImplementsTemplate_20240514;
|
package javaPackage_20240514;
|
||||||
|
|
||||||
public interface Play {
|
public interface Play {
|
||||||
public void play();
|
public void play();
|
@ -1,4 +1,4 @@
|
|||||||
package javaImplementsTemplate_20240514;
|
package javaPackage_20240514;
|
||||||
|
|
||||||
public class Taxi implements Play{
|
public class Taxi implements Play{
|
||||||
|
|
@ -4,7 +4,7 @@
|
|||||||
* Code by: JRNitre
|
* Code by: JRNitre
|
||||||
* */
|
* */
|
||||||
|
|
||||||
package javaImplementsTemplate_20240514;
|
package javaPackage_20240514;
|
||||||
|
|
||||||
public class test {
|
public class test {
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user