第一题
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"异常,如果年龄正常则正常设置.文章来源:https://www.toymoban.com/news/detail-408810.html
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;
}
}
第十一题
使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌.文章来源地址https://www.toymoban.com/news/detail-408810.html
import java.util.ArrayList;
import java.util.Random;
public class test {
public static void main(String[] args) throws SizeZeroExtends {
ArrayList<String> pokerArrayList = new ArrayList<>();
String[] colors = {"♥","♠","♣","♦"};
String[] numbers = "2-A-K-Q-J-10-9-8-7-6-5-4-3".split("-");
for (String str : colors) {
for (String number : numbers) {
pokerArrayList.add(str+number);
}
}
pokerArrayList.add("小王");
pokerArrayList.add("大王");
System.out.println(pokerArrayList);
// 洗牌
pokerArrayList = shuffle(pokerArrayList);
// 发牌
ArrayList<String> player1 = new ArrayList<String>();
ArrayList<String> player2 = new ArrayList<String>();
ArrayList<String> player3 = new ArrayList<String>();
ArrayList<String> dipai = new ArrayList<String>();
for (int i = 0; i < pokerArrayList.size(); i++) {
if (i >= 51) {
dipai.add(pokerArrayList.get(i));
} else {
if (i % 3 == 0) {
player1.add(pokerArrayList.get(i));
} else if (i % 3 == 1) {
player2.add(pokerArrayList.get(i));
} else {
player3.add(pokerArrayList.get(i));
}
}
}
System.out.println("玩家一: " + pokerSort(player1, numbers));
System.out.println("玩家二: " + pokerSort(player2, numbers));
System.out.println("玩家三: " + pokerSort(player3, numbers));
System.out.println("底牌: " + pokerSort(dipai, numbers));
}
// 洗牌
public static ArrayList<String> shuffle(ArrayList<String> arrayList) throws SizeZeroExtends {
if (arrayList == null) {
throw new NullPointerException("传入集合为null");
}
if (arrayList.size() == 0) {
throw new SizeZeroExtends("传入集合长度为0");
}
int total = arrayList.size();
Random random = new Random();
ArrayList<String> newArrayList = new ArrayList<String>();
int count = 0;
while (arrayList.size() > 0) {
int index = random.nextInt(total - count);
String str = arrayList.get(index);
arrayList.remove(index);
newArrayList.add(str);
count++;
}
return newArrayList;
}
// 排序
public static ArrayList<String> pokerSort(ArrayList<String> arrayList, String[] arr) throws SizeZeroExtends {
if (arrayList == null) {
throw new NullPointerException("传入集合为null");
}
if (arrayList.size() == 0) {
throw new SizeZeroExtends("传入集合长度为0");
}
ArrayList<Character> charArrayList = new ArrayList<>();
ArrayList<String> stringArrayList = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
if (arrayList.get(i).equals("小王") || arrayList.get(i).equals("大王")){
continue;
}
charArrayList.add(arrayList.get(i).charAt(1));
}
int count = 0;
while (arrayList.size() > 0) {
if (arrayList.contains("大王")) {
stringArrayList.add("大王");
arrayList.remove("大王");
}
if (arrayList.contains("小王")) {
stringArrayList.add("小王");
arrayList.remove("小王");
}
while (charArrayList.indexOf(arr[count].charAt(0)) != -1) {
int index = charArrayList.indexOf(arr[count].charAt(0));
stringArrayList.add(arrayList.get(index));
arrayList.remove(index);
charArrayList.remove(index);
}
count++;
}
return stringArrayList;
}
}
class SizeZeroExtends extends Exception{
public SizeZeroExtends(){ super(); }
public SizeZeroExtends(String message) {
super(message);
}
}
到了这里,关于java -- 练习题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!