Lab 3

Lab Description:

In this lab, we are trying to use inheritance to create multiple similar files instead of use copy-and-paste approach. By using the inheritance method, it greatly decrease the mistake that will occur when using copy-and-paste, also it saved a lot of times by doing this way.

Code:

MobileDevice.java:

//Create By Kawa Tsang
public class MobileDevice {    //create class MobileDevice

    private String deviceType;    //create private field deviceType

    public MobileDevice(){    //initialized to the string “Mobile Device” in the constructor
        setDeviceType("Mobile Device");
    }

    public void setDeviceType(String type){    //set deviceType
        deviceType = type;
    }

    public String getDeviceType(){    //get deviceType
        return deviceType;
    }
}

SmartPhone.java:

//Create By Kawa Tsang
public class SmartPhone extends MobileDevice {    //inherit MobileDevice
    private String deviceType;    //create private field deviceType
    public SmartPhone(){    //constructor
        deviceType= "Smart Phone";
    }

    @Override
    public String getDeviceType(){    //override get deviceType
        return super.getDeviceType() + "->" + deviceType;
    }

}

Android.java:

//Create By Kawa Tsang
public class Android extends SmartPhone {    //inherit SmartPhone
    private String deviceType;    //create private field deviceType
    public Android(){    //constructor
        deviceType= "Android";
    }

    @Override
    public String getDeviceType(){    //override get deviceType
        return super.getDeviceType() + "->" + deviceType;
    }

}

iPhone.java:

//Create By Kawa Tsang
public class iPhone extends SmartPhone {    //inherit SmartPhone
    private String deviceType;    //create private field deviceType
    public iPhone(){    //constructor
        deviceType= "iPhone";
    }

    @Override
    public String getDeviceType(){    //override get deviceType
        return super.getDeviceType() + "->" + deviceType;
    }

}

WindowsPhone.java:

//Create By Kawa Tsang
public class WindowsPhone extends SmartPhone {    //inherit SmartPhone
    private String deviceType;    //create private field deviceType
    public WindowsPhone(){    //constructor
        deviceType= "Windows Phone";
    }

    @Override
    public String getDeviceType(){    //override get deviceType
        return super.getDeviceType() + "->" + deviceType;
    }

}

MobileDeviceClient.java:

 public class MobileDeviceClient {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        MobileDevice myMobileDevice = new MobileDevice();
        SmartPhone mySmartPhone = new SmartPhone();
        iPhone myiPhone = new iPhone();
        Android myAndroid = new Android();
        WindowsPhone myWindowsPhone = new WindowsPhone();
        System.out.println(myMobileDevice.getDeviceType());
        System.out.println(mySmartPhone.getDeviceType());
        System.out.println(myiPhone.getDeviceType());        
        System.out.println(myAndroid.getDeviceType());
        System.out.println(myWindowsPhone.getDeviceType());

    }

}

Screenshot:

cet

Leave a Reply

Your email address will not be published. Required fields are marked *