Java 课程作业 20240520 实验四
This commit is contained in:
parent
b6ee03b691
commit
0ee9f26fee
@ -3,6 +3,7 @@
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/ch11" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
|
@ -0,0 +1,31 @@
|
||||
package ch11_56.test1;
|
||||
|
||||
// 泛型
|
||||
|
||||
public class App11_1 <T>{
|
||||
private T obj;
|
||||
|
||||
public static void main (String[] args){
|
||||
// 两种方式均可
|
||||
// 实例化 name 对象
|
||||
App11_1<String> name = new App11_1<>();
|
||||
/* App11_1<String> name = new App11_1<String>(); */
|
||||
|
||||
// 实例化 age 对象
|
||||
App11_1<Integer> age = new App11_1<>();
|
||||
|
||||
name.setObj("Todd");
|
||||
System.out.println("Name: " + name.getObj());
|
||||
|
||||
age.setObj(114514);
|
||||
System.out.println("Age: " + age.getObj());
|
||||
}
|
||||
|
||||
public T getObj(){
|
||||
return obj;
|
||||
}
|
||||
|
||||
public void setObj(T obj){
|
||||
this.obj = obj;
|
||||
}
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
package javaAssignment_20240514;
|
||||
|
||||
public class bus implements charge{
|
||||
@Override
|
||||
public void charge() {
|
||||
System.out.println("publicBus: 1$/sheet, No Limit Kilometer");
|
||||
public class bus implements charge{
|
||||
@Override
|
||||
public void charge() {
|
||||
System.out.println("publicBus: 1$/sheet, No Limit Kilometer");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ public class test{
|
||||
* class:
|
||||
* person.java
|
||||
* student.java
|
||||
*
|
||||
public static void main (String[] args){
|
||||
student s1 = new student("test", "man", 10, 101, 80, 60, 50);
|
||||
|
||||
@ -35,6 +36,11 @@ public class test{
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
* class:
|
||||
* goods.java
|
||||
* market.java
|
||||
* person_no3.java
|
||||
public static void main (String[] args){
|
||||
goods g1 = new goods("black_man");
|
||||
|
||||
@ -55,4 +61,5 @@ public class test{
|
||||
pn1.setName("todd");
|
||||
pn1.shopping();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Function: Java 课程实验 四 - 第一题部分
|
||||
* Date: 2024 05 20
|
||||
* Code by: JRNitre
|
||||
* */
|
||||
|
||||
package javaAssignment_20240520;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.IOException;
|
||||
|
||||
public class function_01 {
|
||||
private String fileName;
|
||||
|
||||
// 构造函数, 接收需要创建的文件名
|
||||
public function_01(String name ){
|
||||
this.fileName = name;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: createFile
|
||||
* Description:
|
||||
* 创建文件
|
||||
*
|
||||
* Input: void
|
||||
* Return: boolean 文件是否创建成功
|
||||
* */
|
||||
public boolean createFile(){
|
||||
try {
|
||||
// 实例化 file 对象创建文件
|
||||
File file = new File(fileName);
|
||||
// 判断文件是否创建成功
|
||||
if (file.createNewFile()){
|
||||
System.out.println("File : " + this.fileName + " Create Done!");
|
||||
return true;
|
||||
} else {
|
||||
System.out.println("The file already exists!");
|
||||
return false;
|
||||
}
|
||||
} catch (IOException ioe){
|
||||
System.out.println("Error");
|
||||
ioe.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: writeToFile
|
||||
* Description:
|
||||
* 覆盖写入内容
|
||||
*
|
||||
* Input: String 需要写入的字符串
|
||||
* Return: boolean 是否写入成功
|
||||
* */
|
||||
public boolean writeToFile(String text){
|
||||
try {
|
||||
// 实例化 writer 对象指定需要写入的文件
|
||||
FileWriter writer = new FileWriter(this.fileName);
|
||||
// write 方法写入字符串
|
||||
writer.write(text);
|
||||
// 写入完毕关闭文件
|
||||
writer.close();
|
||||
// 输出提示
|
||||
System.out.println("Write to File : " + this.fileName + " \"" + text + "\"");
|
||||
return true;
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("Write Error!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: addToFile
|
||||
* Description:
|
||||
* 追加写入内容
|
||||
*
|
||||
* Input: String 需要写入的字符串
|
||||
* Return: boolean 是否写入成功
|
||||
*
|
||||
* Other:
|
||||
* 该方法与 writeToFile 实现原理相同
|
||||
* 构造 FileWriter 时传入的参数中 append 可以通过 boolean 控制追加写入还是覆盖写入
|
||||
* true: 追加
|
||||
* false: 覆盖
|
||||
* */
|
||||
public boolean addToFile(String text){
|
||||
try {
|
||||
FileWriter writer = new FileWriter(this.fileName, true);
|
||||
writer.write(text);
|
||||
writer.close();
|
||||
System.out.println("Add to File : " + this.fileName + " \"" + text + "\"");
|
||||
return true;
|
||||
} catch (IOException ioe){
|
||||
System.out.println("Add Error!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: getFile
|
||||
* Description:
|
||||
* 获取文件内容
|
||||
*
|
||||
* Input: void
|
||||
* Return: String 返回文件内容
|
||||
* */
|
||||
public String getFile(){
|
||||
// 实例化 content 对象用于存储文件内容
|
||||
StringBuilder content = new StringBuilder();
|
||||
try {
|
||||
// 实例化 reader 对象用于读取文件内容
|
||||
BufferedReader reader = new BufferedReader(new FileReader(this.fileName));
|
||||
// line 用于存储每一行内容
|
||||
String line;
|
||||
// while 判断行尾不为空 执行循环
|
||||
while ((line = reader.readLine()) != null) {
|
||||
// 将每一行的字符串拼合至 content 中
|
||||
content.append(line);
|
||||
// 通过 System.lineSeparator() 追加一个换行符
|
||||
content.append(System.lineSeparator());
|
||||
}
|
||||
// 关闭文件
|
||||
reader.close();
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("Read File Error!");
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
// 返回读取到的字符串
|
||||
return content.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Function: Java 课程实验 四 - 第二题部分
|
||||
* Date: 2024 05 20
|
||||
* Code by: JRNitre
|
||||
* */
|
||||
|
||||
package javaAssignment_20240520;
|
||||
|
||||
import java.io.*;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class function_02 {
|
||||
private String fileName;
|
||||
private int[] randomArray = new int[15];
|
||||
|
||||
// 构造函数, 接收需要创建的文件名
|
||||
public function_02(String name ){
|
||||
this.fileName = name;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: createRandom
|
||||
* Description:
|
||||
* 生成 15 个随机整数, 并存储到数组中
|
||||
*
|
||||
* Input:
|
||||
* min 随机数最小值
|
||||
* max 随机数最大值
|
||||
* Return: int[] 整型数组
|
||||
* */
|
||||
public int[] createRandom(int min, int max){
|
||||
// 实例化一个 secureRandom 用于生成随机数
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
for (int i = 0; i < 15; i++){
|
||||
// 先生成 0 ~ max - min + 1 之间的随机数
|
||||
// 后将生成结果 + min
|
||||
randomArray[i] = min + secureRandom.nextInt(max - min + 1);
|
||||
}
|
||||
System.out.println("Create Random Array Done!");
|
||||
return randomArray;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: createFile
|
||||
* Description:
|
||||
* 创建文件
|
||||
*
|
||||
* Input: void
|
||||
* Return: boolean 文件是否创建成功
|
||||
* */
|
||||
public boolean createFile(){
|
||||
try {
|
||||
// 实例化 file 对象创建文件
|
||||
File file = new File(fileName);
|
||||
// 判断文件是否创建成功
|
||||
if (file.createNewFile()){
|
||||
System.out.println("File : " + this.fileName + " Create Done!");
|
||||
return true;
|
||||
} else {
|
||||
System.out.println("The file already exists!");
|
||||
return false;
|
||||
}
|
||||
} catch (IOException ioe){
|
||||
System.out.println("Create File Error!");
|
||||
ioe.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: writeToFile
|
||||
* Description:
|
||||
* 向文件写入数据
|
||||
*
|
||||
* Input: data 需要写入的数据
|
||||
* Return: boolean 是否写入成功
|
||||
* */
|
||||
public boolean writeToFile(String data){
|
||||
try {
|
||||
// 实例化 writer 对象用于写入
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(this.fileName));
|
||||
// 写入数据
|
||||
writer.write(data);
|
||||
// 写入完毕关闭文件
|
||||
writer.close();
|
||||
System.out.println("Write File Done!");
|
||||
return true;
|
||||
} catch (IOException ioe){
|
||||
System.out.println("Write File Error!");
|
||||
ioe.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: readFileToArray
|
||||
* Description:
|
||||
* 读取文件中的内容并返回数组
|
||||
*
|
||||
* Input: void
|
||||
* Return: int[] 返回的数组
|
||||
* */
|
||||
public int[] readFileToArray(){
|
||||
try{
|
||||
// 实例化 reader 用于读取文件内容
|
||||
BufferedReader reader = new BufferedReader(new FileReader(this.fileName));
|
||||
// 读取一行的内容
|
||||
String line = reader.readLine();
|
||||
// 以 - 为分隔符分割字符串
|
||||
String[] strArray = line.split("-");
|
||||
// 创建整型数组
|
||||
int[] intArray = new int[15];
|
||||
for (int i = 0; i < 15; i++){
|
||||
// 将字符串数组转换为整型赋值给整型数组
|
||||
intArray[i] = Integer.parseInt(strArray[i]);
|
||||
}
|
||||
System.out.println("Reade File to Array Done!");
|
||||
// 返回数组
|
||||
return intArray;
|
||||
} catch (IOException ioe){
|
||||
System.out.println("Reade File to Array Error!");
|
||||
ioe.printStackTrace();
|
||||
// 发生错误时返回一个空数组
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: bubbleSort
|
||||
* Description:
|
||||
* 冒泡排序算法
|
||||
*
|
||||
* Input: void
|
||||
* Return: int[] 需要排序的数组
|
||||
* */
|
||||
public void bubbleSort(int[] array){
|
||||
for (int i = 0; !(i >= array.length - 1); i++){
|
||||
for (int j = 0; j < array.length - i - 1; j++){
|
||||
if (array[j] > array[j + 1]) {
|
||||
int temp = array[j];
|
||||
array[j] = array[j + 1];
|
||||
array[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Bubble Sort Done!");
|
||||
for (int i = 0; i < array.length; i++){
|
||||
System.out.print(array[i]);
|
||||
if (i != array.length - 1){
|
||||
System.out.print("-");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Function: Java 课程实验 四
|
||||
* Date: 2024 05 20
|
||||
* Code by: JRNitre
|
||||
* */
|
||||
|
||||
package javaAssignment_20240520;
|
||||
|
||||
public class test {
|
||||
public static void main (String[] args){
|
||||
|
||||
/*
|
||||
* 1. 利用文件输入输出流创建一个文件 file1.txt
|
||||
* 2. 写入字符 “文件已被成功创建!”
|
||||
* 3. 文件输入输出流打开文件 file1.txt
|
||||
* 4. 在文件末尾追加一行字符串 “又添加了一行文字”
|
||||
* 5. 打开 file1.txt 读取内容并且显示在屏幕上
|
||||
*
|
||||
* class: function_01.java
|
||||
*
|
||||
|
||||
// 实例化第一个实验内容的对象
|
||||
function_01 f_1 = new function_01("file1.txt");
|
||||
// 创建文件
|
||||
f_1.createFile();
|
||||
// 覆盖写入字符串
|
||||
f_1.writeToFile("文件已被成功创建!");
|
||||
// 追加写入字符串
|
||||
f_1.addToFile("又添加了一行文字!");
|
||||
// 输出文件的内容
|
||||
System.out.println(f_1.getFile());
|
||||
|
||||
*/
|
||||
|
||||
/* ( つ•̀ω•́)つ :-D */
|
||||
|
||||
/*
|
||||
* 1. 产生 15 个 20~9999 之间的随机整数
|
||||
* 2. 利用 BufferedWriter 类将其写入文件 file2.txt 中
|
||||
* 3. 读取该文件中的数据并升序排序
|
||||
*
|
||||
* class: function_02.java
|
||||
*
|
||||
|
||||
// 实例化第二个实验内容的对象
|
||||
function_02 f_2 = new function_02("file2.txt");
|
||||
|
||||
// 创建文件
|
||||
f_2.createFile();
|
||||
|
||||
// 接收 createRandom 返回的随机数数组
|
||||
int[] array = f_2.createRandom(20, 9999);
|
||||
|
||||
// 实例化 data 对象用转换
|
||||
StringBuilder data = new StringBuilder();
|
||||
|
||||
// 将获取到的数组转换为字符串
|
||||
for (int i = 0; i < array.length; i++){
|
||||
data.append(array[i]);
|
||||
// 插入分隔符
|
||||
if (i != array.length - 1){
|
||||
data.append("-");
|
||||
}
|
||||
}
|
||||
|
||||
// 将字符串数组写入文件中
|
||||
f_2.writeToFile(data.toString());
|
||||
|
||||
// 读取文件中内容并返回成数组
|
||||
int[] array_copy = f_2.readFileToArray();
|
||||
|
||||
// 调用冒泡排序算法进行排序
|
||||
f_2.bubbleSort(array_copy);
|
||||
|
||||
*/
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package javaImplementsTemplate_20240514;
|
||||
|
||||
public class Bus implements Charge{
|
||||
@Override
|
||||
public void charge() {
|
||||
System.out.println("PublicBus: 1$/sheet, no limit kilometers");
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package javaImplementsTemplate_20240514;
|
||||
|
||||
public interface Charge {
|
||||
public void charge();
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package javaImplementsTemplate_20240514;
|
||||
|
||||
public class Cinema implements Charge, Play{
|
||||
|
||||
public void charge() {
|
||||
System.out.println("Cinema: 30$/sheet, enjoy half price with student id");
|
||||
}
|
||||
|
||||
public void play(){
|
||||
System.out.println("Showing movie");
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package javaImplementsTemplate_20240514;
|
||||
|
||||
public interface Play {
|
||||
public void play();
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package javaImplementsTemplate_20240514;
|
||||
|
||||
public class Taxi implements Play{
|
||||
|
||||
public void play(){
|
||||
System.out.println("Taxi: 1.6$/kilometer, Starting 3 kilometer");
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Function: 20240516 面向对象程序设计课程实验 NO.2
|
||||
* Date: 2024 05 16
|
||||
* Code by: JRNitre
|
||||
* */
|
||||
|
||||
package javaImplementsTemplate_20240514;
|
||||
|
||||
public class test {
|
||||
|
||||
/*
|
||||
* 1. 创建一个收费接口 Charge, 其中有一个抽象方法 charge();
|
||||
* 2. 创建另一个收费接口 Play, 其中有一个抽象方法 play();
|
||||
*
|
||||
* 3. 声明 Bus 类来实现接口 Charge, 对于 Bus 类中的 charge(); 实现为输出:
|
||||
* "公共汽车: 1元/张, 不计公里数" PublicBus: 1 $/sheet no limit kilometers
|
||||
* 4. 声明 Taxi 类来实现接口 Charge, 对于 Taxi 中的 charge(); 实现为输出:
|
||||
* "出租车: 1.6元/公里, 起价3公里" Taxi: 1.6$/Kilometer, Starting 3 Kilometer
|
||||
* 5. 声明 Cinema 类来实现接口 Charge, 对于 Cinema 中的 charge(); 实现为输出:
|
||||
* "解放电影院: 30元/张, 凭学生证享受半价" Cinema: 30$/sheet, Enjoy half price with student ID
|
||||
* 对于 Cinema 中的 play(); 实现为输出:
|
||||
* "正在放映电影" Showing movie
|
||||
*
|
||||
* 6. 在主类中创建对象, 输出结果
|
||||
* */
|
||||
|
||||
public static void main(String[] args){
|
||||
Bus b1 = new Bus();
|
||||
Taxi t1 = new Taxi();
|
||||
Cinema c1 = new Cinema();
|
||||
|
||||
b1.charge();
|
||||
t1.play();
|
||||
|
||||
c1.charge();
|
||||
c1.play();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user