Javase—OOP基础习题2

来源:互联网 发布:ubuntu 16.04密码忘了 编辑:程序博客网 时间:2024/05/21 11:09

/** * 【练习题】01.类的成员变量:设计一个立方体类Box,定义三个属性,分别是长,宽,高。定义二个方法,分别计算并输出立方体的体积和表面积 */package com.Oracle.oop2;public class Box {int length;int width;int height;void cal() {int volume=length*width*height;int superficial=2*(length*width+length*height+width*height);System.out.println("立方体的体积为:"+volume);System.out.println("立方体的表面积为 :"+superficial);}}package com.Oracle.oop2;public class Text1 {public static void main(String[] args) {Box b=new Box();b.length=5;b.width=4;b.height=3;b.cal();}}

/** * 【练习题】02.类的成员变量:请定义一个交通工具(Vehicle)的类,其中有:属性:速度(speed),体积(size)等等方法:移动(move(int s)),设置速度(setSpeed(int speed)),加速speedUp(),减速speedDown()等等.最后在测试类Vehicle中的main()中实例化一个交通工具对象,并通过方法给它初始化speed,size的值,并且通过打印出来。另外,调用加速,减速的方法对速度进行改变。调用 move方法输出移动距离 */package com.Oracle.oop2;public class Verhicle {int speed,size;int move(int s) {speed+=s;return speed;}int setSpeed(int speed) {return this.speed=speed;}int speedUp() {return ++speed;}int speedDown() {return --speed;}}package com.Oracle.oop2;public class Text2 {public static void main(String[] args) {Verhicle v=new Verhicle();v.speed=50;v.size=5;System.out.println(v.speed);System.out.println(v.size);System.out.println(v.move(10));System.out.println(v.setSpeed(80));System.out.println(v.speedUp());System.out.println(v.speedDown());}}

/** * 【练习题】03.类的成员变量与方法,定义一个Hero类 * 属性有  power,name,分别代表体力值和英雄的名子,体力值默认为100;方法有  1.void go(); //行走的方法,如果体力值为0,则输出不能行走,此英雄已死亡的信息    2.void eat(int n); //吃的方法,参数是补充的血量,将 n的值加到属性power中,power的值最大为100,3.void hurt();//每受到一次伤害,体力值-10,体力值最小不能小于0,编写测试类,来测试以上代码 */package com.Oracle.oop2;public class Text3 {public static void main(String[] args) {Hero h=new Hero();h.power=0;h.name="盖伦";h.go();h.eat(110);System.out.println(h.power);h.hurt();System.out.println(h.power);}}package com.Oracle.oop2;public class Hero {//power代表体力值,默认值为100;name代表英雄的名字;int power=100;String name;void go() {if(power==0) {System.out.println("不能行走,您的英雄已死亡。");}}void eat(int n) {power+=n;if(power>100) {power=100;}}void hurt() {power-=10;if(power<0) {power=0;}}}

/** * 【练习题】04.方法的参数及返回值定义一个计算器;它的功能有加,减,乘,除,累加,阶乘求平方,求次方,判断一个数是否为素数;boolean(int),并写测试类来测试这个方法 */package com.Oracle.oop2;import java.util.Arrays;import java.util.Random;public class Text4 {public static void main(String[] args) {Cal c=new Cal();int[] a=new int[10];System.out.println(c.plus(3, 5));System.out.println(c.minus(80, 45));System.out.println(c.mutiply(84, 95));System.out.println(c.devide(89, 7));Random r=new Random();for(int i=0;i<10;i++) {a[i]=r.nextInt(20);}System.out.println(Arrays.toString(a));System.out.println(c.accumulation(a));System.out.println(c.factorial(5));System.out.println(c.square(13));System.out.println(c.power(3,3));System.out.println(c.isPrime(283));System.out.println(Arrays.toString(c.sort(a)));}}
Cal实现功能的代码

/** * 【练习题】05.构造方法:编写Java程序,用于显示人的姓名和年龄。定义一个人类(Person),该类中应该有两个私有属性,姓名(name)和年龄(age)。定义构造方法,用来初始化数据成员。再定义显示(display)方法,将姓名和年龄打印出来。在main方法中创建人类的实例,然后将信息显示。 */package com.Oracle.oop2;public class Person {String name;int age;public Person(String name,int age){this.name=name;this.age=age;}void display() {System.out.println("姓名:"+this.name);System.out.println("年龄:"+this.age);}}package com.Oracle.oop2;public class Text5 {public static void main(String[] args) {Person p=new Person("赵云", 28);p.display();}}

/** * 【练习题】06.get方法和set方法定义一个类,该类有一个成员变量,通过构造方法将其进行赋初值,并提供该成员的getXXX()和setXXX()方法提示:假设有String name;则有public void setName(String name){this.name = name;}public String getName(){return this.name;} */package com.Oracle.oop2;public class Member {String name;public Member(String name) {this.name=name;}public void setName(String name) {this.name=name;}public String getName() {return this.name;}}package com.Oracle.oop2;public class Text6 {public static void main(String[] args) {Member m=new Member("赵云");System.out.println(m.getName());m.setName("马超");System.out.println(m.getName());}}

/** * 【练习题】07.构造方法与重载为“无名的粉”写一个类:class WuMingFen 要求:1.有三个属性:面码:String theMa 粉的份量(两):int quantity是否带汤:boolean likeSoup2.写一个构造方法,以便于简化初始化过程,如:WuMingFen f1 = new WuMingFen("牛肉",3,true);3.重载构造方法,使得初始化过程可以多样化:WuMingFen f2 = new WuMingFen("牛肉",2);4.如何使得下列语句构造出来的粉对象是酸辣面码、2两、带汤的?WuMingFen f3 = new WuMingFen();5.写一个普通方法:check(),用于查看粉是否符合要求。即:将对象的三个属性打印在控制台上。 */package com.Oracle.oop2;public class WuMingFen {String theMa;int quantity;boolean likeSoup;//getter and setterpublic String getTheMa() {return theMa;}public void setTheMa(String theMa) {this.theMa = theMa;}public int getQuantity() {return quantity;}public void setQuantity(int quantity) {this.quantity = quantity;}public boolean isLikeSoup() {return likeSoup;}public void setLikeSoup(boolean likeSoup) {this.likeSoup = likeSoup;}//构造方法public WuMingFen(String theMa, int quantity, boolean likeSoup) {super();this.theMa = theMa;this.quantity = quantity;this.likeSoup = likeSoup;}public WuMingFen(String theMa, int quantity) {super();this.theMa = theMa;this.quantity = quantity;}//查看粉是否符合要求boolean check(int quantity) {if(quantity>1) {return true;}return false;}}package com.Oracle.oop2;public class Text7 {public static void main(String[] args) {WuMingFen f1 = new WuMingFen("牛肉",3,true);WuMingFen f2 = new WuMingFen("牛肉",1);WuMingFen f3 = new WuMingFen("酸辣面",2,true);System.out.print(f3.getTheMa()+",\t");System.out.print(f3.getQuantity()+",\t");System.out.println(f3.isLikeSoup());System.out.println(f1.check(f1.getQuantity()));System.out.println(f2.check(f2.getQuantity()));}}

/** * 【练习题】08.构造方法的重载:在程序中,经常要对时间进行操作,但是并没有时间类型的数据。那么,我们可以自己实现一个时间类,来满足程序中的需要。定义名为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)分别对时、分、秒进行加减运算 */package com.Oracle.oop2;public class MyTime {int hour,minute,second;public int getHour() {return hour;}public void setHour(int hour) {this.hour = hour;}public int getMinute() {return minute;}public void setMinute(int minute) {this.minute = minute;}public int getSecond() {return second;}public void setSecond(int second) {this.second = second;}public MyTime(int hour, int minute, int second) {super();this.hour = hour;this.minute = minute;this.second = second;}/** * 将时间信息打印出来 */public void display() {System.out.println("hour:"+this.hour+",minute:"+this.minute+",second:"+this.second);}public int addSecond(int sec) {return this.second+=sec;}public int addMinute(int min) {return this.minute+=min;}public int addHour(int hou) {return this.hour+=hou;}public int subSecond(int sec) {return this.second-=sec;}public int subMinute(int min) {return this.minute-=min;}public  int subHour(int hou) {return this.hour-=hou;}}package com.Oracle.oop2;public class Text8 {public static void main(String[] args) {MyTime m=new MyTime(16,44,53);m.display();m.addHour(3);m.display();m.addMinute(5);m.display();m.addSecond(2);m.display();m.subHour(5);m.display();m.subMinute(23);m.display();m.subSecond(19);m.display();}}

/** * 【练习题】09.构造方法与重载:定义一个牛肉面的类(Noodle),它的属性有,牛肉面宽度 width,尺寸:size(大碗还是小碗) 大碗8元,小碗6元,是否加肉:beef:加肉+4元,加蛋的数量:eggs:每个1元;定义构造方法来根据不同的条件创建不同的牛肉面Noodle(){  //不加肉,大碗,不加蛋,中宽;}Noodle(String width,int size)Noodle(String width,int size,boolean beef);Noodle(String width,int size,boolean beef,int eggs);再定义一个方法,用来显示当前牛肉面的信息,并显示金额;void  showNoodleInfo(); */package com.Oracle.oop2;public class Noodle {//牛肉面宽度 width,尺寸:size(大碗还是小碗) 大碗8元,小碗6元,是否加肉:beef:加肉+4元,加蛋的数量:eggs:每个1元;String width;int size;boolean beef;int eggs;public Noodle(boolean beef, int size, int eggs, String width) {super();this.width = width;this.size = size;this.beef = beef;this.eggs = eggs;}public Noodle(String width, int size) {super();this.width = width;this.size = size;}public Noodle(String width, int size, boolean beef) {super();this.width = width;this.size = size;this.beef = beef;}public Noodle(String width, int size, boolean beef, int eggs) {super();this.width = width;this.size = size;this.beef = beef;this.eggs = eggs;}void showNoodleInfo() {int money=0;System.out.println("宽度:"+this.width+",尺寸:"+this.size+",是否加肉:"+this.beef+",加蛋的数量:"+eggs);if(this.size==1) {//size==1表示大碗;money+=8;}else if(this.size==0) {//size==0表示小碗;money+=6;}else {System.out.println("输入错误,请输入0或1,1代表大碗,0代表小碗");}if(this.beef) {money+=4;}for(int i=0;i<eggs;i++) {money+=1;}System.out.println("您需要支付的金额为:"+money);}}package com.Oracle.oop2;public class Text9 {public static void main(String[] args) {Noodle n1=new Noodle(false,1,0,"中宽");n1.showNoodleInfo();}}

/** * 【练习题】10.构造方法与重载、包编写Addition类,该类中应包含一组实现两数相加运算的重载方法。实现加法运算的方法,应接受两个参数(即加数和被加数),方法将两个参数进行加法运算后,返回相加结果。考虑可能针对不同的数据类型进行计算,重载一组方法,包括整型、长整型、浮点型、双精度浮点型、还有字符串。 在main方法中创建Addition类的实例,分别调用重载方法测试其效果。 应将Addition类打入到包中,以自己名字的拼音为包命名。 */package com.Oracle.oop2;public class Addition {public int plus(int a,int b) {return a+b;}public long plus(long a,long b) {return a+b;}public float plus(float a,float b) {return a+b;}public double plus(double a,double b) {return a+b;}public String plus(String a,String b) {return a+b;}}package com.Oracle.oop2;public class Text10 {public static void main(String[] args) {Addition a=new Addition();System.out.println(a.plus(3, 5));System.out.println(a.plus(327478, 952846));System.out.println(a.plus(0.5, 3.7));System.out.println(a.plus(0.74521, 5.32145));System.out.println(a.plus("Hello ", "Java!"));}}

/** * 【练习题】11.构造方法与重载定义一个网络用户类,要处理的信息有用户ID、用户密码、email地址。在建立类的实例时,把以上三个信息都作为构造函数的参数输入,其中用户ID和用户密码时必须的,缺省的email地址是用户ID加上字符串"@gameschool.com" */package com.Oracle.oop2;public class NetUser {int id;String password,email;public NetUser(int id, String password, String email) {super();this.id = id;this.password = password;this.email = email;}public NetUser(int id, String password) {super();this.id = id;this.password = password;this.email=id+"gameschool.com";}public void display() {System.out.println("id:"+this.id+",password:"+this.password+",email:"+this.email);}}package com.Oracle.oop2;public class Text11 {public static void main(String[] args) {NetUser n1=new NetUser(1,"123AsD","1@gameschool.com");n1.display();NetUser n2=new NetUser(2,"456LkJ");n2.display();}}

/** * 【练习题】12.构造方法与重载,建立一个汽车类,包括轮胎个数,汽车颜色,车身重量等属性。并通过不同的构造方法创建事例。至少要求:汽车能够加速,减速,停车。要求:命名规范,代码体现层次,有友好的操作提示。 */package com.Oracle.oop2;public class Car {int tyre=4;String color;int weight=6;int speed=60;public Car(int tyre, String color) {super();this.tyre = tyre;this.color = color;}public Car(int tyre, String color, int weight) {super();this.tyre = tyre;this.color = color;this.weight = weight;}public Car(int tyre, String color, int weight, int speed) {super();this.tyre = tyre;this.color = color;this.weight = weight;this.speed = speed;}public void speedUp(int speed) {this.speed+=speed;}public void speedDown(int speed) {this.speed-=speed;}public void speedStop() {this.speed=0;}public void display() {System.out.println("tyre:"+this.tyre+",color:"+this.color+",weight:"+this.weight+",speed:"+this.speed);}}package com.Oracle.oop2;public class Text12 {public static void main(String[] args) {Car c1=new Car(4,"blue",5,50);Car c2=new Car(4,"yellow");Car c3=new Car(4,"red",4);c2.display();c3.display();c1.display();c1.speedUp(10);c1.display();c1.speedDown(8);c1.display();c1.speedStop();c1.display();}}

/** * 【练习题】13.构造方法与重载,创建一个类,为该类定义三个构造函数,分别执行下列操作:1、传递两个整数值并输出其中较大的一个值2、传递三个double值并计算出其乘积,将结果输出3、传递两个字符串值并将它们连接到一起后输出4、在main方法中测试构造函数的调用 */package com.Oracle.oop2;public class Print {int max;double sum;String connect;public Print(int a,int b) {max=a>b?a:b;System.out.println(a+"和"+b+"中较大的值为:"+max);}public Print(double a,double b,double c) {sum=a*b*c;System.out.println(a+","+b+","+c+"的乘积为:"+sum);}public Print(String a,String b) {connect=a+b;System.out.println(a+"和"+b+"连接在一起为:"+connect);}}package com.Oracle.oop2;public class Text13 {public static void main(String[] args) {@SuppressWarnings("unused")Print p1=new Print(23,41);@SuppressWarnings("unused")Print p2=new Print(3.5,9.4,6.7);@SuppressWarnings("unused")Print p3=new Print("Hello ","Java!");}}

/**【练习题】14.类的综合练习 * 1、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),取款方法withdraw(),存款方法deposit()。Accountprivate int idprivate double balanceprivate double annualInterestRatepublic Account (int id, double balance, double annualInterestRate )public int getId()public double getBalance()public double getAnnualInterestRate()public void setId( int id)public void setBalance(double balance)public void setAnnualInterestRate(double annualInterestRate)public void withdraw (double amount)public void deposit (double amount)提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。 */package com.Oracle.oop2;public class Account {private int id;protected double balance;//private double balance;private double annualInterestRate;//账号id,余额balance,年利率annualInterestRatepublic Account(int id, double balance, double annualInterestRate) {super();this.id = id;this.balance = balance;this.annualInterestRate = annualInterestRate;} public int getId() {return id;}public void setId(int id) {this.id = id;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance = balance;}public double getAnnualInterestRate() {return annualInterestRate;}public void setAnnualInterestRate(double annualInterestRate) {this.annualInterestRate = annualInterestRate;}public void withdraw (double amount) {if(balance>=amount) {this.balance-=amount;}else {System.out.println("余额不足,取款失败。");}}public void deposit (double amount) {this.balance+=amount;}public void display() {System.out.println("id:"+id+", balance:"+balance+", annualInterestRate:"+annualInterestRate);}public void displayBalance() {System.out.println("balance:"+this.getBalance());}public void displayBalanceAndAnnualInterestRate() {System.out.println("balance:"+this.getBalance()+", annualInterestRate:"+this.getAnnualInterestRate());}}
/** * 2. 创建Customer类。Customerprivate String firstNameprivate String lastNameprivate Account accountpublic Customer(String f,String l)public String getFirstName()public String getLastName()public Account getAccount()public void setAccount(Account account)a. 声明三个私有对象属性:firstName、lastName和account。b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f和l)c. 声明两个公有存取器来访问该对象属性,方法getFirstName和getLastName返回相应的属性。d. 声明setAccount 方法来对account属性赋值。e. 声明getAccount 方法以获取account属性。 */package com.Oracle.oop2;public class Customer {private String firstName;private String lastName;private Account account;public Customer(String firstName, String lastName) {super();this.firstName = firstName;this.lastName = lastName;}public String getFirstName() {return firstName;}public String getLastName() {return lastName;}public Account getAccount() {return account;}public void setAccount(Account account) {this.account = account;}public void display() {System.out.println("Customer ["+this.firstName+", "+this.lastName+"] has a account: id is "+account.getId()+", annualInterestRate is "+account.getAnnualInterestRate()*100+"%"+", balance is "+account.getBalance()+"\r\n");}}
/** * 3.写一个测试程序。(1)创建一个Customer ,名字叫 Jane Smith, 他有一个账号为1000,余额为2000元,年利率为 1.23% 的账户。(2)对Jane Smith操作。存入 100 元,再取出960元。再取出2000元。打印出Jane Smith 的基本信息成功存入 :100.0,成功取出:960.0,余额不足,取款失败Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.23%, balance is 1140.0 */package com.Oracle.oop2;public class Text14 {public static void main(String[] args) {//创建一个账户Account a=new Account(1000,2000,0.0123);//创建一个CustomerCustomer c=new Customer("Jane","Smith");//建立账户和Customer之间的关系c.setAccount(a);//存100元并打印基本信息a.deposit(100);a.display();//取960元并打印基本信息a.withdraw(960);a.display();//取 2000元并打印基本信息a.withdraw(2000);c.display();}}




原创粉丝点击