Using Netbeans Please I need help. I am surely struggling right no with this cla
ID: 3816459 • Letter: U
Question
Using Netbeans Please I need help. I am surely struggling right no with this class.
Following previous assignment (development of the Asset class) you now need to extend it and create a few derivative classes, i.e. a Router, a Switch, a Server, a PSU (Power Supply Unit) etc. Each of those new classes has to be declared as extending the base Asset class and introduce new attributes and methods. For example, you may consider the following skeletons or add something that matches your expectations:
Router
String application; // one from the following list "Home", "Core", "Infrastructure", etc
int numOfNICs; // number of network interfaces
String OS; // operating system. including vendor, version, and other details
boolean hasConsole; // flag that indicates presence of management console
Switch
int numOf_100M; // number of 100Mb ethernet interfaces
int numOf_1G; // number of 1Gb ethernet interfaces
int numOf_10G; // number of 10Gb ethernet interfaces
int numOf_SFP; // number of SFP modules
String OS; // operating system. including vendor, version, and other details
String level; one from the following list "L2", "L3", "L4"
Server
String OS; // same as with the Router
int NumOfNICs; // same as with the Router
boolean isVirtualized; // flag that shows whether or not this server is a Virtual Machine running on some virtualization platform
You may add more types of assets, and more attributes into each asset to make it looking more realistic, but the grading decision will be based on whether or not inheritance is defined correctly. Inheritance should not be considered as only including keyword "extends" in the declaration of a class, but mainly as a proper set of methods that have to be either added or overridden. One of such methods would be the constructor, such that constructor of the Router class will be different than constructor of the Switch class, but both should call constructor of the Asset class by using super(); notation. You may also consider adding up getters/setters for the extended set of attributes.
Please also have in mind that a customized version of toString() method is needed for each derivative class in order to be able to "print" corresponding objects. Such method is about to return presentation of all valuable class attributes in form of a formatted String object.
Finally, your Driver program also needs to be revised to update the way how different assets are instantiated:
Previously:
Asset A1 = new Asset("server", "HP Proliant ML350 G6", "876245", ...);
Asset A2 = new Asset("switch", "Netgear GS108T", "782364", ...);
After introducing new sub-classes:
Asset A1 = new Server("server", "HP Proliant ML350 G6", "876245", ...);
Asset A2 = new Switch("switch", "Netgear GS108T", "782364", ...);
Despite both forms look alike, the latter is different as each object is now created by a dedicated constructor so a unique set of attributes is being used.
Explanation / Answer
/**
*
* @author Sam
*/
public class Asset { //parent class
private String type;
private String modelName;
private long serialNo;
public Asset(String type, String modelName, long serialNo) {
this.type = type;
this.modelName = modelName;
this.serialNo = serialNo;
}
@Override
public String toString() {
return "Asset{" + "type=" + type + ", modelName=" + modelName + ", serialNo=" + serialNo + '}';
}
}
class Server extends Asset { //sub class extending parent class
private final String os;
private final int NumOfNics;
private final boolean isVirtualized;
public Server(String os, int NumOfNics, boolean isVirtualized, String type, String modelName, long serialNo) {
super(type, modelName, serialNo); //initialize parent class
this.os = os;
this.NumOfNics = NumOfNics;
this.isVirtualized = isVirtualized;
}
@Override
public String toString() {
return "Server{" + " " + super.toString() /*calling parent class method*/+ " os=" + os + " NumOfNics=" + NumOfNics + " isVirtualized=" + isVirtualized + " }";
}
}
class Router extends Asset{
private String application;
private int numOfNic;
private String OS;
private boolean hasConsole;
public Router(String application, int numOfNic, String OS, boolean hasConsole, String type, String modelName, long serialNo) {
super(type, modelName, serialNo);
this.application = application;
this.numOfNic = numOfNic;
this.OS = OS;
this.hasConsole = hasConsole;
if (!application.equalsIgnoreCase("Home") && !application.equalsIgnoreCase("Core") && !application.equalsIgnoreCase("Infrastructure")) //throw error if requirments are not satisfied
throw new IllegalArgumentException("invalid application");
}
@Override
public String toString() {
return "Router{" + " " + super.toString() + " application=" + application + " numOfNic=" + numOfNic + " OS=" + OS + " hasConsole=" + hasConsole + " }";
}
}
class Switch extends Asset{
int numOf_100M; // number of 100Mb ethernet interfaces
int numOf_1G; // number of 1Gb ethernet interfaces
int numOf_10G; // number of 10Gb ethernet interfaces
int numOf_SFP; // number of SFP modules
String OS; // operating system. including vendor, version, and other details
String level; //one from the following list "L2", "L3", "L4"
public Switch(int numOf_100M, int numOf_1G, int numOf_10G, int numOf_SFP, String OS, String level, String type, String modelName, long serialNo) {
super(type, modelName, serialNo);
this.numOf_100M = numOf_100M;
this.numOf_1G = numOf_1G;
this.numOf_10G = numOf_10G;
this.numOf_SFP = numOf_SFP;
this.OS = OS;
this.level = level;
if (!level.equalsIgnoreCase("L2") && !level.equalsIgnoreCase("L3") && !level.equalsIgnoreCase("L4"))
throw new IllegalArgumentException("invalid level");
}
@Override
public String toString() {
return "Switch{" + " " + super.toString() + " numOf_100M=" + numOf_100M + " numOf_1G=" + numOf_1G + " numOf_10G=" + numOf_10G + " numOf_SFP=" + numOf_SFP + " OS=" + OS + " level=" + level + " }";
}
}
class AssetDriver {
public static void main(String[] args) {
Asset a1 = new Server("Microsoft Windows Server 2007", 7, true, "server", "AsVS205", 47821479);
Asset a2 = new Switch(2, 4, 2, 0, null, "L2", "Switch", "swt10Sk", 14785463289l);
Asset a3 = new Router("Core", 5, "Ipanama Router OS v1.2", false, "Router", null, 51478897456l);
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
}
}
Here is the code. I havnt commented the code since the code is quie simple. If you are having difficulties understanding the problem, please let me know. I shall be glad to help you with the code.