第一题
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类型,将Bi