zoukankan      html  css  js  c++  java
  • 0922继承,练习题目-作业

    1.实现如下类之间的继承关系,并编写Music类来测试这些类。

    package workhome0922休息;
    
    public class People {
    
    	protected double height;
    	protected double weight;
    	
    	public double getHeight() {
    		return height;
    	}
    	public void setHeight(double height) {
    		this.height = height;
    	}
    	public double getWeight() {
    		return weight;
    	}
    	public void setWeight(double weight) {
    		this.weight = weight;
    	}
    	//方法
    	public void speakHello()
    	{
    		System.out.print("我是一个人"+"  ");
    	}
    	public void averageHeight()
    	{
    		System.out.print("我有身高"+"  ");
    	}
    	public void averageWeight()
    	{
    		System.out.println("我有体重"+"  ");
    	}
    	
    }
    
    
    package workhome0922休息;
    
    public class ChinaPeople extends People {
    
    	public void speakHello()
    	{
    		System.out.print("中国人"+"  ");
    	}
    	public double averageHeight(double height)
    	{
    //		return height;
    		this.height=height;//为什么要把height赋给父类的height??????
    		return this.height;	
    	}
    	public double averageWeight(double weight)
    	{
    		this.weight=weight;
    		return this.weight;
    		
    	}
    	public void chinaGongFu()
    	{
    		System.out.println("中国功夫"+"太极拳");
    	}
    }
    
    package workhome0922休息;
    
    public class AmericanPeople extends People {
    
    	public void speakHello()
    	{
    		System.out.print("美国人");
    	}
    	public double averageHeight(double height)
    	{
    		this.height=height;
    		return this.height;
    	}
    	public double averageWeight(double weight)
    	{
    		this.weight=weight;
    		return this.weight;
    	}
    	public void americanBoxing()
    	{
    		System.out.println("美国功夫:"+"直拳");
    	}
    }
    
    package workhome0922休息;
    
    public class Peopletest {
    
    	public static void main(String[] args) {
    		//实例化人
    		People p=new People();
    		p.speakHello();
    		p.averageHeight();
    		p.averageWeight();
    		//实例化中国人
    		ChinaPeople c=new ChinaPeople();
    		c.speakHello();
    		System.out.print(""+"身高:"+c.averageHeight(171.2));
    		System.out.print("  "+"体重:"+c.averageWeight(125)+"  ");
    		c.chinaGongFu();
    		//实例化美国人
    		AmericanPeople a=new AmericanPeople();
    		a.speakHello();
    		System.out.print("  "+"身高:"+a.averageHeight(180));
    		System.out.print("  "+"体重:"+a.averageWeight(160)+"  ");
    		a.americanBoxing();
    		
    	}
    
    }
    

    2.创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople

    AmericanPeople类重写父类的三个方法)。

    package workhome0922休息;
    
    public class Instrument {
    	//方法-输出:弹奏乐器
    	public void play()
    	{
    		System.out.println("弹奏乐器");
    	}
    }
    
    
    package workhome0922休息;
    
    public class Wind extends Instrument {
    	//方法1-输出:弹奏Wind
    	public void play()
    	{
    		System.out.println("弹奏Wind");
    	}
    	//方法2-输出:调用Wind的play2
    	public void play2()
    	{
    		System.out.println("调用Wind的play2");
    	}
    
    }
    
    package workhome0922休息;
    
    public class Brass extends Instrument {
    
    	//方法1-输出:弹奏brass
    	public void play()
    	{
    		System.out.println("弹奏brass");
    	}
    	//方法2-输出:调用brass的play2
    	public void play2()
    	{
    		System.out.println("调用brass的play2");
    	}
    }
    
    
    package workhome0922休息;
    
    public class Music {
    
    	//调用对象i的play方法
    	public static void tune(Instrument i)
    	{
    		i.play();
    	}	
    	public static void main(String[] args) {
    		
    		Wind w=new Wind();
    		Music.tune(w);
    		w.play2();
    		Brass b=new Brass();
    		Music.tune(b);
    	}
    }
    

    3.编写一个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)在主类Emain方法中创建MonkeyPeople类的对象类测试这2个类的功能。

    package workhome2;
    
    public class Monkey {
    
    	//构造方法
    	Monkey(String s)
    	{
    		super();
    		System.out.println(s+1);
    	}
    	Monkey(int ss)
    	{
    		System.out.println(ss);
    	}
    	//方法
    	public void speak()
    	{
    		System.out.println("咿咿呀呀......");
    	}
    }
    
    
    package workhome2;
    
    public class People extends Monkey {
    
    	//继承构造方法
    	public People(String s) {
    		super(s);
    		System.out.println(s+10);
    		
    	}
    	//重写父类方法speak
    	public void speak()
    	{
    		System.out.println("小样的,不错嘛!会说话了");
    	}
    	//新增方法
    	public void think()
    	{
    		System.out.println("别说话!认真思考!");
    	}
    }
    
    
    package workhome2;
    
    public class E {
    
    	public static void main(String[] args) {
    
    		//创建Monkey对象
    		Monkey m=new Monkey("10");
    		m.speak();
    		//创建People对象
    		People p=new People("10");
    		p.speak();
    		p.think();
    
    	}
    
    }
    

    4.定义类Human,具有若干属性和功能;定义其子类ManWoman

    在主类Test中分别创建子类、父类和上转型对象,并测试其特性。

    package workhome3;
    
    public class Human {
    	//人的属性
    	public String leg;
    	public String foot;
    	public String getLeg() {
    		return leg;
    	}
    	public void setLeg(String leg) {
    		this.leg = leg;
    	}
    	public String getFoot() {
    		return foot;
    	}
    	public void setFoot(String foot) {
    		this.foot = foot;
    	}
    	//人的方法
    	public void run()
    	{
    		System.out.println("可以跑");
    	}
    	public void jump()
    	{
    		System.out.println("可以跳");
    	}
    	public int Int(int a)
    	{
    		a=a+1;
    		return a;
    	}
    }
    
    
    
    package workhome3;
    
    public class Man extends Human {
    
    	//男人的方法
    		public void run()
    		{
    			System.out.println("可以跑快");
    		}
    		public void jump()
    		{
    			System.out.println("可以跳高");
    		}
    		public void eat()
    		{
    			System.out.println("吃饭睡觉打豆豆");
    		}
    	
    }
    
    
    
    package workhome3;
    
    public class Woman extends Human {
    
    	//女人的方法
    	public void run()
    	{
    		System.out.println("可以跑慢");
    	}
    	public void jump()
    	{
    		System.out.println("可以跳矮");
    	}
    }
    
    
    
    
    package workhome3;
    
    public class humantest {
    
    	public static void main(String[] args) {
    		
    		//向上转型
    		Human h1=new Man();
    		h1.setFoot("人有两条腿");
    		System.out.println(h1.getFoot());
    		h1.run();
    		System.out.println(h1.Int(10));
    		//向下转型
    		Man m1=(Man)h1;
    		m1.setFoot("男人有两条比女人长的腿");
    		System.out.println(m1.getFoot());
    		m1.jump();
    		System.out.println(m1.Int(10));
    		m1.eat();
    
    	}
    
    }
    

    6.按要求编写一个Java应用程序:

    1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。

    2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。

    3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。

    package workhome4;
    
    public class jivxing {
    //	包含有长、宽两种属性,和计算面积方法。
    	private double chang;
    	private double kuan;
    	public double getChang() {
    		return chang;
    	}
    	public void setChang(double chang) {
    		this.chang = chang;
    	}
    	public double getKuan() {
    		return kuan;
    	}
    	public void setKuan(double kuan) {
    		this.kuan = kuan;
    	}
    	//方法
    	public double mianji(double chang,double kuan)
    	{
    		this.chang=chang;
    		this.kuan =kuan;
    		return this.chang*this.kuan;
    	}
    }
    
    
    package workhome4;
    
    public class jivxing2 extends jivxing {
    	private double gao;
    	
    	public double getGao() {
    		return gao;
    	}
    
    	public void setGao(double gao) {
    		this.gao = gao;
    	}
    
    	public double tiji(double chang,double kuan,double gao)
    	{
    		this.gao=gao;
    		return super.mianji(chang, kuan)*this.gao;
    		
    	}
    
    }
    
    
    
    package workhome4;
    
    public class test {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		jivxing2 j=new jivxing2();
    		System.out.println(j.mianji(1, 2));
    		System.out.println(j.tiji(1, 2, 3));
    
    	}
    
    }
    

    7. 

    编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数

    wheels和车重weight。小车类CarVehicle的子类,其中包含的属性有载人数

    loader。卡车类TruckCar类的子类,其中包含的属性有载重量payload。每个

    类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功

    能。

    package workhome4;
    
    public class Vehicle {
    	//wheels和车重weight
    	private int wheel;//轮胎数量
    	private double weight;//车重
    	
    	public int getWheel() {
    		return wheel;
    	}
    	public void setWheel(int wheel) {
    		this.wheel = wheel;
    	}
    	public double getWeight() {
    		return weight;
    	}
    	public void setWeight(double weight) {
    		this.weight = weight;
    	}
    	//构造方法
    	public Vehicle(int wheel,double weight)
    	{
    		super();
    		this.wheel=wheel;
    		this.weight=weight;
    		
    	}
    	//方法
    	public void vv()
    	{
    		System.out.print("轮胎数:"+this.wheel+"  "+"车重:"+this.weight+"  ");
    	}
    
    }
    
    
    package workhome4;
    
    public class Car extends Vehicle {
    
    	
    	private int loader;//载人数
    	public int getLoader() {
    		return loader;
    	}
    	public void setLoader(int loader) {
    		this.loader = loader;
    	}
    	public Car(int wheel,double weight,int loader) {
    		super(wheel,weight);
    		this.loader=loader;
    		
    	}
    	//方法
    	public void cc()
    	{
    		System.out.println();
    		super.vv();
    		System.out.print("和载人数"+this.loader+"  ");
    	}
    	
    }
    
    
    package workhome4;
    
    public class Truck extends Car {
    
    	private double payload;//载重量
    
    	public double getPayload() {
    		return payload;
    	}
    
    	public void setPayload(double payload) {
    		this.payload = payload;
    	}
    
    	public Truck(int wheel, double weight, int loader, double payload) {
    		super(wheel, weight, loader);
    		this.payload=payload;
    	}
    //方法
    	public void tt()
    	{
    		super.cc();
    		System.out.println("载重量"+this.payload+"  ");
    	}
    	
    	
    }
    
    
    package workhome4;
    
    public class cartest {
    	public static void main(String[] args) {
    			Vehicle v=new Vehicle(4,99999);
    			v.vv();
    			Car c=new Car(4,888888,3);
    			c.cc();
    			Truck t=new Truck(4,777777,6,90000.888);
    			t.tt();
    	
    	}		
    }
    

    8.编写一个Shape类,具有属性:周长和面积;

    定义其子类三角形和矩形,分别具有求周长的方法。

    定义主类E,在其main方法中创建三角形和矩形类的对象,

    并赋给Shape类的对象ab,使用对象ab来测试其特性。

    package workhome5;
    
    public interface Shape {
    	public abstract void zc(int a,int b,int c);
    	public abstract void mj(int a,int b,int c);
    
    }
    
    
    package workhome5;
    
    public class sanjiaoxing implements Shape {
    
    	@Override
    	public void zc(int a,int b,int c) {
    		// TODO Auto-generated method stub
    		System.out.println("三角形的周长为:"+(a+b+c));
    	}
    
    	@Override
    	public void mj(int a,int b,int c) {
    		// TODO Auto-generated method stub
    		System.out.println("三角形的面积为:"+a*b/2);
    	}
    
    }
    
    
    package workhome5;
    
    public class jvxing implements Shape {
    
    	@Override
    	public void zc(int a, int b,int c) {
    		// TODO Auto-generated method stub
    		System.out.println("矩形的周长为:"+(a+b)*2);
    
    	}
    
    	@Override
    	public void mj(int a, int b, int c) {
    		// TODO Auto-generated method stub
    		System.out.println("矩形的面积为:"+2*(a+b));
    	}
    
    }
    
    package workhome5;
    
    public class cheng {
    
    	private Shape i;
    
    	public Shape getI() {
    		return i;
    	}
    
    	public void setI(Shape i) {
    		this.i = i;
    	}
    	public void getit()
    	{
    		i.mj(3, 2, 1);
    		i.zc(3, 2, 1);
    	}
    }
    
    
    package workhome5;
    
    public class test {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		cheng v=new cheng();
    		sanjiaoxing a=new sanjiaoxing();
    		v.setI(a);
    		v.getit();
    		jvxing b=new jvxing();
    		v.setI(b);
    		v.getit();
    	}
    
    }
    

      

     9.

    package jiekouhomework;
    
    public interface ShapePara {
    //	nt getArea():获得图形的面积。int getCircumference():获得图形的周长
    	public abstract double getArea();
    	public abstract double getCircumference();
    }
    
    
    
    package jiekouhomework;
    
    public class Circle implements ShapePara {
    
    //	adius:public 修饰的double类型radius,表示圆的半径。
    //	x:private修饰的double型变量x,表示圆心的横坐标。
    //	y:protected修饰的double型变量y,表示圆心的纵坐标。
    	public double radius;
    	private double x;
    	protected double y;
    	double PI=3.14;
    	
    	public double getRadius() {
    		return radius;
    	}
    
    	public void setRadius(double radius) {
    		this.radius = radius;
    	}
    
    //	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;
    //	}
    	
    	//构造方法
    	Circle(double radius) 
    	{
    		this.radius=radius;
    	}
    	//方法
    	public void setCenter(double x, double y)
    	{
    		this.x=x;
    		this.y=y;
    	}
    
    	@Override
    	public double getArea() {
    		// TODO Auto-generated method stub
    //		System.out.println(PI*radius*radius);
    		
    		return PI*this.radius*this.radius;
    	}
    
    	@Override
    	public double getCircumference() {
    		// TODO Auto-generated method stub
    //		System.out.println(2*PI*this.radius);
    		return 2*PI*this.radius;
    	}
    
    
    package jiekouhomework;
    
    public class jiehe {
    	private ShapePara s;
    
    	public ShapePara getS() {
    		return s;
    	}
    
    	public void setS(ShapePara s) {
    		this.s = s;
    	}
    	public void ceshi()
    	{
    		s.getArea();
    		s.getCircumference();
    	}
    
    }
    
    
    package jiekouhomework;
    
    public class test {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		jiehe ss=new jiehe();
    		Circle cc=new Circle(7);
    		ss.setS(cc);

             System.out.println(cc.getArea());
            System.out.println(cc.getCircumference());

    	}
    
    }
    

      

  • 相关阅读:
    【C#】枚举和字符串以及数字之间的互相转换
    MySQL中int(M)和tinyint(M)数值类型中M值的意义
    C# 将数组拼接为字符串 string.Join 的使用
    MySQL-locate()函数
    C# 4.0 dynamic用法,并且与 var, object的区别
    Go语言 go get 找不到 google.golang.org/protobuf/encoding/prototext 解决办法
    Go语言 中逗号ok模式
    MySQL数据库面试题(2020最新版)
    .Net Core 3.0开源可视化设计CMS内容管理系统建站系统
    SQL Server 全文搜索/全文索引
  • 原文地址:https://www.cnblogs.com/zhangnaitao/p/5897537.html
Copyright © 2011-2022 走看看