**第一題:**
輸入一行字符,分別統計出其英文字母、空格、數字和其它字符的個數。
提示:
對比char字符在ASK碼的范圍,就可以確定它符號的類別
char字符ASK碼的范圍
* 數字0到9:?48~57
* 字母A到Z:65到90?a到z:97到122
* 空格是32
如:
~~~
char a = 'a';
97<= a <=122
~~~
答:
~~~java
public class Client {
public Map<String, Integer> getCharNumber(String str) {
int num = 0,letter = 0,space = 0,other = 0;//變量的共同定義,表示這四個變量都是int類型
Map<String,Integer> map = new HashMap<String, Integer>();
if(str != null && str.length() > 0) {
char[] charArray = str.toCharArray();//將字符串分割為char數組
for(char cr:charArray) {
if(cr >= 48 && cr <= 57) {//數字
num++;
} else if((cr >= 65 && cr <= 90) || (cr >= 97 && cr <= 122)) {//字母
letter++;
} else if(cr == 32) {//空格
space++;
} else {//其他
other++;
}
}
}
map.put("數字", num);
map.put("字母", letter);
map.put("空格", space);
map.put("其他", other);
return map;
}
public static void main(String[] args) {
Client client = new Client();
//如果方法存在返回值,需要創建一個與返回值類型相同的變量來接收返回值
Map<String, Integer> map = client.getCharNumber("n wn =-0 qu wr s['");
System.out.println(map);
}
}
~~~
**第二題**
要求寫一個秒表類(StopWatch),該秒表類該類在系統中只能有存在一個實例。并且存在star方法,可以用于開始計時,并且輸出開始時間,同時存在stop方法用于停止計時,輸出結束時間,要求秒表可以存儲以毫秒為單位十個計時任務
~~~java
/**
* 秒表類
* 只能存在一個實例-采用單例設計模式
* @author 一教室
*
*/
public class StopWatch {
/**
* 私有化構造器
*/
private StopWatch() {
}
/**
* 創建時間格式化對象作為屬性,方便調用
*/
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
//實例化一個秒表對象,將該對象作為秒表類的屬性
private static StopWatch stopWatch = new StopWatch();
/**
* 創建公共方法,對外提供創建出來的stopWatch對象
* @return
*/
public static StopWatch getInstance() {
return stopWatch;
}
/**
* 設置一個屬性(開始時間記錄器),用于存儲秒表開始計時的時間
*/
private Date startTime;
/**
* 設置一個屬性(計時任務存儲器),用于保存十個計時任務
*/
private List<Long> record = new ArrayList<Long>();
/**
* 只能生成get方法,對外提供計時任務存儲器數據,不能生成set方法,防止直接修改存儲器數據
* @return
*/
public List<Long> getRecord() {
return record;
}
/**
* 開始計時方法
*/
public void start() {
Date date = new Date();
startTime = date;//將開始計時的時間存儲到開始時間記錄器中
System.out.println("開始時間:" + sdf.format(date));
}
/**
* 結束計時方法
*/
public void stop() {
if(startTime == null) {//說明沒有調用開始計時方法
System.out.println("未曾開始計時");
} else {
Date date = new Date();
System.out.println("結束時間:" + sdf.format(date));
long time = date.getTime() - startTime.getTime();//結束時間-開始時間 = 計時任務要求毫秒數
if(record.size() >= 10) {
record.remove(0);//將最早的計時任務刪除
record.add(time);
} else {
record.add(time);
}
}
}
}
~~~
~~~java
public class Test {
public static void main(String[] args) throws InterruptedException {
StopWatch stw = StopWatch.getInstance();
//開始第一次計時
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(40);//讓程序暫停40毫秒
stw.stop();
System.out.println(stw.getRecord());
//開始第二次計時
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(30);
stw.stop();
System.out.println(stw.getRecord());
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(30);
stw.stop();
System.out.println(stw.getRecord());
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(30);
stw.stop();
System.out.println(stw.getRecord());
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(30);
stw.stop();
System.out.println(stw.getRecord());
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(30);
stw.stop();
System.out.println(stw.getRecord());
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(30);
stw.stop();
System.out.println(stw.getRecord());
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(30);
stw.stop();
System.out.println(stw.getRecord());
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(30);
stw.stop();
System.out.println(stw.getRecord());
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(30);
stw.stop();
System.out.println(stw.getRecord());
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(30);
stw.stop();
System.out.println(stw.getRecord());
stw.start();
System.out.println(stw.getRecord());
//讓程序休息方法
Thread.sleep(30);
stw.stop();
System.out.println(stw.getRecord());
}
}
~~~
# 綜合案例(面向對象)
## 場景案例:
學校新開一門專業,叫做計算機科學與應用,專業編號J0001,學制年限為4年。
現有三名學生如下表所示,加入該專業學習,要求采用Java面向對象的思維描述該場景:
| 姓名 | 學號 | 性別 | 年齡 |
| --- | --- | --- | --- |
| 張三 | S01 | 男 | 18歲 |
| 李四 | S02 | 女 | 17歲 |
| 王五 | S03 | 男 | 18歲 |
## 實現效果

## 案例分析
1. 本場景中共存在幾個對象?各自的特征是什么?
2. 我們又可以針對這幾個對象抽象歸納出幾個類出來
## 代碼實現