第一題
1.定義一個(gè)Person類,要求有姓名和年齡,并且符合JavaBean標(biāo)準(zhǔn),定義Student類繼承Person,定義測(cè)試類,創(chuàng)建Student對(duì)象,要求創(chuàng)建Student對(duì)象的同時(shí),指定Student對(duì)象的姓名為"張三",只能指定姓名不許指定年齡
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
吃 貓吃魚
睡 貓?zhí)芍? 抓老鼠
Home
定義一個(gè)動(dòng)物在家吃飯的方法 要求貓和狗都可以傳入
定義測(cè)試類 測(cè)試 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("貓?zhí)芍?);
}
}
第三題
3.鍵盤錄入一個(gè)字符串,判斷這個(gè)字符串是否是對(duì)稱的字符串 比如 abcba abba aabbebbaa 如果是打印"是對(duì)稱的字符串",如果不是打印"不是對(duì)稱的字符串"
public class test{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("輸入一個(gè)字符串: ");
String str = sc.nextLine();
char[] charList = str.toCharArray();
boolean b = check(charList);
System.out.println(b?"是對(duì)稱的字符串":"不是對(duì)稱的字符串");
}
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 " 轉(zhuǎn)換為 "EW-EKIL-AVAJ" 也就是去掉前后空格,并將每個(gè)單詞反轉(zhuǎn).
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.網(wǎng)絡(luò)程序中,如聊天室,聊天軟件等,經(jīng)常需要對(duì)用戶提交的內(nèi)容進(jìn)行敏感詞過濾如"槍","軍火"等,這些詞都不可以在網(wǎng)上進(jìn)行傳播,需要過濾掉或者用其他詞語替換.鍵盤錄入一個(gè)字符串 將敏感詞替換成 "*" **
String[] blockKeys = {"槍", "軍火"};
System.out.print("輸入要提交的內(nèi)容: ");
String comment = sc.nextLine();
for (int i = 0; i < blockKeys.length; i++) {
comment = comment.replaceAll(blockKeys[i],"*");
}
System.out.println(comment);
第六題
6.計(jì)算 987654321123456789000 除以 123456789987654321的值,注意這個(gè)結(jié)果為BigInteger類型,將BigInteger類型轉(zhuǎn)換為字符串類型,然后轉(zhuǎn)換為double類型.精確計(jì)算3120.25乘以1.25,注意這個(gè)結(jié)果為BigDecimal類型,同樣轉(zhuǎn)換為字符串類型,然后轉(zhuǎn)換為double類型,然后獲取這兩個(gè)結(jié)果的最大值
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.鍵盤錄入一個(gè)生日的字符串(xxxx-xx-xx) 計(jì)算這個(gè)人活了多少天
System.out.print("請(qǐng)輸入您的生日(年-月-日): ");
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("輸入錯(cuò)誤");
}
第八題
8.鍵盤錄入一個(gè)指定的年份,獲取指定年份的2月有多少天
public class test{
public static void main(String[] args) throws PrintDataException {
System.out.print("請(qǐng)輸入年份");
String printYear = sc.nextLine();
try{
int intPrintYear = Integer.parseInt(printYear);
if (intPrintYear < 0){
throw new PrintDataException("輸入數(shù)據(jù)錯(cuò)誤");
}
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("輸入錯(cuò)誤");
}
}
}
class PrintDataException extends Exception {
public PrintDataException() { super();}
public PrintDataException(String message) {
super(message);
}
}
第九題
9.將"Hello AbcDe"這個(gè)字符串轉(zhuǎn)換為一個(gè)byte類型的數(shù)組,將數(shù)組的后5個(gè)元素復(fù)制到一個(gè)長(zhǎng)度為5的byte數(shù)組中,然后將數(shù)組中的元素進(jìn)行降序排列,將數(shù)組中的前3個(gè)元素放入到一個(gè)新的長(zhǎng)度為3的數(shù)組中,并升序排列,最后查找字符'c'代表數(shù)值在新數(shù)組中的索引位置(可以使用Arrays工具類)
byte[] byteArr1 = "Hello AbcDe".getBytes();
byte[] byteArr2 = new byte[5];
System.arraycopy(byteArr1,byteArr1.length - 5, byteArr2, 0, 5);
// 排序
Arrays.sort(byteArr2);
// 反轉(zhuǎn)
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.定義一個(gè)Person類,,要求有年齡,提供get/set方法,要求設(shè)置年齡時(shí),如果年齡小于0或者年齡大于200拋出"NoAgeException"異常,如果年齡正常則正常設(shè)置.文章來源:http://www.zghlxwxcb.cn/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張牌打亂順序,三個(gè)玩家參與游戲,三人交替摸牌,每人17張牌,最后三張留作底牌.文章來源地址http://www.zghlxwxcb.cn/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);
// 發(fā)牌
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("傳入集合長(zhǎng)度為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("傳入集合長(zhǎng)度為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);
}
}
到了這里,關(guān)于java -- 練習(xí)題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!