java练习(二)

题目

Java程序用于显示人的姓名和年龄。 定义一个人类Person该类中应该有两个私有属性姓名name和年龄age。定义构造方法用来初始化数据成员。再定义显示display方法将姓名和年龄打印出来。 在main方法中创建人类的实例然后将信息显示。

代码

Person.Java

public class Person {
    private static String name;
    private static int age;

    public static void main(String[] args) {

        Person person = new Person();
        person.setName("xz");
        person.setAge(3);
        display();
    }

    private static void display() {

        System.out.println(name);
        System.out.println(age);
    }

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Person{name = " + name + ", age = " + age + "}";
    }
}

题目

构造方法的重载 定义一个名Vehicles 交通工具的基类,该类中应包含String类型的成员属性brand 商标 和color 颜色,还应包含成员方法run行驶, 在控制台显示“我已经开动了” 和showInforclk返回商标和颜色 并编写构造方法初始化其成员属性。 编写Car 小汽车 类继承于Vehicles类 增加int型成员属性seats 座位 还应增加成员方法showCar ,返回小汽车的信息,并编写构造方法。 编写Truck 卡车 类继承于Vehicles类 增加float型成员属性load 载重 还应增加成员方法showTruck,返回卡车的信息,并编写构造方法。 在main方法中测试以上各类。

代码

public class Vehicles {
    static String brand;
    static String color;

    private static void run() {
        System.out.println("我已经开动了");

    }
    private static void init() {
        Vehicles vehicles = new Vehicles();
        vehicles.setBrand("宝马");
        vehicles.setColor("白色");
        Car car = new Car();
car.setSeats(15);
Truck truck = new Truck();
truck.setLoad(20);
    }
    public static class Truck extends Vehicles{
        static float load;

        public float getLoad() {
            return load;
        }

        public void setLoad(float load) {
            this.load = load;
        }
    }
    public static class Car extends Vehicles{
        static int seats;

        public int getSeats() {
            return seats;
        }

        public void setSeats(int seats) {
            this.seats = seats;
        }
    }
    /*
    构造方法的重载 定义一个名Vehicles 交通工具的基类,该类中应包含String类型的成员属性brand 商标 和color 颜色,还应包含成员方法run行驶,
    在控制台显示“我已经开动了” 和showInforclk返回商标和颜色 并编写构造方法初始化其成员属性。 编写Car 小汽车 类继承于Vehicles类 增加int型成员属性seats
     座位
    还应增加成员方法showCar ,返回小汽车的信息,并编写构造方法。 编写Truck 卡车 类继承于Vehicles类 增加float型成员属性load 载重
    还应增加成员方法showTruck
    ,返回卡车的信息,并编写构造方法。 在main方法中测试以上各类。
     */
    public static void main(String[] args) {
        init();
        run();
        showInforclk();
        showCar();
        showTruck();

        System.out.println(brand);
        System.out.println(color);
        System.out.println(Car.seats);
        System.out.println(Truck.load);

    }

    private static void showTruck() {
    }

    private static void showCar() {


    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    private static void showInforclk() {


    }
}

题目

构造方法与重载,定义一个网络用户类,要处理的信息有用户ID、用户密码、email地址。在建立类的实例时,通过构造方法为属性赋值,其中用户ID和用户密码时必须的,缺省的email地址是用户ID加上字符串"@gameschool.com"

代码

public class NetWeb {
    static int id;
    static int password;
    static String email;

    public static void main(String[] args) {
       NetWeb netWeb = new NetWeb();
      netWeb.setId(521);
      netWeb.setPassword(521);
      netWeb.setEmail(email);
        System.out.println("账号是"+NetWeb.id);
        System.out.println("密码是"+NetWeb.password);
        System.out.println("邮箱是"+NetWeb.getEmail());


    }

    public NetWeb() {
    }

    public NetWeb(int id, int password, String email) {
        this.id = id;
        this.password = password;
        this.email = email;
    }

    /**
     * 获取
     * @return id

     */
    public int getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * 获取
     * @return password
     */
    public int getPassword() {
        return password;
    }

    /**
     * 设置
     * @param password
     */
    public void setPassword(int password) {
        this.password = password;
    }

    /**
     * 获取
     * @return email
     */
    public static String getEmail() {
        return email=id+"@gameschool.com";
    }

    /**
     * 设置
     * @param email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    public String toString() {
        return "NetWeb{id = " + id + ", password = " + password + ", email = " + email + "}";
    }
}

题目

构造方法与重载、包:编写Addition类,该类中应包含一组实现两数相加运算的重载方法。 实现加法运算的方法add,应接受两个参数,即加数和被加数,方法将两个参数进行加法运算后输出运算结果。考虑可能针对不同的数据类型进行计算,重载一组方法,包括整型、长整型、浮点型、双精度浮点型、还有字符串。 在main方法中创建Addition类的实例 分别调用重载方法测试其效果

代码

public class Addition {
  static int a=1;
  static int b=2;
  static Integer a1=1;
  static Integer b1=2;
  static float a2=1;
  static float b2=2;
  static double a3=1;
  static double b3=2;
  static String a4="1";
  static String b4="2";
  static short a5=1;
  static short b5=2;

    public static void main(String[] args) {
        System.out.println("a=1,b=2");
        IntAddition();
        IntegerAddition();
        FloatAddition();
        DoubleAddition();
        StringAddition();
        ShortAddition();

        Addition addition = new Addition();
        Addition.IntAddition();
        Addition.DoubleAddition();
        Addition.FloatAddition();
        Addition.IntegerAddition();
        Addition.ShortAddition();
        Addition.StringAddition();



    }

    private static void ShortAddition() {
        short c;
        c= (short) (a5+b5);
        System.out.println("短整型的结果"+c);
    }

    private static void StringAddition() {
        String c;
        c=a4+b4;
        System.out.println("字符串的结果"+c);
    }

    private static void DoubleAddition() {
        double c;
        c=a3+b3;
        System.out.println("双精度浮点型"+c);

    }

    private static void FloatAddition() {
        float c;
        c = a2+b2;
        System.out.println("浮点类型"+c);


    }

    private static void IntegerAddition() {
        Integer c;
        c=a1+b1;
        System.out.println("包装类的整数型"+c);
    }

    private static void IntAddition() {
        int c;
        c=a+b;
        System.out.println("整数型"+c);
    }

}

猜你喜欢

转载自blog.csdn.net/u013074761/article/details/106249660