该文章Github地址:https://github.com/AntonyCheng/java-notes
在此介绍一下作者开源的SpringBoot项目初始化模板(Github仓库地址:https://github.com/AntonyCheng/spring-boot-init-template & CSDN文章地址:https://blog.csdn.net/AntonyCheng/article/details/136555245),该模板集成了最常见的开发组件,同时基于修改配置文件实现组件的装载,除了这些,模板中还有非常丰富的整合示例,同时单体架构也非常适合SpringBoot框架入门,如果觉得有意义或者有帮助,欢迎Star & Issues & PR!
上一章:由浅到深认识Java语言(30):集合
41.阶段性练习
练习一:
- 创建一个动物类,定义种类,性别和属性;
- 从键盘录入数据,通过构造方法存入对象中,并调用对象的方法打印;
import java.util.Scanner;public class Demo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入物种名字:");String species = sc.next();System.out.println("请输入物种性别:");char sex = sc.next().charAt(0);Animal a = new Animal(species, sex);a.printObject();}
}class Animal{String species;char sex;public Animal(String species,char sex) {this.species = species;this.sex = sex;}public void printObject() {System.out.println("物种:"+species+" 性别:"+sex);}
}
打印效果如下:
练习二:
- 描述猫类,有属性 name、color、sex,通过构造方法给猫的属性赋值(一个是波斯猫,一个是土猫,其他属性自定);
- 为猫类添加吃饭,抓老鼠方法,创建两只猫的对象;
- 分别调用猫的吃饭,抓老鼠方法;
public class Demo {public static void main(String[] args) {Cat bosi = new Cat("波斯猫", "红色", '雄');Cat tumao = new Cat("土猫", "绿色", '雌');bosi.eat(bosi.name);tumao.catchMouse(tumao.name);}
}class Cat {String name;String color;char sex;public Cat() {super();// TODO Auto-generated constructor stub}public Cat(String name, String color, char sex) {super();this.name = name;this.color = color;this.sex = sex;}public void eat(String name) {System.out.println(name+"吃饭!");}public void catchMouse(String name) {System.out.println(name+"抓老鼠!");}
}
打印效果如下:
练习三:
- 创建一个工具类,定义方法用来将基本数据类型转换成字符串;
public class Demo {public static void main(String[] args) {Tools t = new Tools();byte b = 123;String s1 = t.getStringFromByte(b);short s = 1234;String s2 = t.getStringFromShort(s);char c = '1';String s3 = t.getStringFromBChar(c);int i = 12345;String s4 = t.getStringFromInt(i);float f = 12345.6f;String s5 = t.getStringFromFloat(f);double d = 1234567.8;String s6 = t.getStringFromDouble(d);long l = 12345678;String s7 = t.getStringFromLong(l);boolean bool = false;String s8 = t.getStringFromBoolean(bool);System.out.printf("%s %s %s %s %s %s %s %s",s1,s2,s3,s4,s5,s6,s7,s8);}
}class Tools {public String getStringFromByte(byte num) {return num+"";}public String getStringFromShort(short num) {return num+"";}public String getStringFromBChar(char num) {return num+"";}public String getStringFromInt(int num) {return num+"";}public String getStringFromFloat(float num) {return num+"";}public String getStringFromDouble(double num) {return num+"";}public String getStringFromLong(long num) {return num+"";}public String getStringFromBoolean(boolean num) {return num+"";}
}
打印效果如下:
练习四:
- 一档选秀节目,设计了一个平台共参与者展示才艺,不同的参与者有不同的才艺,使用多态编程模拟;
public class Demo {public static void main(String[] args) {People coder = new Coder("程序员",21);People singer = new Singer("歌手",22);People dancer = new Dancer("舞者",21);System.out.println("请各位选手自我介绍一下:");System.out.println("我叫"+coder.name+",今年"+coder.age+"岁;");coder.haveRest();Coder c = (Coder)coder;c.playComputer();System.out.println("我叫"+singer.name+",今年"+singer.age+"岁;");singer.haveRest();Singer s = (Singer)singer;s.sing();System.out.println("我叫"+dancer.name+",今年"+dancer.age+"岁;");dancer.haveRest();Dancer d = (Dancer)dancer;d.dance();}
}class People{String name;int age;public People(String name, int age) {super();this.name = name;this.age = age;}public People() {super();// TODO Auto-generated constructor stub}public void haveRest() {System.out.println("我会休息!");}
}class Coder extends People{public Coder(String name, int age) {super(name, age);// TODO Auto-generated constructor stub}public void playComputer() {System.out.println("我会玩电脑!");}
}class Singer extends People{public Singer(String name, int age) {super(name, age);// TODO Auto-generated constructor stub}public void sing() {System.out.println("我会唱歌!");}
}class Dancer extends People {public Dancer(String name, int age) {super(name, age);// TODO Auto-generated constructor stub}public void dance() {System.out.println("我会跳舞!");}
}
打印效果如下:
练习五:
- 学生都有学习和做作业的能力,但是学习的方式不同,使用抽象类的方法模拟编程;
public class Demo {public static void main(String[] args) {Student good = new Good();Student poor = new Poor();good.name = "xiaochen";good.age = 20;System.out.println("我是"+good.name+" 年龄"+good.age);good.studyMethod();poor.name = "xiaowu";poor.age = 20;System.out.println("我是"+poor.name+" 年龄"+poor.age);poor.studyMethod();}
}abstract class Student{String name;int age;abstract void studyMethod();
}class Good extends Student{@Overridevoid studyMethod() {// TODO Auto-generated method stubSystem.out.println("卷!");}}class Poor extends Student{@Overridevoid studyMethod() {// TODO Auto-generated method stubSystem.out.println("摆!");}}
打印效果如下:
练习六:
- 创建一个乐器接口,定义演奏方法 play ,定义两个实现类(钢琴 Piano,小提琴 Violin),分别实现乐器接口并做出不同的演奏;
public class Demo {public static void main(String[] args) {Tools piano = new Piano();Tools violin = new Violin();piano.play();violin.play();}
}interface Tools {void play();
}class Piano implements Tools {@Overridepublic void play() {// TODO Auto-generated method stubSystem.out.println("演奏钢琴曲!");}
}class Violin implements Tools{@Overridepublic void play() {// TODO Auto-generated method stubSystem.out.println("演奏小提琴曲!");}
}
打印效果如下:
练习七:
- 国家规定,汽车必须能点火,能开动,国家将文件发送到汽车厂商的手里,厂商生产的汽车必须符合要求,可以有特性功能,编程模拟;
public class Demo {public static void main(String[] args) {CarShop1 c1 = new CarShop1(1,0);CarShop2 c2 = new CarShop2(1,1);c1.getLisence(c1.getName());c2.getLisence(c2.getName());}
}class CarShop {int canLaunch;int canMove;public CarShop() {super();// TODO Auto-generated constructor stub}public CarShop(int canLaunch, int canMove) {super();this.canLaunch = canLaunch;this.canMove = canMove;}public void getLisence(String name) {if (this.canLaunch != 0 && this.canMove != 0) {System.out.println(name+"能够得到国家许可");} else {System.out.println(name+"不能得到国家许可");}}
}class CarShop1 extends CarShop {private String name = "CarShop1";public String getName() {return this.name;}public void Character() {System.out.println("时速300km/h");}public CarShop1() {super();// TODO Auto-generated constructor stub}public CarShop1(int canLaunch, int canMove) {super(canLaunch, canMove);// TODO Auto-generated constructor stub}}class CarShop2 extends CarShop {private String name = "CarShop2";public String getName() {return this.name;}public void Character() {System.out.println("时速320km/h");}public CarShop2() {super();// TODO Auto-generated constructor stub}public CarShop2(int canLaunch, int canMove) {super(canLaunch, canMove);// TODO Auto-generated constructor stub}
}
打印效果如下:
练习八:
- 定义一个动物类,自定义属性;
- 定义吃饭和睡觉两个方法;
- 创建两个对象(小猫和小狗),分别调用吃饭和睡觉方法;
- 打印结果是:小猫吃小鱼干,甜甜地睡着了,小狗吃骨头,憨憨地睡着了;
class Demo {public static void main(String[] args) {Animal Cat = new Animal("小猫",2);Animal Dog = new Animal("小狗",1);System.out.println(Cat.name+Cat.eat("小鱼干")+","+Cat.sleep("甜甜")+","+Dog.name+Dog.eat("骨头")+","+Dog.sleep("憨憨"));}
}class Animal {String name;int age;public Animal() {super();// TODO Auto-generated constructor stub}public Animal(String name, int age) {super();this.name = name;this.age = age;}public String sleep(String description) {return description + "地睡着了";}public String eat(String thing) {return "吃" + thing;}
}
打印效果如下:
练习九:
- 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数;
- ABCDEabcd123456!@#$%^
方法一:
import java.util.Scanner;class Demo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入要计数的字符串:");String str = sc.next();sc.close();char ch[] = str.toCharArray();int matchNum = 0;int matchBig = 0;int matchSmall = 0;int matchOther = 0;for (int i = 0;i<ch.length;i++) {if(ch[i]>='a'&&ch[i]<='z') {matchSmall++;}else if(ch[i]>='A'&&ch[i]<='Z') {matchBig++;}else if(ch[i]>='1'&&ch[i]<='9') {matchNum++;}else {matchOther++;}}System.out.println("数字:"+matchNum);System.out.println("大写字母:"+matchBig);System.out.println("小写字母:"+matchSmall);System.out.println("其他字符:"+matchOther);}
}
方法二:
import java.util.Scanner;class Demo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入需要计数的字符串:");String str = sc.next();sc.close();char ch[] = str.toCharArray();int matchNum = 0;int matchBig = 0;int matchSmall = 0;int matchOther = 0;for (int i = 0;i<ch.length;i++) {String s = Character.toString(ch[i]);if(s.matches("[1-9]")) {matchNum++;}else if(s.matches("[a-z]")) {matchSmall++;}else if(s.matches("[A-Z]")) {matchBig++;}else {matchOther++;}}System.out.println("数字:"+matchNum);System.out.println("大写字母:"+matchBig);System.out.println("小写字母:"+matchSmall);System.out.println("其他字符:"+matchOther);}
}
打印效果如下:
练习十:
-
把数组中的数据按照指定格式拼接成一个字符串
-
举例
int[] arr = {1,2,3};
-
输出结果 “[1,2,3]”
-
import java.util.Scanner;class Demo {public static void main(String[] args) {Scanner sc= new Scanner(System.in);System.out.println("你要输入的数据长度:");int n = sc.nextInt();int[] arr = new int[n];System.out.println("请输入你要格式化的数字:");for (int i = 0; i < arr.length; i++) {arr[i] = sc.nextInt();}sc.close();System.out.println("格式化后输出为:");System.out.print("[");for (int i = 0; i < arr.length-1; i++) {System.out.print(arr[i]+",");}System.out.print(arr[n-1]+"]");}
}
打印效果如下:
练习十一:
- 把字符串反转
- “abc” ==> “cba”
import java.util.Scanner;class Demo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入要反转的字符串:");String str = sc.next();sc.close();char[] ch = str.toCharArray();int n = ch.length / 2;for (int i = 0; i < ch.length; i++) {if(i>=n) {break;}char temp = ch[i];ch[i] = ch[ch.length-1-i];ch[ch.length-1-i] = temp;}System.out.println("反转后得:");System.out.println(ch);}
}
打印效果如下:
练习十二:
-
统计大串中小串出现的次数
- 大串:Adcddkiqooqkdddleef
- 小串:dd
方法一:
class Demo {public static void main(String[] args) {String str = "Adcddkiqooqkdddleef";String mstr = "dd";char[] cs = str.toCharArray();char[] mcs = mstr.toCharArray();int count = 0;int b1 = 0;int condition = mcs.length;int b2 = 0;for (int i = 0; i < cs.length; i++) {if (i + mcs.length > cs.length - 1) {break;}b1 = 0;for (int j = 0; j < mcs.length; j++) {if (cs[i + j] == mcs[j]) {b1++;}}if (b1 == mcs.length) {count++;}}while(condition>0) {if(cs[cs.length-1]==mcs[condition-1]) {b2++;}if(b2==mcs.length) {count++;}condition--;}System.out.println("检测重复到" + count + "次");}
}
打印效果如下:
方法二:
package top.sharehome.BigJava;import java.util.Scanner;public class Demo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String str = sc.next();String str0 = sc.next();int count = 0;int index = 0;while ((index = str.indexOf(str0)) != -1) {count++;str = str.substring(index+1,str.length());}System.out.println(count);}
}
打印效果如下:
练习十三:
- 除去一个字符串中相邻重复的字符
- abbddAffeeEddcccwceaa ==> abdAfeEdcwcea
import java.util.Scanner;class Demo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入需要去重的字符串:");String str = sc.next();sc.close();StringBuffer sb = new StringBuffer();char[] charArray = str.toCharArray();for (int i = 0; i < charArray.length; i++) {if (i + 1 != charArray.length) {if (charArray[i] != charArray[i + 1]) {sb.append(charArray[i]);}}else {sb.append(charArray[i]);}}System.out.println("去除重复后:");System.out.println(sb);}
}
打印效果如下: