Lab Description:
This lab is similar to the previous lab 4, where a hierarchy is formed where “MobileDevice” is the superclass, “Tablet” and “SmartPhone” are subclasses of “MobileDevice”, and then “iPad” branches off from the subclass of “Tablet” and finally “Android” branches off the subclass “SmartPhone”. Abstract classes are introduced in this lab which, provide a superclass from which other classes can inherit and thus share a common design. Concrete classes are also implemented for all the classes that inherited “MobileDevice”, these classes provide implementations of every method they declare (including those inherited). An array is also implemented the size is specified by the for loop in the application portion.
Code:
package Lab5;public class DeviceApp {
/**
* @param args
*/
public static void main (String[] args) {
// TODO Auto-generated method stub
//Array locations being assigned
MobileDevice[] mobileDevices = new MobileDevice[6];
mobileDevices[0] = new Tablet();
mobileDevices[1] = new iPad();
mobileDevices[2] = new KindleFire();
mobileDevices[3] = new SmartPhone();
mobileDevices[4] = new Android();
mobileDevices[5] = new WindowsPhone();//Array size is 6 since arrays start at 0
for (int i = 0; i < 6; i++) { System.out.println("This device is: " + mobileDevices[i].getDeviceType()); } } } ****************************************************************************************************** package Lab5; public abstract class MobileDevice { private String deviceType = "Mobile Device"; public abstract void setDeviceType(); public String getDeviceType() { // TODO Auto-generated method stub return deviceType; } @Override public String toString() { return deviceType; } } ******************************************************************************************************* package Lab5; 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; } } ******************************************************************************************************** package Lab5; public class iPad extends Tablet{ private String deviceType; public iPad() { deviceType = "iPad"; } @Override public String getDeviceType() { return deviceType; } @Override public void setDeviceType() { // TODO Auto-generated method stub deviceType = "iPad"; } @Override public String toString() { return deviceType; } } ************************************************************************************************************* package Lab5; public class KindleFire extends Tablet{ private String deviceType ; public KindleFire(){ deviceType= "KindleFire"; } @Override public String getDeviceType() { deviceType = "KindleFire"; return deviceType; } @Override public void setDeviceType() { // TODO Auto-generated method stub deviceType = "KindleFire"; } @Override public String toString() { return deviceType; } } *************************************************************************************************************** package Lab5; 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; } } ****************************************************************************************************************** package Lab5; public class Android extends SmartPhone { private String deviceType ; public Android(){ deviceType= "Android"; } @Override public String getDeviceType() { deviceType = "Android"; return deviceType; } @Override public void setDeviceType() { // TODO Auto-generated method stub deviceType = "Android"; } @Override public String toString() { return deviceType; } } ********************************************************************************************************************* package Lab5; public class WindowsPhone extends SmartPhone { private String deviceType ; public WindowsPhone(){ deviceType= "WindowsPhone"; } @Override public String getDeviceType() { deviceType = "WindowsPhone"; return deviceType; } @Override public void setDeviceType() { // TODO Auto-generated method stub deviceType = "WindowsPhone"; } @Override public String toString() { return deviceType; } } Screenshots: