From 9c78e2a16f9a72566bcb5f4d3cf1c946b3b62efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=98=89=E4=B9=90?= Date: Fri, 12 May 2023 13:38:35 +0800 Subject: [PATCH 1/2] =?UTF-8?q?509=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../509\344\275\234\344\270\232.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 "13 \350\224\241\345\230\211\344\271\220/509\344\275\234\344\270\232.md" diff --git "a/13 \350\224\241\345\230\211\344\271\220/509\344\275\234\344\270\232.md" "b/13 \350\224\241\345\230\211\344\271\220/509\344\275\234\344\270\232.md" new file mode 100644 index 0000000..6c0fc6a --- /dev/null +++ "b/13 \350\224\241\345\230\211\344\271\220/509\344\275\234\344\270\232.md" @@ -0,0 +1,28 @@ +```java +package Day0509; + +import java.util.Scanner; + +public class D1 { + public static void main(String[] args) { + System.out.println("输入两个整数并求和"); + while (true) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入两个整数"); + + try { + + int a = sc.nextInt(); + int b = sc.nextInt(); + System.out.println("输入的两个数分别为"+a+"和"+b); + System.out.println("求出的和是"+(a+b)); + + } catch (Exception e) { + System.out.println("输入的是错误字符"); + } + + } + + } +} +``` \ No newline at end of file -- Gitee From cc86f8804ed08f1a5c04902b76fb2fee09916e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=98=89=E4=B9=90?= Date: Sun, 14 May 2023 22:28:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0425\346\212\275\350\261\241.md" | 89 ++ .../0428.md" | 175 ++++ .../3030\344\275\234\344\270\232.md" | 130 +++ .../404\344\275\234\344\270\232.md" | 142 +++ .../420\344\275\234\344\270\232.md" | 222 +++++ .../509\344\275\234\344\270\232.md" | 4 +- ...24\344\270\200\344\275\234\344\270\232.md" | 178 ++++ ...17\347\273\203\344\271\240\351\242\230.md" | 912 ++++++++++++++++++ ...00\351\203\250\345\217\230\351\207\217.md" | 203 ++++ .../\347\273\247\346\211\2770424.md" | 467 +++++++++ 10 files changed, 2521 insertions(+), 1 deletion(-) create mode 100644 "13 \350\224\241\345\230\211\344\271\220/0425\346\212\275\350\261\241.md" create mode 100644 "13 \350\224\241\345\230\211\344\271\220/0428.md" create mode 100644 "13 \350\224\241\345\230\211\344\271\220/3030\344\275\234\344\270\232.md" create mode 100644 "13 \350\224\241\345\230\211\344\271\220/404\344\275\234\344\270\232.md" create mode 100644 "13 \350\224\241\345\230\211\344\271\220/420\344\275\234\344\270\232.md" create mode 100644 "13 \350\224\241\345\230\211\344\271\220/\344\272\224\344\270\200\344\275\234\344\270\232.md" create mode 100644 "13 \350\224\241\345\230\211\344\271\220/\344\276\213\345\217\230\351\207\217\347\273\203\344\271\240\351\242\230.md" create mode 100644 "13 \350\224\241\345\230\211\344\271\220/\346\210\220\345\221\230\345\217\230\351\207\217\345\222\214\345\261\200\351\203\250\345\217\230\351\207\217.md" create mode 100644 "13 \350\224\241\345\230\211\344\271\220/\347\273\247\346\211\2770424.md" diff --git "a/13 \350\224\241\345\230\211\344\271\220/0425\346\212\275\350\261\241.md" "b/13 \350\224\241\345\230\211\344\271\220/0425\346\212\275\350\261\241.md" new file mode 100644 index 0000000..df883dd --- /dev/null +++ "b/13 \350\224\241\345\230\211\344\271\220/0425\346\212\275\350\261\241.md" @@ -0,0 +1,89 @@ +创建类描述黄瓜、茄子、香蕉、榴莲;它们各自拥有的属性和功能如下:(后面带小括号的是功能) + +黄瓜:颜色;重量;可以吃(){黄瓜炒蛋};可以使用(){做面膜} + +茄子:颜色;重量;可以吃(){油焖茄子};可以使用(){做中药} + +香蕉:颜色;重量;可以吃(){脆皮香蕉};可以使用(){做香蕉面膜} + +榴莲:颜色;重量;可以吃(){榴莲酥};可以使用(){砸人} + +请用继承的思想设计设计以上类型。 + +答案: + +```java +//创建类描述黄瓜、茄子、香蕉、榴莲;它们各自拥有的属性和功能如下:(后面带小括号的是功能) +//黄瓜:颜色;重量;可以吃(){黄瓜炒蛋};可以使用(){做面膜} +//茄子:颜色;重量;可以吃(){油焖茄子};可以使用(){做中药} +//香蕉:颜色;重量;可以吃(){脆皮香蕉};可以使用(){做香蕉面膜} +//榴莲:颜色;重量;可以吃(){榴莲酥};可以使用(){砸人} +//请用继承的思想设计设计以上类型。 + +public abstract class fruit { + String color; + String weight; + + public abstract void eat(); + public abstract void use(); + + +} +class brinjaul extends fruit{ + + public void eat(){ + System.out.println("茄子"+"是"+color+"的"+"重量: "+weight+" 可以吃油焖茄子"); + } + public void use(){ + System.out.println("茄子"+"是"+color+"的"+"重量: "+weight+" 可以使用茄子做中药"); + } +} +class cuke extends fruit{ + public void eat(){ + System.out.println("黄瓜"+"是"+color+"的"+"重量: "+weight+" 可以吃黄瓜炒蛋"); + } + public void use(){ + System.out.println("黄瓜"+"是"+color+"的"+"重量: "+weight+" 可以使用黄瓜做面膜"); + } +} +class banana extends fruit{ + public void eat(){ + System.out.println("香蕉"+"是"+color+"的"+"重量: "+weight+" 可以吃脆皮香蕉"); + } + public void use(){ + System.out.println("香蕉"+"是"+color+"的"+"重量: "+weight+" 可以使用香蕉做香蕉面膜"); + } +} +class durian extends fruit{ + public void eat(){ + System.out.println("榴莲"+"是"+color+"的"+"重量: "+weight+" 可以吃榴莲酥"); + } + public void use(){ + System.out.println("榴莲"+"是"+color+"的"+"重量: "+weight+" 可以使用榴莲砸人"); + } +} +class test1{ + public static void main(String[] args) { + brinjaul a=new brinjaul(); + a.color="紫色"; + a.weight="500"; + a.eat(); + a.use(); + cuke b=new cuke(); + b.color="黑色"; + b.weight="400"; + b.eat(); + b.use(); + banana c=new banana (); + c.color="黄色"; + c.weight="4552"; + c.eat(); + c.use(); + durian d=new durian(); + d.color="绿色"; + d.weight="966"; + d.use(); + d.eat(); + } +} +``` \ No newline at end of file diff --git "a/13 \350\224\241\345\230\211\344\271\220/0428.md" "b/13 \350\224\241\345\230\211\344\271\220/0428.md" new file mode 100644 index 0000000..ff3ea52 --- /dev/null +++ "b/13 \350\224\241\345\230\211\344\271\220/0428.md" @@ -0,0 +1,175 @@ +将以下描述信息转换为java代码: + +1、定义员工Employee类,该类具有如下成员: + +```java +(1) 属性:姓名(name,字符串类型),工号(workId,字符串类型),部门(dept,字符串类型),属性私有 +(2) 方法: + 1. 空参数构造和满参数构造 + 2. 提供属性的set/get方法 + 3. 定义showMsg方法抽象方法 +``` + +2、定义经理Manager类继承Employee类,该类具有如下成员: + +```java +(1) 属性:职员Clerk(该经理的职员) +(2) 方法: + 1. 空参数构造方法和满参数构造方法 + 2. 属性的get和set方法 + 3. 重写父类的showMsg方法,按照要求实现信息打印 +``` + +3、定义职员Clerk类继承Employee类,该类具有如下成员: + +```java +(1) 属性:经理Manager(该职员的经理) +(2) 方法: + 1. 空参数构造方法和满参数构造方法 + 2. 属性的get和set方法 + 3. 重写父类的showMsg方法,按照要求实现信息打印 +``` + +4、创建Test测试类,测试类中创建main方法,main方法中创建经理对象和职员对象,信息分别如下: + +```java +经理:工号为 M001,名字为 张小强,部门为 销售部 +职员:工号为 C001,名字为 李小亮,部门为 销售部 + +经理的职员为李小亮,职员的经理为张小强 +``` + +分别调用经理的showMsg方法和职员的showMsg方法打印结果: + +```java +销售部的:张小强,员工编号:M001 +他的职员是李小亮 +销售部的:李小亮,员工编号:C001 +他的经理是张小强 +``` + +答案 + +```java +package Day20230427; + +public abstract class Employee { + // (1) 属性:姓名(name,字符串类型),工号(workId,字符串类型),部门(dept,字符串类型),属性私有 + private String name; + private String workId; + private String dept; +// (2) 方法: +// 1 空参数构造和满参数构造 + + public Employee() { + } + + public Employee(String name, String workId, String dept) { + this.name = name; + this.workId = workId; + this.dept = dept; + } + // 2. 提供属性的set/get方法 + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getWorkId() { + return workId; + } + + public void setWorkId(String workId) { + this.workId = workId; + } + + public String getDept() { + return dept; + } + + public void setDept(String dept) { + this.dept = dept; + } + + // 3. 定义showMsg方法抽象方法 + public abstract void showMsg(); +} + class Manager extends Employee{ + // (1) 属性:职员Clerk(该经理的职员) + private String Clerk; +// (2) 方法: +// 1. 空参数构造方法和满参数构造方法 + + public Manager(String clerk) { + Clerk = clerk; + } + + public Manager() { + } + +// 2. 属性的get和set方法 + + public String getClerk() { + return Clerk; + } + + public void setClerk(String clerk) { + Clerk = clerk; + } + +// 3. 重写父类的showMsg方法,按照要求实现信息打印 + + @Override + public void showMsg() { + System.out.println(getName()+" "+getDept()+" "+getWorkId()+"\n"+"他的职员是李小亮"); + } +} +class Clerk extends Employee{ +// (1) 属性:经理Manager(该职员的经理) + private String Manager; +// (2)方法: +// 1. 空参数构造方法和满参数构造方法 + + public Clerk(String manager) { + Manager = manager; + } + + public Clerk() { + } + // 2. 属性的get和set方法 + + public String getManager() { + return Manager; + } + + public void setManager(String manager) { + Manager = manager; + } + +// 3. 重写父类的showMsg方法,按照要求实现信息打印 + + + @Override + public void showMsg() { + System.out.println(getName()+" "+getDept()+" "+getWorkId()+"\n"+"他的经理是张小强"); + } +} +abstract class test{ + public static void main(String[] args) { + Manager a= new Manager(); + a.setName("张小强"); + a.setWorkId("M001"); + a.setDept("销售部"); + a.showMsg(); + Clerk b=new Clerk(); + b.setName("李小亮"); + b.setWorkId("C001"); + b.setDept("销售部"); + b.showMsg(); + } +} +``` \ No newline at end of file diff --git "a/13 \350\224\241\345\230\211\344\271\220/3030\344\275\234\344\270\232.md" "b/13 \350\224\241\345\230\211\344\271\220/3030\344\275\234\344\270\232.md" new file mode 100644 index 0000000..d285160 --- /dev/null +++ "b/13 \350\224\241\345\230\211\344\271\220/3030\344\275\234\344\270\232.md" @@ -0,0 +1,130 @@ +**、判断一个字符数据是否是数字字符 ** + +**分析:** + + 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 + + 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0,并且还要下于或等于字符9即可。 + + 3、判断完成之后,打印判断的结果。 + +```java +public static void main(String[] args) { + char a = '1' ; + if (a>='0' && a<='9'){ + System.out.println("是数字字符"); + }else{ + System.out.println("不是数字字符"); + } +} +``` + +**2、判断一个字符数据是否是字母字符** + +**分析:** + + 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 + + 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符,因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z + + 3、判断完成之后,打印判断的结果。 + +```java +public static void main(String[] args) { + char a = '1' ; + if (a>='a' && a<='z' || a>='A' && a<='Z'){ + System.out.println("是字母字符"); + }else{ + System.out.println("不是字母字符"); + } +} +``` + +**3、判断指定的年份是否为闰年,请使用键盘录入** + +**分析:** + + 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 + + 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 + +```java +public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int date = scanner.nextInt(); + if ((date%4==0 && date%100!=0) || date%400==0){ + System.out.println("是闰年"); + }else{ + System.out.println("不是闰年"); + } +} +``` + +**4、判断一个数字是否为水仙花数,请使用键盘录入** + +水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, + +**分析:** + + 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 + + 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 + + 2、判断的过程 + + a) 将3位数字的每一位上的数字拆分下来 + + b) 计算每位数字的3次幂之和 + + C) 用和值 和 原来的数字进行比较 + + D) 打印判断的比较结果即可 + +```java +public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int a = scanner.nextInt(); + int a1 = ((a%10)*(a%10)*(a%10))+((a/10%10)*(a/10%10)*(a/10%10))+((a/100%10)*(a/100%10)*(a/100%10)); + String b = "" + a; + if (b.length()==3 && a1==a){ + System.out.println("是水仙花数"); + }else{ + System.out.println("不是水仙花数"); + } +} +``` + +**5、判断一个5位数字是否为回文数,使用键盘录入** + +五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 + +**分析:** + + 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 + + 2、判断的过程 + + a) 将5位数字的万、千、十、个位数拆分出来 + + b) 判断比较万位和个位 、 千位和十位是否相等 + + 3、判断完成之后,打印判断的结果。 + +```java +public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int a = scanner.nextInt(); + String b = ""+a; + int a1 = a%10; + int a2 = a/10%10; + int a3 = a/1000%10; + int a4 = a/10000%10; + if (b.length()!=5) { + System.out.println("不是五位数"); + }else if(a1==a4 && a2==a3){ + System.out.println("是回文数"); + }else{ + System.out.println("不是回文数"); + } +} +``` \ No newline at end of file diff --git "a/13 \350\224\241\345\230\211\344\271\220/404\344\275\234\344\270\232.md" "b/13 \350\224\241\345\230\211\344\271\220/404\344\275\234\344\270\232.md" new file mode 100644 index 0000000..fcda715 --- /dev/null +++ "b/13 \350\224\241\345\230\211\344\271\220/404\344\275\234\344\270\232.md" @@ -0,0 +1,142 @@ +## 冒泡法 + +```java +int []arr={3,5,2,15,38,26,27}; +System.out.println("排序前"); +for (int n:arr){ + System.out.print(n+" "); +} +for (int i = 0; i < arr.length; i++) { + for (int j = 0; j < arr.length-1; j++) { + if (arr[j]>arr[j+1]) { + int temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + } +} +System.out.println(); +System.out.println("排序后"); +for (int n:arr){ + System.out.print(n+" "); +} +``` + +## 编码题 + +**1.定义一个含有五个元素的数组,并为每个元素赋值,求数组中所有元素的最小值** + +**操作步骤:** + + 1.定义5个元素数组 + + 2.可以使用初始化数组的两种方式之一为数组元素赋值 + + 3.遍历数组求数组中的最小值 + +```java +int arr[]={5,2,3,4,1}; +int min=arr[0]; +int index=0; +for (int i = 0; i < arr.length; i++) { + if (min>arr[i]){ + index=i; + } +} +System.out.println("这个数组的最小值是"+arr[index]); +``` + +**2.需求:求出数组中索引与索引对应的元素都是奇数的元素** + +**分析:** + + 1、遍历数组 + + 2、判断索引是否是奇数(索引 % 2 != 0) + + 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) + + 4、满足条件输出结果 + +```java +int arr[]={5,2,3,4,1}; +int count=0; +System.out.println("索引值与对应元素都是奇数的数有:"); +for (int i = 0; i < arr.length; i++) { + if (i%2!=0 && arr[i]%2!=0){ + System.out.print(arr[i]+" "); + count++; + } +} +if(count==0){ + System.out.println("此条件没有符合的数字"); +} +``` + +**3.按要求在main方法中完成以下功能:** + + a. 定义一个长度为5的int型数组arr,提示用户输入5个1-60之间的数字作为数组元素 + + b. 生成2-10(范围包含2和10)之间的随机数num + + c. 遍历数组arr,筛选出数组中不是num倍数的元素并输出 + + **PS:输入的数组元素范围包括1和60,不需要代码判断** + +```java +Scanner sc = new Scanner(System.in); +Random r =new Random(); +int arr[]=new int[5]; +int count=0; +for (int i = 0; i < 5; i++) { + System.out.println("请输入第"+(i+1)+"个数字,要在1-60之间"); + arr[i]= sc.nextInt(); +} +int num=r.nextInt(8)+2; +System.out.print("是倍数的有:"); +for (int j = 0; j < arr.length; j++) { + if (arr[j]%num!=0){ + System.out.print(arr[j]+" "); + } +} +``` + +**4.有一个数组int[] arr = {9,1,3,4,54,56,23,22,20,43,45,78};,要求打印数组中能被6整除的元素。** + +```java +int arr[]={9,1,3,4,54,56,23,22,20,43,45,78}; +int count=0; +System.out.println("能被6整除的数有:"); +for (int i = 0; i < arr.length; i++) { + if(arr[i]%2==0){ + System.out.print(arr[i]+" "); + count++; + } +} +if (count==0){ + System.out.println("没有符合这个条件的数字"); +} +System.out.println(6%3); +``` + +**5.定义一个长度为20的数组,元素为20-40的随机数,要求判断指定元素在数组中出现的次**数,指定元素为键盘录入范围为20-40之间。 + +```java +Random r=new Random(); +int arr[]=new int[20]; +int count=0; +Scanner sc=new Scanner(System.in); +System.out.println("请输入指定数字,范围在20-40之间"); +int n= sc.nextInt(); +for (int i = 0; i < arr.length; i++) { + arr[i] = r.nextInt(21) + 20; + if (n == arr[i]) { + count++; + } +} +if(count==0){ + System.out.println("没有出现指定数字"); +}else{ + System.out.println("出现数字"+n+"出现了"+count+"次"); +} +``` \ No newline at end of file diff --git "a/13 \350\224\241\345\230\211\344\271\220/420\344\275\234\344\270\232.md" "b/13 \350\224\241\345\230\211\344\271\220/420\344\275\234\344\270\232.md" new file mode 100644 index 0000000..627ba4f --- /dev/null +++ "b/13 \350\224\241\345\230\211\344\271\220/420\344\275\234\344\270\232.md" @@ -0,0 +1,222 @@ +```java +public class Student { + private String name; + private String id; + private char sex; + private int age; + + public Student(String name, String id, char sex, int age) { + this.name = name; + this.id = id; + this.sex = sex; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public char getSex() { + return sex; + } + + public void setSex(char sex) { + this.sex = sex; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public String toString() { + if(name.length()==3) return id+"\t\t"+name+"\t\t"+sex+"\t\t"+age; + else return id+"\t\t"+name+"\t\t\t"+sex+"\t\t"+age; + } +} +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Stu { + public static Scanner sc = new Scanner(System.in);//扫描器 + + public static void main(String[] args) { + ArrayList stu = new ArrayList<>(); //储存学生类 + while (true) { + System.out.println("-- 欢迎使用学生管理系统 --\n" + + "\t1 添加学生信息\n" + + "\t2 删除学生信息\n" + + "\t3 修改学生信息\n" + + "\t4 查询学生信息\n" + + "\t5 查询所有学生信息\n" + + "\t6 退出系统\n" + + "-- 输入对应数字使用相关功能 --"); + String n = sc.next(); + switch (n) { + case "1": + stuadd(stu); + break; + case "2": + sture(stu); + break; + case "3": + stuset(stu); + break; + case "4": + stuget(stu); + break; + case "5": + stugetall(stu); + break; + case "6": + System.out.println("感谢使用"); + System.exit(0); + break; + default: + System.out.println("别乱jb输入"); + } + } + } + + private static void stugetall(ArrayList list) { + if (list.size() == 0) { + cls(); + System.out.println("暂无学生数据"); + } else { + cls(); + System.out.println("学号\t\t姓名\t\t\t性别\t\t年龄"); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + } + } + + private static void stuset(ArrayList list) { + if (list.size() == 0) { + cls(); + System.out.println("暂无学生数据"); + } else { + System.out.println("请输入要修改的学生学号或姓名"); + String n = sc.next(); + for (int i = 0; i < list.size(); i++) { + if (list.get(i).getName().equals(n) || list.get(i).getId().equals(n)) { + System.out.println("请输入修改后学号"); + String id = sc.next(); + System.out.println("请输入修改后姓名"); + String name = sc.next(); + System.out.println("请输入修改后性别"); + char sex = sc.next().charAt(0); + System.out.println("请输入修改后年龄"); + int age = sc.nextInt(); + for (int j = 0; j < list.size(); j++) { + if (list.get(i).getId().equals(id)) { + cls(); + list.set(i, new Student(name, id, sex, age)); + System.out.println("修改成功"); + return; + } else if (list.get(j).getId().equals(id)) { + cls(); + System.out.println("学号重复请重新修改"); + return; + } + } + cls(); + list.set(i, new Student(name, id, sex, age)); + System.out.println("修改成功"); + } + } + } + cls(); + System.out.println("无该学生"); + }//修改方法 + + private static void stuget(ArrayList list) { + if (list.size() == 0) { + cls(); + System.out.println("暂无学生数据"); + } else { + System.out.println("请输入要查询的学生学号或姓名"); + String n = sc.next(); + for (int i = 0; i < list.size(); i++) { + if (list.get(i).getName().equals(n) || list.get(i).getId().equals(n)) { + System.out.println("学号\t\t姓名\t\t\t性别\t\t年龄"); + cls(); + System.out.println(list.get(i)); + return; + } + } + cls(); + System.out.println("无该学生"); + } + }//查询方法 + + private static void sture(ArrayList list) { + if (list.size() == 0) { + cls(); + System.out.println("暂无学生数据"); + } else { + System.out.println("请输入要删除的学生学号或姓名"); + String n = sc.next(); + for (int i = 0; i < list.size(); i++) { + if (list.get(i).getName().equals(n) || list.get(i).getId().equals(n)) { + cls(); + list.remove(i); + System.out.println("删除成功"); + return; + } + } + cls(); + System.out.println("无该学生"); + } + }//删除方法 + + public static void stuadd(ArrayList list) { + System.out.println("请输入学号"); + String id = sc.next(); + System.out.println("请输入姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + System.out.println("请输入年龄"); + int age = sc.nextInt(); + for (int i = 0; i < list.size(); i++) { + if (list.get(i).getId().equals(id)) { + cls(); + System.out.println("学号重复请重新添加"); + return; + } else { + cls(); + list.add(new Student(name, id, sex, age)); + System.out.println("添加成功"); + return; + } + } + cls(); + list.add(new Student(name, id, sex, age)); + System.out.println("添加成功"); + }//添加方法 + + public static void cls() { + for (int i = 0; i < 100; i++) { + System.out.println(); + } + }//伪cls +} +``` \ No newline at end of file diff --git "a/13 \350\224\241\345\230\211\344\271\220/509\344\275\234\344\270\232.md" "b/13 \350\224\241\345\230\211\344\271\220/509\344\275\234\344\270\232.md" index 6c0fc6a..4a20e5c 100644 --- "a/13 \350\224\241\345\230\211\344\271\220/509\344\275\234\344\270\232.md" +++ "b/13 \350\224\241\345\230\211\344\271\220/509\344\275\234\344\270\232.md" @@ -1,4 +1,5 @@ ```java +```java package Day0509; import java.util.Scanner; @@ -25,4 +26,5 @@ public class D1 { } } -``` \ No newline at end of file +``` + diff --git "a/13 \350\224\241\345\230\211\344\271\220/\344\272\224\344\270\200\344\275\234\344\270\232.md" "b/13 \350\224\241\345\230\211\344\271\220/\344\272\224\344\270\200\344\275\234\344\270\232.md" new file mode 100644 index 0000000..e2c9297 --- /dev/null +++ "b/13 \350\224\241\345\230\211\344\271\220/\344\272\224\344\270\200\344\275\234\344\270\232.md" @@ -0,0 +1,178 @@ + + +**父类是:战斗机** + +1. **属性:名称,颜色,载弹量,打击半径** +2. **功能:起飞,巡航,降落,雷达扫射,开火** +3. **接口:对空,对地** +4. 子类: + 1. **空空战斗机(只对空)** + 2. **空地战斗机(能对空能对地)** + 3. **轰炸机 (只能对地)** +5. 测试类: + 1. **生成每一种战斗并属性赋值,并调用相关功能** + +父类: + +```java +public class Warcraft { + private String name; + private String colour; + private double bombLoad; + private double strikeRadius; + + public Warcraft() { + } + + public Warcraft(String name, String colour, double bombLoad, double strikeRadius) { + this.name = name; + this.colour = colour; + this.bombLoad = bombLoad; + this.strikeRadius = strikeRadius; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getColour() { + return colour; + } + + public void setColour(String colour) { + this.colour = colour; + } + + public double getBombLoad() { + return bombLoad; + } + + public void setBombLoad(double bombLoad) { + this.bombLoad = bombLoad; + } + + public double getStrikeRadius() { + return strikeRadius; + } + + public void setStrikeRadius(double strikeRadius) { + this.strikeRadius = strikeRadius; + } + + public void fly(){ + System.out.println(name + "起飞"); + } + + public void cruise(){ + System.out.println(name+"正在巡航"); + } + + public void landing(){ + System.out.println(name+"正在降落"); + } + + public void radarScanning(){ + System.out.println(name + "正在执行雷达扫描"); + } + + public void fire(){ + System.out.println(name+"正在开火"); + } +} +接口1 对空: + +public interface AirAttack { + void AirAttack(); +} +接口2 对地: + +public interface Bomb { + void Bomb(); +} +接口3 对空对地: + +public interface GroundAttack { + void GroundAttack(); +} +子类1 对空战斗机: + +public class AirToAirFighter extends Warcraft implements AirAttack { + + + public AirToAirFighter() { + } + + public AirToAirFighter(String name, String colour, double bombLoad, double strikeRadius) { + super(name, colour, bombLoad, strikeRadius); + } + + public AirToAirFighter(String s) { + } + + @Override + public void AirAttack() { + System.out.println(getName() + "正在对空扫射"); + } +} +子类2 轰炸机: + +public class Bomber extends Warcraft implements Bomb{ + public Bomber() { + } + + public Bomber(String name, String colour, double bombLoad, double strikeRadius) { + super(name, colour, bombLoad, strikeRadius); + } + + @Override + public void Bomb() { + System.out.println(getName()+"正在对地面轰炸"); + } +} +子类3 对空对地战斗机: + +public class AirToGroundFighter extends Warcraft implements GroundAttack{ + public AirToGroundFighter() { + } + + public AirToGroundFighter(String name, String colour, double bombLoad, double strikeRadius) { + super(name, colour, bombLoad, strikeRadius); + } + + @Override + public void GroundAttack() { + System.out.println(getName() + "正在上下扫射"); + } +} +测试类 + +public class Test { + public static void main(String[] args) { + //生成对空战斗机 + AirToAirFighter air = new AirToAirFighter("B-1B","黑色",1000,100); + air.fly(); + air.cruise(); + air.radarScanning(); + air.AirAttack(); + air.landing(); + //生成轰炸机 + Bomber bomber = new Bomber("B-17","白色",10000,50); + bomber.fly(); + bomber.cruise(); + bomber.radarScanning(); + bomber.Bomb(); + bomber.landing(); + //生成空地战斗机 + AirToGroundFighter groundFighter = new AirToGroundFighter("B-21","灰色",2000,50); + groundFighter.fly(); + groundFighter.cruise(); + groundFighter.radarScanning(); + groundFighter.GroundAttack(); + groundFighter.landing(); + } +} +``` \ No newline at end of file diff --git "a/13 \350\224\241\345\230\211\344\271\220/\344\276\213\345\217\230\351\207\217\347\273\203\344\271\240\351\242\230.md" "b/13 \350\224\241\345\230\211\344\271\220/\344\276\213\345\217\230\351\207\217\347\273\203\344\271\240\351\242\230.md" new file mode 100644 index 0000000..2b240a8 --- /dev/null +++ "b/13 \350\224\241\345\230\211\344\271\220/\344\276\213\345\217\230\351\207\217\347\273\203\344\271\240\351\242\230.md" @@ -0,0 +1,912 @@ +## 例变量练习题 + +### 1、圆类 + +(1)声明一个圆的图形类,包含实例变量/属性:半 + +(2)在测试类的main中,创建2个圆类的对象,并给两个圆对象的半径属性赋值,最后显示两个圆的半径值、周长和面积值 + + 提示:圆周率可以使用Math.PI + +``` +public class A2 { + public static void main(String[] args) { + A1 a1 = new A1(23); + A1 a2 = new A1(14); + a1.area(); + a2.area(); + } +} +public class A1 { + private int r; + + public int getR() { + return r; + } + + public void setR(int r) { + this.r = r; + } + + public String toString() { + return "半径"; + } + + public A1(int r) { + this.r = r; + } + public A1() { + } + + public void area(){ + System.out.println("半径为"+r+"的圆的面积为"+Math.PI*r*r+"周长为"+Math.PI*r*2); + } +} +``` + +### 2、学生类 + +(1)声明一个学生类,包含实例变量/属性:姓名和成绩 + +(2)在测试类的main中,创建2个学生类的对象,并给两个学生对象的姓名和成绩赋值,最后输出显示 + +``` +public class A1 { + private String name; + private double cj; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getCj() { + return cj; + } + + public void setCj(double cj) { + this.cj = cj; + } + + public String toString() { + return "stu"; + } + + public A1(String name, double cj) { + this.name = name; + this.cj = cj; + } + + public A1() { + } + public void a(){ + System.out.println(name+"的成绩为"+cj); + } +} +public class A2 { + public static void main(String[] args) { + A1 a1 = new A1("xm",12); + A1 a2 = new A1("xh",14); + a1.a(); + a2.a(); + } +} +``` + +### 3、日期和员工类 + +(1)声明一个MyDate类型,有属性:年,月,日 + +(2)声明另一个Employee类型,有属性:姓名(String类型),生日(MyDate类型) + +(3)在测试类中的main中,创建两个员工对象,并为他们的姓名和生日赋值,并显示 + +``` +public class MyDate { + private int year; + private int month; + private int day; + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public int getMonth() { + return month; + } + + public void setMonth(int month) { + this.month = month; + } + + public int getDay() { + return day; + } + + public void setDay(int day) { + this.day = day; + } + + public MyDate(int year, int month, int day) { + this.year = year; + this.month = month; + this.day = day; + } + + public MyDate() { + } +} +public class Employee{ + private String name; + private MyDate date; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public MyDate getDate() { + return date; + } + + public void setDate(MyDate date) { + this.date = date; + } +} +public class Main { + public static void main(String[] args) { + Employee emp1 = new Employee(); + Employee emp2 = new Employee(); + emp1.setName("小明"); + emp1.setName("小红"); + emp1.setDate(new MyDate(2013,12,24)); + emp2.setDate(new MyDate(2015,3,2)); + a(emp1); + a(emp2); + + } + public static void a(Employee a){ + System.out.println(a.getName()+"是"+a.getDate().getYear()+"年"+a.getDate().getMonth()+"月"+a.getDate().getDay()+"日出生的"); + } +} +``` + +## 实例方法的声明与调用练习 + +### 1、圆类改造 + +(1)声明一个圆的图形类,包含实例变量/属性:半径 + +(2)将圆求面积、求周长、返回圆对象信息分别封装为3个方法 + +double area():求面积 + +double perimeter():求周长 + +String getInfo():返回圆对象信息,例如:"半径:xx,周长:xx,面积:xx" + +(3)在测试类的main中,创建2个圆类的对象,并给两个圆对象的半径属性赋值,最后显示两个圆的半径值、周长和面积值 + + 提示:圆周率可以使用Math.PI + +``` +public class A1 { + private int r; + + public int getR() { + return r; + } + + public void setR(int r) { + this.r = r; + } + + public String toString() { + return "半径"; + } + + public A1(int r) { + this.r = r; + } + public A1() { + } + + public String getInfo(){ + return "半径为"+r+"的圆的面积为"+area()+"周长为"+perimeter(); + } + public double perimeter(){ + return Math.PI*r*2; + } + public double area(){ + return Math.PI*r*r; + } +} +public class A2 { + public static void main(String[] args) { + A1 a1 = new A1(23); + A1 a2 = new A1(14); + System.out.println(a1.getInfo()); + System.out.println(a2.getInfo()); + } +} +``` + +### 2、日期和员工类改造 + +(1)声明一个MyDate类型 + +- 有属性:年,月,日 +- 增加一个String getDateInfo()方法,用于返回日期对象信息,例如:xx年xx月xx日 + +(2)声明另一个Employee类型, + +- 有属性:姓名(String类型),生日(MyDate类型) +- 增加一个void setBirthday(int year, int month, int day)方法,用于给员工生日赋值 +- 增加一个String getEmpInfo()方法,用于返回员工对象信息,例如:姓名:xx,生日:xx年xx月xx日 + +(3)在测试类中的main中,创建两个员工对象,并为他们的姓名和生日赋值,并显示 + +``` +public class MyDate { + private int year; + private int month; + private int day; + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public int getMonth() { + return month; + } + + public void setMonth(int month) { + this.month = month; + } + + public int getDay() { + return day; + } + + public void setDay(int day) { + this.day = day; + } + + public MyDate(int year, int month, int day) { + this.year = year; + this.month = month; + this.day = day; + } + + public MyDate() { + } + + public String getDateInfo(){ + return year+"年"+month+"月"+day+"日"; + } +} +public class Employee{ + private String name; + private MyDate date; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public MyDate getDate() { + return date; + } + + public void setDate(MyDate date) { + this.date = date; + } + public String getEmpInfo(){ + return "姓名:"+name+"生日"+date.getDateInfo(); + } + public void setBirthday(int year, int month, int day){ + this.date=new MyDate(year,month,day); + } +} +public class Main { + public static void main(String[] args) { + Employee emp1 = new Employee(); + Employee emp2 = new Employee(); + emp1.setName("小明"); + emp2.setName("小红"); + emp1.setBirthday(2013,12,23); + emp2.setBirthday(2011,3,13); + System.out.println(emp1.getEmpInfo()); + System.out.println(emp2.getEmpInfo()); + } +} +``` + +### 3、MyInt类 + +(1)声明一个MyInt类, + +- 包含一个int类型的value属性 +- 包含一个方法boolean isNatural()方法,用于判断value属性值是否是自然数。自然数是大于等于0的整数。 +- 包含一个方法int approximateNumberCount()方法,用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 +- 包含一个方法boolean isPrimeNumber()方法,用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数,并且value是大于1的自然数,那么value就是素数。 +- 包含一个方法int[] getAllPrimeNumber()方法,用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 + +(2)测试类的main中调用测试 + +``` +public class MyInt { + private int Value; + + public int getValue() { + return Value; + } + + public void setValue(int value) { + Value = value; + } + + public boolean isNatural(){ + boolean a = true; + if (Value<0){ + a=false; + } + return a; + } + public int approximateNumberCount(){ + int count=0; + for (int i = 1; i < Value; i++) { + if(Value % i==0){ + count++; + } + } + return count; + } + public boolean isPrimeNumber(){ + boolean a = true; + for (int i = 2; i < Value; i++) { + if(Value % i == 0){ + a=false; + } + } + return a; + } + + public int[] getAllPrimeNumber(){ + int[] a = new int[0]; + int count=0; + for (int i = 1; i <= Value; i++) { + if(Value % i==0){ + a= Arrays.copyOf(a,a.length+1); + a[count]=i; + count++; + } + } + return a; + } +} +public class Main { + public static void main(String[] args) { + MyInt my1 = new MyInt(); + my1.setValue(41); + System.out.println(my1.isNatural()); + System.out.println(my1.approximateNumberCount()); + System.out.println(my1.isPrimeNumber()); + System.out.println(Arrays.toString(my1.getAllPrimeNumber())); + } +} +``` + +## 参数练习 + +### 1、n个整数中的最小值和n个整数的最大公约数 + +(1)声明方法int min(int... nums) :返回n个整数中的最小值 + +(2)声明方法int maxApproximate(int... nums):返回n个整数的最大公约数 + +``` +public class M { + public static void main(String[] args) { + A a = new A(); + int[] arr = {32,42,544,36,434}; + a.setA(arr); + System.out.println(a.maxApproximate()); + System.out.println(a.min()); + } +} +public class A { + private int[] a; + + public int[] getA() { + return a; + } + + public void setA(int[] a) { + this.a = a; + } + + public int min() { + int min = a[0]; + for (int j : a) { + if (min > j) { + min = j; + } + } + return min; + } + + public int maxApproximate() { + int[] b = a; + Arrays.sort(b); + int min = 1; + for (int i = 0; i < b.length-1; i++) { + for (int j = 1; j <= b[i]; j++) { + if (b[i] % j == 0 && b[i+1] % j == 0) { + min=j; + } + } + } + return min; + } +} +``` + +### 2、判断程序运行结果 + +``` +public class Tools{ + public static void main(String[] args) { + int i = 0; + new Tools().change(i); + i = i++; + System.out.println("i = " + i); + } + + void change(int i){ + i++; + } +} +``` + +### 3、数组长度扩大2倍 + +(1)声明数组工具类ArraysTools + +- 方法1:String toString(int[] arr),遍历结果形式:[元素1,元素2,。。。] +- 方法2:int[] grow(int[] arr),可以实现将一个数组扩大为原来的2倍 + +(2)在测试类的main中调用测试 + +``` +import java.util.Arrays; + +public class Arr { + public int[] arr(int[] arr){ + int[] b = new int[arr.length*2]; + for (int i = 0; i < arr.length; i++) { + b[i]=arr[i]; + } + return b; + } + public String[] arr(String[] arr){ + String[] b = new String[arr.length*2]; + for (int i = 0; i < arr.length; i++) { + b[i]=arr[i]; + } + return b; + } + public double[] arr(double[] arr){ + double[] b = new double[arr.length*2]; + for (int i = 0; i < arr.length; i++) { + b[i]=arr[i]; + } + return b; + } + public float[] arr(float[] arr){ + float[] b = new float[arr.length*2]; + for (int i = 0; i < arr.length; i++) { + b[i]=arr[i]; + } + return b; + } + public char[] arr(char[] arr){ + char[] b = new char[arr.length*2]; + for (int i = 0; i < arr.length; i++) { + b[i]=arr[i]; + } + return b; + } +} +import java.util.Arrays; + +public class M { + public static void main(String[] args) { + int[] arr = {1,3,4,13,3,7}; + Arr arr1 = new Arr(); + System.out.println(Arrays.toString(arr)); + arr=arr1.arr(arr); + System.out.println(Arrays.toString(arr)); + } +} +``` + +## 方法重载练习 + +### 1、比较两个数大小关系 + +(1)声明MathTools工具类,包含: + +- int compare(int a, int b):比较两个整数大小关系,如果第一个整数比第二个整数大,则返回正整数,如果第一个整数比第二个整数小,则返回负整数,如果两个整数相等则返回0; +- int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大,则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; +- int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大,则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; + +(2)在测试类的main方法中调用 + +``` +import java.util.Arrays; + +public class MathTools { + public int compare(int a, int b){ + if(a>b)return -1; + else if (ab)return -1; + else if (ab)return -1; + else if (a