Java总练习.docx_第1页
Java总练习.docx_第2页
Java总练习.docx_第3页
Java总练习.docx_第4页
Java总练习.docx_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第一题:1、定义类Test1,在类中定义以下方法:定义方法isEven(int num)判断是否是偶数,返回类型为布尔类型。在main方法中输入一个整数,调用isEven方法,如果是偶数,输出“偶数,能被2整除”,否则输出“奇数,不能被2整除” 2、定义类Test2,在类中定义以下方法:定义方法max(int x,int y),求两个数的最大值。在main方法中输入三个数,调用max方法,输出三个数的最大值。2.1.package ct1;import java.util.Scanner;public class Test1 public boolean isEven(int num)if(num%2=0)return true;elsereturn false;public static void main(String args) Scanner input = new Scanner(System.in);System.out.println(请输入一个整数:);int num = input.nextInt();Test1 test = new Test1();if(test.isEven(num)System.out.println(是偶数);elseSystem.out.println(是奇数);2.package ct1;import java.util.Scanner;public class Test2 public int max(int x,int y)if(xy)return x;elsereturn y;public static void main(String args) int four;Scanner input=new Scanner(System.in);System.out.print(请输入三个整数:);int one=input.nextInt();int two=input.nextInt();int three=input.nextInt();Test2 test=new Test2();four=test.max(one, two);System.out.println(test.max(three, four);第二题:1、从键盘接收10个整数,统计正数的个数和负数的个数(用数组做)2、计算100以内奇数之和。2.1.package ct2;import java.util.Scanner;public class Test3 public static void main(String args) int num1 = new int10;int c1=0,c2=0;for(int i=1;i0) c1+;else c2+;System.out.println(正数的个数为+c1);System.out.println(负数的个数为+c2);2. package ct2;public class Test4 public static void main(String args) int sum=0;for(int i=0;i50)System.out.println(您的年龄已经超过50岁,不能当厨师); = name;this.age = age;this.phone = phone;this.food = food;public Cook() super();/ TODO Auto-generated constructor stub厨师测试类:package ct3;public class CookTest public void AdjustAge(Cook a)a.setAge(a.getAge()+1);a.shuchu();private void makefood() / TODO Auto-generated method stubSystem.out.println(做的菜是:);public static void main(String args) / TODO Auto-generated method stubCookTest c = new CookTest();c.AdjustAge(new Cook(王力,55,123456,湘菜);c.makefood();第四题:1、定义汽车(Mobile)类,具有属性name,具有行为:行驶(run)、刹车(stop)汽车包括:卡车(Truck)和轿车(Car)这些汽车行驶的行为各不相同(卡车烧柴油,轿车烧汽油),进行方法覆盖,刹车行为一致,不需要覆盖。2、根据实验三中定义的矩形类,定义子类RoundRectangle定义属性radius表示圆角半径,定义带三个参数的构造方法,参数分别是height,width和radius,调用父类Rectangle中带参数的构造方法实现。1. 父类:package ct4;public class Mobile private String name;public Mobile(String name) super(); = name;public Mobile() super();/ TODO Auto-generated constructor stubpublic void run()System.out.println(行驶);public void stop()System.out.println(刹车);子类:package ct4;public class Truck extends Mobile public Truck() super();/ TODO Auto-generated constructor stubpublic Truck(String name) super(name);/ TODO Auto-generated constructor stubpublic void run()System.out.println(卡车烧柴油);package ct4;public class Car extends Mobile public Car() super();/ TODO Auto-generated constructor stubpublic Car(String name) super(name);/ TODO Auto-generated constructor stubpublic void run()System.out.println(轿车烧汽油);测试类:package ct4;public class MobileTest public static void main(String args) / TODO Auto-generated method stubTruck t = new Truck();t.run();t.stop();System.out.println(n);Car c = new Car();c.run();c.stop();2. package ct66;public class Rectangle double height;double width;public double getHeight() return height;public void setHeight(double height) this.height = height;public double getWidth() return width;public void setWidth(double width) this.width = width;public Rectangle(double height, double width) super();this.height = height;this.width = width;public Rectangle() super();/ TODO Auto-generated constructor stubpublic double getArea()return height*width;public void show()System.out.println(矩形的长为:+height);System.out.println(矩形的宽为:+width);System.out.println(矩形的面积为:+getArea();package ct66;public class RoundRectangle extends Rectangleprivate double radius;public RoundRectangle(double height,double width,double radius)super(height,width);this.radius=radius;第五题:1、交通工具(Transportation)分为:汽车(Mobile)、飞机(Plain),各种交通工具的运输(transport)方法各不相同,编写一个测试类TransportationTest,要求如下。编写方法testTransport,对各种交通工具进行运输测试。要依据交通工具的不同,进行相应的运输。2、利用接口实现(1)的功能。1. package ct5;public class Transportation private String transportationName;public String getTransportationName() return transportationName;public void setTransportationName(String transportationName) this.transportationName = transportationName;public Transportation() super();/ TODO Auto-generated constructor stubpublic Transportation(String transportationName) super();this.transportationName = transportationName;public void Transport()System.out.println(transportationName+正在行驶);package ct5;public class Mobile extends Transportationpublic Mobile (String MobileName)super(MobileName);public void Transport()System.out.println(super.getTransportationName()+在陆地上运输);package ct5;public class Plain extends Transportationpublic Plain (String PlainName)super(PlainName);public void Transport()System.out.println(super.getTransportationName()+在空中运输);package ct5;public class TransportationTest public void testTransportation( Transportation t)t.Transport();public static void main(String args) / TODO Auto-generated method stubTransportationTest test = new TransportationTest();test.testTransportation(new Mobile(汽车);test.testTransportation(new Mobile(飞机);2. package ct55;public interface ITransportation public void transport();package ct55;public class Mobile implements ITransportation private String name;public Mobile(String name) super(); = name;Overridepublic void transport() / TODO Auto-generated method stubSystem.out.println(name+在陆地上运输);package ct55;public class Plain implements ITransportationprivate String name;public Plain(String name) super(); = name;Overridepublic void transport() / TODO Auto-generated method stubSystem.out.println(name+在空中运输);package ct55;public class TestTransport public void testTransport(ITransportation I1)I1.transport();public static void main(String args) / TODO Auto-generated method stubTestTransport T2=new TestTransport();T2.testTransport(new Mobile(汽车);T2.testTransport(new Plain(飞机);第六题:1、判断字符数组中的字符是否是字母、数字

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论