xoxobool

成功者,永远成功,失败者,永远失败,我要面对者,走向成功!

导航

类和对象的基础题

1.按要求编写Java应用程序:

1)编写西游记人物类(XiYouJiRenWu

其中属性有:身高(height),名字(name),武器(weapon

方法有:显示名字(printName),显示武器(printWeapon)

2)在主类的main方法中创建二个对象:zhuBaJiesunWuKong。并分别为他们的两个属性(name,weapon)赋值,最后分别调用printName, printWeapon方法显示二个对象的属性值。

public class XiYouJiRenWu {
                double height;
                String name;
                String Weapon;
                
                String printName()
                {
                    return name;
                }
                String printWeapon()
                {
                    return Weapon;
                }
}
public class TestXiYouJi {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        XiYouJiRenWu  xyj1=new XiYouJiRenWu();
        xyj1.name ="zhubajie";
        xyj1.Weapon="九齿钉耙";
        XiYouJiRenWu  xyj2=new XiYouJiRenWu();
        xyj2.name="sunwukong";
        xyj2.Weapon="如意金箍棒";
        System.out.println(xyj1.printName()+" "+xyj1.printWeapon());
        System.out.println(xyj2.printName()+" "+xyj2.printWeapon());
    }

}

 

 2.编写Java应用程序。首先定义一个描述银行账户的Account类,包括成员变量“账号”和“存款余额”,成员方法有“存款”、“取款”和“余额查询”。其次,编写一个主类,  在主类中测试Account类的功能。

 

public class Account {
    String account;

    double balance; 

    //构造方法
    Account(String id)
    {
        account = id;
    }
    //同时存钱
    Account(String id,double ck)
    {
        account = id;
        balance += ck;
    }
//成员方法
    
    //存钱
    //有参数无返回值
    void cunQian(double ck)
    {
        balance +=ck;
        
        System.out.println("存入"+ck);
    }
    //取钱
    //有参数有返回值
    boolean quQian(double qk)
    {
        if(qk <=balance)
        {
            balance -= qk;
            System.out.println("取出"+qk);
            return true;
        }
        else
        {
            System.out.println("余额不足");
            return false;
        }
    }
    //获取余额
    double getBalance()
    {
        return balance;
    }
    //显示账号和余额
    //无参数无返回值
    void showAccount()
    {
        System.out.println(" 账号= "+" "+account+" 余额 ="+balance);
    }


}

 

package com.hanqi.test;

public class TestAccount {
        public static void main(String[]args)
        {
            //测试Account
            Account account = new Account("3445",1000);
            account.showAccount();
            account.cunQian(100);
            account.quQian(30);
            account.showAccount();
            if(account.quQian(1080))
            {
                System.out.println("余额不足");
            }
            account.showAccount();
            
        

3.编写Java应用程序。首先,定义一个时钟类——Clock,它包括三个int型成员变量分别表示时、分、秒,一个构造方法用于对三个成员变量(时、分、秒进行初始化,还有一个成员方法show()用于显示时钟对象的时间。其次,再定义一个主类——TestClass,在主类的main方法中创建多个时钟类的对象,使用这些对象调用方法show()来显示时钟的时间。

public class Clock {
        int hour;
        int minute;
        int second;
        
        Clock(int h,int m,int s)
        {
             hour =h;
            minute =m;
            second =s;
        }
        void show()
        {
            System.out.println("时间为:"+hour+":"+minute+" :"+second+":");
        }
        
}
public class TextClock {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
    Clock clock = new Clock(10,23,21);
    clock.show();
    
    }

}

 

posted on 2016-09-19 10:30  春之林木  阅读(262)  评论(0编辑  收藏  举报