收藏 分销(赏)

Java面向对象编程练习题.doc

上传人:二*** 文档编号:4479550 上传时间:2024-09-24 格式:DOC 页数:26 大小:138KB 下载积分:5 金币
下载 相关 举报
Java面向对象编程练习题.doc_第1页
第1页 / 共26页
本文档共26页,全文阅读请下载到手机保存,查看更方便
资源描述
. . . . [练习题]01.类的成员变量: 猜数字游戏:一个类A有一个成员变量v,有一个初值100。定义一个类,对A类的成员变量v进行猜。如果大了则提示大了,小了则提示小了。等于则提示猜测成功。 import java.util.*; class A{ int v = 100; A(){ this.v = v; } } public class b01 { public static void main(String args[]){ A a = new A(); Scanner intput = new Scanner(System.in); int intputvalue = intput.nextlnt(); if(intputvalue>a.v) System.out.println("你的输入大于这个值"); if(intputvalue<a.v) System.out.println("你的输入小于这个值"); if(intputvalue==a.v) System.out.println("猜测成功"); } } [练习题]02.类的成员变量: 请定义一个交通工具(Vehicle)的类,其中有: 属性:速度(speed),体积(size)等等 方法:移动(move()),设置速度(setSpeed(int speed)),加速speedUp(),减速speedDown()等等. 最后在测试类Vehicle中的main()中实例化一个交通工具对象,并通过方法给它初始化speed,size的值,并且通过打印出来。另外,调用加速,减速的方法对速度进行改变。 class Vehicle {int speed; int size; void move(){ }; void setSpeed(int speed){ this.speed = speed; } void speedUp(){ speed = speed+1; this.speed=speed; } void speedDown(){ speed = speed-1; this.speed=speed; } } public class b01 { public static void main(String args[]){ Vehicle a = new Vehicle(); a.setSpeed(55); a.size = 80; a.speedUp(); System.out.println("Speed:"+a.speed+"\nsize:"+a.size); } } [练习题]03.类的成员变量与方法、构造方法 在程序中,经常要对时间进行操作,但是并没有时间类型的数据。那么,我们可以自己实现一个时间类,来满足程序中的需要。 定义名为MyTime的类,其中应有三个整型成员:时(hour),分(minute),秒(second),为了保证数据的安全性,这三个成员变量应声明为私有。 为MyTime类定义构造方法,以方便创建对象时初始化成员变量。 再定义diaplay方法,用于将时间信息打印出来。 为MyTime类添加以下方法: addSecond(int sec) addMinute(int min) addHour(int hou) subSecond(int sec) subMinute(int min) subHour(int hou) 分别对时、分、秒进行加减运算。 class MyTime { private int hour; private int minute; private int second; public MyTime(int hour,int minute,int second){ this.hour = hour; this.minute = minute; this.second = second; } void diaplay(){ System.out.println("时间:"+hour+":"+minute+":"+second ); } void addSecond(int sec){ second = second+sec; } void addMinute(int min){ minute = minute+min; } void addHour(int hou){ hour = hour+hou; } void subSecond(int sec){ second = second-sec; } void subMinute(int min){ minute = minute-min; } void subHour(int hou){ hour = hour-hou; } void Test(){ if(hour<=23&hour>=0&minute<=59&minute>=0&second<=59&second>=0) System.out.println("\nTrue\n"); else System.out.println("\nFalse\n"); } } public class b01 { public static void main(String args[]){ MyTime b = new MyTime(11,4,55); b.subHour(5); b.subMinute(5); b.subSecond(2); b.diaplay(); b.Test(); } } [练习题]04.构造方法 编写Java程序,模拟简单的计算器。 定义名为Number的类,其中有两个整型数据成员n1和n2,应声明为私有。编写构造方法,赋予n1和n2初始值,再为该类定义加(addition)、减(subtration)、乘(multiplication)、除(division)等公有成员方法,分别对两个成员变量执行加、减、乘、除的运算。 在main方法中创建Number类的对象,调用各个方法,并显示计算结果。 class Number { private int n1; private int n2; public Number(int n1,int n2){ this.n1 = n1; this.n2 = n2; } void addition(){ int equal = n1+n2; System.out.println("n1+n2="+equal); } void subtration(){ int equal = n1-n2; System.out.println("n1-n2="+equal); } void multiplication(){ int equal = n1*n2; System.out.println("n1*n2="+equal); } void division(){ int equal = n1/n2; System.out.println("n1/n2="+equal); } } public class b01 { public static void main(String args[]){ Number num = new Number(6,3); num.division(); } } [练习题]05.构造方法: 编写Java程序,用于显示人的和年龄。 定义一个人类(Person),该类中应该有两个私有属性,(name)和年龄(age)。定义构造方法,用来初始化数据成员。再定义显示(display)方法,将和年龄打印出来。 在main方法中创建人类的实例,然后将信息显示。 class Person { private String name; private int age; public Person(String name,int age){ this.name = name; this.age = age; } void display(){ System.out.println("——>"+name+"\n年龄——>"+age); } } public class b01 { public static void main(String args[]){ Person per = new Person("奥巴马",20); per.display(); } } [练习题]06.get方法和set方法 定义一个类,该类有一个私有成员变量,通过构造方法将其进行赋初值,并提供该成员的get##X()和set##X()方法 提示:假设有private String name;则有 public void setName(String name){ this.name = name; } public String getName(){ return this.name; } class Person { private String name; public Person (String n){ this.setName(n); } public void setName(String n){ name = n ; } public String getName(){ return name; } public void getInfo(){ System.out.println("名字:"+name); } } public class b01 { public static void main(String args[]){ Person per = new Person("奥巴马"); per.getName(); per.getInfo(); } } [练习题]07.构造方法与重载 为“无名的粉”写一个类:class WuMingFen 要求: 1.有三个属性:面码:String theMa 粉的分量(两):int quantity 是否带汤:boolean likeSoup 2.写一个构造方法,以便于简化初始化过程,如: WuMingFen f1 = new WuMingFen("牛肉",3,true); 3.重载构造方法,使得初始化过程可以多样化: WuMingFen f2 = new WuMingFen("牛肉",2); 4.如何使得以下语句构造出来的粉对象是酸辣面码、2两、带汤的? WuMingFen f3 = new WuMingFen(); 5.写一个普通方法:check(),用于查看粉是否符合要求。即:将对象的三个属性打印在控制台上。 class WuMingFen { String theMa; int quantitu; boolean likeSoup; public WuMingFen(){ } public WuMingFen(String theMa,int quantitu){ this.theMa = theMa; this.quantitu = quantitu; } public WuMingFen(String theMa,int quantitu,boolean likeSoup){ this(theMa,quantitu); this.likeSoup = likeSoup; } public void check(){ System.out.println("面码:"+theMa+"\n粉的份量:"+quantitu+"\n是否带汤:"+likeSoup); } } public class b01 { public static void main(String args[]){ WuMingFen f1 = new WuMingFen("牛肉",3,true); WuMingFen f2 = new WuMingFen("牛肉",2); WuMingFen f3 = new WuMingFen(); f1.check(); f2.check(); f3.check(); } } [练习题]08.构造方法的重载: 定义一个名为Vehicles(交通工具)的基类,该类中应包含String类型的成员属性brand(商标)和color(颜色),还应包含成员方法run(行驶,在控制台显示“我已经开动了”)和showInfo(显示信息,在控制台显示商标和颜色),并编写构造方法初始化其成员属性。 编写Car(小汽车)类继承于Vehicles类,增加int型成员属性seats(座位),还应增加成员方法showCar(在控制台显示小汽车的信息),并编写构造方法。 编写Truck(卡车)类继承于Vehicles类,增加float型成员属性load(载重),还应增加成员方法showTruck(在控制台显示卡车的信息),并编写构造方法。 在main方法中测试以上各类。 class Vehicles { private String brand; private String color; public Vehicles(String brand,String color){ this.brand = brand; this.color = color; } void run(){ System.out.println("我已经开动了"); } void showInfo(){ System.out.println("商标:"+brand+"\n颜色:"+color); } } class Car extends Vehicles { private int seats; public Car(String brand,String color,int seats){ super(brand,color); this.seats = seats; } void showCar(){ super.showInfo(); System.out.println("座位:"+seats); } } class Truck extends Vehicles{ private float load; public Truck(String brand,String color,float load){ super(brand,color); this.load = load; } void showTruck(){ super.showInfo(); System.out.println("载重:"+load); } } public class b01 { public static void main(String args[]){ Vehicles f1 = new Vehicles("宝马","白色"); Car f2 = new Car("法拉利","黑色",4); Truck f3 = new Truck("解放","绿色",5000); f1.run(); f1.showInfo(); f2.run(); f2.showCar(); f3.run(); f3.showTruck(); } } [练习题]09.构造方法与重载 定义一个网络用户类,要处理的信息有用户ID、用户密码、email地址。在建立类的实例时,把以上三个信息都作为构造函数的参数输入,其中用户ID和用户密码时必须的,缺省的email地址是用户ID加上字符串"gameschool." class NetUser { private String ID; private String key; private String emailDress; public NetUser(String ID,String key){ this.ID = ID; this.key = key; this.emailDress = ID+"gameschool."; } void output(){ System.out.println("ID:"+ID+"\n用户密码"+key+"\n用户地址"+emailDress); } } public class b01 { public static void main(String args[]){ NetUser user = new NetUser("5566","yan"); user.output(); } } [练习题]10.构造方法与重载、包 编写Addition类,该类中应包含一组实现两数相加运算的重载方法。 实现加法运算的方法,应承受两个参数(即加数和被加数),方法将两个参数进行加法运算后,返回相加结果。考虑可能针对不同的数据类型进行计算,重载一组方法,包括整型、长整型、浮点型、双精度浮点型、还有字符串。 在main方法中创建Addition类的实例,分别调用重载方法测试其效果。 应将Addition类打入到包中,以自己名字的拼音为包命名。 package yan.cong; class Addition{ public Addition(){ } void add(int a,int b){ int c = a+b; System.out.println("a+b="+c); } void add(long a,long b){ long c = a+b; System.out.println("a+b="+c); } void add(float a,float b){ float c = a+b; System.out.println("a+b="+c); } void add(double a,double b){ double c = a+b; System.out.println("a+b="+c); } void add(String a,String b){ String c = a+b; System.out.println("a+b="+c); } } public class b01 { public static void main(String args[]){ Addition m = new Addition(); Addition n = new Addition(); m.add(5,6); n.add("hello","world"); } } [练习题]11.构造方法与重载 将上次练习题三中编写的MyTime类打到以自己名字的拼音命名的包中,并为该类重载一组构造方法,以方便使用者能够以多种形式初始化该类的实例。 package yan.cong; class MyTime { private int hour; private int minute; private int second; public MyTime(int hour,int minute,int second){ this.hour = hour; this.minute = minute; this.second = second; } public MyTime(int minute,int second){ this.minute = minute; this.second = second; } void diaplay(){ System.out.println("时间:"+hour+":"+minute+":"+second ); } void addSecond(int sec){ second = second+sec; } void addMinute(int min){ minute = minute+min; } void addHour(int hou){ hour = hour+hou; } void subSecond(int sec){ second = second-sec; } void subMinute(int min){ minute = minute-min; } void subHour(int hou){ hour = hour-hou; } void Test(){ if(hour<=23&hour>=0&minute<=59&minute>=0&second<=59&second>=0) System.out.println("\nTrue\n"); else System.out.println("\nFalse\n"); } } public class b01 { public static void main(String args[]){ MyTime b = new MyTime(11,4,55); b.subHour(5); b.subMinute(5); b.subSecond(2); b.diaplay(); b.Test(); } } [练习题]12.构造方法与重载 建立一个汽车类,包括轮胎个数,汽车颜色,车身重量等属性。并通过不同的构造方法创建事例。 至少要求:汽车能够加速,减速,停车。 要求:命名规,代码表达层次,有友好的操作提示。 class Car { private int tires; private String color; private int load; public Car(int tires){ this.tires = tires; System.out.println("轮胎个数:"+tires); } public Car(String color){ this.color = color; System.out.println("汽车颜色:"+color); } public Car(int tires,String color,int load){ this(tires); this.color = color; System.out.println("汽车颜色:"+color); this.load = load; System.out.println("汽车重量:"+load); } void upSpeed(){ System.out.println("车正在加速"); } void downSpeed(){ System.out.println("车正在减速"); } void stop(){ System.out.println("车已经停止了"); } } public class b01 { public static void main(String args[]){ Car a = new Car(6); Car b = new Car(4,"白色",4000); a.upSpeed(); b.stop(); } } [练习题]13.构造方法与重载 创建一个类,为该类定义三个构造函数,分别执行以下操作: 1、传递两个整数值并找出其中较大的一个值 2、传递三个double值并求出其乘积 3、传递两个字符串值并检查其是否相同 4、在main方法中测试构造函数的调用 class Check { public Check(int a ,int b){ int c=Math.max(a,b); System.out.println("较大值为:"+c); } public Check(double a,double b,double c){ double d = a*b*c; System.out.println("a*b*c = "+d); } public Check(String a,String b){ boolean c = a.equals(b); System.out.println("两字符串相等:"+c); } } public class b01 { public static void main(String args[]){ Check m = new Check(4,8); Check n = new Check(3.3,4.2,4.0); Check p = new Check("ac","ab"); } } [练习题]14.Static关键字与普通变量的区别: 创建一个名称为StaticDemo的类,并声明一个静态变量和一个普通变量。对变量分别赋予10和5的初始值。在main()方法中输出变量值。 class StaticDemo { private static int a=10; private int b=5; public String getInfo(){ return "a="+a+"\nb="+b; } } public class b01 { public static void main(String args[]){ StaticDemo m = new StaticDemo(); System.out.println(m.getInfo()); } } [练习题]15.继承: 创建一个名称为MainPackage的包,使它包含MainClass和MainSubClass类。MainClass类应当包含变量声明,其值从构造函数中输出。MainSubClass类从MainClass派生而来。试执行以下操作:创建一个名称为SamePackage的类,使它导入上述包,并创建一个MainSubClass类的对象。 package MainPackage; class MainClass { String str; public MainClass(String str){ this.str = str; System.out.println(str); } } class MainSubClass extends MainClass{ public MainSubClass(String str){ super(str); System.out.println("***"); } } import MainPackage.MainSubClass; class SamePackage { MainSubClass b = new MainSubClass("a;ledkfjl"); } [练习题]16.包、继承、构造函数 创建一个名称为MainPackage的包,使它包含MainClass和MainSubClass类。MainClass类应当包含变量声明,其值从构造方法中输出。MainSubClass类从MainClass派生而来。试执行以下操作:创建一个名称为SamePackage的类,使它导入上述包,并创建一个MainSubClass类的对象。 [练习题]17.继承: 创建一个父类和子类,父类有一个数据成员,子类继承父类的该数据成员,通过构造函数初始化并显示该数据成员的值。 class Father { int a; public Father(int a){ this.a = a; } } class Son extends Father { public Son(int a){ super(a); System.out.println("a="+a); } } public class b01 { public static void main(String args[]){ Son e = new Son(4); } } [练习题]18.覆盖、继承 建立一个汽车Auto类,包括轮胎个数,汽车颜色,车身重量、速度等成员变量。并通过不同的构造方法创建实例。至少要求: 汽车能够加速,减速,停车。 再定义一个小汽车类Car,继承Auto,并添加空调、CD等成员变量,覆盖加速,减速的方法、class Auto { int tires; String color; int load; int speed; public Auto(int tires,String color,int load,int speed){ this.tires = tires; this.color = color; this.load = load; this.speed = speed; } void upSpeed(){ System.out.println("加速了"); } void downSpeed(){ System.out.println("减速了"); } void stop(){ System.out.println("停车了"); } void getInfo(){ System.out.println("轮胎数:"+tires+"\n颜色:"+color+"\n车身重量:"+load+"\n速度"+speed); } } class Car extends Auto { boolean Aircondition; boolean CD; public Car(int tires,String color,int load,int speed,boolean Aircondition,boolean CD){ super(tires,color,load,speed); this.Aircondition = Aircondition; this.CD = CD; } void upSpeed(){ System.out.println("开始加速了"); } void downSpeed(){ System.out.println("开始减速了"); } void getInfo(){ System.out.println("轮胎数:"+tires+"\n颜色:"+color+"\n车身重量:"+load+"t\n速度"+speed+"Km/h\n有空调:"+Aircondition+"\n有CD:"+CD); } } public class b01 { public static void main(String args[]){ Car m = new Car(4,"白色",50,100,false,true); m.downSpeed(); m.getInfo(); } } [练习题]19.抽象类与继承 设计一个能细分为矩形、三角形、圆形和椭圆形的“图形”类。使用继承将这些图形分类,找出能作为基类部分的共同特征(如校准点)和方法(如画法、初始化),并看看这些图形是否能进一步划分为子类。 此题只考虑实现思想,不考虑具体实现的细节,实现方式可自由发挥。 import java.awt.Point; abstract class Graph { public Point center; public Graph(Point center) { this.center = center; } public abstract void getInfo(); } class Rectangle extends Graph { private int length; private int width; public Rectangle(Point center,int length,int width){ super(center); this.length = length; this.width = width; } public void getInfo(){ System.out.println("矩形:\n"+"校准点:"+center +"\n矩形长度:"+length+"\n矩形宽度:"+width); } } class Circle extends Graph { private int radius; public Circle(Point center, int radius){ super(center); this.radius = radius; } public void getInfo(){ System.out.println("圆形:\n"+"校准点:"+center +"\n圆形半径:"+radius); } } public class b01 { public static void m
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服