|
Interfaces and Abstract Classes
in Detailed Design |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
Sortable
To Think About:
.java appended)./**
* The requirements of Prioritized objects
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public interface Prioritized
{
/**
* Return the priority of this Object.
*
* @return The priority
*/
public abstract int getPriority();
}
/**
* A message containing emergency information.
*
* This version implements the Prioritized interface.
*
* @author Prof. David Bernstein, James Madison University
* @version 2.0
*/
public class EmergencyMessage implements Prioritized
{
private int priority; // Note: These attributes are private
private String message; // not protected.
/**
* Explicit Value Constructor.
*
* @param message The text of the mesage
*/
public EmergencyMessage(String message)
{
this.message = message;
}
/**
* Return the text of this message.
*
* @return The text of the message
*/
public String getMessage()
{
return message;
}
/**
* Return the priority of this message (required by Prioritized).
*
* @return The priority of the message
*/
public int getPriority()
{
return priority;
}
/**
* Set the priority of this message.
*
* @param priority The priority of the message
*/
public void setPriority(int priority)
{
if (priority < 0) this.priority = 0;
else if (priority > 10) this.priority = 10;
else this.priority = priority;
}
}
/**
* A message containing normal emergency information and a
* supplemental alert.
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class Alert extends EmergencyMessage
{
private String supplement;
/**
* Explicit Value Constructor.
*
* @param message The main mesage
* @param supplement The supplemental information
*/
public Alert(String message, String supplement)
{
super(message);
this.supplement = supplement;
}
/**
* Return the supplemental information.
*
* @return The supplemental information
*/
public String getSupplement()
{
return supplement;
}
}
/**
* An accident report
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class AccidentReport implements Prioritized
{
private int size;
private String type;
/**
* Explicit Value Constructor.
*
* @param type The type of accident
* @param size The number of vehicles involved
*/
public AccidentReport(String type, int size)
{
this.type = type;
this.size = size;
}
/**
* Return the priority of this AccidentReport (required by
* Prioritized).
*
* @return The priority of this AccidentReport
*/
public int getPriority()
{
int priority;
if (size > 3) priority = 5;
else if (size > 5) priority = 10;
else if (type.equals("HEAD ON")) priority = 8;
else priority = 3;
return priority;
}
/**
* Set the size of the accident.
*
* @param size The number of vehicles involved
*/
public void setSize(int size)
{
this.size = size;
}
}
/**
* An example that uses interfaces and derived classes
*
* @version 1.0
* @author Prof. David Bernstein, James Madison University
*/
public class Driver
{
public static void main(String[] args)
{
AccidentReport accident1, accident2;
int k;
Prioritized[] priorities;
Alert alert;
EmergencyMessage message;
priorities = new Prioritized[4];
message = new EmergencyMessage("Congestion on I81 north!");
priorities[0] = message;
alert = new Alert("Route 11 Closed in Both Directions",
"Use I81 Instead");
alert.setPriority(4);
priorities[1] = alert;
accident1 = new AccidentReport("FENDER BENDER", 2);
priorities[2] = accident1;
accident2 = new AccidentReport("HEAD ON", 2);
priorities[3] = accident2;
k = countHighPriorityItems(priorities);
System.out.println("\n\n\n");
if (k == 1)
{
System.out.println("1 item is crucial!");
} else {
System.out.println(k+" items are crucial!");
}
accident1.setSize(4);
k = countHighPriorityItems(priorities);
System.out.println("\n\n\n");
if (k == 1)
{
System.out.println("1 item is crucial!");
} else {
System.out.println(k+" items are crucial!");
}
}
/**
* Determine the number of high priorty items
*/
public static int countHighPriorityItems(
Prioritized[] items)
{
int i, count;
count = 0;
for (i=0; i < items.length; i++)
{
if (items[i].getPriority() >= 5) count++;
}
return count;
}
}
.java appended).abstract keyword (and
have an empty body).package abstracts;
import java.util.*;
/**
* An abstract "piece of" content that will be extended
* to create concrete media (e.g., Image, VideoClip)
*
* @version 1.0
* @author Prof. David Bernstein, James Madison University
*/
public abstract class AbstractContent
{
private Date creationDate;
private String author, title;
/**
* "Constructs" a new AbstractContent
* (Note: This method should be called by the
* constructor of all child classes)
*/
public AbstractContent()
{
creationDate = new Date();
}
/**
* Returns the author of this AbstractContent
*/
public String getAuthor()
{
return author;
}
/**
* Returns the creation date
*/
public Date getCreationDate()
{
return creationDate;
}
/**
* Returns the title of this AbstractContent
*/
public String getTitle()
{
return title;
}
/**
* Show/play/render this AbstractContent
*/
public abstract void show();
/**
* Sets the author of this AbstractContent
*/
public void setAuthor(String author)
{
this.author = author;
}
/**
* Sets the title of this AbstractContent
*/
public void setTitle(String title)
{
this.title = title;
}
}