



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1. 按要求编写Java应用程序。(1)建立一个名叫Cat的类:属性:姓名、毛色、年龄 行为:显示姓名、喊叫(2)编写主类:创建一个对象猫,姓名为“妮妮”,毛色为“灰色”,年龄为2岁,在屏幕上输出该对象的毛色和年龄,让该对象调用显示姓名和喊叫两个方法。public class Cat String name;String color;int age;public void ShowNme()System.out.println(我叫+name);public void Bark()System.out.println(喵喵-);public class Test /* * param args */public static void main(String args) / TODO Auto-generated method stubCat cat=new Cat();=妮妮;cat.color=灰色;cat.age=2;cat.ShowNme();cat.Bark();System.out.println(毛色:+cat.color);System.out.println(年龄:+cat.age); 2. 按要求编写Java应用程序。(1)创建一个叫做People的类:属性:姓名、年龄、性别、身高行为:说话、计算加法、改名编写能为所有属性赋值的构造方法;(2)创建主类:创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80;让该对象调用成员方法:说出“你好!”public class People String name;int age;String sex;double height;public People(String name,int age,String sex,double height)=name;this.age=age;this.sex=sex;this.height=height;public void speak()System.out.println(你好!);public void add()public void rename()public class Test /* * param args */public static void main(String args) / TODO Auto-generated method stubPeople people=new People(张三,18,男,1.80);=张三;people.age=18;people.sex=男;people.height=1.80;people.speak();System.out.println(姓名:++n年龄:+people.age+n性别:+people.sex+n身高:+people.height);3利用接口做参数,写个计算器,能完成+-*/运算(1)定义一个接口Compute含有一个方法int computer(int n,int m);(2)设计四个类分别实现此接口,完成+-*/运算(3)设计一个类UseCompute,含有方法:public void useCom(Compute com, int one, int two)此方法要求能够:a用传递过来的对象调用computer方法完成运算 b输出运算的结果(4)设计一个测试类,调用UseCompute中的方法useCom来完成+-*/运算public interface Compute int computer(int n,int m);public class Compute1 implements Computepublic int computer(int n,int m)return n+m;public class Compute2 implements Computepublic int computer(int n,int m)return n-m;public class Compute3 implements Computepublic int computer(int n,int m)return n*m;public class Compute4 implements Computepublic int computer(int n,int m)return n/m;public class UseCompute public void useCom(Compute com,int one,int two)puter(one, two);public class Test /* * param args */public static void main(String args) / TODO Auto-generated method stubCompute c1=new Compute1();System.out.println(puter(10,2);Compute c2=new Compute2();System.out.println(puter(10, 2);Compute c3=new Compute3();System.out.println(puter(10, 2);Compute c4=new Compute4();System.out.println(puter(10, 2);4. 编写一个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个类的功能。public class People extends Monkeypublic People(String s)super(s);public void speak()System.out.println(小样的,不错嘛!会说话了!);public void think()System.out.println(别说话!认真思考!);public class E /* * param args */public static void main(String args) / TODO Auto-generated method stubMonkey monkey=new Monkey(a);monkey.speak();People people=new People(a);people.speak();people.think();public class Monkey public Monkey(String s)public void speak()System.out.println(咿咿呀呀.);5. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能。public class Vehicle int wheels;double weight;public Vehicle(int wheels,double weight)this.wheels=wheels;this.weight=weight;public void print()System.out.println(汽车:+n车轮有+wheels+个+n车重:+weight+吨);public class Car extends Vehicleint loader;public Car(int wheels,double weight,int loader)super(wheels,weight);this.loader=loader;public void print()System.out.println(汽车:+n车轮有+wheels+个+n车重:+weight+吨+n载人数:+loader);public class Truck extends Cardouble payload;public Truck(int wheels,double weight,int loader,double payload)super(wheels,weight,loader);this.payload=payload;public void print()System.out.println(汽车:+n车轮有+wheels+个+n车重:+weight+吨+n载人数:+loader);public class Test /* * param args */public static void main(String args) / TODO Auto-generated method stubVehicle vi=new Vehicle(4,2);vi.print();Car car=new Car(4,5,3);car.print();Truck truck=new Truck(6,3,5,6);truck.print();6. 实现如下类之间的继承关系,并编写Music类来测试这些类。public class Instrument public void play()System.out.println(弹奏乐器);public class Wind extends Instrument public void play()System.out.println(弹奏Wind);public void play2()System.out.println(调用wind的play2);public class Brass extends Instrumentpublic void play()System.out.println(弹奏Brass);public void play2()Syste
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年绥化市中医医院招聘模拟试卷及1套参考答案详解
- 2025湖南益阳市市直事业单位引进紧缺(急需)专业人才62人考前自测高频考点模拟试题及答案详解(易错题)
- 2025黑龙江哈尔滨市五常市万宝学校9大岗位招聘28人模拟试卷及完整答案详解一套
- 2025年甘肃交通职业技术学院招聘工作人员考前自测高频考点模拟试题及参考答案详解
- 浙江国企招聘2025浙江省低空产业发展有限公司招聘笔试历年参考题库附带答案详解
- 四川川藏工业园区发展有限责任公司公开招聘人选笔试历年参考题库附带答案详解
- 九江市赣北劳动保障事务代理所公开招聘劳务派遣工笔试历年参考题库附带答案详解
- 2025恒邦财产保险股份有限公司营业部招聘1人模拟试卷及答案详解(夺冠系列)
- 2025重庆江跳线轨道交通运营管理有限公司社会招聘笔试历年参考题库附带答案详解
- 2025重庆城市职业学院劳务派遣招聘笔试历年参考题库附带答案详解
- 2024年中国盐渍海带市场调查研究报告
- 新学期新起点励志奋斗青春初三毕业班开学第一课主题班会课件
- 分包单位与班组签订合同
- 盐酸右美托咪定鼻喷雾剂-临床用药解读
- 危险货物装载与卸载操作规程
- 《映山红》PPT课件(安徽省市级优课)-五年级音乐课件
- 林则徐课件完整版
- 投资学英文版课件Ch 3 Securities markets
- 氟喹诺酮类药物残留的检测课件
- 2021Z世代职场现状与趋势调研报告
- 全国编辑记者资格证考试复习资料
评论
0/150
提交评论