1、编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类E。
要求:
(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()方法,在speak方法中输出“咿咿呀呀…”的信息。
(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法中输出“小样的,不错嘛!会说话了!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”的信息。
(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功能。
package Day0920.D01;
public class Monkey {
public Monkey() {
}
public Monkey(String s) {
}
public void speak() {
System.out.println("咿咿呀呀......");
}
}
package Day0920.D01;
public class People extends Monkey {
public People() {
}
public People(String s) {
super(s);
}
public void think() {
System.out.println("别说话!认真思考!");
}
}
package Day0920.D01;
public class E {
public static void main(String[] args) {
Monkey m = new Monkey();
m.speak();
People p = new People();
p.think();
}
}
2、编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。
小车类Car是Vehicle的子类,其中包含的属性有载人数loader。
卡车类Truck是Car类的子类,其中包含的属性有载重量payload。
每个类都有构造方法和输出相关数据的方法。
最后,写一个测试类来测试这些类的功能。
package Day0920.D02;
public class Car extends Vehicle{
private int loader; // 载人数
public Car() {
}
public Car(int loader) {
this.loader = loader;
}
public int getLoader() {
return loader;
}
public void setLoader(int loader) {
this.loader = loader;
}
public void driving(){
System.out.println("小车在高速行驶!");
}
}
package Day0920.D02;
public class Truck extends Car{
private double payload; // 载重量
public Truck() {
}
public Truck(double payload) {
this.payload = payload;
}
public double getPayload() {
return payload;
}
public void setPayload(double payload) {
this.payload = payload;
}
public void cargo (){
System.out.println("卡车载了一车的水泥!");
}
}
package Day0920.D02;
public class Vehicle {
private int wheels; // 车轮个数
private double weight; // 车重
public Vehicle() {
}
public Vehicle(int wheels) {
this.wheels = wheels;
}
public int getWheels() {
return wheels;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void run(){
System.out.println("汽车在跑!");
}
}
package Day0920.D02;
public class Test {
public static void main(String[] args) {
Vehicle v = new Vehicle();
v.run();
Truck t = new Truck();
t.cargo();
Car c = new Car();
c.driving();
}
}
3、按要求编写一个Java应用程序
(1)定义一个形状类Shape,由求周长和面积的方法。
(2)定义一个圆形类Circle,继承Shape。
(3)定义一个矩形类Rect,继承Shape。
(4)定义一个正方形类Square,继承Rect。
(5)测试。
package Day0920.D03;
public class Shape{
public double zc(){
return 0;
}
public double area(){
return 0;
}
public void show(){
System.out.println();
System.out.println("周长为:"+zc()+" "+"面积为:"+area());
}
}
package Day0920.D03;
public class Circle extends Shape {
private final double PI = 3.14;
private double r;
public Circle(double r) {
this.r = r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
@Override
public double zc() {
return 2 * PI * r;
}
@Override
public double area() {
return PI * r * r;
}
}
package Day0920.D03;
public class Rect extends Shape {
private double weight;
private double leight;
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getLeight() {
return leight;
}
public void setLeight(double leight) {
this.leight = leight;
}
public Rect() {
}
public Rect(double weight) {
this.weight = weight;
}
public Rect(double weight, double leight) {
this.weight = weight;
this.leight = leight;
}
@Override
public double zc() {
return (weight + leight) * 2;
}
@Override
public double area() {
return weight * leight;
}
}
package Day0920.D03;
public class Square extends Rect {
@Override
public double zc() {
return getWeight() * 4;
}
@Override
public double area() {
return getWeight() * getWeight();
}
public Square(double weight) {
super(weight);
}
}
package Day0920.D03;
public class Test {
public static void main(String[] args) {
Circle c1 = new Circle(5);
c1.show();
Rect r1 = new Rect(5,10);
r1.show();
Square s1 = new Square(20);
s1.show();
}
}
4、(1) 写一个Person类,有编号id,姓名name,职位job,构造方法带三个参数。方法:自我介绍talk(2) 写一个学生Student类继承Person类,方法有:考试test,属性有:学费money(3) 写一个老师类Teacher类,属性有:工资salary,方法有:工作work(4) 写一个测试类testPerson,测试学生和老师学生:姓名-张三 职位-学生 学费-18000老师:姓名-李四 职位-老师 工资-8000
package Day0920.D04;
public class Person {
private String id; // 编号
private String name; // 姓名
private String job; // 职位
public Person() {
}
public Person(String id, String name, String job) {
this.id = id;
this.name = name;
this.job = job;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 void talk(){
System.out.println("My is people");
}
}
package Day0920.D04;
public class Student extends Person {
private int money; // 学费
public Student() {
}
public Student(int money) {
this.money = money;
}
public Student(String id, String name, String job, int money) {
super(id, name, job);
this.money = money;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public void test() {
System.out.println("姓名-张三 职位-学生 学费-18000");
}
}
package Day0920.D04;
public class Teacher extends Student{
private double salary; // 工资
public Teacher() {
}
public Teacher(double salary) {
this.salary = salary;
}
public Teacher(int money, double salary) {
super(money);
this.salary = salary;
}
public Teacher(String id, String name, String job, int money, double salary) {
super(id, name, job, money);
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void work(){
System.out.println("姓名-李四 职位-老师 工资-8000");
}
}
package Day0920.D04;
public class TestPerson {
public static void main(String[] args) {
Person p = new Person();
p.talk();
Student s = new Student();
s.test();
Teacher t = new Teacher();
t.work();
}
}
5、(1)设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的,成员变量x和y,获取和设置x和y值的public方法。(2)设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected类型的成员变量r、获取和设置r值的public方法、计算圆面积的public方法。(3)设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包含有表示圆柱体高的protected类型的成员变量h、获取和设置h值的public方法、计算圆柱体体积的public方法。(4)建立Cylinder对象,输出其轴心位置坐标、半径、面积、高及其体积的值。
package Day0920.D05;
public class Point{
protected double x;
protected double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point() {
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
package Day0920.D05;
public class Circle extends Point {
/**
* r
*/
protected double r;
/**
* π
*/
protected final double PI = 3.14;
/**
* 圆
*/
public Circle() {
}
/**
* 圆
*
* @param x x
* @param y y
* @param r r
*/
public Circle(double x, double y, double r) {
super(x, y);
this.r = r;
}
/**
* getr
*
* @return double
*/
public double getR() {
return r;
}
/**
* setr
*
* @param r r
*/
public void setR(double r) {
this.r = r;
}
/**
* 区域
*
* @return double
*/
public double area() {
return PI * r * r;
}
}
package Day0920.D05;
public class Cylinder extends Circle {
/**
* h
*/
protected double h;
/**
*
*/
public Cylinder() {
}
/**
*
*
* @param x x
* @param y y
* @param r r
* @param h h
*/
public Cylinder(double x, double y, double r, double h) {
super(x, y, r);
this.h = h;
}
/**
* geth
*
* @return double
*/
public double getH() {
return h;
}
/**
*
*
* @param h h
*/
public void setH(double h) {
this.h = h;
}
/**
*
*
* @return double
*/
@Override
public double area() {
return super.area() * h;
}
}
package Day0920.D05;
public class Test {
public static void main(String[] args) {
Cylinder c1 = new Cylinder(10, 10, 10, 10);
System.out.println("坐标是: " + c1.x + "," + c1.y + " 半径是:" + c1.r + " 底面积是:" + (c1.area()) / c1.h + " 高是:" + c1.h + " 体积是:" + c1.area());
}
}