Lab 3

Description: Create 5 classes called MobileDevice, SmartPhone, Android, iPhone, WindowsPhone, with a private field deviceType that will be initialized to the string “Mobile Device in the constructor including the corresponding setter and getter methods. The program will shows the result as:
Mobile Device –> SmartPhone –> Android
Mobile Device –> SmartPhone –> iPhone
Mobile Device –> SmartPhone –> WindowsPhone

Code:

Main

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());

       }

}

 

Mobile Device

public class MobileDevice {

private String deviceType = “Mobile Device”;

public void deviceType() {

deviceType = “Mobile Device”;

}

public String getDeviceType(){

return this.deviceType;

}

}

 

Smart Phone

public class SmartPhone extends MobileDevice {

private String mySmartPhone = “SmartPhone”;

public SmartPhone(){

mySmartPhone = “SmartPhone”;

}

public void setDeviceType(){

this.mySmartPhone = “SmartPhone”;

}

public String getDeviceType(){

return super.getDeviceType() + ” –> ” + mySmartPhone;

}

}

 

Android

public class Android extends SmartPhone{

private String myAndroid = “Android”;

public Android(){

myAndroid = “Android”;

}

public void setDeviceType(){

this.myAndroid = “Android”;

}

public String getDeviceType(){

return super.getDeviceType()+ ” –> ” + myAndroid;

}

}

 

iPhone

public class iPhone extends SmartPhone  {

private String myiPhone = “iPhone”;

public iPhone(){

myiPhone = “iPhone”;

}

public void setDeviceType(){

this.myiPhone = “iPhone”;

}

public String getDeviceType(){

return super.getDeviceType()+ ” –> ” + myiPhone;

}

}

 

Windows Phone

public class WindowsPhone extends SmartPhone {

private String myWindowsPhone = “WindowsPhone”;

public WindowsPhone(){

myWindowsPhone = “WindowsPhone”;

}

public void setDeviceType(){

this.myWindowsPhone = “WindowsPhone”;

}

public String getDeviceType(){

return super.getDeviceType()+ ” –> ” + myWindowsPhone;

}

}

 

3

Leave a Reply

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