求java代码,1,2,3题

实验题目:

1、 设计一个汽车类Auto,其中包含一个表示速度的double型的成员变量speed和表示启动的start()方法、表示加速的speedUp()方法以及表示停止的stop()方法。再设计一个Auto类的子类Bus表示公共汽车,在Bus类中定义一个int型的表示乘客数的成员变量passenger,另外定义两个方法gotOn()和gotOff()表示乘客上车和下车。编写一个应用程序测试Bus类的使用。
2、(1)声明一个Person类,有name(String类型)、age(int类型)、sex(char类型)属性。通过构造方法进行赋值。一个show方法,返回String类型,内容如下:某某男(女)年龄

(2)声明一个Student类,继承Person类,增加id(int,学号)属性,通过构造方法,利用super 调用父类构造方法来进行变量赋值。Override父类的show方法,返回String类型,内容如下:某某男(女)年龄学号
提示:利用super调用父类的show方法得到除学号部分的String,然后加上学号的信息。
(3)声明一个Teacher类,继承Person,增加course(String,所教课程)属性,通过构造方法,利用super 调用父类构造方法来进行变量赋值。Overide父类的show方法,返回String类型,内容如下:某某男(女)年龄所教课程
提示:利用super 调用父类的show方法得到除所教课程部分的String,然后加上所教课程的信息。
(4)声明PersonApp类,在其中的main 方法中分别声明 Person、Student、Teacher类型的变量,并通过构造方法初始化,然后显示各自的信息。
3、

(1)设计一个抽象形状类 Shape,包含一个 getArea()方法,该方法不包含实际语句。
(2)在 Shape 类基础上设计圆形、矩形、三角形和梯形四个子类,要求根据实际形状重
写 getArea()方法。
(3)设计一个 TestShape 类,包含变量 area(存储总面积)、静态方法 countArea(Shape s),该方法负责把参数中的形状面积加入到 area 中。在 main 函数中新建(2)中四种类型的对象s1、s2、s3、s4,通过调用 countArea 方法把四个对象面积累加到 area 中,最后输出 area。

程序代码及运行结果:

实验1:

public class Auto {
  private double speed;

  public void start() {
    System.out.println("启动...");
  }

  public void speedUp() {
    System.out.println("加速...速度:" + (++speed));
  }

  public void stop() {
    speed = 0D;
    System.out.println("停止...");
  }
}
public class Bus extends Auto {
  private int passenger;

  public void gotOn() {
    System.out.println(String.format("乘客上车,乘客人数:%d", ++passenger));
  }

  public void gotOff() {
    System.out.println(String.format("乘客下车,乘客人数:%d", --passenger));
  }
}

public class BusApp {
  public static void main(String[] args) {
    Bus bus = new Bus();
    bus.gotOn();
    bus.start();
    bus.speedUp();
    bus.stop();
    bus.gotOff();
  }
}

实验2

public class Person {
  private String name;
  private int age;
  // 'm' 男 , 'f' 女
  private char sex;

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

  public String show() {
    return String.format("姓名:%s,性别:%s,年龄:%d", name, (sex == 'm' ? "男" : "女"), age);
  }
}
public class Student extends Person {
  private int id;

  public Student(String name, int age, char sex, int id) {
    super(name, age, sex);
    this.id = id;
  }

  @Override
  public String show() {
    return String.format("%s,学号:%d", super.show(), id);
  }
}
public class Teacher extends Person {
  private String course;

  public Teacher(String name, int age, char sex, String course) {
    super(name, age, sex);
    this.course = course;
  }

  @Override
  public String show() {
    return String.format("%s,所教课程:%s", super.show(), course);
  }
}
public class PersonApp {
  public static void main(String[] args) {
    Person person = new Person("Jack", 30, 'm');
    Student student = new Student("Tom", 8, 'm', 7);
    Teacher teacher = new Teacher("Dora", 21, 'f', "English");
    System.out.println(person.show());
    System.out.println(student.show());
    System.out.println(teacher.show());
  }
}

实验3

public abstract class Shape {
  abstract int getArea();
}
public class Circle extends Shape {
  @Override
  int getArea() {
    return 2;
  }
}
public class Rectangle extends Shape {
  @Override
  int getArea() {
    return 4;
  }
}

public class Triangle extends Shape {
  @Override
  int getArea() {
    return 3;
  }
}
public class Trapezoid extends Shape {
  @Override
  int getArea() {
    return 8;
  }
}
public class TestShape {
  private static int area;

  private static void countArea(Shape s) {
    area += s.getArea();
  }

  public static void main(String[] args) {
    Circle s1 = new Circle();
    Rectangle s2 = new Rectangle();
    Triangle s3 = new Triangle();
    Trapezoid s4 = new Trapezoid();
    countArea(s1);
    countArea(s2);
    countArea(s3);
    countArea(s4);
    System.out.println(area);
  }
}

看下这个Java入门视频教程,可以快速上手,轻松完成此类实验题目。

第一题:

public class Auto {
    public double speed;

    public void start(){
        System.out.println("汽车启动!");
    }
    public void speedUp(double speed){
        this.speed += speed;
        System.out.println("汽车开始加速!");
    }
    public void stop(){
        System.out.println("汽车停止!");
    }
}

public class Bus extends Auto{
    public int passenger;

    public void gotOn(int num){
        this.passenger += num;
        System.out.println(num+"乘客上车");
    }
    public void gotOff(int num){
        this.passenger -= num;
        System.out.println(num+"乘客下车");
    }
}

public class TestMain {
    public static void main(String[] args) {
        Bus bus = new Bus();
        bus.passenger = 0;
        bus.speed = 0;
        bus.gotOn(10);
        bus.gotOff(5);
        bus.start();
        bus.speedUp(50);
        bus.stop();
    }
}

结果:
10乘客上车
5乘客下车
汽车启动!
汽车开始加速!
汽车停止!

第二题:


public class Person {
    private String name;
    private int age;
    private char sex;

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

    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 char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public String show(){
        return this.getName() + " "+ this.getSex()+" "+ this.getAge();
    }
}

public class Student extends Person{
    private int id;

    public Student(String name, int age, char sex) {
        super(name, age, sex);
    }

    public Student(String name, int age, char sex, int id) {
        super(name, age, sex);
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String show(){
        return super.show() +" "+ this.getId();
    }
}


public class Teacher extends Person{
    private String course;

    public Teacher(String name, int age, char sex) {
        super(name, age, sex);
    }

    public Teacher(String name, int age, char sex, String course) {
        super(name, age, sex);
        this.course = course;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public String show(){
        return super.show() + " "+ this.getCourse();
    }
}



public class PersonApp {
    public static void main(String[] args) {
        Person person = new Person("person", 19, '男');
        Student student = new Student("张三", 20, '男', 10001);
        Teacher teacher = new Teacher("老师", 38, '女', "语数英");
        System.out.println(person.show());
        System.out.println( student.show());
        System.out.println( teacher.show());
    }
}

结果:
person 男 19
张三 男 20 10001
老师 女 38 语数英

第三题:

public abstract class Shape {
    double getArea() {
        return 0;
    }
}

public class Square extends Shape{
    private double len;
    private double width;

    public double getArea() {
        return this.len * this.width;
    }

    public double getLen() {
        return len;
    }

    public void setLen(double len) {
        this.len = len;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }
}


public class Circle extends Shape{
    public static final double PI = 3.14;

    private double r;

    public double getArea() {
        return PI * this.r * this.r;
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }
}


/**
 * 三角形
 */
public class Triangle extends Shape {
    private double len;
    private  double height;

    public double getArea() {
        return len * height / 2;
    }

    public double getLen() {
        return len;
    }

    public void setLen(double len) {
        this.len = len;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}


/**
 * 梯形
 */
public class Trapezium extends Shape{
    private double sLen;
    private  double xLen;
    private double height;

    public double getArea() {
        return (this.sLen + this.xLen) * this.height / 2;
    }

    public double getsLen() {
        return sLen;
    }

    public void setsLen(double sLen) {
        this.sLen = sLen;
    }

    public double getxLen() {
        return xLen;
    }

    public void setxLen(double xLen) {
        this.xLen = xLen;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}


public class TestShape {
    public static double area;

    public static void countArea(Shape s){
        area += s.getArea();
    }
}


public class PersonApp {
    public static void main(String[] args) {
        Square square = new Square();
        square.setLen(5);
        square.setWidth(3);
        TestShape.countArea(square);

        Circle circle = new Circle();
        circle.setR(5.5);
        TestShape.countArea(circle);

        Triangle triangle = new Triangle();
        triangle.setLen(6);
        triangle.setHeight(4);
        TestShape.countArea(triangle);

        Trapezium trapezium = new Trapezium();
        trapezium.setsLen(5.5);
        trapezium.setsLen(7.5);
        trapezium.setHeight(9);
        TestShape.countArea(trapezium);

        System.out.println("总面积为:"+ TestShape.area);
    }
}


结果:总面积为:155.735

如果对你有帮助,请采纳一下哈!!!