From 1c869f816f15df1bd58db1431e2fdd637b5d9ad9 Mon Sep 17 00:00:00 2001 From: KiriAky 107 Date: Wed, 29 Oct 2025 00:52:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=83=E6=AC=A1=E5=AE=9E=E9=AA=8C?= =?UTF-8?q?=E8=AF=BE=E4=BB=A3=E7=A0=81=20=E6=B7=BB=E5=8A=A0C04~C07?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++ .idea/MarsCodeWorkspaceAppSettings.xml | 6 ++ .idea/misc.xml | 6 ++ .idea/modules.xml | 14 ++++ .idea/vcs.xml | 6 ++ C03 ControlFlow/src/EncryptDemo.java | 4 +- C03 ControlFlow/src/NumberCounter.java | 1 - C04 ClassDefine/C04 ClassDefine.iml | 11 +++ C04 ClassDefine/src/Hero.java | 68 ++++++++++++++++ C04 ClassDefine/src/Test.java | 38 +++++++++ C05 Extends/C05 Extends.iml | 11 +++ C05 Extends/src/shape/Circle.java | 35 +++++++++ C05 Extends/src/shape/Rectangle.java | 14 ++++ C05 Extends/src/shape/Shape.java | 52 +++++++++++++ C05 Extends/src/test/ShapeDrawerTest.java | 55 +++++++++++++ C06 Polymorphism/C06 Polymorphism.iml | 11 +++ C06 Polymorphism/src/shape/Circle.java | 49 ++++++++++++ C06 Polymorphism/src/shape/Rectangle.java | 56 ++++++++++++++ C06 Polymorphism/src/shape/Shape.java | 52 +++++++++++++ .../src/test/ShapeDrawerTest.java | 77 +++++++++++++++++++ C07 StaticAndEnum/C07 StaticAndEnum.iml | 11 +++ C07 StaticAndEnum/src/CaptchaUtils.java | 52 +++++++++++++ C07 StaticAndEnum/src/LoginStatus.java | 72 +++++++++++++++++ C07 StaticAndEnum/src/Test.java | 55 +++++++++++++ 24 files changed, 761 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/MarsCodeWorkspaceAppSettings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 C04 ClassDefine/C04 ClassDefine.iml create mode 100644 C04 ClassDefine/src/Hero.java create mode 100644 C04 ClassDefine/src/Test.java create mode 100644 C05 Extends/C05 Extends.iml create mode 100644 C05 Extends/src/shape/Circle.java create mode 100644 C05 Extends/src/shape/Rectangle.java create mode 100644 C05 Extends/src/shape/Shape.java create mode 100644 C05 Extends/src/test/ShapeDrawerTest.java create mode 100644 C06 Polymorphism/C06 Polymorphism.iml create mode 100644 C06 Polymorphism/src/shape/Circle.java create mode 100644 C06 Polymorphism/src/shape/Rectangle.java create mode 100644 C06 Polymorphism/src/shape/Shape.java create mode 100644 C06 Polymorphism/src/test/ShapeDrawerTest.java create mode 100644 C07 StaticAndEnum/C07 StaticAndEnum.iml create mode 100644 C07 StaticAndEnum/src/CaptchaUtils.java create mode 100644 C07 StaticAndEnum/src/LoginStatus.java create mode 100644 C07 StaticAndEnum/src/Test.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/MarsCodeWorkspaceAppSettings.xml b/.idea/MarsCodeWorkspaceAppSettings.xml new file mode 100644 index 0000000..e2a065b --- /dev/null +++ b/.idea/MarsCodeWorkspaceAppSettings.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d19f3f8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/C03 ControlFlow/src/EncryptDemo.java b/C03 ControlFlow/src/EncryptDemo.java index 1652e09..1a79c9a 100644 --- a/C03 ControlFlow/src/EncryptDemo.java +++ b/C03 ControlFlow/src/EncryptDemo.java @@ -13,7 +13,7 @@ public class EncryptDemo { // for(char c:enc) // System.out.print(c); - System.out.println(Arrays.toString(enc)); + System.out.println("密文:"+Arrays.toString(enc)); char[] dec= new char[enc.length]; for(int i=0;i + + + + + + + + + + \ No newline at end of file diff --git a/C04 ClassDefine/src/Hero.java b/C04 ClassDefine/src/Hero.java new file mode 100644 index 0000000..708b4a2 --- /dev/null +++ b/C04 ClassDefine/src/Hero.java @@ -0,0 +1,68 @@ +public class Hero { + String name;//名称 + char sex;//性别 + int cost;//费用 + int health;//生命值 + int magic;//魔法值 + int attack;//攻击力 + int level;//等级(1-3) 1 + int physicalResist;//物理抗性 + int magicResist;//魔法抗性 + + //升级行为方法 + public void levelUp(){ + if (level < 3) { + this.level++; + this.health = (int) (this.health * 1.5); + this.attack = (int) (this.attack * 1.5); + System.out.format("%s升级到%d级\n", this.name, this.level); + } + + } + + //攻击行为方法 + public void attack(Hero target){ + if(this.health>0) { + int damage = calculateDamage(attack, target.physicalResist); + System.out.format("%s攻击了%s,造成了%d点伤害\n", name, target.name, attack); + //必须为有效伤害 + if (damage > 0) + target.takeDamage(damage); + else + System.out.println("攻击失败"); + } + else + System.out.println(name+"已死亡,攻击失败"); + } + + //受到伤害行为方法 + public void takeDamage(int damage){ + health -= damage; + health=Math.max(health,0); + if(health>0) + System.out.format("%s受到了%d点伤害,剩余生命值%d\n",name,damage,health); + else + System.out.format("%s受到了%d点伤害,剩余生命值为0,已被击败\n",name,damage); + } + + private int calculateDamage(int damage,int resist){ + return (int)(damage*1.0/(1+resist*1.0/100)); + } + + //无参构造器 + public Hero() { + this.level=1; + } + //有参构造器 + public Hero(String name, char sex, int cost, int health, int magic, int attack, int physicalResist, int magicResist) { + this();//在有参构造器中调用无参构造器 + this.name = name; + this.sex = sex; + this.cost = cost; + this.health = health; + this.magic = magic; + this.attack = attack; + this.physicalResist = physicalResist; + this.magicResist = magicResist; + } +} diff --git a/C04 ClassDefine/src/Test.java b/C04 ClassDefine/src/Test.java new file mode 100644 index 0000000..4c5951b --- /dev/null +++ b/C04 ClassDefine/src/Test.java @@ -0,0 +1,38 @@ +public class Test { + public static void main(String[] args) { + //1.无参构造器,逐一赋值 + Hero almin = new Hero(); + almin.name="almin"; + almin.sex='m'; + almin.cost=1; + almin.health=1000; + almin.magic=100; + almin.attack=60; + almin.physicalResist=50; + almin.magicResist=50; + System.out.println(almin.name); + System.out.println(almin.health); + System.out.println(almin.level); + //2.有参构造器 + Hero alen = new Hero("alen",'m',1,600,100,60,100,50); + System.out.println(alen.name); + System.out.println(alen.health); + System.out.println(alen.level); + + + //3.攻击 + //almin.attack(alen); + System.out.println("游戏开始"); + alen.levelUp(); + int round = 0; + while(almin.health>0 && alen.health>0){ + round++; + System.out.println("第"+round+"回合"); + almin.attack(alen); + alen.attack(almin); + System.out.println("====================="); + } + System.out.format("%s获胜,%s失败",almin.health==0?alen.name:almin.name, + almin.health==0?almin.name:alen.name); + } +} diff --git a/C05 Extends/C05 Extends.iml b/C05 Extends/C05 Extends.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/C05 Extends/C05 Extends.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/C05 Extends/src/shape/Circle.java b/C05 Extends/src/shape/Circle.java new file mode 100644 index 0000000..42fbdc1 --- /dev/null +++ b/C05 Extends/src/shape/Circle.java @@ -0,0 +1,35 @@ +package shape; +import java.awt.*; + +public class Circle extends Shape { + private int radius; + + @Override + public double getArea(){ + return Math.PI*radius*radius; + } + + @Override + public double getPerimeter(){ + return 2*Math.PI*radius; + } + @Override + public void draw(Graphics g) { + g.setColor(color); + // 绘制圆形 + g.fillOval(x - radius, y - radius, radius * 2, radius * 2); + + // 绘制信息 + g.setColor(Color.BLACK); + g.drawString(String.format("面积: %.2f", getArea()), x + radius + 5, y); + g.drawString(String.format("周长: %.2f", getPerimeter()), x + radius + 5, y + 15); + } + + public Circle(){ + this(Color.RED,100,100,50); + } + public Circle(Color color,int x,int y,int radius){ + super(color,x,y); + this.radius=radius; + } +} diff --git a/C05 Extends/src/shape/Rectangle.java b/C05 Extends/src/shape/Rectangle.java new file mode 100644 index 0000000..acc7863 --- /dev/null +++ b/C05 Extends/src/shape/Rectangle.java @@ -0,0 +1,14 @@ +package shape; +import java.awt.*; + +public class Rectangle extends Shape { + private int width,height; + public Rectangle() { + super(); + } + public Rectangle(Color color, int x, int y, int width, int height) { + super(color, x, y); + this.width = width; + this.height = height; + } +} diff --git a/C05 Extends/src/shape/Shape.java b/C05 Extends/src/shape/Shape.java new file mode 100644 index 0000000..6afc876 --- /dev/null +++ b/C05 Extends/src/shape/Shape.java @@ -0,0 +1,52 @@ +package shape; +import java.awt.*; + +public class Shape { + protected Color color; + protected int x,y; + + public void setColor(Color color) { + this.color = color; + } + public Color getColor() { + return color; + } + public double getArea(){ + return x*y; + } + + public void setX(int x){ + this.x=x; + } + public int getX(){ + return x; + } + + public void setY(int y){ + this.y=y; + } + public int getY(){ + return y; + } + + public double getPerimeter(){ + return x*y; + } + + public void move(int dx,int dy){ + x += dx; + y += dy; + } + + public void draw(Graphics g){ + g.setColor(color); + g.fillRect(x,y,1,1); + } + + Shape(){} + Shape(Color color,int x,int y){ + this.color=color; + this.x=x; + this.y=y; + } +} diff --git a/C05 Extends/src/test/ShapeDrawerTest.java b/C05 Extends/src/test/ShapeDrawerTest.java new file mode 100644 index 0000000..8a564de --- /dev/null +++ b/C05 Extends/src/test/ShapeDrawerTest.java @@ -0,0 +1,55 @@ +package test; + +import javax.swing.*; +import java.awt.*; +import shape.Circle; + +public class ShapeDrawerTest extends JFrame { + /*******请在下面添加**************/ + // 【任务1】定义一个包含5个元素的圆形数组 + Circle[] circles = new Circle[5]; + /*******请在上面添加**************/ + + // 初始化图形 + private void initShapes() { + /*******请在下面添加**************/ + // 【任务2】采用无参构造添加一个红色,坐标为(100,100),半径为50的圆形,并加入对应数组 + circles[0] = new Circle(); + // 【任务3】采用有参构造添加一个黄色,坐标为(150,300),半径为70的圆形,并加入对应数组 + circles[1] = new Circle(Color.YELLOW, 150, 300, 70); + circles[2] = new Circle(); + circles[3] = new Circle(); + circles[4] = new Circle(); + /*******请在上面添加**************/ + } + + // 重写绘制方法 + @Override + public void paint(Graphics g) { + super.paint(g); + /*******请在下面添加**************/ + // 绘制所有图形 + // 【任务4】采用增强型for循环绘制所有图形 + for (Circle circle : circles) { + circle.draw(g); + } + /*******请在上面添加**************/ + // 绘制标题 + g.setColor(Color.BLACK); + g.drawString("Java继承图形绘制示例", 300, 30); + } + public ShapeDrawerTest() { + // 初始化窗口 + setTitle("图形绘制程序"); + setSize(800, 600); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + // 创建各种图形对象 + initShapes(); + } + public static void main(String[] args) { + new ShapeDrawerTest().setVisible(true); + } +} + diff --git a/C06 Polymorphism/C06 Polymorphism.iml b/C06 Polymorphism/C06 Polymorphism.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/C06 Polymorphism/C06 Polymorphism.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/C06 Polymorphism/src/shape/Circle.java b/C06 Polymorphism/src/shape/Circle.java new file mode 100644 index 0000000..8f235a4 --- /dev/null +++ b/C06 Polymorphism/src/shape/Circle.java @@ -0,0 +1,49 @@ +package shape; +import java.awt.*; + +public class Circle extends Shape { + private int radius; + + public void setRadius(int r) { + radius = r; + } + + public int getRadius() { + return radius; + } + @Override + public double getArea(){ + return Math.PI*radius*radius; + } + + @Override + public double getPerimeter(){ + return 2*Math.PI*radius; + } + @Override + public void draw(Graphics g) { + g.setColor(color); + // 绘制圆形 + g.fillOval(x - radius, y - radius, radius * 2, radius * 2); + + // 绘制信息 + g.setColor(Color.BLACK); + g.drawString(String.format("面积: %.2f", getArea()), x + radius + 5, y); + g.drawString(String.format("周长: %.2f", getPerimeter()), x + radius + 5, y + 15); + } + public void drawCenterMark(Graphics g) { + g.setColor(Color.BLACK); + g.drawLine(x-5,y,x+5,y); + g.drawLine(x,y-5,x,y+5); + g.drawString("O", x+5, y+5); + } + + public Circle(){ + super(); + } + public Circle(Color color,int x,int y,int radius){ + super(color,x,y); + this.radius=radius; + } + +} diff --git a/C06 Polymorphism/src/shape/Rectangle.java b/C06 Polymorphism/src/shape/Rectangle.java new file mode 100644 index 0000000..cdb4e29 --- /dev/null +++ b/C06 Polymorphism/src/shape/Rectangle.java @@ -0,0 +1,56 @@ +package shape; +import java.awt.*; + +public class Rectangle extends Shape { + private int width,height; + + public void setWidth(int width) { + this.width = width; + } + + public void setHeight(int height) { + this.height = height; + } + + public int getHeight() { + return height; + } + + public int getWidth() { + return width; + } + + @Override + public double getArea() { + return width*height; + } + @Override + public double getPerimeter() { + return 2*(width+height); + } + @Override + public void draw(Graphics g) { + g.setColor(color); + g.fillRect(x, y, width, height); + // 绘制信息 + g.setColor(Color.BLACK); + g.drawString(String.format("面积: %.2f", getArea()), x + width + 5, y); + g.drawString(String.format("周长: %.2f", getPerimeter()), x + width + 5, y + 15); + } + + public void drawDiagonals(Graphics g) { + g.setColor(Color.BLACK); + g.drawLine(x, y, x+width, y+height); + g.drawLine(x, y+height, x+width, y); + } + + public Rectangle() { + super(); + } + + public Rectangle(Color color, int x, int y, int width, int height) { + super(color, x, y); + this.width = width; + this.height = height; + } +} diff --git a/C06 Polymorphism/src/shape/Shape.java b/C06 Polymorphism/src/shape/Shape.java new file mode 100644 index 0000000..6afc876 --- /dev/null +++ b/C06 Polymorphism/src/shape/Shape.java @@ -0,0 +1,52 @@ +package shape; +import java.awt.*; + +public class Shape { + protected Color color; + protected int x,y; + + public void setColor(Color color) { + this.color = color; + } + public Color getColor() { + return color; + } + public double getArea(){ + return x*y; + } + + public void setX(int x){ + this.x=x; + } + public int getX(){ + return x; + } + + public void setY(int y){ + this.y=y; + } + public int getY(){ + return y; + } + + public double getPerimeter(){ + return x*y; + } + + public void move(int dx,int dy){ + x += dx; + y += dy; + } + + public void draw(Graphics g){ + g.setColor(color); + g.fillRect(x,y,1,1); + } + + Shape(){} + Shape(Color color,int x,int y){ + this.color=color; + this.x=x; + this.y=y; + } +} diff --git a/C06 Polymorphism/src/test/ShapeDrawerTest.java b/C06 Polymorphism/src/test/ShapeDrawerTest.java new file mode 100644 index 0000000..bd507ba --- /dev/null +++ b/C06 Polymorphism/src/test/ShapeDrawerTest.java @@ -0,0 +1,77 @@ +package test; + +import shape.Circle; +import shape.Rectangle; +import javax.swing.*; +import java.awt.*; + +public class ShapeDrawerTest extends JFrame { + /*******请在下面添加**************/ + // 【任务1】定义一个包含5个元素的圆形数组 + Circle[] circles = new Circle[5]; + Rectangle[] rectangles = new Rectangle[2]; + private shape.Shape[] shapes = new shape.Shape[4]; + + /*******请在上面添加**************/ + + // 初始化图形 + private void initShapes() { + /*******请在下面添加**************/ + // 【任务2】采用无参构造添加一个红色,坐标为(100,100),半径为50的圆形,并加入对应数组 + shapes[0] = new Circle(); + ((Circle)shapes[0]).setX(100); + ((Circle)shapes[0]).setY(100); + ((Circle)shapes[0]).setRadius(50); + ((Circle)shapes[0]).setColor(Color.RED); + // 【任务3】采用有参构造添加一个黄色,坐标为(150,300),半径为70的圆形,并加入对应数组 + shapes[1] = new Circle(Color.YELLOW, 150, 300, 70); + // 【任务4】采用无参构造添加一个绿色,坐标为(250,80),宽为100,高为80的矩形,并加入对应数组 + shapes[2] = new Rectangle(); + ((Rectangle)shapes[2]).setX(250); + ((Rectangle)shapes[2]).setY(80); + ((Rectangle)shapes[2]).setWidth(100); + ((Rectangle)shapes[2]).setHeight(80); + ((Rectangle)shapes[2]).setColor(Color.GREEN); + // 【任务5】采用有参构造添加一个粉色,坐标为(300,300),宽为120,高为60的矩形 + shapes[3] = new Rectangle(Color.PINK, 300, 300, 120, 60); + // 【任务6】下移动第二个矩形50个单位 + ((Rectangle)shapes[3]).setY(this.getY()-50); + /*******请在上面添加**************/ + } + + // 重写绘制方法 + @Override + public void paint(Graphics g) { + super.paint(g); + /*******请在下面添加**************/ + // 绘制所有图形 + // 【任务4】采用增强型for循环绘制所有图形 + for (shape.Shape shape : shapes) { + shape.draw(g); + if (shape instanceof Circle) { + ((Circle)shape).drawCenterMark(g); + } + if (shape instanceof Rectangle) { + ((Rectangle)shape).drawDiagonals(g); + } + } + /*******请在上面添加**************/ + // 绘制标题 + g.setColor(Color.BLACK); + g.drawString("Java继承图形绘制示例", 300, 50); + } + public ShapeDrawerTest() { + // 初始化窗口 + setTitle("图形绘制程序"); + setSize(800, 600); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + // 创建各种图形对象 + initShapes(); + } + public static void main(String[] args) { + new ShapeDrawerTest().setVisible(true); + } +} + diff --git a/C07 StaticAndEnum/C07 StaticAndEnum.iml b/C07 StaticAndEnum/C07 StaticAndEnum.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/C07 StaticAndEnum/C07 StaticAndEnum.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/C07 StaticAndEnum/src/CaptchaUtils.java b/C07 StaticAndEnum/src/CaptchaUtils.java new file mode 100644 index 0000000..3758bb3 --- /dev/null +++ b/C07 StaticAndEnum/src/CaptchaUtils.java @@ -0,0 +1,52 @@ +import java.util.*; + +/** + * 验证码工具类 + */ +public class CaptchaUtils { // final修饰类:不能被继承 + // 【任务1】静态常量:字符集(全局固定) + static final String CHAR_SET="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + // 【任务2】静态常量:验证码长度(全局固定) + static final int CAPTCHA_LENGTH=4; + + // 【任务3】私有构造方法:禁止创建实例(工具类无需实例化) + private CaptchaUtils(){ + } + + /** + * 【任务4】 + * 静态方法:generateCaptcha 生成随机验证码 + * @return String 返回生成的随机验证码字符串 + */ + public static String generateCaptcha(){ + Random random = new Random(); + //生成随机验证码 + StringBuilder sb = new StringBuilder(); + for(int i=0;i