commit 6d5c439fb2007987ac1718d7599489c3eb806735 Author: skyofdream <1662992055@qq.com> Date: Thu May 9 09:10:51 2024 +0800 第一次提交 diff --git a/Class/c7/CenterFrameDemo.java b/Class/c7/CenterFrameDemo.java new file mode 100644 index 0000000..fb30836 --- /dev/null +++ b/Class/c7/CenterFrameDemo.java @@ -0,0 +1,24 @@ +package Class.c7; + +import java.awt.Dimension; +import java.awt.Frame; +import java.awt.Toolkit; + +public class CenterFrameDemo { + public static void main(String[] args) { + Toolkit tk =Toolkit.getDefaultToolkit(); + Dimension ScreenSize = tk.getScreenSize(); + int width = (int)ScreenSize.getWidth()/2; + int hight = (int)ScreenSize.getHeight()/2; + int x = width/2; + int y = hight/2; + + + Frame f = new Frame(); + f.setTitle("CenterFrame"); + f.setSize(width, hight); + f.setLocation(x, y); + f.setVisible(true); + + } +} \ No newline at end of file diff --git a/Class/c7/FirstGUI.java b/Class/c7/FirstGUI.java new file mode 100644 index 0000000..dea631a --- /dev/null +++ b/Class/c7/FirstGUI.java @@ -0,0 +1,11 @@ +package Class.c7; + +import java.awt.Frame; + +public class FirstGUI { + public static void main(String[] args) { + Frame f = new Frame("My Fist Gui"); + f.setBounds(100,100,200,100); + f.setVisible(true); + } +} diff --git a/cn/gdpu/roleExpansion.java b/cn/gdpu/roleExpansion.java new file mode 100644 index 0000000..58ef073 --- /dev/null +++ b/cn/gdpu/roleExpansion.java @@ -0,0 +1,5 @@ +package cn.gdpu; + +public interface roleExpansion { + void finishingMove(); +} diff --git a/com/experiment/DragonSlayer.java b/com/experiment/DragonSlayer.java new file mode 100644 index 0000000..84ec7a4 --- /dev/null +++ b/com/experiment/DragonSlayer.java @@ -0,0 +1,16 @@ +package com.experiment; + +public class DragonSlayer extends Role { + + public DragonSlayer(){ + super("屠龙者", "0001", 10000); + } + + public DragonSlayer(String name, String id, int health){ + super(name, id, health); + } + + public void finishingMove(){ + System.out.println(getName()+"发动了"+"降龙掌"); + } +} diff --git a/com/experiment/FingerMan.java b/com/experiment/FingerMan.java new file mode 100644 index 0000000..cb37c82 --- /dev/null +++ b/com/experiment/FingerMan.java @@ -0,0 +1,16 @@ +package com.experiment; + +public class FingerMan extends Role { + + public FingerMan(){ + super("段氏", "0003", 9000); + } + + public FingerMan(String name, String id, int health){ + super(name, id, health); + } + + public void finishingMove(){ + System.out.println(getName()+"发动了"+"一阳指"); + } +} diff --git a/com/experiment/Role.java b/com/experiment/Role.java new file mode 100644 index 0000000..0eee3e6 --- /dev/null +++ b/com/experiment/Role.java @@ -0,0 +1,49 @@ +package com.experiment; + +import cn.gdpu.roleExpansion; + +public abstract class Role implements roleExpansion{ + private String name; + private String id; + private int health; + + public void setName(String name){ + this.name = name; + } + + public void setID(String id){ + this.id = id; + } + + public void setHealth(int health){ + this.health = health; + } + + public String getName(){ + return this.name; + } + + public String getID(){ + return this.id; + } + + public int getHealth(){ + return this.health; + } + + public Role(){ + setName("人机"); + setID("0000"); + setHealth(1000); + } + + public Role(String name, String id, int health){ + setName(name); + setID(id); + setHealth(health); + } + + public void move(){ + System.out.println(""); + } +} diff --git a/com/experiment/SwordSaint.java b/com/experiment/SwordSaint.java new file mode 100644 index 0000000..994d0a0 --- /dev/null +++ b/com/experiment/SwordSaint.java @@ -0,0 +1,16 @@ +package com.experiment; + +public class SwordSaint extends Role { + + public SwordSaint(){ + super("剑圣", "0002", 8000); + } + + public SwordSaint(String name, String id, int health){ + super(name, id, health); + } + + public void finishingMove(){ + System.out.println(getName()+"发动了"+"六脉剑"); + } +} diff --git a/exp1/3.java b/exp1/3.java new file mode 100644 index 0000000..926fde0 --- /dev/null +++ b/exp1/3.java @@ -0,0 +1 @@ +package exp1; diff --git a/exp1/Compare.java b/exp1/Compare.java new file mode 100644 index 0000000..a285ea0 --- /dev/null +++ b/exp1/Compare.java @@ -0,0 +1,31 @@ +package exp1; +//这段代码用于获取三个数据并输出其中的最大值 +//编写于 2024.2.27 +//Java核心技术(实验一)课堂任务 + +import java.util.Scanner; + +public class Compare{ + public static void main(String[] args) + { + Scanner scan =new Scanner(System.in); + + System.out.print("请输入第一个数字: "); + int a = scan.nextInt(); + + System.out.print("请输入第二个数字: "); + int b = scan.nextInt(); + + System.out.print("请输入第三个数字: "); + int c = scan.nextInt(); + + scan.close(); + + int max = 0; + if(a>b && a>c) {max = a;} + else if(b>a && b>c) {max = b;} + else if(c>a && c>b) {max = c;} + + System.out.println("最大数是:" + max); + } +} \ No newline at end of file diff --git a/exp1/Score.java b/exp1/Score.java new file mode 100644 index 0000000..77031d3 --- /dev/null +++ b/exp1/Score.java @@ -0,0 +1,27 @@ +package exp1; + +import java.util.Scanner; + +public class Score { + public static void main(String[] args){ + Scanner scan = new Scanner(System.in); + System.out.print("请输入百分制成绩: "); + int score = scan.nextInt(); + scan.close(); + + if(score>100 || score<0){ + System.out.println("输入错误"); + }else if(score>=90 && score<=100){ + System.out.println("优秀"); + }else if(score>=80 && score<90){ + System.out.println("良好"); + }else if(score>=70 && score<80){ + System.out.println("中等"); + }else if(score>=60 && score<70){ + System.out.println("及格"); + }else{ + System.out.println("不及格"); + } + } + +} diff --git a/exp1/Smp3.java b/exp1/Smp3.java new file mode 100644 index 0000000..c1d0427 --- /dev/null +++ b/exp1/Smp3.java @@ -0,0 +1,14 @@ +package exp1; + +public class Smp3 +{ + public static void main(String[] args) + { + int a; + a = (int)3L * 4; + System.out.println(a); + float b; + b = (float)3.2 * 4; + System.out.println(b); + } +} diff --git a/exp1/calculate.java b/exp1/calculate.java new file mode 100644 index 0000000..7987742 --- /dev/null +++ b/exp1/calculate.java @@ -0,0 +1,9 @@ +package exp1; + +public class calculate { + public static void main(String[] args){ + int c; + c = 0b10100001 & 0b10000000; + System.out.println(Integer.toBinaryString(c)); + } +} diff --git a/exp10/CopyFile.java b/exp10/CopyFile.java new file mode 100644 index 0000000..ead3715 --- /dev/null +++ b/exp10/CopyFile.java @@ -0,0 +1,67 @@ +package exp10; + +import java.io.*; + +public class CopyFile { + public static void main(String[] args) { + String dirPath = "D:/MyProjects/CoreJava/exp10/MultiFile"; //源文件路径 + String dstPath = "C:/test.java"; //目标文件路径 + File dir = new File(dirPath); + File dst = new File(dstPath); + File[] files = dir.listFiles(); + + try{ //通过三种方式将源文件复制到目标文件中 + charCopy(files[0], dst); + charArryCopy(files[1], dst); + bufferCopy(files[2], dst); + }catch(IOException e){ + System.out.println("程序因为IO异常结束运行!"); + } + + } + + //一次一个字节地读写 + public static void charCopy(File src, File dst) throws IOException{ + InputStream in = new FileInputStream(src); + OutputStream out = new FileOutputStream(dst, true); + int b; + + while ((b=in.read()) != -1){ + out.write(b); + } + + in.close(); + out.close(); + } + + //一次一个季节数组地读写 + public static void charArryCopy(File src, File dst) throws IOException{ + InputStream in = new FileInputStream(src); + OutputStream out = new FileOutputStream(dst, true); + byte[] buffer = new byte[1024]; + int size; + + while((size=in.read(buffer)) != -1){ + out.write(buffer, 0, size);; + } + + in.close(); + out.close(); + } + + //缓冲字节流读写 + public static void bufferCopy(File src, File dst) throws IOException{ + BufferedInputStream in = new BufferedInputStream(new FileInputStream(src)); + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dst, true)); + byte[] buffer = new byte[1024]; + int size; + + while((size=in.read(buffer)) != -1){ + out.write(buffer, 0, size);; + } + out.flush(); + + in.close(); + out.close(); + } +} diff --git a/exp10/ListFiles.java b/exp10/ListFiles.java new file mode 100644 index 0000000..d15e51c --- /dev/null +++ b/exp10/ListFiles.java @@ -0,0 +1,27 @@ +package exp10; + +import java.io.File; + +public class ListFiles { + public static void main(String[] args) { + String dirPath = "D:/JavaFile"; //要扫描的路径 + list(dirPath); + } + + //递归遍历dirPath里的所有文件,并打印java文件的绝对路径 + public static void list(String dirpath){ + File dir = new File(dirpath); + String[] files = dir.list(); //获取该文件夹内的所有文件 + for (String filename: files){ + String filePath = dirpath+"/"+filename; + File f = new File(filePath); + if(f.isDirectory()){ //如果是文件夹则递归遍历 + list(filePath); + }else if(f.getName().endsWith(".java")){ //如果是java文件则打印绝对路径 + System.out.println(filePath); + }else{ + + } + } + } +} diff --git a/exp10/MultiFile/file1.java b/exp10/MultiFile/file1.java new file mode 100644 index 0000000..759f8b0 --- /dev/null +++ b/exp10/MultiFile/file1.java @@ -0,0 +1,2 @@ +package exp10.MultiFile; +public class file1 {} diff --git a/exp10/MultiFile/file2.java b/exp10/MultiFile/file2.java new file mode 100644 index 0000000..8ac705f --- /dev/null +++ b/exp10/MultiFile/file2.java @@ -0,0 +1,2 @@ +package exp10.MultiFile; +public class file2 {} diff --git a/exp10/MultiFile/file3.java b/exp10/MultiFile/file3.java new file mode 100644 index 0000000..1f4a7de --- /dev/null +++ b/exp10/MultiFile/file3.java @@ -0,0 +1,2 @@ +package exp10.MultiFile; +public class file3 {} diff --git a/exp11/AccountDemo.java b/exp11/AccountDemo.java new file mode 100644 index 0000000..075b08b --- /dev/null +++ b/exp11/AccountDemo.java @@ -0,0 +1,7 @@ +package exp11; + +import javax.swing.*; + +public class AccountDemo { + JFrame rejister = new JFrame(); +} diff --git a/exp2/Read.java b/exp2/Read.java new file mode 100644 index 0000000..7db3e2e --- /dev/null +++ b/exp2/Read.java @@ -0,0 +1,50 @@ +package exp2; + +class Person { + //姓名 + String name; + //年龄 + int age; + //国籍 + //String country; + static String country; + + public Person(){} + + public Person(String name,int age) { + this.name = name; + this.age = age; + } + + public Person(String name,int age,String country) { + this.name = name; + this.age = age; + Person.country = country; + } + + public void show() { + System.out.println("姓名:"+name+",年龄:"+age+",国籍:"+country); + } +} + +class PersonDemo { + public static void main(String[] args) { + //创建对象1 + Person p1 = new Person("邓丽君",16,"中国"); + p1.show(); + + //创建对象2 + Person p2 = new Person("杨幂",22); + p2.show(); + + //创建对象3 + Person p3 = new Person("凤姐",20); + p3.show(); + + Person.country = "美国"; + p3.show(); + + p1.show(); + p2.show(); + } +} diff --git a/exp2/StudentTest.java b/exp2/StudentTest.java new file mode 100644 index 0000000..dd02cf6 --- /dev/null +++ b/exp2/StudentTest.java @@ -0,0 +1,59 @@ +package exp2; + +//学生类 +class Student { + private String name; + private String age; + + //无参构造 + public Student() { + this.name = "张三"; + this.age = "38"; + } + + //带参构造 + public Student(String name, String age) { + this.name = name; + this.age = age; + } + + void show(){ + System.out.println(this.name); + System.out.println(this.age); + } + + //设置姓名 + static void setName(Student s, String name) { + s.name = name; + } + + //设置年龄 + static void setAge(Student s, String age) { + s.age = age; + } + + //进行学习行为 + static void study(Student s) { + System.out.println(s.name+"学习"); + } + + //进行考试行为 + static void exam(Student s) { + System.out.println(s.name+"考试"); + } + +} + + +//测试类 +public class StudentTest { + public static void main(String[] args) { + //创建学生s1 + Student s1 = new Student(); + Student s2 = new Student("李四", "36"); + + Student.study(s1); + Student.exam(s2); + } + +} diff --git a/exp3/Animal.java b/exp3/Animal.java new file mode 100644 index 0000000..ea03e7c --- /dev/null +++ b/exp3/Animal.java @@ -0,0 +1,32 @@ +package exp3; + +public class Animal { + //成员变量 + String name; + int weight; + String color; + + //无参构造 + public Animal(){ + this.weight = 2; + this.color = "黑色"; + this.name = "Animal"; + } + + //全参构造 + public Animal(String name,int weight, String color){ + this.name = name; + this.color = color; + this.weight = weight; + } + + //动作-行走 + void walk(){ + System.out.println(this.name+"行走..."); + } + + //动作-奔跑 + void run(){ + System.out.println(this.name+"奔跑..."); + } +} diff --git a/exp3/AnimalExtendsTest.java b/exp3/AnimalExtendsTest.java new file mode 100644 index 0000000..1f4ee1a --- /dev/null +++ b/exp3/AnimalExtendsTest.java @@ -0,0 +1,13 @@ +package exp3; + +public class AnimalExtendsTest { + public static void main(String[] args){ + Cat cat = new Cat(); + Dog dog = new Dog("大黄", 5, "黄色", "金毛"); + + cat.walk(); + cat.miaow(); + dog.run(); + dog.bark(); + } +} diff --git a/exp3/Car.java b/exp3/Car.java new file mode 100644 index 0000000..b8d3355 --- /dev/null +++ b/exp3/Car.java @@ -0,0 +1,21 @@ +package exp3; + +public class Car extends Vehicle { + //成员属性 + int loader; + + public Car(){ + super(); + this.loader = 0; + } + + public Car(String brand, int wheels, float weight, int loader){ + super(brand, wheels, weight); + this.loader = loader; + } + + void show(){ + super.show(); + System.out.println("载人数: "+this.loader); + } +} diff --git a/exp3/Cat.java b/exp3/Cat.java new file mode 100644 index 0000000..8d89e7c --- /dev/null +++ b/exp3/Cat.java @@ -0,0 +1,24 @@ +package exp3; + +public class Cat extends Animal { + //成员属性 + String breed; + + //无参构造 + public Cat(){ + super(); + this.breed = "橘猫"; + } + + //全参构造 + public Cat(String name, int weight, String color, String breed){ + super(name, weight, color); + this.breed = breed; + } + + //行为-喵喵叫 + public void miaow(){ + System.out.println(this.name+"在喵喵叫"); + } + +} diff --git a/exp3/Dog.java b/exp3/Dog.java new file mode 100644 index 0000000..672c53f --- /dev/null +++ b/exp3/Dog.java @@ -0,0 +1,24 @@ +package exp3; + +public class Dog extends Animal { + //成员属性 + String breed; + + //无参构造 + public Dog(){ + super(); + this.breed = "中华田园犬"; + } + + //全参构造 + public Dog(String name, int weight, String color, String breed){ + super(name, weight, color); + this.breed = breed; + } + + //行为-吠叫 + void bark(){ + System.out.println(this.name+"在吠叫"); + } + +} diff --git a/exp3/ExtendsDemo.java b/exp3/ExtendsDemo.java new file mode 100644 index 0000000..be3a1ca --- /dev/null +++ b/exp3/ExtendsDemo.java @@ -0,0 +1,88 @@ +package exp3; + + +//类-人 +class Person{ + //成员变量 + String name; //姓名 + int age; //年龄 + + //无参构造"人" + public Person(){ + this.name = "无名"; + this.age = 18; + } + + //有参构造"人" + public Person(String name, int age){ + this.name = name; + this.age = age; + } + + + //设置姓名 + void setName(String name){ + this.name = name; + } + + //设置年龄 + void setAge(int age){ + this.age = age; + } + + //行为-吃饭 + void eat(Person p){ + System.out.println(p.name+"吃饭"); + } + + //行为-睡觉 + void sleep(Person p){ + System.out.println(p.name+"睡觉"); + } +} + + +//类-学生 继承自类-人 +class Student extends Person{ + //成员变量 + String id; + + //无参构造 + public Student(){ + super(); + this.id = "0000"; + } + + //有参构造 + public Student(String name, int age, String id){ + super(name, age); + this.id = id; + } + + //设置id + void setId(String id){ + this.id = id; + } + + //行为-学习 + void study(Student s){ + System.out.println(s.name+"学习"); + } + + //方法-展示信息 + void show(){ + System.out.println("姓名: "+this.name); + System.out.println("年龄: "+this.age); + System.out.println("学号: "+this.id+"\n"); + } +} + +public class ExtendsDemo { + public static void main(String[] args){ + Student s1 = new Student(); + Student s2 = new Student("梦之空", 20, "2230502119"); + + s1.show();; + s2.show(); + } +} diff --git a/exp3/Line.java b/exp3/Line.java new file mode 100644 index 0000000..12137b5 --- /dev/null +++ b/exp3/Line.java @@ -0,0 +1,37 @@ +package exp3; + +import java.lang.Math; + +class Line extends Point{ + protected int x,y; + + Line(int a,int b){ + super(a,b); + setLine(a,b); + } + + public void setLine(int x,int y) { + this.x=x+x; + this.y=y+y; + } + + public double length(){ + int x1=super.x,y1=super.y,x2=this.x,y2=this.y; + return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); + } + public String toString(){ + return "直线端点:["+super.x+","+super.y+"]["+x+","+y+"]直线长度:"+this.length(); + } + public int getX() { + return x; + } + public int getY() { + return y; + } + public void setX(int x) { + this.x = x; + } + public void setY(int y) { + this.y = y; + } +} diff --git a/exp3/LineDemo.java b/exp3/LineDemo.java new file mode 100644 index 0000000..80db66b --- /dev/null +++ b/exp3/LineDemo.java @@ -0,0 +1,8 @@ +package exp3; + +public class LineDemo { + public static void main(String args[]){ + Line line = new Line(50,50); + System.out.println("\n"+line.toString()); + } +} diff --git a/exp3/Point.java b/exp3/Point.java new file mode 100644 index 0000000..5a1e1ba --- /dev/null +++ b/exp3/Point.java @@ -0,0 +1,15 @@ +package exp3; + +class Point{ + protected int x,y; + Point(){} + + Point(int a,int b){ + setPoint(a,b); + } + + public void setPoint(int a,int b){ + x=a; + y=b; + } +} diff --git a/exp3/Trunk.java b/exp3/Trunk.java new file mode 100644 index 0000000..52103d4 --- /dev/null +++ b/exp3/Trunk.java @@ -0,0 +1,21 @@ +package exp3; + +public class Trunk extends Car { + //成员属性 + float payload; + + public Trunk(){ + super(); + this.payload = 0; + } + + public Trunk(String brand, int wheels, float weight, int loader, float payload){ + super(brand, wheels, weight, loader); + this.payload = payload; + } + + void show(){ + super.show(); + System.out.println("载重数: "+this.payload); + } +} diff --git a/exp3/Vehicle.java b/exp3/Vehicle.java new file mode 100644 index 0000000..ef304a1 --- /dev/null +++ b/exp3/Vehicle.java @@ -0,0 +1,28 @@ +package exp3; + +public class Vehicle { + //成员属性 + String brand; //品牌 + int wheels; //车轮数 + float weight; //重量 + + public Vehicle(){ + this.brand = "无品牌"; + this.wheels = 0; + this.weight = 0; + } + + public Vehicle(String brand, int wheels, float weight){ + this.brand = brand; + this.wheels = wheels; + this.weight = weight; + } + + void show(){ + System.out.println("enter super class method..."); + System.out.println("品牌: "+this.brand); + System.out.println("车轮数: "+this.wheels); + System.out.println("重量: "+this.weight); + } + +} \ No newline at end of file diff --git a/exp3/VehicleClient.java b/exp3/VehicleClient.java new file mode 100644 index 0000000..9df1caf --- /dev/null +++ b/exp3/VehicleClient.java @@ -0,0 +1,23 @@ +package exp3; + +public class VehicleClient +{ + public static void main(String args[]) + { + System.out.println("输出相关数据"); + Car car=new Car("",4,1500,4); //创建一个Car类对象 + car.show(); + Trunk trunk=new Trunk("", 8,7000,3,25000); + trunk.show(); + Vehicle vehicle= new Car("", 4,2000,4); + vehicle.show(); + if(vehicle instanceof Car){ + ((Car)vehicle).show(); + } + else{ + System.out.println("不能强制转换类型!"); + } + vehicle=new Trunk("", 9,8000,4,30000); + vehicle.show(); + } +} diff --git a/exp3/test.java b/exp3/test.java new file mode 100644 index 0000000..bd67ca7 --- /dev/null +++ b/exp3/test.java @@ -0,0 +1,48 @@ +package exp3; + +class A{ + int sum,num1,num2; + public A(){ + num1=10; + num2=20; + sum=0; + } + void sum1(){ + sum=num1+num2; + System.out.println("sum="+num1+"+"+num2+"="+sum); + } + void sum2(int n){ + num1=n; + sum=num1+num2; + System.out.println("sum="+num1+"+"+num2+"="+sum); + } +} + +class B extends A{ + int num2; + public B(){ + num2=200; + } + void sum2(){ + sum=num1+num2; + System.out.println("sum="+num1+"+"+num2+"="+sum); + } + void sum2(int n){ + num1=n; + sum=num1+num2; + System.out.println("sum="+num1+"+"+num2+"="+sum); + } + void sum3(int n){ + super.sum2(n); + System.out.println("sum="+num1+"+"+num2+"="+sum); + } +} +public class test{ + public static void main (String arg[]){ + B m=new B(); + m.sum1(); + m.sum2(); + m.sum2(50); + m.sum3(50); + } +} diff --git a/exp4/AnimalTool.java b/exp4/AnimalTool.java new file mode 100644 index 0000000..e5c7959 --- /dev/null +++ b/exp4/AnimalTool.java @@ -0,0 +1,62 @@ +package exp4; + +abstract class Animal{ + String name; + int age; + + public Animal(){}; + public Animal(String name, int age){ + this.name = name; + this.age = age; + } + + abstract void eat(); + abstract void show(); +} + +class Cat extends Animal{ + public Cat(){}; + public Cat(String name, int age){ + super(name, age); + } + + void eat(){ + System.out.println("猫吃鱼"); + } + + void show(){ + System.out.println(this.name+","+this.age); + } +} + +class Dog extends Animal{ + public Dog(){}; + public Dog(String name, int age){ + super(name, age); + } + + void eat(){ + System.out.println("狗吃肉"); + } + + void show(){ + System.out.println(this.name+","+this.age); + } +} + + +public class AnimalTool { + public static void main(String[] args){ + Animal a = new Cat(); + Animal b = new Dog("旺财", 3); + + a.name = "Tom"; + a.age = 999; + + a.show(); + b.show(); + + a.eat(); + b.eat(); + } +} diff --git a/exp4/PolymorphicDemo.java b/exp4/PolymorphicDemo.java new file mode 100644 index 0000000..43b11c3 --- /dev/null +++ b/exp4/PolymorphicDemo.java @@ -0,0 +1,25 @@ +package exp4; + +class A{ + String member = "fu"; + + void show(){ + System.out.println(this.member); + } +} + +class B extends A{ + String member = "zi"; + + void show(){ + System.out.println(this.member); + } +} + +public class PolymorphicDemo { + public static void main(String[] args){ + A a = new B(); + a.show(); + System.out.println(a.member); + } +} diff --git a/exp4/PolymorphismTest.java b/exp4/PolymorphismTest.java new file mode 100644 index 0000000..cadbf9f --- /dev/null +++ b/exp4/PolymorphismTest.java @@ -0,0 +1,30 @@ +package exp4; + +public class PolymorphismTest { + public static void main(String[] args) { + Student[] studentArray = new Student[5]; + studentArray[1] = new Undergraduate("鲁向东"); //本科生鲁向东 + studentArray[0] = new Undergraduate("陈建平"); //本科生陈建平 + studentArray[2] = new Postgraduate("匡晓华"); //研究生匡晓华 + studentArray[3] = new Undergraduate("周丽娜"); //本科生周丽娜 + studentArray[4] = new Postgraduate("梁欣欣"); //研究生梁欣欣 + + for (int i=0; i=90){ + currentGrade = "优秀"; + } + else if (average>=80&&average<90) { + currentGrade = "良好"; + } + else if (average>=70&&average<80) { + currentGrade = "一般"; + } + else if (average>=60&&average<70) { + currentGrade = "及格"; + } + else{ + currentGrade = "不及格"; + } + setCourseGrade(currentGrade); + } + } \ No newline at end of file diff --git a/exp4/ShapeDemo.java b/exp4/ShapeDemo.java new file mode 100644 index 0000000..646ed14 --- /dev/null +++ b/exp4/ShapeDemo.java @@ -0,0 +1,74 @@ +package exp4; + +abstract class Shapes { + public int x,y; //x,y为的坐标 + public int width,height; + public Shapes(int x,int y,int width,int height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + public abstract double getArea(); + public abstract double getPerimeter(); + public abstract void show(); + } + + class Rectangle extends Shapes{ + double a; + double b; + + Rectangle(double a, double b){ + super(0, 0, 0, 0); + this.a=a; + this.b=b; + } + + public double getArea(){ + return a*b; + } + + public double getPerimeter(){ + return (a+b)*2; + } + + public void show(){ + System.out.println("矩形 长:"+this.a+"宽:"+this.b); + } + } + + class Circle extends Shapes{ + double radius;//半径 + + Circle(double radius){ + super(0, 0, 0, 0); + this.radius=radius; + } + + public double getArea(){ + return 3.14159265324*radius*radius; + } + + public double getPerimeter(){ + return 3.14159265324*radius*2; + } + + public void show(){ + System.out.println("圆 半径:"+this.radius); + } + } + + +public class ShapeDemo { + public static void main(String[] args){ + Shapes r = new Rectangle(2, 3); + Shapes c = new Circle(5); + + r.show(); + System.out.println("面积:"+r.getArea()+" 周长:"+r.getPerimeter()+"\n"); + + c.show(); + System.out.println("面积:"+c.getArea()+" 周长:"+c.getPerimeter()+"\n"); + + } +} diff --git a/exp4/Student.java b/exp4/Student.java new file mode 100644 index 0000000..adae2fa --- /dev/null +++ b/exp4/Student.java @@ -0,0 +1,56 @@ +package exp4; + +public abstract class Student { + final static int CourseNo = 3; + private String name; + private String type; + private int[] courses; + private String courseGrade; + public Student(){ + + } + public Student(String name,String type) + { + this.name = name; + this.type = type; + courses = new int[3]; + courseGrade=""; + } + public abstract void calculateGrade(); + public String getName( ) + { + return name; + } + public String getType( ) + { + return type; //返回学生类型 + } + public String getCourseGrade( ) + { + return courseGrade; + } + + public int getCourseScore(int courseNumber) + { + return courses[courseNumber]; + } + + public void setName(String name) + { + this.name = name; + } + + public void setType(String type) + { + this.type = type; + } + + public void setCourseScore(int courseNumber, int courseScore) + { + courses[courseNumber] = courseScore;//按课程索引号设置课程成绩 + } + public void setCourseGrade(String courseGrade) { + this.courseGrade = courseGrade; + } +} + diff --git a/exp4/Undergraduate.java b/exp4/Undergraduate.java new file mode 100644 index 0000000..6dc48a1 --- /dev/null +++ b/exp4/Undergraduate.java @@ -0,0 +1,33 @@ +package exp4; + +class Undergraduate extends Student { + public Undergraduate(String name) + { + super(name,"本科生"); + } + public void calculateGrade() { + int total = 0; + double average = 0; + for (int i = 0; i < CourseNo; i++) { + total=total+getCourseScore(i); // 累加各门课程成绩 + }; + average = total / CourseNo; + String currentGrade=""; + if (average>=80&&average<100){ + currentGrade = "优秀"; + } + else if (average>=70&&average<80){ + currentGrade = "良好"; + } + else if (average>=60&&average<70){ + currentGrade = "一般"; + } + else if (average>=50&&average<60){ + currentGrade = "及格"; + } + else{ + currentGrade = "不及格"; + } + setCourseGrade(currentGrade); + } +} \ No newline at end of file diff --git a/exp5/Outer.java b/exp5/Outer.java new file mode 100644 index 0000000..15b81fb --- /dev/null +++ b/exp5/Outer.java @@ -0,0 +1,21 @@ +package exp5; + +class Outer { + public int num = 10; + class Inner { + public int num = 20; + public void show() { + int num = 30; + System.out.println(num); + System.out.println(this.num); + System.out.println(Outer.this.num); + } + } + + public static void main(String[] args){ + Outer outer = new Outer(); + Outer.Inner inner = outer.new Inner(); + + inner.show(); + } +} diff --git a/exp5/OuterDemo.java b/exp5/OuterDemo.java new file mode 100644 index 0000000..6152a65 --- /dev/null +++ b/exp5/OuterDemo.java @@ -0,0 +1,18 @@ +package exp5; + +interface Inter { void show(); } + class Outer1 { public static Inter method() { + return new Inter() { + @Override + public void show() { + System.out.println("HelloWorld"); + } + }; + } +} + +class OuterDemo { + public static void main(String[] args) { + Outer1.method().show(); + } +} diff --git a/exp5/PersonDancerDemo.java b/exp5/PersonDancerDemo.java new file mode 100644 index 0000000..3714746 --- /dev/null +++ b/exp5/PersonDancerDemo.java @@ -0,0 +1,33 @@ +package exp5; + +abstract class Person { + public abstract void show(); +} + +class Dancer extends Person { + @Override + public void show() { + System.out.println("Dancing!!!"); + } +} + +class PersonTool { + public void method(Person p) { + p.show(); + } +} + +public class PersonDancerDemo { + public static void main(String[] args) { + PersonTool pd = new PersonTool(); + Person p = new Dancer(); + pd.method(p); + System.out.println("----------------"); + pd.method(new Person() { + @Override + public void show() { + System.out.println("Dancing!!!"); + } + }); + } +} \ No newline at end of file diff --git a/exp5/RoleDemo.java b/exp5/RoleDemo.java new file mode 100644 index 0000000..3321cbd --- /dev/null +++ b/exp5/RoleDemo.java @@ -0,0 +1,33 @@ +package exp5; + +import com.experiment.DragonSlayer; +import com.experiment.FingerMan; +import com.experiment.SwordSaint; + +public class RoleDemo { + public static void main(String[] args){ + DragonSlayer d = new DragonSlayer(); + SwordSaint s = new SwordSaint(); + FingerMan f = new FingerMan(); + + System.out.println(d.getName()+"加入战斗"); + System.out.println(s.getName()+"加入战斗"); + System.out.println(f.getName()+"加入战斗"); + System.out.println("\n作战开始!\n"); + + /*交互 + * 战斗过程... + * 以后再写^v^ + */ + System.out.println("***激烈的战斗过程***\n"); + + d.finishingMove(); + s.finishingMove(); + f.finishingMove(); + + + System.out.println("\n全死了!\n"); + + System.out.println("\n战斗结束\n"); + } +} diff --git a/exp6/AdapterDemo.java b/exp6/AdapterDemo.java new file mode 100644 index 0000000..416e8dd --- /dev/null +++ b/exp6/AdapterDemo.java @@ -0,0 +1,29 @@ +package exp6; + +interface InnerAnonymousInterface { + void fun1(); + void fun2(); + void fun3(); +} + +abstract class Outer implements InnerAnonymousInterface{ +} + + +public class AdapterDemo { + public static void main(String[] args) { + Outer o = new Outer(){ + public void fun1(){ + System.out.println("方法1"); + } + public void fun2(){ + System.out.println("方法2"); + } + public void fun3(){ + System.out.println("方法3"); + } + }; + + o.fun1(); + } +} diff --git a/exp6/Calculator.java b/exp6/Calculator.java new file mode 100644 index 0000000..7f8af1a --- /dev/null +++ b/exp6/Calculator.java @@ -0,0 +1,24 @@ +package exp6; + +import java.util.Scanner; + +public class Calculator { + public static void main(String[] args) { + double a, b; + String opType; + Scanner in =new Scanner(System.in); + + System.out.print("请输入第一个数: "); + a = in.nextDouble(); + System.out.print("请输入第二个数: "); + b = in.nextDouble(); + System.out.print("请选择运算(+-*/): "); + opType = in.next(); + in.close(); + + Operation op = OperationFactory.createOperate(opType); + op.setNumber(a, b); + + System.out.println("结果是: "+op.result()); + } +} diff --git a/exp6/Operation.java b/exp6/Operation.java new file mode 100644 index 0000000..9833efc --- /dev/null +++ b/exp6/Operation.java @@ -0,0 +1,14 @@ +package exp6; + +public abstract class Operation { + double a; + double b; + + public void setNumber(double a, double b){ + this.a = a; + this.b = b; + } + + abstract double result(); + +} diff --git a/exp6/OperationAddtion.java b/exp6/OperationAddtion.java new file mode 100644 index 0000000..81ea187 --- /dev/null +++ b/exp6/OperationAddtion.java @@ -0,0 +1,8 @@ +package exp6; + +public class OperationAddtion extends Operation{ + + double result(){ + return a+b; + } +} diff --git a/exp6/OperationDivision.java b/exp6/OperationDivision.java new file mode 100644 index 0000000..8f77767 --- /dev/null +++ b/exp6/OperationDivision.java @@ -0,0 +1,8 @@ +package exp6; + +public class OperationDivision extends Operation{ + + double result(){ + return a/b; + } +} diff --git a/exp6/OperationFactory.java b/exp6/OperationFactory.java new file mode 100644 index 0000000..3608215 --- /dev/null +++ b/exp6/OperationFactory.java @@ -0,0 +1,24 @@ +package exp6; + +public class OperationFactory { + public static Operation createOperate(String operate) { + Operation oper = null; + switch(operate) { + case "+": + oper = new OperationAddtion(); + break; + case "-": + oper = new OperationSubtraction(); + break; + case "*": + oper = new OperationMultiplication(); + break; + case "/": + oper = new OperationDivision(); + break; + + } + return oper; + + } +} diff --git a/exp6/OperationMultiplication.java b/exp6/OperationMultiplication.java new file mode 100644 index 0000000..42318ab --- /dev/null +++ b/exp6/OperationMultiplication.java @@ -0,0 +1,8 @@ +package exp6; + +public class OperationMultiplication extends Operation{ + + double result(){ + return a*b; + } +} diff --git a/exp6/OperationSubtraction.java b/exp6/OperationSubtraction.java new file mode 100644 index 0000000..5281217 --- /dev/null +++ b/exp6/OperationSubtraction.java @@ -0,0 +1,8 @@ +package exp6; + +public class OperationSubtraction extends Operation{ + + double result(){ + return a-b; + } +} diff --git a/exp7/ButtonChangeColor.java b/exp7/ButtonChangeColor.java new file mode 100644 index 0000000..dde4465 --- /dev/null +++ b/exp7/ButtonChangeColor.java @@ -0,0 +1,54 @@ +package exp7; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class ButtonChangeColor { + public static void main(String[] args) { + // 创建并设置JFrame实例 + JFrame frame = new JFrame("按钮改变字体颜色"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭操作为退出程序 + frame.setSize(400, 200); // 设置窗体大小 + frame.setLocationRelativeTo(null); + + // 设置窗体布局为FlowLayout + frame.setLayout(new FlowLayout()); + + // 创建标签用于显示文本 + JLabel label = new JLabel("点击按钮改变字体颜色"); + frame.add(label); // 将标签添加到窗体 + + // 创建三个按钮并添加到窗体 + JButton redButton = new JButton("红色"); + redButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + label.setForeground(Color.RED); // 设置标签字体颜色为红色 + } + }); + frame.add(redButton); + + JButton greenButton = new JButton("绿色"); + greenButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + label.setForeground(Color.GREEN); // 设置标签字体颜色为绿色 + } + }); + frame.add(greenButton); + + JButton blueButton = new JButton("蓝色"); + blueButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + label.setForeground(Color.BLUE); // 设置标签字体颜色为蓝色 + } + }); + frame.add(blueButton); + + // 显示窗体 + frame.setVisible(true); + } +} diff --git a/exp7/EventExit.java b/exp7/EventExit.java new file mode 100644 index 0000000..c6e4751 --- /dev/null +++ b/exp7/EventExit.java @@ -0,0 +1,15 @@ +package exp7; + +import javax.swing.JFrame;; + +public class EventExit { + public static void main(String[] args) { + JFrame f = new JFrame("窗体关闭事件"); + f.setDefaultCloseOperation(3); + f.setSize(400, 200); + f.setLocationRelativeTo(null); + + + f.setVisible(true); + } +} \ No newline at end of file diff --git a/exp7/QQLongin.java b/exp7/QQLongin.java new file mode 100644 index 0000000..4a22130 --- /dev/null +++ b/exp7/QQLongin.java @@ -0,0 +1,38 @@ +package exp7; + +import java.awt.*; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JTextField; + + +public class QQLongin { + public static void main(String[] args) { + JFrame f = new JFrame("QQ登录"); + f.setDefaultCloseOperation(3); + f.setSize(600, 400); + f.setLocationRelativeTo(null); + f.setLayout(new GridBagLayout()); + + JLabel idText = new JLabel("账号"); + f.add(idText); + + JTextField idField = new JTextField(); + f.add(idField); + + JLabel pwdText = new JLabel("密码"); + f.add(pwdText); + + JTextField pwdField = new JTextField(); + f.add(pwdField); + + JButton loginButton = new JButton("登录"); + JButton registerButton = new JButton("注册"); + f.add(loginButton); + f.add(registerButton); + + f.setVisible(true); + } +} diff --git a/exp7/Stest.java b/exp7/Stest.java new file mode 100644 index 0000000..d2640b9 --- /dev/null +++ b/exp7/Stest.java @@ -0,0 +1,43 @@ +package exp7; + +import java.awt.EventQueue; + +import javax.swing.JFrame; + +public class Stest { + + private JFrame frame; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + Stest window = new Stest(); + window.frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + /** + * Create the application. + */ + public Stest() { + initialize(); + } + + /** + * Initialize the contents of the frame. + */ + private void initialize() { + frame = new JFrame(); + frame.setBounds(100, 100, 450, 300); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + +} diff --git a/exp7/qq.java b/exp7/qq.java new file mode 100644 index 0000000..fa8fbeb --- /dev/null +++ b/exp7/qq.java @@ -0,0 +1,86 @@ +package exp7; + +import java.awt.EventQueue; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import java.awt.GridBagLayout; +import javax.swing.JLabel; +import java.awt.GridBagConstraints; +import java.awt.Insets; +import javax.swing.JTextField; + +public class qq extends JFrame { + + private static final long serialVersionUID = 1L; + private JPanel contentPane; + private JTextField textField; + private JTextField textField_1; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + qq frame = new qq(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + /** + * Create the frame. + */ + public qq() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 450, 300); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + + setContentPane(contentPane); + GridBagLayout gbl_contentPane = new GridBagLayout(); + gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0}; + gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0}; + gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE}; + gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; + contentPane.setLayout(gbl_contentPane); + + JLabel lblNewLabel = new JLabel("账号"); + GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); + gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel.gridx = 1; + gbc_lblNewLabel.gridy = 1; + contentPane.add(lblNewLabel, gbc_lblNewLabel); + + textField = new JTextField(); + GridBagConstraints gbc_textField = new GridBagConstraints(); + gbc_textField.insets = new Insets(0, 0, 5, 0); + gbc_textField.fill = GridBagConstraints.HORIZONTAL; + gbc_textField.gridx = 3; + gbc_textField.gridy = 1; + contentPane.add(textField, gbc_textField); + textField.setColumns(10); + + JLabel lblNewLabel_1 = new JLabel("密码"); + GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints(); + gbc_lblNewLabel_1.insets = new Insets(0, 0, 0, 5); + gbc_lblNewLabel_1.gridx = 1; + gbc_lblNewLabel_1.gridy = 3; + contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1); + + textField_1 = new JTextField(); + GridBagConstraints gbc_textField_1 = new GridBagConstraints(); + gbc_textField_1.fill = GridBagConstraints.HORIZONTAL; + gbc_textField_1.gridx = 3; + gbc_textField_1.gridy = 3; + contentPane.add(textField_1, gbc_textField_1); + textField_1.setColumns(10); + } + +} diff --git a/exp8/CollectionSort.java b/exp8/CollectionSort.java new file mode 100644 index 0000000..533ed6b --- /dev/null +++ b/exp8/CollectionSort.java @@ -0,0 +1,36 @@ +package exp8; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Scanner; + +public class CollectionSort { + public static void main(String[] args) { + + //获取以','分割数字的字符串, 以"-1"结束 + Scanner scan = new Scanner(System.in); + System.out.println("输入以','分割的数字, 以\"-1\"结束"); + String str = scan.nextLine(); + scan.close(); + + //分割字符串 + String[] sNums = str.split(","); + + //将字符串数组转为整形数组 + ArrayList nums = new ArrayList<>(); + for (String s : sNums) { + if(s.equals("-1")) + break; + else + nums.add(Integer.parseInt(s)); + } + + //对整形数组进行排序 + Collections.sort(nums); + + //输出整形数组 + System.out.println("\n排序后的数组:"); + for (Integer num : nums) { + System.out.print(num+" "); + } + } +} diff --git a/exp8/MyList.java b/exp8/MyList.java new file mode 100644 index 0000000..9929643 --- /dev/null +++ b/exp8/MyList.java @@ -0,0 +1,46 @@ +package exp8; + +import java.util.LinkedList; + +public class MyList { + private LinkedList list; + public MyList(){ + list = new LinkedList<>(); + } + + public void show(){ + System.out.print("["); + for(E e :list){ + System.out.print(e+","); + } + System.out.println("\b]\n"); + } + + public void push(E elem){ + list.add(elem); + System.out.println("Push(" + elem +")"); + show(); + } + + public E pop(){ + E elem = list.removeLast(); + System.out.println("Pop(" + elem +")"); + show(); + return elem; + } + + + public static void main(String[] args) { + MyList list = new MyList<>(); + + list.push(1); + list.push(2); + list.push(3); + + list.pop(); + + list.push(4); + } +} + + diff --git a/exp8/Student.java b/exp8/Student.java new file mode 100644 index 0000000..3271552 --- /dev/null +++ b/exp8/Student.java @@ -0,0 +1,15 @@ +package exp8; + +class Student{ + String name; + String calssName; + + public Student(String name, String className){ + this.name = name; + this.calssName = className; + } + + public void show(){ + System.out.println(this.name+","+this.calssName); + } +} \ No newline at end of file diff --git a/exp8/StudentsList.java b/exp8/StudentsList.java new file mode 100644 index 0000000..62b3d9b --- /dev/null +++ b/exp8/StudentsList.java @@ -0,0 +1,31 @@ +package exp8; + +import java.util.ArrayList; + +public class StudentsList { + public static void main(String[] args) { + ArrayList students = new ArrayList(); + + // 外包1班的学生 + students.add(new Student("张三", "外包1班")); + students.add(new Student("李四", "外包1班")); + students.add(new Student("王五", "外包1班")); + students.add(new Student("赵六", "外包1班")); + students.add(new Student("孙七", "外包1班")); + + // 计算机应用1班的学生 + students.add(new Student("周八", "计算机应用1班")); + students.add(new Student("吴九", "计算机应用1班")); + students.add(new Student("郑十", "计算机应用1班")); + + // 计算机应用2班的学生 + students.add(new Student("陈十一", "计算机应用2班")); + students.add(new Student("蒋十二", "计算机应用2班")); + students.add(new Student("沈十三", "计算机应用2班")); + students.add(new Student("韩十四", "计算机应用2班")); + + for(Student s: students){ + s.show(); + } + } +} diff --git a/exp8/TreeSetDemo.java b/exp8/TreeSetDemo.java new file mode 100644 index 0000000..4d4ab4d --- /dev/null +++ b/exp8/TreeSetDemo.java @@ -0,0 +1,24 @@ +package exp8; + +import java.util.Comparator; +import java.util.TreeSet; + +class Roomate{ + String name; + int age; + + Roomate(String name, int age){ + this.name = name; + this.age = age; + } +} + +public class TreeSetDemo { + TreeSet rs = new TreeSet<>(new Comparator() { + @Override + public int compare(Roomate r1, Roomate r2){ + return Integer.compare(r1.age, r2.age); + } + }); + +} diff --git a/exp9/EceptionExample1.java b/exp9/EceptionExample1.java new file mode 100644 index 0000000..d27b6ac --- /dev/null +++ b/exp9/EceptionExample1.java @@ -0,0 +1,16 @@ +package exp9; + +import java.io.*; +public class EceptionExample1 +{ + public static void main(String args[]) throws IOException + { + int c; + //try{ + while((c=System.in.read())!=-1) + System.out.println(c); + //} + //catch(IOException e) + //{ } + } +} \ No newline at end of file diff --git a/exp9/ExceptionDemo.java b/exp9/ExceptionDemo.java new file mode 100644 index 0000000..0e49ab7 --- /dev/null +++ b/exp9/ExceptionDemo.java @@ -0,0 +1,70 @@ +// 当出现继承关系时,异常处理中要注意下面三中情况: +// * A:子类重写父类方法时,子类的方法必须抛出相同的异常或父类异常的子类。 +// * B:如果父类抛出了多个异常,子类重写父类时,只能抛出相同的异常或者是他的子集,子类不能抛出父类没有的异常 +// * C:如果被重写的方法没有异常抛出,那么子类的方法绝对不可以抛出异常,如果子类方法内有异常发生,那么子类只能try,不能throws。 +// 请在下面代码的基础上,通过增加、修改代码对上面三种情况进行测试。 + +package exp9; + +import java.io.FileReader; +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class ExceptionDemo { + public static void main(String[] args) { + Fu o = new Zi(); + try{ + o.show(); + o.method2(); + o.method(); + }catch(Exception e){ + } + } + +} + +class Fu { + public void show() throws Exception { + } + + public void method() { + } + + public void method2() throws Exception{ + } +} + + +class Zi extends Fu { + @Override +// public void show() throws Exception{ + // [* A] + public void show() throws ArithmeticException { + System.out.println(1/0); + } + + @Override + //[* B] + public void method2() throws IOException, ArrayIndexOutOfBoundsException/*, RuntimeException*/{ + new FileReader("NotExistFile").close(); + int[] arr = new int[3]; + System.out.println(arr[5]); + } + + //测试throws和try方法 + // [* C] + @Override + public void method()/*throws ParseException*/{ + try{ + String s = "2014-11-20"; + SimpleDateFormat sdf = new SimpleDateFormat(); + Date d = sdf.parse(s); + System.out.println(d); + }catch(ParseException e){ + System.out.println("解析日期格式失败!"); + } + + } +} \ No newline at end of file diff --git a/exp9/ExceptionExample3.java b/exp9/ExceptionExample3.java new file mode 100644 index 0000000..5ff1980 --- /dev/null +++ b/exp9/ExceptionExample3.java @@ -0,0 +1,21 @@ +package exp9; + +class myexcep extends Exception +{ + //myexcep() + //{System.out.println("Excep occured");} +} +public class ExceptionExample3 +{ + static void f1() throws myexcep + { throw new myexcep(); } + public static void main(String args[]) + { + try + { f1(); } + catch(myexcep e1) + {System.out.println("Exception 1");} + finally //可要可不要的finally部分 +{System.out.println("this part can remain or not!"); } + } +} \ No newline at end of file diff --git a/exp9/ExceptionExample4.java b/exp9/ExceptionExample4.java new file mode 100644 index 0000000..b909aa8 --- /dev/null +++ b/exp9/ExceptionExample4.java @@ -0,0 +1,39 @@ +package exp9; + +class ArgumentOutOfBoundsException extends Exception // 自定义一种异常 +{ + ArgumentOutOfBoundsException() { + System.out.println("输入错误!欲判断的数不能为负数!"); + } +} + +public class ExceptionExample4 { + public static boolean prime(int m) throws ArgumentOutOfBoundsException { + if (m < 0) { + ArgumentOutOfBoundsException ae = new ArgumentOutOfBoundsException(); + throw ae; + } else { + boolean isPrime = true; + for (int i = 2; i < m; i++) + if (m % i == 0) { + isPrime = false; + break; + } + return isPrime; + } + } + + public static void main(String args[]) { + if (args.length != 1) { + System.out.println("输入格式错误!请按照此格式输入:java Usedefine Exception m"); + System.exit(0); + } + int m = Integer.parseInt(args[0]); // 读入这个整数 + try { + boolean result = prime(m); // 调用方法判断是否为素数 + System.out.println("对您输入的整数" + m + "是否为素数的判断结果为:" + result); + } catch (ArgumentOutOfBoundsException e) { + System.out.println("异常名称:" + e.toString()); + } + } +} \ No newline at end of file diff --git a/exp9/ExceptionExmaple2.java b/exp9/ExceptionExmaple2.java new file mode 100644 index 0000000..03518be --- /dev/null +++ b/exp9/ExceptionExmaple2.java @@ -0,0 +1,17 @@ +package exp9; + +import java.io.*; +public class ExceptionExmaple2 +{ + public static void main(String args[]) throws NumberFormatException + { +System.out.println("Please Input an Interger:"); +try{ //此处可能会抛出I/O异常 + BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); + int a=Integer.parseInt(in.readLine()); //此处可能会抛出数据格式异常 + System.out.println("the Integer you input is:"+a); + } +catch(IOException e) //捕获I/O异常并处理 + { System.out.println("I/O error"); } + } +} \ No newline at end of file diff --git a/exp9/ExceptionTest.java b/exp9/ExceptionTest.java new file mode 100644 index 0000000..38a6762 --- /dev/null +++ b/exp9/ExceptionTest.java @@ -0,0 +1,27 @@ +package exp9; + +import java.util.Scanner; + +public class ExceptionTest { + public static void main(String[] args) { + int[] nums = new int[5]; + int size=0; + Scanner scan = new Scanner(System.in); + + while (true) { + try { + String str = scan.nextLine(); + nums[size] = Integer.parseInt(str); + size++; + } catch (NumberFormatException e) { + System.out.println("不接受非整数字符!"); + } catch (ArrayIndexOutOfBoundsException e){ + System.out.println("数组已满!"); + break; + } + } + + scan.close(); + } + +} \ No newline at end of file diff --git a/exp9/FinallyDemo2.java b/exp9/FinallyDemo2.java new file mode 100644 index 0000000..0c15575 --- /dev/null +++ b/exp9/FinallyDemo2.java @@ -0,0 +1,29 @@ +// 如下所示代码中,如果catch里面有return语句,请问finally的代码还会执行吗? +// 如果会,请问是在return前还是return后。请使用debug方法查看程序中的变量a的取值是多少。 +// 如果将最后一名return a;语句移到finally中,那么又会是什么情况。 + +package exp9; + +public class FinallyDemo2 { + public static void main(String[] args) { + System.out.println(getInt()); + } + public static int getInt() { + int a = 10; + try { + System.out.println("try"); + System.out.println(a / 0); + a = 20; + } catch (ArithmeticException e) { + System.out.println("catch"); + a = 30; + return a; + + } finally { + System.out.println("finial"); + a = 40; + //return a; + } + return a; + } +} \ No newline at end of file diff --git a/test/PolymorphismTest.java b/test/PolymorphismTest.java new file mode 100644 index 0000000..600fde1 --- /dev/null +++ b/test/PolymorphismTest.java @@ -0,0 +1,30 @@ +package test; + +public class PolymorphismTest { + public static void main(String[] args) { + Student[] studentArray = new Student[5]; + studentArray[0] = new Undergraduate("陈建平"); //本科生陈建平 + studentArray[1] = new Undergraduate("鲁向东"); //本科生鲁向东 + studentArray[2] = new Postgraduate("匡晓华"); //研究生匡晓华 + studentArray[3] = new Undergraduate("周丽娜"); //本科生周丽娜 + studentArray[4] = new Postgraduate("梁欣欣"); //研究生梁欣欣 + + for (int i=0; i=90){ + currentGrade = "优秀"; + } + else if (average>=80&&average<90) { + currentGrade = "良好"; + } + else if (average>=70&&average<80) { + currentGrade = "一般"; + } + else if (average>=60&&average<70) { + currentGrade = "及格"; + } + else{ + currentGrade = "不及格"; + } + setCourseGrade(currentGrade); + } + } \ No newline at end of file diff --git a/test/ShapeDemo.java b/test/ShapeDemo.java new file mode 100644 index 0000000..7bf4d7b --- /dev/null +++ b/test/ShapeDemo.java @@ -0,0 +1,74 @@ +package test; + +abstract class Shapes { + public int x,y; //x,y为的坐标 + public int width,height; + public Shapes(int x,int y,int width,int height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + public abstract double getArea(); + public abstract double getPerimeter(); + public abstract void show(); + } + + class Rectangle extends Shapes{ + double a; + double b; + + Rectangle(double a, double b){ + super(0, 0, 0, 0); + this.a=a; + this.b=b; + } + + public double getArea(){ + return a*b; + } + + public double getPerimeter(){ + return (a+b)*2; + } + + public void show(){ + System.out.println("矩形 长:"+this.a+"宽:"+this.b); + } + } + + class Circle extends Shapes{ + double radius;//半径 + + Circle(double radius){ + super(0, 0, 0, 0); + this.radius=radius; + } + + public double getArea(){ + return 3.14159265324*radius*radius; + } + + public double getPerimeter(){ + return 3.14159265324*radius*2; + } + + public void show(){ + System.out.println("圆 半径:"+this.radius); + } + } + + +public class ShapeDemo { + public static void main(String[] args){ + Shapes r = new Rectangle(2, 3); + Shapes c = new Circle(5); + + r.show(); + System.out.println("面积:"+r.getArea()+" 周长:"+r.getPerimeter()+"\n"); + + c.show(); + System.out.println("面积:"+c.getArea()+" 周长:"+c.getPerimeter()+"\n"); + + } +} diff --git a/test/Student.java b/test/Student.java new file mode 100644 index 0000000..ae4e00c --- /dev/null +++ b/test/Student.java @@ -0,0 +1,56 @@ +package test; + +public abstract class Student { + final static int CourseNo = 3; + private String name; + private String type; + private int[] courses; + private String courseGrade; + public Student(){ + + } + public Student(String name,String type) + { + this.name = name; + this.type = type; + courses = new int[3]; + courseGrade=""; + } + public abstract void calculateGrade(); + public String getName( ) + { + return name; + } + public String getType( ) + { + return type; //返回学生类型 + } + public String getCourseGrade( ) + { + return courseGrade; + } + + public int getCourseScore(int courseNumber) + { + return courses[courseNumber]; + } + + public void setName(String name) + { + this.name = name; + } + + public void setType(String type) + { + this.type = type; + } + + public void setCourseScore(int courseNumber, int courseScore) + { + courses[courseNumber] = courseScore;//按课程索引号设置课程成绩 + } + public void setCourseGrade(String courseGrade) { + this.courseGrade = courseGrade; + } +} + diff --git a/test/Undergraduate.java b/test/Undergraduate.java new file mode 100644 index 0000000..3b3df8d --- /dev/null +++ b/test/Undergraduate.java @@ -0,0 +1,33 @@ +package test; + +class Undergraduate extends Student { + public Undergraduate(String name) + { + super(name,"本科生"); + } + public void calculateGrade() { + int total = 0; + double average = 0; + for (int i = 0; i < CourseNo; i++) { + total=total+getCourseScore(i); // 累加各门课程成绩 + }; + average = total / CourseNo; + String currentGrade=""; + if (average>=80&&average<100){ + currentGrade = "优秀"; + } + else if (average>=70&&average<80){ + currentGrade = "良好"; + } + else if (average>=60&&average<70){ + currentGrade = "一般"; + } + else if (average>=50&&average<60){ + currentGrade = "及格"; + } + else{ + currentGrade = "不及格"; + } + setCourseGrade(currentGrade); + } +} \ No newline at end of file