马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
第一题
1.定义一个Person类,要求有姓名和年龄,并且符合JavaBean标准,定义Student类继承Person,定义测试类,创建Student对象,要求创建Student对象的同时,指定Student对象的姓名为"张三",只能指定姓名不许指定年龄- class Person {
- private String name;
- private int age;
- public Person() {}
- public Person(String name) {
- this.name = name;
- }
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String toString() {
- return "Person{name = " + name + ", age = " + age + "}";
- }
- }
- class Student extends Person{
- public Student() {
- }
- public Student(String name) {
- super(name);
- }
- }
- public class Test {
- public static void main(String[] args) {
- Student student=new Student("张三");
- }
- }
复制代码 第二题
2.按照以下要求定义类- Animal
- 吃
- 睡
- Dog
- 吃 狗吃肉
- 睡 狗趴着睡
- 看门
- Cat
- 吃 猫吃鱼
- 睡 猫躺着睡
- 抓老鼠
- Home
- 定义一个动物在家吃饭的方法 要求猫和狗都可以传入
- 定义测试类 测试 Home类在家吃饭的方法
复制代码- public class test{
- public static void main(String[] args) {
- new Home().inHomeEat(new Dog());
- new Home().inHomeEat(new Cat());
- }
- }
- abstract class Animal {
- public abstract void eat();
- public abstract void sleep();
- }
- class Home {
- void inHomeEat(Animal animal) {
- System.out.print("在家: ");
- animal.eat();
- }
- }
- class Dog extends Animal {
- @Override
- public void eat() {
- System.out.println("狗吃肉");
- }
- @Override
- public void sleep() {
- System.out.println("狗趴着睡");
- }
- }
- class Cat extends Animal {
- @Override
- public void eat() {
- System.out.println("猫吃鱼");
- }
- @Override
- public void sleep() {
- System.out.println("猫躺着睡");
- }
- }
复制代码 第三题
3.键盘录入一个字符串,判断这个字符串是否是对称的字符串 比如 abcba abba aabbebbaa 如果是打印"是对称的字符串",如果不是打印"不是对称的字符串"- public class test{
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("输入一个字符串: ");
- String str = sc.nextLine();
- char[] charList = str.toCharArray();
- boolean b = check(charList);
- System.out.println(b?"是对称的字符串":"不是对称的字符串");
- }
- public static boolean check(char[] charList) {
- int maxIndex = charList.length - 1;
- for (int i = 0; i < charList.length / 2; i++) {
- if (charList[i] != charList[maxIndex]) {
- return false;
- }
- maxIndex--;
- }
- return true;
- }
- }
复制代码 第四题
4.将字符串 " we-like-java " 转换为 "EW-EKIL-AVAJ" 也就是去掉前后空格,并将每个单词反转.- String string = " we-like-java ";
- String[] arr = string.trim().toUpperCase().split("-");
- for (int i = arr.length - 1; i >= 0; i--) {
- StringBuilder sb = new StringBuilder(arr[i]);
- arr[i] = sb.reverse().toString();
- }
- StringBuilder sb =new StringBuilder();
- for (int i = 0; i < arr.length; i++) {
- sb.append(arr[i]);
- if (i < arr.length - 1) {
- sb.append("-");
- }
- }
- System.out.println(sb);
复制代码 第五题
**5.网络程序中,如聊天室,聊天软件等,经常需要对用户提交的内容进行敏感词过滤如"枪","军火"等,这些词都不可以在网上进行传播,需要过滤掉或者用其他词语替换.键盘录入一个字符串 将敏感词替换成 "*" **- String[] blockKeys = {"", "枪", "军火"};
- System.out.print("输入要提交的内容: ");
- String comment = sc.nextLine();
- for (int i = 0; i < blockKeys.length; i++) {
- comment = comment.replaceAll(blockKeys[i],"*");
- }
- System.out.println(comment);
复制代码 第六题
6.计算 987654321123456789000 除以 123456789987654321的值,注意这个结果为BigInteger类型,将BigInteger类型转换为字符串类型,然后转换为double类型.精确计算3120.25乘以1.25,注意这个结果为BigDecimal类型,同样转换为字符串类型,然后转换为double类型,然后获取这两个结果的最大值- BigInteger bint1 = new BigInteger("987654321123456789000");
- BigInteger bint2 = new BigInteger("123456789987654321");
- Double d1 = Double.parseDouble(bint1.divide(bint2).toString());
- Double d2 = Double.parseDouble(new BigDecimal(3120.25/1.25).toString());
- System.out.println("较大的值为: " + Math.max(d1,d2));
复制代码 第七题
7.键盘录入一个生日的字符串(xxxx-xx-xx) 计算这个人活了多少天- System.out.print("请输入您的生日(年-月-日): ");
- String personBirthday = sc.nextLine();
- DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- try {
- Date birthDay = df.parse(personBirthday);
- System.out.println(("您活了" + (new Date().getTime() - birthDay.getTime())/1000/60/60/24) + "天");
- } catch (ParseException e) {
- System.out.println("输入错误");
- }
复制代码 第八题
8.键盘录入一个指定的年份,获取指定年份的2月有多少天- public class test{
- public static void main(String[] args) throws PrintDataException {
- System.out.print("请输入年份");
- String printYear = sc.nextLine();
- try{
- int intPrintYear = Integer.parseInt(printYear);
- if (intPrintYear < 0){
- throw new PrintDataException("输入数据错误");
- }
- Calendar c = Calendar.getInstance();
- c.set(intPrintYear, 2, 1);
- c.add(Calendar.DAY_OF_MONTH, -1);
- System.out.println(c.get(Calendar.DAY_OF_MONTH));
- } catch (NumberFormatException e) {
- System.out.println("输入错误");
- }
- }
- }
- class PrintDataException extends Exception {
- public PrintDataException() { super();}
- public PrintDataException(String message) {
- super(message);
- }
- }
复制代码 第九题
9.将"Hello AbcDe"这个字符串转换为一个byte类型的数组,将数组的后5个元素复制到一个长度为5的byte数组中,然后将数组中的元素进行降序排列,将数组中的前3个元素放入到一个新的长度为3的数组中,并升序排列,最后查找字符'c'代表数值在新数组中的索引位置(可以使用Arrays工具类)- byte[] byteArr1 = "Hello AbcDe".getBytes();
- byte[] byteArr2 = new byte[5];
- System.arraycopy(byteArr1,byteArr1.length - 5, byteArr2, 0, 5);
- // 排序
- Arrays.sort(byteArr2);
- // 反转
- for (int i = 0; i < byteArr2.length / 2; i++ ) {
- byte tmp = byteArr2[i];
- byteArr2[i] = byteArr2[byteArr2.length - 1 - i];
- byteArr2[byteArr2.length - 1 - i] = tmp;
- }
- byte[] byteArr3 = Arrays.copyOf(byteArr2, 3);
- Arrays.sort(byteArr3);
- for (int i = 0; i < byteArr3.length; i++) {
- if (byteArr3[i] == 'c') {
- System.out.println("c的索引为: " + i);
- break;
- }
- }
复制代码 第十题
10.定义一个Person类,,要求有年龄,提供get/set方法,要求设置年龄时,如果年龄小于0或者年龄大于200抛出"NoAgeException"异常,如果年龄正常则正常设置.- class NoAgeException extends Exception {
- public NoAgeException() {super();}
- public NoAgeException(String message) {
- super(message);
- }
- }
- class Person {
- private int age;
- public int getAge() {
- return age;
- }
- public void setAge(int age) throws NoAgeException {
- if (age < 0 || age > 200){
- throw new NoAgeException();
- }
- this.age = age;
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |