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 }
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 }
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();
}
}
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)));
}
}
答:1. try、catch、finally、throw、throws。
答:2. 程序通过 try 语句捕获可能出现的异常,如果 try 语句没有捕获到异常,则直接跳出 try…catch 语句块执行其他程序;如果在 try 语句中捕获到了异常,则程序会自动跳转到 catch 语句中找到匹配的异常类型进行相应的处理。如果 try 语句捕获到的异常与 catch 语 句例的异常匹配,则先执行 catch 中的语句,最后执行其他程序语句。
答:3. 处理编译时期的异常有两种方式如下: (1)使用 try…catch 语句对异常进行捕获处理。 (2)使用 throws 关键字声明抛出异常,调用者对异常进行处理。
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 }
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 }
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 hs = new HashSet();
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 }
1 interface Generic{
2 public abstract void get(T t){}
3 }
4 class Generic implements Generic{
5 public void get(T t){}
6 }
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 }
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 }