JavaSE面向对象练习

来源:互联网 发布:淘宝卖家花呗开通好吗 编辑:程序博客网 时间:2024/04/27 00:03

题1:

创建一个Person类,声明属性和行为,在测试类中实例化对象,并通过调用对象的方法打印以下内容:

“小王是一个男胖子,体重200.0kg。某天他开始减肥,经过不懈努力,减了40kg。减肥后的体重是160.0kg。”

public class Test {
public static void main(String[] args) {

Person p = new Person("小王",'男',200);
p.jianfei(40);

Person p2 = new Person("铁锤",'女',260);
p2.jianfei(42);
}
}



public class Person {
public String name;
public char sex;
public double weight;

public Person(){

}

//初始化成员变量
public Person(String name, char sex, double weight) {
this.name = name;
this.sex = sex;
this.weight = weight;
}


public void jianfei(double i){
String s = "他";
if(sex == '女'){
s = "她";
}

System.out.println(name+"是一个"+sex+"胖子,体重"+weight+"kg。某天"+s+"开始减肥,经过不懈努力,减了"+i+"kg。减肥后的体重是"+(weight-i)+"kg。");
}
}



题2:

创建一个圆Circle类。

为该类提供一个变量r表示半径,一个常量PI表示圆周率;

同时为该类提供两个方法:方法一用于求圆的面积,方法二用于求圆的周长;

为该类提供一个无参的构造方法,用于初始化r的值为4。

public class Circle {
      public double r;
      public static final double PI =3.14;
      
      public double Area(){
    return PI*r*r;
      }
      
      public double perimete(){
     return PI*2*r;
      }

public Circle() {
this.r = 4;
}

public Circle(double r){
this.r = r;
}

}


public class Test {


public static void main(String[] args) {
Circle c = new Circle();
 
double Area = c.Area();
double perimete = c.perimete();
System.out.println("面积="+Area);
System.out.println("周长="+perimete);
}
}


题3:

为“无名的粉”写一个类: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(),用于查看粉是否带汤,并且把内容打印出来。

 

即:将对象的三个属性打印在控制台上。打印结果如下:

 

牛肉面,3两,带汤

牛肉面,2两,不带汤

酸辣面,4两,带汤


public class WuMingFen {
public String theTea;
public int quantity;
public boolean likeSoup;

public WuMingFen(){
this("酸辣",2,true);
}


public WuMingFen(String theTea, int quantity, boolean likeSoup) {
this.theTea = theTea;
this.quantity = quantity;
this.likeSoup = likeSoup;
}



public WuMingFen(String theTea, int quantity) {
this.theTea = theTea;
this.quantity = quantity;

}


public void check(){
if(likeSoup){
System.out.println(theTea + "面," +" " + quantity + "两," + "带汤");
}else{
System.out.println(theTea + "面," + "   " + quantity + "两," + "不带汤");
}
}
}

public class Test {
public static void main(String[] args) {
WuMingFen  f1 = new WuMingFen("牛肉",3,true);
f1.check();
WuMingFen  f2 = new WuMingFen("牛肉",2);
f2.check();
WuMingFen  f3 = new WuMingFen("酸辣",4,true);
f3.check();
}
}


题4:

创建一个账户Account类,该类有id:账户号码(字符串类型),password:账户密码(int),name:真实姓名(字符串类型),

personId:身份证号码(字符串类型),email:客户的电子邮箱(字符串类型),balance:账户余额(double)。

存款方法:deposit(double balance),取款方法:withdraw(doublebalance)。构造方法:有参和无参,有参构造方法用于设置必要的属性

并且通过test()方法将所有成员变量打印出来,调用存款和取款方法


import java.util.Date;


public class Account {
public String id;
public String password;
public String name;
public String personId;
public String email;
public double balance;

public Account(){

}

public Account( String password, String name, String personId, String email, double balance) {
super();
this.id = String.valueOf(new Date().getTime());
this.password = password;
this.name = name;
this.personId = personId;
this.email = email;
this.balance = balance;
}

public void cun(double money){
balance += money;
System.out.println("账户:"+id+"存了"+money+"元,当前余额为"+balance+"元");
}

public void qu(double money){
if(balance < money){
System.out.println("余额不足");
}else{
balance -= money;
System.out.println("账户:"+id+"取了"+money+"元,当前余额为"+balance+"元");
}
}

@Override
public String toString() {
return "Account [id=" + id + ", password=" + password + ", name=" + name + ", personId=" + personId + ", email="
+ email + ", balance=" + balance + "]";
}


}



public class Test {

public static void main(String[] args) {
Account a = new Account("000000", "张三", "123456", "111@qq.com", 10);
System.out.println(a.toString());

a.cun(100);
a.qu(50);

}
}

题5:

食物类,动物类,饲养员类,测试类。实现饲养员给动物喂食物。

食物类:    属性name

动物类:    属性name方法void eat

饲养员类:  属性name方法void feed

测试类打印的结果:

    张三喂大熊猫

    大熊猫吃竹笋

public class Foods {
public String name;
}


public class Animal {

public String name;

public void eat(){

}
}


public class Breeder {
public String name;

public void feed(){
System.out.println(name+" " );
}
}


public class Test {
public static void main(String[] args) {
Animal A = new Animal();
A.name = "大熊猫";

Foods f = new Foods();
f.name = "竹子";

Breeder b = new Breeder();
b.name = "张三";

System.out.println(b.name+"wei"+A.name);
System.out.println(A.name+"chi"+f.name);
}

}

题6:

某公司的雇员分为以下若干类:

Employee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:void getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励1000元。

SalariedEmployee:Employee的子类,拿固定工资的员工。属性:月薪

HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数

SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率

BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。属性:底薪。


public class Employee {
public String name;
public int month;
public int birthday;

public void getSalary(int month){

}
}


public class SalariedEmployee extends Employee{

public double yx;

@Override
public void getSalary(int month){

if(birthday==month){
System.out.println(name+month+"月份的工资为"+(yx+1000)+"元");
}else{
System.out.println(name+month+"月份的工资为"+yx+"元");
}
}
}


public class HourlyEmployee extends Employee  {
public double hour;
public double hourMoney;

@Override
public void getSalary(int month) {
double money;
if(hour<=160){
money = hour*hourMoney;

}else{
money =  160*hourMoney+(hour-160)*hourMoney*1.5;
}

if(birthday==month){
System.out.println(name+month+"月份的工资为"+(hourMoney+1000)+"元");
}else{
System.out.println(name+month+"月份的工资为"+hourMoney+"元");
}
}
}


public class SalesEmployee extends Employee{
public double sales;
public double rate;
@Override
public void getSalary(int month) {
if(birthday == month){
System.out.println(name+month+"月份的工资为"+(sales*rate+1000)+"元");
}else{
System.out.println(name+month+"月份的工资为"+(sales*rate)+"元");
}
}
}



public class BasePlusSalesEmployee extends SalesEmployee{


public double dx;
@Override
public void getSalary(int month) {
if(birthday == month){
System.out.println(name+month+"月份的工资为"+(sales*rate+1000)+"元");
}else{
System.out.println(name+month+"月份的工资为"+(sales*rate)+"元");
}
}
}

题7:

设计一个台灯类(Lamp)其中台灯有灯泡类(Buble)这个属性,还有开灯(on)这个方法。设计一个灯泡类(Buble),灯泡类有发亮的方法,其中有红灯泡类(RedBuble)和绿灯泡类(GreenBuble)他们都继承灯泡类(Buble)一个发亮的方法,请设计出一段代码可以使台灯开启灯泡发亮,并且保证替换不同种类的灯泡,台灯类代码不被修改。

public class Lamp {


public Buble b;// Buble b = new Buble();   Buble b = new RedBuble();  Buble b = new GreenBuble();

public Lamp(Buble b){
this.b = b;
}

//开灯方法
public void on(){
b.faliang();//调用子类重写后的方法
}
}


public class Buble {
public void faliang(){
System.out.println("发亮。。。");
}
}


public class GreenBuble extends Buble{
@Override
public void faliang() {
System.out.println("发绿光。。。。");
}
}



public class RedBuble  extends Buble {


@Override
public void faliang() {
System.out.println("发红光。。。");
}
}


public class Test {
public static void main(String[] args) {

Buble red = new RedBuble();
Buble green = new GreenBuble();
Lamp l = new Lamp(green);
l.on();
}
}

题8:

设计一个游戏角色类,

属性:角色姓名、性别、等级、生命值;

方法:发大招,需要传递两个参数,被打击对象和杀伤力,根据杀伤力的值,减去相应的被打击对象的生命值;

游戏场景:创建两个游戏角色,荆轲和妲己,荆轲调用发大招方法,攻击妲己,妲己生命值减少。

public class Game {
public String name;
public char sex;
public int lv;
public int smz;//生命值

public void fdz(String dj ,int ssl){
System.out.println(name+"攻击"+dj+","+dj+"生命值-"+ssl+"剩余生命值="+(smz-ssl));
}

public Game(String name, char sex) {
this.name = name;
this.sex = sex;
this.lv = 1;
this.smz = 100;
}
}



public class Test {
public static void main(String[] args) {
Game g = new Game("荆轲",'男');

g.fdz("妲己",10);
Game g2  = new Game("妲己",'女');

}
}