目录
第一题
题目
我的代码
Person类
错误
正确写法
输出结果
附加要求
代码
结果
第二题
题目
答案
第三题
题目
代码
总结不足
创建对象并运行
第四题
题目
运行结果编辑
第五题
题目
第六题
题目
第一题
题目
我的代码
Person类
package com.hspedu.homework;
public class Person {
// 1.定义一个Person类{name,age,job],初始化Person 对象数组,
// 有3个person对象,并 按照 age 从 大到 小进行排序, 提示,
// 使用冒泡排序 Homework01.java
private String name;
private int age;
private String job;
public Person(String name, int age, String job) {
this.name = name;
this.age = age;
this.job = job;
}
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 getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
错误
题目含义理解错误,误以为是将三个对象的age从小到大排出来,然后输出
报错:Variable expected
分析:person[i].getAge()是一个常量
错误原因:没有理解题意:题目的意思是按照age的大小给三个对象的输出顺序排序
正确写法
因为最后输出的是对象的属性,所以最后需要用到toString方法
package com.hspedu.homework.homework01;
public class HomeWork01 {
public static void main(String[] args) {
//初始化Person 对象数组,有3个person对象,
//并 按照 age 从 大到 小进行排序,
//提示:使用冒泡排序 Homework01.java
Person[] person = new Person[3];
person[0] = new Person("曹操", 53, "魏王");
person[1] = new Person("周瑜", 30, "大都督");
person[2] = new Person("诸葛亮", 38, "军师");
System.out.println("======冒泡排序前的顺序======");
for (int i = 0; i < person.length; i++) {
System.out.println(person[i]);
}
//冒泡排序,双层循环,
// 一共3个元素,外层循环两次
//第一次外层循环时,内循环两次
//第二次外层循环时,内循环一次
Person temp = null;
for (int i = 0; i < person.length - 1; i++) {
for (int j = 0; j < person.length - 1 -i; j++) {
if(person[j].getAge() < person[j+1].getAge()){
temp = person[j];
person[j] = person[j+1];
person[j+1] = temp;
}
}
}
//冒牌排序结束后重新输出
System.out.println("======冒泡排序后的顺序======");
for (int i = 0; i < person.length; i++) {
System.out.println(person[i]);
}
}
}
输出结果
附加要求
按照名字长度排序
需要用到person[j].getName().length()
代码
//名字长度从小到大
for (int i = 0; i < person.length - 1; i++) {
for (int j = 0; j < person.length - 1 -i; j++) {
if(person[j].getName().length() > person[j+1].getName().length()){
temp = person[j];
person[j] = person[j+1];
person[j+1] = temp;
}
}
}
//冒牌排序结束后重新输出
System.out.println("======名字长度冒泡排序后的顺序======");
for (int i = 0; i < person.length; i++) {
System.out.println(person[i]);
}
结果
第二题
题目
答案
第三题
题目
代码
package com.hspedu.homework;
public class Teacher {
//3.编写老师类
//(1)要求有属性“姓名name”"年龄age""职称post”基本工资salary
// 2编写业务方法,introduce () ,实现输出一个教师的信息。
// 工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。
private String name;
private int age;
private String post;
private double salary;
private double grade;
public void introduce(){
System.out.println("姓名:" + name + "\t年龄:" + age + "\t职称:" +
post + "\t基本工资:" + salary + "\t工资级别:" + grade);
}
public Teacher(String name, int age, String post, double salary, double grade) {
this.name = name;
this.age = age;
this.post = post;
this.salary = salary;
this.grade = grade;
}
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 getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
package com.hspedu.homework;
public class Professor extends Teacher{
// (3)编写教师类的三个子类: 教授类(Professor )、副教授类、讲师类。
public Professor(String name, int age, String post, double salary, double grade) {
super(name, age, post, salary, grade);
}
@Override
public void introduce() {
System.out.println("教授级别的信息:");
super.introduce();
}
}
package com.hspedu.homework;
public class AssProfessor extends Teacher{
public AssProfessor(String name, int age, String post, double salary, double grade) {
super(name, age, post, salary, grade);
}
@Override
public void introduce() {
System.out.println("副教授级别的信息:");
super.introduce();
}
}
package com.hspedu.homework;
public class Lecturer extends Teacher{
public Lecturer(String name, int age, String post, double salary, double grade) {
super(name, age, post, salary, grade);
}
@Override
public void introduce() {
System.out.println("讲师级别的信息:");
super.introduce();
}
}
三个子类都没有自己特有的成员方法
总结不足
1.没有定义工资级别:grade这个属性,直接在创建新对象的时候给了数值
2.题目要求打印,一开始做的时候返回的字符串,在调用方法时需要输出语句
创建对象并运行
package com.hspedu.homework.homework03;
public class HomeWork03 {
public static void main(String[] args) {
// 工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。
// 在三个子类里面都重写父类的introduce () 方法
// (4)定义并初始化一个老师对象,
// 调用业务方法,实现对象基本信息的后台打印.
Teacher[] teacher = new Teacher[3];
teacher[0] = new Professor("Alan", 50,"正高",30000,1.3);
teacher[1] = new AssProfessor("Bobby", 50,"副高",13450,1.2);
teacher[2] = new Lecturer("Carl", 35,"中级",7500,1.1);
for (int i = 0; i < teacher.length; i++) {
//动态绑定机制,会根据运行类型来调用方法
teacher[i].introduce();
}
}
}
第四题
题目
对编程思想的培养:题目给的是1000,理解成奖金,在子类中定义一个变量bonus
package com.hspedu.homework;
public class Employee {
private String name;
private double daysal;
private double daysnum;
private double grade;
public void printsal(){
System.out.println(name + "的年工资是" + daysal *daysnum * grade);
}
public Employee(String name, double daysal, double daysnum, double grade) {
this.name = name;
this.daysal = daysal;
this.daysnum = daysnum;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getDaysal() {
return daysal;
}
public void setDaysal(double daysal) {
this.daysal = daysal;
}
public double getDaysnum() {
return daysnum;
}
public void setDaysnum(double daysnum) {
this.daysnum = daysnum;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
package com.hspedu.homework.homework05;
public class Manager extends Employee {
private double bonus;//题目中给的1000是变量,不是固定的
public Manager(String name, double daysal, double daysnum, double grade) {
super(name, daysal, daysnum, grade);
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
//bonus是灵活变化的,所以在完成对象属性初始化的时候不包括bonus
this.bonus = bonus;
}
@Override
public void printsal() {
System.out.println("经理" + getName() + "的年工资是" + (getDaysal()
* getDaysnum()* getGrade() + bonus));
}
}
通过set方法给bonus赋值,可以根据实际情况灵活给数据
package com.hspedu.homework;
public class Worker extends Employee{
public Worker(String name, double daysal, double daysnum, double grade) {
super(name, daysal, daysnum, grade);
}
@Override
public void printsal() {
System.out.print("普通员工");
super.printsal();
}
}
package com.hspedu.homework.homework05;
public class HomeWork04 {
public static void main(String[] args) {
Worker worker = new Worker("smith", 200, 300, 1.0);
Manager manager = new Manager("barney", 400, 280, 1.2);
manager.setBonus(1000);
worker.printsal();
manager.printsal();
}
}
为了方便阅读,把每道练习题都新建一个包,所以包的位置会不太一样
printsal方法的另一种写法
public double showsal(){
return daysal *daysnum * grade;
}
这种写法在子类中可以直接重写,加上bonus就可以了,但是问题在于,这样写就没有办法加上职业和名字了,只能在调用方法的时候再输出职业和名字
运行结果
第五题
题目
package com.hspedu.homework.homework05;
public class Employee {
//设计父类一员工类。
private String name;
private String job;
private double sal;
private int salMonth = 12;//13薪、15薪
public Employee(String name, String job, double sal) {
this.name = name;
this.job = job;
this.sal = sal;
}
public void printyear(){
System.out.println(job + name + "的年工资=" + (sal * salMonth));
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public int getSalMonth() {
return salMonth;
}
public void setSalMonth(int salMonth) {
this.salMonth = salMonth;
}
}
考虑实际情况,工作中有12薪的、13薪、14薪的,将工作月份也作为一个属性来定义
package com.hspedu.homework.homework05;
public class Worker extends Employee{
public Worker(String name, String job, double sal) {
super(name, job, sal);
}
@Override
public void printyear() {
super.printyear();
}
}
package com.hspedu.homework.homework05;
public class Peasant extends Employee{
public Peasant(String name, String job, double sal) {
super(name, job, sal);
}
@Override
public void printyear() {
super.printyear();
}
}
package com.hspedu.homework.homework05;
public class Waiter extends Employee{
public Waiter(String name, String job, double sal) {
super(name, job, sal);
}
@Override
public void printyear() {
super.printyear();
}
}
package com.hspedu.homework.homework05;
public class Teacher extends Employee{
private double classSal;
private double classDay;
public Teacher(String name, String job, double sal) {
super(name, job, sal);
}
@Override
public void printyear() {
System.out.println(getJob() + getName() + "的年工资=" +
(getSal() * getSalMonth() + getClassSal() * getClassDay()));
}
public double getClassSal() {
return classSal;
}
public void setClassSal(double classSal) {
this.classSal = classSal;
}
public double getClassDay() {
return classDay;
}
public void setClassDay(double classDay) {
this.classDay = classDay;
}
}
package com.hspedu.homework.homework05;
public class Scientist extends Employee{
private double bonus;//动态的给奖金
public Scientist(String name, String job, double sal) {
super(name, job, sal);
}
@Override
public void printyear() {
System.out.println(getJob() + getName() + "的年工资=" +
(getSal() * getSalMonth() + bonus ));
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
第六题
题目
package com.hspedu.homework.homework06;
class Grand {
String name="AA";
private int age=100;
public void g1(){};
}
package com.hspedu.homework.homework06;
class Father extends Grand{
String id="001";
private double score;
public void f1(){
//super可以访问哪些成员(属性和方法) ?
// 1、super.name; 2、 super.g1()
// this可以访问哪些成员?
//1、this.id; 2、this.score 3、this.f1(); 4、this.name; 5、this.g1()
}
}
package com.hspedu.homework.homework06;
class Son extends Father{
String name="BB";
public void g1(){}
private void show(){
//super可以访问哪些成员(属性和方法)?
//this可以访问哪些成员?
//super可以访问父类的属性和方法,同时还有放温暖修饰符的限制
//可以访问Father的属性id,但是不能访问属性score,因为是private修饰的
//也可以访问Father的f1();
//Grand类的name、g1();
//综上所述:super可以访问的有
// 1、super.id;2、super.f1(); 3、super.name; 4、super.g1()
//this从本类开始查找访问,可以访问name,g1(),和show(),虽然是private,但是在一个类中
//Father类的id和f1()
//grand类的name,g1(),子类可以调用,就不会再调用父类的
//综上所述:this可以访问的有
//1、this.name; 2、this.g1(); 3、this.show(); 4、this.id; 5、this.f1()
}
}