Lab 3

Description:

After being introduced to Inheritance in Lecture 5, its form of software reuse was required for Lab 3. Lab 3 was about creating new classes (subclasses); one of which will inherit from an existing class (superclass) and three more subclasses that will inherit from that one subclass. Inheriting classes will inherit existing class’s methods, objects, instance variables, etc. Each of these classes were created and named. The superclass was called MobileDevice and its inheriting subclass was called SmartPhone. SmartPhone’s inheriting subclasses were called Android, iPhone, and WindowsPhone. Given an application, it had to be made sure that the getter method of each subclass were properly overridden so that the application would display the correct output once executed. The application would have to display the class names corresponding to the correct classes (superclass included). In other words, MobileDevice would have to display “Mobile Device”, SmartPhone would have to display “Mobile Device -> Smart Phone”, iPhone would have to display “Mobile Device -> Smart Phone -> iPhone”, etc. Overall, inheritance saved developed time and made it easier to implement and maintain the system effectively.

Code:

package edu.cuny.citytech.CET3640.f13.Lab3;

public class MobileDevice extends Object
{

	private String deviceType;

	public MobileDevice()
	{

		deviceType = "Mobile Device";
	}

	public void setDeviceType(String deviceType) 
	{
		this.deviceType = deviceType;
	}

	public String getDeviceType() {
		return deviceType;
	}

}
package edu.cuny.citytech.CET3640.f13.Lab3;

public class SmartPhone extends MobileDevice
{
	private String deviceType;

	public SmartPhone()
	{

		deviceType = "Smart Phone";
	}

@Override

	public String getDeviceType() 
	{
		return String.format("Mobile Device" + " -----> " + deviceType);
	}

}
package edu.cuny.citytech.CET3640.f13.Lab3;

public class iPhone extends SmartPhone 
{
	private String deviceType;

	public iPhone()
	{

		deviceType = "iPhone";
	}

@Override	
	public String getDeviceType() {

		return String.format("Mobile Device" + " -----> " + "Smart Phone" + " -----> " + deviceType);
	}
}
package edu.cuny.citytech.CET3640.f13.Lab3;

public class Android extends SmartPhone
{
	private String deviceType;

	public Android()
	{

		deviceType = "Android";
	}

@Override
	public String getDeviceType() {

		return String.format("Mobile Device" + " -----> " + "Smart Phone" + " -----> " + deviceType);
	}

}
package edu.cuny.citytech.CET3640.f13.Lab3;

public class WindowsPhone extends SmartPhone
{
	private String deviceType;

	public WindowsPhone()
	{

		deviceType = "Windows Phone";
	}

@Override
	public String getDeviceType() {
		return String.format("Mobile Device" + " -----> " + "Smart Phone" + " -----> " + deviceType);
	}

}
package edu.cuny.citytech.CET3640.f13.Lab3;

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

	}
}

Screenshots:

CET3640 Lab 3

CET3640 Lab #3

Leave a Reply

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