您的位置:

java练习toteacher(java基础训练题)

本文目录一览:

用JAVA (1)定义一个Teacher类,为其添加编号(String)、姓名、年龄3个变量和一个无参构造方法

完整代码

public class Teacher {

private String number;

private String name;

private int age;

public Teacher(){

//Java 默认初始化

}

public Teacher(String number,String name,int age){

this.number = number;

this.name = name;

this.age = age;

}

public static void main(String[] args) {

Teacher teacher = new Teacher();

System.out.println("Default teacher number: " + teacher.number);

System.out.println("Default teacher name: " + teacher.name);

System.out.println("Default teacher age: " + teacher.age);

Teacher teacher2 = new Teacher("001", "Miss Gao", 25);

System.out.println("Constructor by Teacher(String number,String name,int age) teacher number: " + teacher2.number);

System.out.println("Constructor by Teacher(String number,String name,int age) teacher name: " + teacher2.name);

System.out.println("Constructor by Teacher(String number,String name,int age) teacher age: " + teacher2.age);

}

}

---------------运行结果

Default teacher number: null

Default teacher name: null

Default teacher age: 0

Constructor by Teacher(String number,String name,int age) teacher number: 001

Constructor by Teacher(String number,String name,int age) teacher name: Miss Gao

Constructor by Teacher(String number,String name,int age) teacher age: 25

求Java高手: 写程序!

给你写的例子 你可以仿照写Director类 ,Director是 extends Teacher implements IPerson ,你可以练习下

IPerson接口:

--------------------------------------

package com.demo;

/**

* p

* Description: 接口

* /p

*

*/

public interface IPerson {

public void print();

}

------------------------------------------------------

Teacher类:

package com.demo;

/**

* p

* Description:老师类

* /p

*/

public class Teacher implements IPerson {

private String ID;//工号ID,

private String name;//姓名name,

private double salary;//工资salary,

private String job;//职务job;

public void Tesacher() {

}

public Teacher(String ID, double salary, String job) {

this.ID = ID;

this.salary = salary;

this.job = job;

}

public Teacher(String ID, String name, double salary, String job) {

this.ID = ID;

this.name = name;

this.salary = salary;

this.job = job;

}

/**

* @return the iD

*/

public String getID() {

return ID;

}

/**

* @param iD the iD to set

*/

public void setID(String iD) {

ID = iD;

}

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the salary

*/

public double getSalary() {

return salary;

}

/**

* @param salary the salary to set

*/

public void setSalary(double salary) {

this.salary = salary;

}

/**

* @return the job

*/

public String getJob() {

return job;

}

/**

* @param job the job to set

*/

public void setJob(String job) {

this.job = job;

}

/* (non-Javadoc)

* @see com.demo.IPerson#print()

*/

public void print() {

System.out.println("老师工号:" + getID());

System.out.println("老师姓名:" + getName());

System.out.println("老师职务:" + getJob());

System.out.println("老师工资:" + getSalary());

}

}

-----------------------------------------------------

Test类:

package com.demo;

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

IPerson teacher = new Teacher("1", "张三丰", 20000, "太极");

teacher.print();

}

}

一道java编程题 急!!!

public class Test {

    public static void main(String args[]) {

        Teacher t1 = new Teacher("001","张三","23","40");

        Teacher t2 = new Teacher("002","李四","45","102");

        Teacher t3 = new Teacher("003","王五","34","80");

        Teacher t4 = new Teacher("004","赵六","19","15");

        Teacher t5 = new Teacher("005","前期","24","50");

        List list = new ArrayList();

        list.add(t1);

        list.add(t2);

        list.add(t3);

        list.add(t4);

        list.add(t5);

        Object[] techers = list.toArray();

        System.out.println ("排序前。。。。");

        for (int i = 0; iteachers.length; i++){

           System.out.println (teachers[i]);

        }

        //调用排序方法

        java.util.Arrays.sort(teachers, new MyComparator());

        System.out.println ("排序后。。。。");

        for (int i = 0; iteachers.length; i++){

           System.out.println (teachers[i]);

        }

    }

}

class Teacher {

    private String no;

    private String name;

    private int age;

    private int count;

    public Teacher(String no, String name, int age, int count) {

        this.no = no;

        this.name = name;

        this.age = age;

        this.count =count;

    }

    public void setNo(String no){

        this.no = no;

    } 

    public void setName(String name) {

        this.name = name;

    }

    public void setAge(int age) {

        this.age = age;

    }

    public void setCount(int count) {

        this.count = count;

    }

    public String getNo() {

        return this.no;

    }

    public String getName() {

        return this.name;

    }

    public int getAge() {

        return this.age;

    }

    public int getCount() {

        return this.count;

    }    

}

class MyComparator implements Comparator {

    public int compare(Object o1, Object o2) {

        Teacher t1 = (Teacher) o1;

        Teacher t2 = (Teacher) o2;

        if(t1.getCount()  t2.getCount())

            return 1;

        else if(t1.getCount()  t2.getCount())

            return -1;

        else {

            //论文数量相同,按年龄排序

            if(t1.getAge()  t2.getAge()) {

                return 1;

            } else if(t1.getAge()  t2.getAge()) {

                return -1;

            } else { 

                //利用String自身的排序方法。按名字排序

                return t1.getName().compareTo(t2.getName());

            }

        }    

    }   

}

JAVA高手,请修改代码TestTeacher.java,把p1、p2、s1、s2、s3、t1打印出来。。

代码已经改好,给你说说问题所在:

Person作为父类,没有提供空参数构造器,导致子类在没有显示调用父类有参构造器报错,所以父类提供了一个无惨构造器,

后面的

s3.name = "刘七";

s3.location = "儋州";

s3.school = "海南大学";

这三句出错,原因就是name等属性在类中设置为private,私有的,所有不能直接赋值给他,所有我在类中提供了set、get方法。一下程序是我改后的,直接可以运行

class Person {

private String name;

private String location;

Person(){}

Person(String name) {

this.name = name;

location = "三亚";

}

Person(String name,String location) {

this.name = name;

this.location = "三亚";

}

public String info() {

return "姓名: "+name+" 地址: "+location;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getLocation() {

return location;

}

public void setLocation(String location) {

this.location = location;

}

}

class Teacher extends Person {

private String capital;

Teacher(String name, String capital) {

this(name, "三亚", capital);

}

Teacher(String n, String l, String capital) {

super(n,l);

this.capital = capital;

}

public String info() {

return super.info()+" " + "职称:" + capital;

}

}

class Student extends Person {

private String school;

Student(){}

Student(String name,String school) {

this(name,"三亚", school);

}

Student(String n,String l,String school) {

super(n,l);

this.school = school;

}

public String info() {

return super.info()+" " + "学校名称:"+school;

}

public String getSchool() {

return school;

}

public void setSchool(String school) {

this.school = school;

}

}

public class Tteacher {

public static void main(String[] args) {

Person p1 = new Person("张三");

Person p2 = new Person("李四","三亚");

Student s1 = new Student("王五","琼州学院电子信息工程学院");

Student s2 = new Student("赵六","海口","海南大学");

Student s3 = new Student();

// s3.name = "刘七";

s3.setName("刘七");

//s3.location = ;

s3.setLocation("儋州");

// s3.school = "海南大学";

s3.setSchool("海南大学");

Teacher t1 = new Teacher("崔二", "教授");

//以下六句不可修改且为必须要实现的部分,每句十分。

System.out.println(p1.info());

System.out.println(p2.info());

System.out.println(s1.info());

System.out.println(s2.info());

System.out.println(s3.info());

System.out.println(t1.info());

}

}