第 1 章 Java 開發(fā)入門
第 2 章 Java 編程基礎(chǔ)
1. 編寫程序,計算1+3+…+99的值,要求如下:
(1)使用循環(huán)語句實(shí)現(xiàn)1~99的遍歷
(2)在遍歷過程中,通過條件判斷當(dāng)前的數(shù)是否為奇數(shù),如果是就累加,否則不加。
public class getSum {
2 public static void main(String[] args) {
3 int sum = 0;
4 for (int i = 1; i < 100; i++) {
5 if (i % 2 != 0)
6 sum += i;
7 }
8 System.out.println(sum);
9 }
10 }
2.使用do… while循環(huán)語句計算正數(shù)5的階乘。
1 public class Test {
2 public static void main(String[] args) {
3 int i = 1;
4 long sum = 1;
5 do {
6 sum *= i;
7 i++;
8 } while (i <= 5);
9 System.out.println(sum);
10 }
11 }
第 3 章 面向?qū)ο螅ㄉ希?/h2>
某公司正在進(jìn)行招聘工作,被招聘人員需要填寫個人信息,編寫個人簡歷的封裝類Resume,并編寫測試。Resume類圖及輸出效果
Example.java
class Resume {
private String name;
private String sex;
private int age;
public Resume(){
}
public Resume(String name,String sex,int age){
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName(){
return name;
}
public String getSex(){
return sex;
}
public int getAge(){
return age;
}
public void introduce(){
System.out.println(" 姓 名 : "+this.getName()+"\n 性 別 :
"+this.getSex()+"\n 年齡:"+this.getAge());
}
}
public class Example{
public static void main(String[] args){
Resume re = new Resume("李四","男",20);
re.introduce();
}
}
第 4 章 面向?qū)ο螅ㄏ拢?/h2>
某公司的員工分為5類,每類員工都有相應(yīng)的封裝類,這5個類的信息如下。
(1) Emplovee:這是所有員工的父類。
①屬性:員工的姓名、員工的生日月份
②方法: Salary int month)根據(jù)參數(shù)月份確定工資。如果該月員工過生日,則公司會額外發(fā)放100元。
(2) Salariedemployee: Employee的子類,拿固定工資的員工。
屬性:月薪
(3) Hourlyemployee: Employee的子類,按小時拿工資的員工,每月工作超出160h部分按照1.5倍工資發(fā)放。
屬性:每小時的工資、每月工作的小時數(shù)。
(4) Salesemployee: Employee的子類,銷售人員,工資由月銷售額和提成率決定。
屬性:月銷售額、提成率。
(5) Baseplussalesemploye: Salesemployee的子類,有固定底薪的銷售人員,工資底薪加上銷售提成。
屬性:底薪。
本題要求根據(jù)上述員工分類(編寫一個程序,實(shí)現(xiàn)以下功能:
(1)創(chuàng)建 Employee,分別創(chuàng)建若干不同的 Employee對象,并打印某個用
(2)每個類都完全封裝,不允許有非私有化屬性。
Employee.java
abstract class Employee{
private String name; //定義姓名 name 并私有化屬性
private int month; //定義生日月份 month 并私有化屬性
public Employee(){} //無參構(gòu)造器
public Employee(String name,int month){ //有參構(gòu)造方法
this.name = name; //給屬性 name 初始化賦值
this.month = month; //給屬性 month 初始化賦值
}
//獲取屬性 name 的方法
public String getName(){
return name; //返回 name 屬性
}
//獲取屬性 month 的方法
public int getMonth(){
return month; //返回 month 屬性
}
//給屬性 name 賦初始值
public void setName(String name){
this.name = name; //本類中的屬性 name
}
//給屬性 month 賦初始值
public void setMonth(int month){
this.month = month; //本類中的屬性 month
}
//創(chuàng)建一個方法 getSalary()用來計算工資,參數(shù) month 是月份,如果當(dāng)月是員工生日,獎
勵 100 元
public double getSalary(int month){
double salary = 0; //定義工資變量
//判斷當(dāng)前月份是否是員 工的生日月份,如果是獎勵 100 元
if(this.month == month){
salary = salary + 100;
return salary; //返回工資 salary
}
}
}
SalariedEmployee.java
class SalariedEmployee extends Employee{
private double monthSalary; //封裝 monthSalary 屬性
public SalariedEmployee(){} //無參構(gòu)造方法
//有參構(gòu)造方法 參數(shù) 姓名 生日月份 月薪
public SalariedEmployee(String name,int month,double monthSalary){
super(name,month); //調(diào)用父類有參構(gòu)造方法
this.monthSalary = monthSalary; //為屬性 monthSalary 初始化賦值
}
//獲取 monthSalary 的值
public double getMonthSalary(){
return monthSalary;
}
//給 monthSalary 賦值
public void setMonthSalary(double monthSalary){
this.monthSalary = monthSalary;
}
//覆蓋父類中的方法
public double getSalary(int month){
double salary = monthSalary+super.getSalary(month); //定義工資變量
return salary;
}
}
HourlyEmployee.java
class HourlyEmployee extends Employee{
private double hourlySalary; //定義屬性 hourlySalary 每小時的工資
private int hours; //定義屬性 hours 每月工作的小時數(shù)
public HourlyEmployee(){} //無參構(gòu)造方法
//有參構(gòu)造方法 參數(shù) 姓名 生日月份 每小時的工資 每月工作的小時數(shù)
public HourlyEmployee(String name,int month,double hourlySalary,int
hours){
super(name,month); //調(diào)用父類有參構(gòu)造方法
this.hourlySalary = hourlySalary ; //為屬性 hourlySalary 初始化賦值
this.hours = hours; //為屬性 hours 初始化賦值
}
public double getHourlySalary(){ //獲取 hourlySalary 的值
return hourlySalary;
}
public int getHours(){ //獲取 hours 的值
return hours;
}
//定義 set 方法設(shè)置 hourlySalary hours 的值
public void setHourlySalary(double hourlySalary){
this.hourlySalary =hourlySalary;
}
public void setHourly(int hours){
this.hours = hours;
}
//覆蓋父類方法
public double getSalary(int month){
if(hours < 0){ //如果工作小時數(shù)小于 0 輸出數(shù)據(jù)錯誤
System.out.println("數(shù)據(jù)錯誤");
return 0;
}
//小于 160 個小時的 按照每個月的工作小時數(shù)乘以每小時的工資
else if(hours <= 160)
return hourlySalary*hours+super.getSalary(month);
//超出 160 個小時的小時數(shù) 按照 1.5 倍計算
else return hourlySalary*160+hourlySalary*1.5*(hours160)+super.getSalary(month);
}
}
SalesEmployee.java
class SalesEmployee extends Employee{
private double sales ; //定義銷售額 sales
private double rate; //定義提成率 rate
public SalesEmployee(){}
public SalesEmployee(String name,int month,double sales,double rate){
super(name,month);
this.sales = sales;
this.rate = rate;
}
public double getSales(){
return sales;
}
public double getRate(){
return rate;
}
public void setSales(double sales){
this.sales = sales;
}
public void setRate(double rate){
this.rate = rate;
}
public double getSalary(int month){
return this.getSales()*(1+this.getRate())+super.getSalary(month);
}
}
BasePlusSalesEmployee.java
class BasePlusSalesEmployee extends SalesEmployee{
private double baseSalary; //定義基礎(chǔ)工資 baseSalary
//無參構(gòu)造方法
public BasePlusSalesEmployee(){}
//有參構(gòu)造方法
public BasePlusSalesEmployee(String name,int month,double sales,double
rate,double baseSalary){
super(name,month,sales,rate);
this.baseSalary = baseSalary;
}
//get/set 方法對私有屬性的調(diào)用和設(shè)置
public double gatBaseSalary(){
return baseSalary;
}
public void setBaseSalary(){
this.baseSalary = baseSalary;
}
public double getSalary(int month){
return baseSalary+super.getSalary(month);
}
}
Test.java
//定義一個測試類
public class Test{
public static void main(String[] args){
//聲明一個 Employee 類型的數(shù)組,并創(chuàng)建不同子類型的對象
Employee[] employee = {new SalariedEmployee(“張三”,1,6000),new
HourlyEmployee(“李 四”,2,50,180),new SalesEmployee(“王
五”,3,6500,0.15),new BasePlusSalesEmployee(“趙六”,4,5000,0.15,2000)};
//打印每個員工的工資
for(int i = 0; i < employee.length ;i++)
System.out.println(Math.round(employee[i].getSalary(10)));
}
}
第 5 章 異常
一.簡答題
1.寫出處理異常的五個關(guān)鍵字
答:1. try、catch、finally、throw、throws。
2.簡述try...catch語句的一場處理流程并畫出流程圖
答:2. 程序通過 try 語句捕獲可能出現(xiàn)的異常,如果 try 語句沒有捕獲到異常,則直接跳出 try…catch 語句塊執(zhí)行其他程序;如果在 try 語句中捕獲到了異常,則程序會自動跳轉(zhuǎn)到 catch 語句中找到匹配的異常類型進(jìn)行相應(yīng)的處理。如果 try 語句捕獲到的異常與 catch 語 句例的異常匹配,則先執(zhí)行 catch 中的語句,最后執(zhí)行其他程序語句。
3.簡述處理編譯異常的兩種方法
答:3. 處理編譯時期的異常有兩種方式如下: (1)使用 try…catch 語句對異常進(jìn)行捕獲處理。 (2)使用 throws 關(guān)鍵字聲明拋出異常,調(diào)用者對異常進(jìn)行處理。
第 6 章 Java API
五、1.每次隨機(jī)生成10個0~100的隨機(jī)正整數(shù)。
2.計算從今天算起100天以后是幾月幾日,并格式化成×××X年X月×日的形式打印出來。
提示:
(1)調(diào)用Calendar類的add()方法計算100天后的日期。
(2)調(diào)用Calendar類的getTime()方法返回Date類型的對象。
(3)使用FULL格式的DateFormat對象,調(diào)用format()方法格式化Date對象。
Example.java
import java.util.Random;
2 public class Example {
3 public static void main(String[] args) {
4 for(int i=0;i<10;i++){
5 System.out.println(new Random().nextInt(100));
6 }
7 }
8 }
Test.java文章來源:http://www.zghlxwxcb.cn/news/detail-404378.html
1 import java.text.DateFormat;
2 import java.util.Calendar;
3 import java.util.Date;
4 public class Test {
5 public static void main(String[] args) {
6 Calendar calendar = Calendar.getInstance();
7 calendar.add(Calendar.DATE, 100);
8 Date date = calendar.getTime();
9 DateFormat format = DateFormat.getDateInstance(DateFormat.FULL);
10 String string = format.format(date);
11 System.out.println(string);
12 }
13 }
第 7 章 集合類
1.編寫程序,向ArmsList集合中添加元素,然后遍歷并輸出這些元素。
1 public class Example {
2 public static void main(String[] args) {
3 ArrayList list = new ArrayList<>();
4 list.add("a");
5 list.add("b");
6 list.add("c");
7 list.add("a");
8 for(Iterator it = list.iterator();it.hasNext();){
9 System.out.println(it.next());
10 }
11 }
12 }
2.請發(fā)照下列要求編寫程序。
(1)寫一個Student類,包含name和age屬性,提供有參構(gòu)造方法。
(2)在Snudent 類中,重寫 toString()方法,輸出age和name的值。
(3)在Student類中,重寫hashCode()和equals()方法。
.hshCode()的返回值是name的哈希值與age的和。
.equals()判斷對象的name和age是否相同,相同則返回true,不同則返回false。
4)編寫一個測試類,創(chuàng)建一個HashSet<Student>對象hs,向hs中添加多個sat對象,假設(shè)有兩個Student對象相等。輸出HashSet集合,觀察Student對象是否添加成功。
Test.java文章來源地址http://www.zghlxwxcb.cn/news/detail-404378.html
1 import java.util.*;
2 class Student {
3 private int age;
4 private String name;
5 public Student(int age, String name) {
6 this.age = age;
7 this.name = name;
8 }
9 public String toString() {
10 return age + ":" + name;
11 }
12 public int hashCode() {
13 return name.hashCode() + age;
14 }
15 public boolean equals(Object obj) {
16 if (this == obj)
17 return true;
18 if (!(obj instanceof Student))
19 return false;
20 Student stu = (Student) obj;
21 return this.name.equals(stu.name) && this.age == stu.age;
22 }
23 }
24 public class Test {
25 public static void main(String[] args) {
26 HashSet<Student> hs = new HashSet<Student>();
27 hs.add(new Student(18, "zhangsan"));
28 hs.add(new Student(20, "lisa"));
29 hs.add(new Student(20, "lisa"));
30 System.out.println(hs);
31 }
32 }
第 8 章 泛型
按照下列提示編寫一個泛型接口以及其實(shí)現(xiàn)類
(1)創(chuàng)建泛型接口Generic<T>,并創(chuàng)建抽象方法get(T t)。
(2)創(chuàng)建實(shí)現(xiàn)類GenericImpl<T> , 并實(shí)現(xiàn)get(T t)方法。
1 interface Generic<T>{
2 public abstract void get(T t){}
3 }
4 class Generic<T> implements Generic{
5 public void get(T t){}
6 }
第 10 章 IO(輸入輸出) ? ?
編寫一個程序,分別使用字節(jié)流和字符流復(fù)制一個文本文件。要求如下:
(1)使用?FilelnputStream?,?FileOutputStreaem?和?FileReader?,?FileWriter?分別進(jìn)行
復(fù)制。
(2)使用字節(jié)流復(fù)制時,定義一個長度為1024的字節(jié)數(shù)組作為緩沖區(qū),使用字符流
復(fù)制。
Test01.Java
1 import java.io.*;
2 public class Test01 {
3 public static void main(String[] args) throws Exception {
4 // 字節(jié)流拷貝
5 FileInputStream in = new FileInputStream("E:/src.txt");
6 FileOutputStream out = new FileOutputStream("E:/des1.txt");
7 byte[] buf = new byte[1024];
8 int len;
9 while ((len = in.read(buf)) != -1) {
10 out.write(buf, 0, len);
11 }
12 in.close();
13 out.close();
14 // 字符流拷貝
15 BufferedReader bf = new BufferedReader(new
16 FileReader("E:/src.txt"));
17 BufferedWriter bw = new BufferedWriter(new
18 FileWriter("E:/des2.txt"));
19 String str;
20 while ((str = bf.readLine()) != null) {
21 bw.write(str);
22 bw.newLine();
23 }
24 bf.close();
25 bw.close();
26 }
27 }
第 12 章 多線程
編寫一個多線程程序,模擬火車售票窗口的售票功能。創(chuàng)建線程1和線程2,通過這
兩個線程共同售出100張票。
?Example.java
Example.java
1 public class Example {
2 public static void main(String[] args) {
3 TicketWindow tw = new TicketWindow();
4 new Thread(tw, "線程 1").start();
5 new Thread(tw, "線程 2").start();
6 }
7 }
8 class TicketWindow implements Runnable {
9 private int num = 100;
10 public void run() {
11 while (num > 0) {
12 Thread th = Thread.currentThread();
13 String th_name = th.getName();
14 System.out.println(th_name + " 正在發(fā)售第 " + num-- + " 張票 ");
15 }
16 }
到了這里,關(guān)于《Java基礎(chǔ)入門》第三版--黑馬程序員課后習(xí)題(編程部分)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!