《Java基础入门》第三版--黑马程序员课后习题(编程部分)

第 1 章 Java 开发入门

第 2 章 Java 编程基础

1. 编写程序,计算1+3+…+99的值,要求如下:

(1)使用循环语句实现1~99的遍历

(2)在遍历过程中,通过条件判断当前的数是否为奇数,如果是就累加,否则不加。

 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循环语句计算正数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 章 面向对象(上)

某公司正在进行招聘工作,被招聘人员需要填写个人信息,编写个人简历的封装类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 章 面向对象(下)

某公司的员工分为5类,每类员工都有相应的封装类,这5个类的信息如下。

(1) Emplovee:这是所有员工的父类。

①属性:员工的姓名、员工的生日月份

②方法: Salary int month)根据参数月份确定工资。如果该月员工过生日,则公司会额外发放100元。

(2) Salariedemployee: Employee的子类,拿固定工资的员工。

属性:月薪

(3) Hourlyemployee: Employee的子类,按小时拿工资的员工,每月工作超出160h部分按照1.5倍工资发放。

属性:每小时的工资、每月工作的小时数。

(4) Salesemployee: Employee的子类,销售人员,工资由月销售额和提成率决定。

属性:月销售额、提成率。

(5) Baseplussalesemploye: Salesemployee的子类,有固定底薪的销售人员,工资底薪加上销售提成。

属性:底薪。

本题要求根据上述员工分类(编写一个程序,实现以下功能:

(1)创建 Employee,分别创建若干不同的 Employee对象,并打印某个用

(2)每个类都完全封装,不允许有非私有化属性。

Employee.java


abstract class Employee{
private String name; //定义姓名 name 并私有化属性
private int month; //定义生日月份 month 并私有化属性
public Employee(){} //无参构造器
public Employee(String name,int month){ //有参构造方法
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
}
//创建一个方法 getSalary()用来计算工资,参数 month 是月份,如果当月是员工生日,奖
励 100 元
public double getSalary(int month){
double salary = 0; //定义工资变量
//判断当前月份是否是员 工的生日月份,如果是奖励 100 元
if(this.month == month){
salary = salary + 100; 
return salary; //返回工资 salary
}
}
}

SalariedEmployee.java

class SalariedEmployee extends Employee{
private double monthSalary; //封装 monthSalary 属性
public SalariedEmployee(){} //无参构造方法
//有参构造方法 参数 姓名 生日月份 月薪
public SalariedEmployee(String name,int month,double monthSalary){
super(name,month); //调用父类有参构造方法
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 每月工作的小时数
public HourlyEmployee(){} //无参构造方法
//有参构造方法 参数 姓名 生日月份 每小时的工资 每月工作的小时数 
public HourlyEmployee(String name,int month,double hourlySalary,int 
hours){
super(name,month); //调用父类有参构造方法 
this.hourlySalary = hourlySalary ; //为属性 hourlySalary 初始化赋值
this.hours = hours; //为属性 hours 初始化赋值
}
public double getHourlySalary(){ //获取 hourlySalary 的值
return hourlySalary;
}
public int getHours(){ //获取 hours 的值
return hours;
}
//定义 set 方法设置 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){ //如果工作小时数小于 0 输出数据错误
System.out.println("数据错误");
return 0;
}
//小于 160 个小时的 按照每个月的工作小时数乘以每小时的工资
else if(hours <= 160) 
return hourlySalary*hours+super.getSalary(month); 
//超出 160 个小时的小时数 按照 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; //定义基础工资 baseSalary
//无参构造方法
public BasePlusSalesEmployee(){}
//有参构造方法
public BasePlusSalesEmployee(String name,int month,double sales,double 
rate,double baseSalary){
super(name,month,sales,rate);
this.baseSalary = baseSalary;
}
//get/set 方法对私有属性的调用和设置
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 类型的数组,并创建不同子类型的对象
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.写出处理异常的五个关键字

答:1. try、catch、finally、throw、throws。

2.简述try...catch语句的一场处理流程并画出流程图

答:2. 程序通过 try 语句捕获可能出现的异常,如果 try 语句没有捕获到异常,则直接跳出 try…catch 语句块执行其他程序;如果在 try 语句中捕获到了异常,则程序会自动跳转到 catch 语句中找到匹配的异常类型进行相应的处理。如果 try 语句捕获到的异常与 catch 语 句例的异常匹配,则先执行 catch 中的语句,最后执行其他程序语句。

3.简述处理编译异常的两种方法

答:3. 处理编译时期的异常有两种方式如下: (1)使用 try…catch 语句对异常进行捕获处理。 (2)使用 throws 关键字声明抛出异常,调用者对异常进行处理。

第 6 章 Java API

五、1.每次随机生成10个0~100的随机正整数。

2.计算从今天算起100天以后是几月几日,并格式化成×××X年X月×日的形式打印出来。

提示:

(1)调用Calendar类的add()方法计算100天后的日期。

(2)调用Calendar类的getTime()方法返回Date类型的对象。

(3)使用FULL格式的DateFormat对象,调用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

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.请发照下列要求编写程序。

(1)写一个Student类,包含name和age属性,提供有参构造方法。

(2)在Snudent 类中,重写 toString()方法,输出age和name的值。

(3)在Student类中,重写hashCode()和equals()方法。

.hshCode()的返回值是name的哈希值与age的和。

.equals()判断对象的name和age是否相同,相同则返回true,不同则返回false。

4)编写一个测试类,创建一个HashSet<Student>对象hs,向hs中添加多个sat对象,假设有两个Student对象相等。输出HashSet集合,观察Student对象是否添加成功。

Test.java

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 章 泛型

按照下列提示编写一个泛型接口以及其实现类

(1)创建泛型接口Generic<T>,并创建抽象方法get(T t)。

(2)创建实现类GenericImpl<T> , 并实现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(输入输出)    

编写一个程序,分别使用字节流和字符流复制一个文本文件。要求如下:
(1)使用 FilelnputStream , FileOutputStreaem 和 FileReader , FileWriter 分别进行
复制。
(2)使用字节流复制时,定义一个长度为1024的字节数组作为缓冲区,使用字符流
复制。

Test01.Java

1 import java.io.*;
2 public class Test01 {
3 public static void main(String[] args) throws Exception {
4 // 字节流拷贝
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 章 多线程

编写一个多线程程序,模拟火车售票窗口的售票功能。创建线程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 + " 正在发售第 " + num-- + " 张票 ");
15 }
16 }

猜你喜欢

转载自blog.csdn.net/WZY22502701/article/details/128234656