Java习题——方法与构造方法练习(二)

【练习题】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(),用于查看粉是否符合要求。即:将对象的三个属性打印在控制台上。

/*【练习题】07.构造方法与重载
为“无名的粉”写一个类:class WuMingFen 要求:*/
package cn.edu.nefu1;

public class WuMingFen {
    /*1.有三个属性:面码:String theMa 粉的份量(两):int quantity
是否带汤:boolean likeSoup*/
    String theMa;
    int quantity;
    boolean likeSoup;
    //2.写一个构造方法,以便于简化初始化过程,如:
    //WuMingFen f1 = new WuMingFen("牛肉",3,true);
    WuMingFen(String theMa,int quantity,boolean likeSoup){
        this.theMa = theMa;
        this.quantity = quantity;
        this.likeSoup = likeSoup;
    }

    //3.重载构造方法,使得初始化过程可以多样化:
    //WuMingFen f2 = new WuMingFen("牛肉",2);
    WuMingFen(String theMa,int quantity){
        this.theMa = theMa;
        this.quantity = quantity;
    }

    //4.如何使得下列语句构造出来的粉对象是酸辣面码、2两、带汤的?
    //WuMingFen f3 = new WuMingFen();
    WuMingFen(){
        this.theMa = "酸辣面码";
        this.quantity = 2;
        this.likeSoup = true;
    }

    //5.写一个普通方法:check(),用于查看粉是否符合要求。即:将对象的三个属性打印在控制台上。
    void check(){
        System.out.println("面码:"+theMa+"\t粉的份量:"+quantity+"两\t是否带汤:"+likeSoup);
    }

    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();
        f3.check();
    }



}

【练习题】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)

分别对时、分、秒进行加减运算。

/*【练习题】08.构造方法的重载:
在程序中,经常要对时间进行操作,但是并没有时间类型的数据。那么,我们可以自己实现一个时间类,来满足程序中的需要。*/
package cn.edu.nefu1;
//定义名为MyTime的类
public class MyTime {
//    其中应有三个整型成员:时(hour),分(minute),秒(second)
    int hour;
    int minute;
    int second;
//    为MyTime类定义构造方法,以方便创建对象时初始化成员变量。
    MyTime(int hour,int minute,int second){
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }
//    再定义diaplay方法,用于将时间信息打印出来。
    void dispaly(){
        System.out.println(hour+"时"+minute+"分"+second+"秒");
    }
//    为MyTime类添加以下方法:   分别对时、分、秒进行加减运算。
    void addSecond(int sec){
        minute += (second+sec)/60;
        second = (second+sec)%60;
        hour += minute/60;
        minute = minute%60;
        hour = hour%24;
        dispaly();      //打印时间
    }
    void addMinute(int min){
        hour += (minute+min)/60;
        minute = (minute+min)%60;
        hour = hour%24;
        dispaly();
    }
    void addHour(int hou){
        hour = (hour+hou)%24;
        dispaly();
    }
    void subSecond(int sec){
        if(second >= (sec%60)){
            second -= sec%60;
        }else{
            second = 60+second-sec%60;
            minute--;
            if(minute == -1){
                minute = 59;
                hour--;
                if(hour == -1){
                    hour = 23;
                }
            }
        }
        if(minute >= sec/60%60){
            minute -= sec/60%60;
        }else {
            minute = 24+minute-sec/60%60;
            hour --;
            if(hour == -1){
                hour = 23;
            }
        }
        if(hour >= sec/60/60%24){
            hour -= sec/60/60%24;
        }else {
            hour = 24+hour-sec/60/60%24;
        }
        dispaly();
    }
    void subMinute(int min){
        if(minute >= min%60){
            minute -= min%60;
        }else {
            minute = 60+minute-min%60;
            hour--;
            if(hour == -1){
                hour = 23;
            }
        }
        if(hour >= min/60%24){
            hour -= min/60%24;
        }else{
            hour = 24+hour-min/60%24;
        }
        dispaly();
    }
    void subHour(int hou){
        if(hour >= hou%24){
            hour -= hou%24;
        }else{
            hour = 24+hour-hou%24;
        }
        dispaly();
    }

    public static void main(String[] args) {
        MyTime time = new MyTime(20,23,34);
        time.addSecond(110);
        time.addMinute(100);
        time.addHour(90);
        time.subSecond(80);
        time.subMinute(70);
        time.subHour(60);

    }

}

【练习题】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();

/*【练习题】09.构造方法与重载*/
package cn.edu.nefu1;

//    定义一个牛肉面的类(Noodle)
public class Noodle {
//    它的属性有:牛肉面宽度 width,尺寸:size  (大碗还是小碗) 大碗8元,小碗6元,是否加肉:beef   :加肉+4元,
//    加蛋的数量:eggs  :每个1元;
    String width;
    int size;
    boolean beef;
    int eggs;
//    定义构造方法来根据不同的条件创建不同的牛肉面
    Noodle(){
       //不加肉,大碗,不加蛋,中宽;
        this.beef = false;
        this.size = 8;
        this.eggs = 0;
        this.width = "中宽";
    }
    Noodle(String width,int size){
        this.width = width;
        this.size = size;
    }
    Noodle(String width,int size,boolean beef){
        this.width = width;
        this.size = size;
        this.beef = beef;
    }
    Noodle(String width,int size,boolean beef,int eggs){
        this.width = width;
        this.size = size;
        this.beef = beef;
        this.eggs = eggs;
    }
//    再定义一个方法,用来显示当前牛肉面的信息,并显示金额;
    void  showNoodleInfo(){
        int money;
        money = size+eggs;

        if(size == 6){
            if(beef == true){
                money += 4;
                System.out.println(width+",小碗,加肉,加"+eggs+"个蛋.金额是:"+money);
            }else {
                System.out.println(width + ",小碗,不加肉,加" + eggs + "个蛋.金额是:"+money);
            }
        }else {
            if(beef == true){
                money += 4;
                System.out.println(width+",大碗,加肉,加"+eggs+"个蛋.金额是:"+money);
            }else {
                System.out.println(width+",大碗,不加肉,加" + eggs + "个蛋.金额是:"+money);
            }
        }
    }

    public static void main(String[] args) {
        Noodle noodle1 = new Noodle();
        noodle1.showNoodleInfo();
        Noodle noodle2 = new Noodle("细",6);
        noodle2.showNoodleInfo();
        Noodle noodle3 = new Noodle("中宽",8,true);
        noodle3.showNoodleInfo();
        Noodle noodle4 = new Noodle("宽",6,true,2);
        noodle4.showNoodleInfo();
    }
}

【练习题】10.构造方法与重载、包

编写Addition类,该类中应包含一组实现两数相加运算的重载方法。

实现加法运算的方法,应接受两个参数(即加数和被加数),方法将两个参数进行加法运算后,返回相加结果。考虑可能针对不同的数据类型进行计算,重载一组方法,包括整型、长整型、浮点型、双精度浮点型、还有字符串。 在main方法中创建Addition类的实例,分别调用重载方法测试其效果。 应将Addition类打入到包中,以自己名字的拼音为包命名。

/*10.构造方法与重载、包
编写Addition类,该类中应包含一组实现两数相加运算的重载方法。
实现加法运算的方法,应接受两个参数(即加数和被加数),方法将两个参数进行加法运算后,返回相加结果。
考虑可能针对不同的数据类型进行计算,重载一组方法,包括整型、长整型、浮点型、双精度浮点型、还有字符串。
在main方法中创建Addition类的实例,分别调用重载方法测试其效果。 应将Addition类打入到包中,以自己名字的拼音为包命名。*/
package cn.edu.nefu1;

public class Addition {
    //整型
    int add(int a,int b){
        return a+b;
    }

    //长整型
    long add(long a,long b){
        return a+b;
    }

    //浮点型
    float add(float a,float b){
        return a+b;
    }

    //双精度浮点型
    double add(double a,double b){
        return a+b;
    }

    //字符串
    String add(String a,String b){
        return a+b;
    }

    public static void main(String[] args) {
        Addition addition = new Addition();
        System.out.println(addition.add(1,2));
        System.out.println(addition.add(1234,12121));
        System.out.println(addition.add(12.1,22));
        System.out.println(addition.add(123.234,23.12));
        System.out.println(addition.add("123","123"));

    }
}

【练习题】11.构造方法与重载

定义一个网络用户类,要处理的信息有用户ID、用户密码、email地址。在建立类的实例时,把以上三个信息都作为构造函数的参数输入,其中用户ID和用户密码时必须的,缺省的email地址是用户ID加上字符串"@gameschool.com"

/*定义一个网络用户类,要处理的信息有用户ID、用户密码、email地址。
在建立类的实例时,把以上三个信息都作为构造函数的参数输入,其中用户ID和用户密码时必须的,
缺省的email地址是用户ID加上字符串"@gameschool.com"*/
package cn.edu.nefu1;

public class Netuser {
    String ID;
    String password;
    String email;

    Netuser(String ID,String password){
        this.ID = ID;
        this.password = password;
        this.email = ID+"@gameschool.com";
    }

    public static void main(String[] args) {
        Netuser netuser = new Netuser("nefu","123456");
        System.out.println(netuser.ID);
        System.out.println(netuser.email);
    }

}

猜你喜欢

转载自blog.csdn.net/sunshinecollege/article/details/85369374