第一題
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;
}
}
第十一題
使用54張牌打亂順序,三個玩家參與游戲,三人交替摸牌,每人17張牌,最后三張留作底牌.
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/549543.html
標籤:Java
上一篇:現在還值得學Python嗎?
下一篇:Java 例外處理:使用和思考
