Lab Description:
Lab 5 is very similar to lab 4 in which both of the lab follow a hierarchy but don’t be fool. Two pea in pot isn’t always the same. One of the first thing lab 5 need is to create a abstract class and call it MobileDevice. So what is abstract class you ask? Well young grasshopper, an abstract class is a class where the keyword “abstract” is use. An eye opening right. The second part to the abstract class is that there might not be a methods to the class. Whoa!? Right, I know when i hear that my mind was blow! Now on to the meat of lab 5. Here is the hierarchy we need to follow:
The class: MobileDevice need to have private field : deviceType and need to have 3 methods: abstract setDeviceType(), concrete getDeviceType() and have an overridden version of toString() that will display these words ” Mobile Device”. OK got that, good. Moving on. After creating the abstract class, the next step is to create a set of concrete classes and they all need to inherit MobileDevice. Sound very familiar right? The other classes sound be the following Tablet, SmartPhone, iPad, and Android. The last part of the lab is to create a Java Application that contain an array of MobileDevice.
Code:
public abstract class MobileDevice { private String deviceType = "Mobile Device"; public abstract void setDeviceType(); public String getDeviceType() { return deviceType; } @Override public String toString() { return deviceType; } }
public class MobileDeviceApps { public static void main (String [] args){ MobileDevice [] mobileDevices = new MobileDevice[4]; mobileDevices [0] = new SmartPhone(); mobileDevices [1] = new Android(); mobileDevices [2] = new Tablet(); mobileDevices [3] = new Ipad(); for (int x = 0; x < 4; x++){ System.out.println("This device is: " + mobileDevices[x].getDeviceType()); } } }
public class SmartPhone extends MobileDevice { private String deviceType; public SmartPhone() { deviceType = "SmartPhone"; } @Override public String getDeviceType() { return deviceType; } @Override public void setDeviceType() { // TODO Auto-generated method stub deviceType = "SmartPhone"; } @Override public String toString() { return deviceType; } }
public class Ipad extends Tablet { private String deviceType; public Ipad() { deviceType = "iPad 3"; } @Override public String getDeviceType() { return deviceType; } @Override public void setDeviceType() { // TODO Auto-generated method stub deviceType = "iPad"; } @Override public String toString() { return deviceType; } }
public class Android extends SmartPhone { private String deviceType; public Android() { deviceType = "Samsung Galaxy 3"; } @Override public String getDeviceType() { return deviceType; } @Override public void setDeviceType() { // TODO Auto-generated method stub deviceType = "Andriod"; } @Override public String toString() { return deviceType; } }
public class Tablet extends MobileDevice { private String deviceType; public Tablet() { deviceType = "Tablet"; } @Override public String getDeviceType() { return deviceType; } @Override public void setDeviceType() { // TODO Auto-generated method stub deviceType = "Tablet"; } @Override public String toString() { return deviceType; } }