IT评测·应用市场-qidao123.com技术社区

标题: java -- 练习题 [打印本页]

作者: 北冰洋以北    时间: 2023-4-9 19:20
标题: java -- 练习题
第一题

1.定义一个Person类,要求有姓名和年龄,并且符合JavaBean标准,定义Student类继承Person,定义测试类,创建Student对象,要求创建Student对象的同时,指定Student对象的姓名为"张三",只能指定姓名不许指定年龄
  1. class Person {
  2.     private String name;
  3.     private int age;
  4.     public Person() {}
  5.     public Person(String name) {
  6.         this.name = name;
  7.     }
  8.     public Person(String name, int age) {
  9.         this.name = name;
  10.         this.age = age;
  11.     }
  12.    
  13.     public String getName() {
  14.         return name;
  15.     }
  16.     public void setName(String name) {
  17.         this.name = name;
  18.     }
  19.     public int getAge() {
  20.         return age;
  21.     }
  22.     public void setAge(int age) {
  23.         this.age = age;
  24.     }
  25.     public String toString() {
  26.         return "Person{name = " + name + ", age = " + age + "}";
  27.     }
  28. }
  29. class Student extends Person{
  30.     public Student() {
  31.     }
  32.     public Student(String name) {
  33.         super(name);
  34.     }
  35. }
  36. public class Test {
  37.     public static void main(String[] args) {
  38.         Student student=new Student("张三");
  39.     }
  40. }
复制代码
第二题

2.按照以下要求定义类
  1. Animal
  2.         吃
  3.         睡
  4. Dog
  5.         吃  狗吃肉
  6.         睡  狗趴着睡
  7.         看门  
  8. Cat  
  9.         吃  猫吃鱼
  10.         睡  猫躺着睡
  11.         抓老鼠
  12. Home
  13.         定义一个动物在家吃饭的方法 要求猫和狗都可以传入
  14. 定义测试类  测试 Home类在家吃饭的方法
复制代码
  1. public class test{
  2.     public static void main(String[] args) {
  3.         new Home().inHomeEat(new Dog());
  4.         new Home().inHomeEat(new Cat());
  5.     }
  6. }
  7. abstract class Animal {
  8.     public abstract void eat();
  9.     public abstract void sleep();
  10. }
  11. class Home {
  12.     void inHomeEat(Animal animal) {
  13.         System.out.print("在家: ");
  14.         animal.eat();
  15.     }
  16. }
  17. class Dog extends Animal {
  18.     @Override
  19.     public void eat() {
  20.         System.out.println("狗吃肉");
  21.     }
  22.     @Override
  23.     public void sleep() {
  24.         System.out.println("狗趴着睡");
  25.     }
  26. }
  27. class Cat extends Animal {
  28.     @Override
  29.     public void eat() {
  30.         System.out.println("猫吃鱼");
  31.     }
  32.     @Override
  33.     public void sleep() {
  34.         System.out.println("猫躺着睡");
  35.     }
  36. }
复制代码
第三题

3.键盘录入一个字符串,判断这个字符串是否是对称的字符串 比如  abcba   abba   aabbebbaa 如果是打印"是对称的字符串",如果不是打印"不是对称的字符串"
  1. public class test{
  2.     public static void main(String[] args) {
  3.         Scanner sc = new Scanner(System.in);
  4.         System.out.print("输入一个字符串: ");
  5.         String str = sc.nextLine();
  6.         char[] charList = str.toCharArray();
  7.         boolean b = check(charList);
  8.         System.out.println(b?"是对称的字符串":"不是对称的字符串");
  9.     }
  10.     public static boolean check(char[] charList) {
  11.         int maxIndex = charList.length - 1;
  12.         for (int i = 0; i < charList.length / 2; i++) {
  13.             if (charList[i] != charList[maxIndex]) {
  14.                 return false;
  15.             }
  16.             maxIndex--;
  17.         }
  18.         return true;
  19.     }
  20. }
复制代码
第四题

4.将字符串 "         we-like-java       " 转换为 "EW-EKIL-AVAJ"   也就是去掉前后空格,并将每个单词反转.
  1.         String string = "         we-like-java       ";
  2.         String[] arr = string.trim().toUpperCase().split("-");
  3.         for (int i = arr.length - 1; i >= 0; i--) {
  4.             StringBuilder sb = new StringBuilder(arr[i]);
  5.             arr[i] = sb.reverse().toString();
  6.         }
  7.         StringBuilder sb =new StringBuilder();
  8.         for (int i = 0; i < arr.length; i++) {
  9.             sb.append(arr[i]);
  10.             if (i < arr.length - 1) {
  11.                 sb.append("-");
  12.             }
  13.         }
  14.         System.out.println(sb);
复制代码
第五题

**5.网络程序中,如聊天室,聊天软件等,经常需要对用户提交的内容进行敏感词过滤如"枪","军火"等,这些词都不可以在网上进行传播,需要过滤掉或者用其他词语替换.键盘录入一个字符串 将敏感词替换成 "*" **
  1.         String[] blockKeys = {"", "枪", "军火"};
  2.         System.out.print("输入要提交的内容: ");
  3.         String comment = sc.nextLine();
  4.         for (int i = 0; i < blockKeys.length; i++) {
  5.             comment = comment.replaceAll(blockKeys[i],"*");
  6.         }
  7.         System.out.println(comment);
复制代码
第六题

6.计算 987654321123456789000 除以 123456789987654321的值,注意这个结果为BigInteger类型,将BigInteger类型转换为字符串类型,然后转换为double类型.精确计算3120.25乘以1.25,注意这个结果为BigDecimal类型,同样转换为字符串类型,然后转换为double类型,然后获取这两个结果的最大值
  1. BigInteger bint1 = new BigInteger("987654321123456789000");
  2. BigInteger bint2 = new BigInteger("123456789987654321");
  3. Double d1 = Double.parseDouble(bint1.divide(bint2).toString());
  4. Double d2 = Double.parseDouble(new BigDecimal(3120.25/1.25).toString());
  5. System.out.println("较大的值为: " + Math.max(d1,d2));
复制代码
第七题

7.键盘录入一个生日的字符串(xxxx-xx-xx) 计算这个人活了多少天
  1.         System.out.print("请输入您的生日(年-月-日): ");
  2.         String personBirthday = sc.nextLine();
  3.         DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  4.         try {
  5.             Date birthDay = df.parse(personBirthday);
  6.             System.out.println(("您活了" + (new Date().getTime() - birthDay.getTime())/1000/60/60/24) + "天");
  7.         } catch (ParseException e) {
  8.             System.out.println("输入错误");
  9.         }
复制代码
第八题

8.键盘录入一个指定的年份,获取指定年份的2月有多少天
  1. public class test{
  2.     public static void main(String[] args) throws PrintDataException {
  3.         System.out.print("请输入年份");
  4.         String printYear = sc.nextLine();
  5.         try{
  6.             int intPrintYear = Integer.parseInt(printYear);
  7.             if (intPrintYear < 0){
  8.                 throw new PrintDataException("输入数据错误");
  9.             }
  10.             Calendar c = Calendar.getInstance();
  11.             c.set(intPrintYear, 2, 1);
  12.             c.add(Calendar.DAY_OF_MONTH, -1);
  13.             System.out.println(c.get(Calendar.DAY_OF_MONTH));
  14.         } catch (NumberFormatException e) {
  15.             System.out.println("输入错误");
  16.         }
  17.     }
  18. }
  19. class PrintDataException extends Exception {
  20.     public PrintDataException() { super();}
  21.     public PrintDataException(String message) {
  22.         super(message);
  23.     }
  24. }
复制代码
第九题

9.将"Hello AbcDe"这个字符串转换为一个byte类型的数组,将数组的后5个元素复制到一个长度为5的byte数组中,然后将数组中的元素进行降序排列,将数组中的前3个元素放入到一个新的长度为3的数组中,并升序排列,最后查找字符'c'代表数值在新数组中的索引位置(可以使用Arrays工具类)
  1. byte[] byteArr1 = "Hello AbcDe".getBytes();
  2. byte[] byteArr2 = new byte[5];
  3. System.arraycopy(byteArr1,byteArr1.length - 5, byteArr2, 0, 5);
  4. // 排序
  5. Arrays.sort(byteArr2);
  6. // 反转
  7. for (int i = 0; i < byteArr2.length / 2; i++ ) {
  8.     byte tmp = byteArr2[i];
  9.     byteArr2[i] = byteArr2[byteArr2.length - 1 - i];
  10.     byteArr2[byteArr2.length - 1 - i] = tmp;
  11. }
  12. byte[] byteArr3 = Arrays.copyOf(byteArr2, 3);
  13. Arrays.sort(byteArr3);
  14. for (int i = 0; i < byteArr3.length; i++) {
  15.     if (byteArr3[i] == 'c') {
  16.         System.out.println("c的索引为: " + i);
  17.         break;
  18.     }
  19. }
复制代码
第十题

10.定义一个Person类,,要求有年龄,提供get/set方法,要求设置年龄时,如果年龄小于0或者年龄大于200抛出"NoAgeException"异常,如果年龄正常则正常设置.
  1. class NoAgeException extends Exception {
  2.     public NoAgeException() {super();}
  3.     public NoAgeException(String message) {
  4.         super(message);
  5.     }
  6. }
  7. class Person  {
  8.     private int age;
  9.     public int getAge() {
  10.         return age;
  11.     }
  12.     public void setAge(int age) throws NoAgeException {
  13.         if (age < 0 || age > 200){
  14.             throw new NoAgeException();
  15.         }
  16.         this.age = age;
  17.     }
  18. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4