第一次提交

This commit is contained in:
skyofdream 2024-05-09 09:10:51 +08:00
commit 6d5c439fb2
74 changed files with 2193 additions and 0 deletions

View File

@ -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);
}
}

11
Class/c7/FirstGUI.java Normal file
View File

@ -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);
}
}

View File

@ -0,0 +1,5 @@
package cn.gdpu;
public interface roleExpansion {
void finishingMove();
}

View File

@ -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()+"发动了"+"降龙掌");
}
}

View File

@ -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()+"发动了"+"一阳指");
}
}

49
com/experiment/Role.java Normal file
View File

@ -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("");
}
}

View File

@ -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()+"发动了"+"六脉剑");
}
}

1
exp1/3.java Normal file
View File

@ -0,0 +1 @@
package exp1;

31
exp1/Compare.java Normal file
View File

@ -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);
}
}

27
exp1/Score.java Normal file
View File

@ -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("不及格");
}
}
}

14
exp1/Smp3.java Normal file
View File

@ -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);
}
}

9
exp1/calculate.java Normal file
View File

@ -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));
}
}

67
exp10/CopyFile.java Normal file
View File

@ -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();
}
}

27
exp10/ListFiles.java Normal file
View File

@ -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{
}
}
}
}

View File

@ -0,0 +1,2 @@
package exp10.MultiFile;
public class file1 {}

View File

@ -0,0 +1,2 @@
package exp10.MultiFile;
public class file2 {}

View File

@ -0,0 +1,2 @@
package exp10.MultiFile;
public class file3 {}

7
exp11/AccountDemo.java Normal file
View File

@ -0,0 +1,7 @@
package exp11;
import javax.swing.*;
public class AccountDemo {
JFrame rejister = new JFrame();
}

50
exp2/Read.java Normal file
View File

@ -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();
}
}

59
exp2/StudentTest.java Normal file
View File

@ -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);
}
}

32
exp3/Animal.java Normal file
View File

@ -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+"奔跑...");
}
}

View File

@ -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();
}
}

21
exp3/Car.java Normal file
View File

@ -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);
}
}

24
exp3/Cat.java Normal file
View File

@ -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+"在喵喵叫");
}
}

24
exp3/Dog.java Normal file
View File

@ -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+"在吠叫");
}
}

88
exp3/ExtendsDemo.java Normal file
View File

@ -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();
}
}

37
exp3/Line.java Normal file
View File

@ -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;
}
}

8
exp3/LineDemo.java Normal file
View File

@ -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());
}
}

15
exp3/Point.java Normal file
View File

@ -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;
}
}

21
exp3/Trunk.java Normal file
View File

@ -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);
}
}

28
exp3/Vehicle.java Normal file
View File

@ -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);
}
}

23
exp3/VehicleClient.java Normal file
View File

@ -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();
}
}

48
exp3/test.java Normal file
View File

@ -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);
}
}

62
exp4/AnimalTool.java Normal file
View File

@ -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();
}
}

25
exp4/PolymorphicDemo.java Normal file
View File

@ -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);
}
}

View File

@ -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<studentArray.length;i++) {
studentArray[i].setCourseScore(0,87);
studentArray[i].setCourseScore(1,90);
studentArray[i].setCourseScore(2,78);
}
for (int i=0; i<5 ;i++) {
studentArray[i].calculateGrade();
}
System.out.println("姓名" + " 类型" +" 成绩");
System.out.println("-----------------------");
for (int i=0; i<5 ;i++) {
System.out.println(studentArray[i].getName( )+" "+
studentArray[i].getType( )+" "+
studentArray[i].getCourseGrade( ));
}
}
}

34
exp4/Postgraduate.java Normal file
View File

@ -0,0 +1,34 @@
package exp4;
class Postgraduate extends Student {
public Postgraduate(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>=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);
}
}

74
exp4/ShapeDemo.java Normal file
View File

@ -0,0 +1,74 @@
package exp4;
abstract class Shapes {
public int x,y; //xy为的坐标
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");
}
}

56
exp4/Student.java Normal file
View File

@ -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;
}
}

33
exp4/Undergraduate.java Normal file
View File

@ -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);
}
}

21
exp5/Outer.java Normal file
View File

@ -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();
}
}

18
exp5/OuterDemo.java Normal file
View File

@ -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();
}
}

View File

@ -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!!!");
}
});
}
}

33
exp5/RoleDemo.java Normal file
View File

@ -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");
}
}

29
exp6/AdapterDemo.java Normal file
View File

@ -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();
}
}

24
exp6/Calculator.java Normal file
View File

@ -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());
}
}

14
exp6/Operation.java Normal file
View File

@ -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();
}

View File

@ -0,0 +1,8 @@
package exp6;
public class OperationAddtion extends Operation{
double result(){
return a+b;
}
}

View File

@ -0,0 +1,8 @@
package exp6;
public class OperationDivision extends Operation{
double result(){
return a/b;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,8 @@
package exp6;
public class OperationMultiplication extends Operation{
double result(){
return a*b;
}
}

View File

@ -0,0 +1,8 @@
package exp6;
public class OperationSubtraction extends Operation{
double result(){
return a-b;
}
}

View File

@ -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);
}
}

15
exp7/EventExit.java Normal file
View File

@ -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);
}
}

38
exp7/QQLongin.java Normal file
View File

@ -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);
}
}

43
exp7/Stest.java Normal file
View File

@ -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);
}
}

86
exp7/qq.java Normal file
View File

@ -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);
}
}

36
exp8/CollectionSort.java Normal file
View File

@ -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<Integer> 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+" ");
}
}
}

46
exp8/MyList.java Normal file
View File

@ -0,0 +1,46 @@
package exp8;
import java.util.LinkedList;
public class MyList<E> {
private LinkedList<E> 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<Integer> list = new MyList<>();
list.push(1);
list.push(2);
list.push(3);
list.pop();
list.push(4);
}
}

15
exp8/Student.java Normal file
View File

@ -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);
}
}

31
exp8/StudentsList.java Normal file
View File

@ -0,0 +1,31 @@
package exp8;
import java.util.ArrayList;
public class StudentsList {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
// 外包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();
}
}
}

24
exp8/TreeSetDemo.java Normal file
View File

@ -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<Roomate> rs = new TreeSet<>(new Comparator<Roomate>() {
@Override
public int compare(Roomate r1, Roomate r2){
return Integer.compare(r1.age, r2.age);
}
});
}

View File

@ -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)
//{ }
}
}

70
exp9/ExceptionDemo.java Normal file
View File

@ -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("解析日期格式失败!");
}
}
}

View File

@ -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!"); }
}
}

View File

@ -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());
}
}
}

View File

@ -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"); }
}
}

27
exp9/ExceptionTest.java Normal file
View File

@ -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();
}
}

29
exp9/FinallyDemo2.java Normal file
View File

@ -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;
}
}

View File

@ -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<studentArray.length;i++) {
studentArray[i].setCourseScore(0,87);
studentArray[i].setCourseScore(1,90);
studentArray[i].setCourseScore(2,78);
}
for (int i=0; i<5 ;i++) {
studentArray[i].calculateGrade();
}
System.out.println("姓名" + " 类型" +" 成绩");
System.out.println("-----------------------");
for (int i=0; i<5 ;i++) {
System.out.println(studentArray[i].getName( )+" "+
studentArray[i].getType( )+" "+
studentArray[i].getCourseGrade( ));
}
}
}

34
test/Postgraduate .java Normal file
View File

@ -0,0 +1,34 @@
package test;
class Postgraduate extends Student {
public Postgraduate(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>=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);
}
}

74
test/ShapeDemo.java Normal file
View File

@ -0,0 +1,74 @@
package test;
abstract class Shapes {
public int x,y; //xy为的坐标
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");
}
}

56
test/Student.java Normal file
View File

@ -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;
}
}

33
test/Undergraduate.java Normal file
View File

@ -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);
}
}