Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Driver code^ org unit code: public class OrgUnit { String unitName,mainProject; double cashAvailable; int numEmployees; public String getUnitName() { return unitName; } public void setUnitName(String

image text in transcribedimage text in transcribedimage text in transcribed

Driver code^

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

org unit code:

public class OrgUnit { String unitName,mainProject; double cashAvailable; int numEmployees; public String getUnitName() { return unitName; } public void setUnitName(String unitName) { this.unitName = unitName; } public String getMainProject() { return mainProject; } public void setMainProject(String mainProject) { this.mainProject = mainProject; } public double getCashAvailable() { return cashAvailable; } public void setCashAvailable(double cashAvailable) { this.cashAvailable = cashAvailable; } public int getNumEmployees() { return numEmployees; } public void setNumEmployees(int numEmployees) { this.numEmployees = numEmployees; } public OrgUnit(String unitName, String mainProject, double cashAvailable, int numEmployees) { super(); this.unitName = unitName; this.mainProject = mainProject; this.cashAvailable = cashAvailable; this.numEmployees = numEmployees; } public boolean addCash(double cash) { if(cash

management unit code:

import java.util.ArrayList;

public class ManagementUnit extends OrgUnit { ArrayList unit = new ArrayList();

public ManagementUnit(String unitName, String mainProject, double cashAvailable, int numEmployees) { super(unitName, mainProject, cashAvailable, numEmployees); unit.add(new OrgUnit(unitName, mainProject, cashAvailable, numEmployees)); } public void addSupervisedUnit(OrgUnit orgUnit) { unit.add(orgUnit); } public String getSupervisedUnits() { String str = ""; for(int i=0;i

Government unit code:

public interface GovernmentUnit { static int normal=0, classified=1, secret=2,topsecret=3; int getClearance(); String getAgency(); boolean setClearance(int clear); default boolean isValidClearance(int clear) { if(clear >= normal && clear

Governmentorgunit code:

public class GovernmentOrgUnit extends OrgUnit implements GovernmentUnit {

String agencyName; static int CLEARANCE_TOPSECRET;

public GovernmentOrgUnit(String unitName, String mainProject, double cashAvailable, int numEmployees, String agencyName) { super(unitName, mainProject, cashAvailable, numEmployees); this.agencyName = agencyName; }

@Override public int getClearance() { return CLEARANCE_TOPSECRET; }

@Override public String getAgency() { return agencyName; }

@Override public boolean setClearance(int clear) { if(isValidClearance(clear)) { this.CLEARANCE_TOPSECRET=clear; return true; } else { return false; } }

@Override public String getClearanceLevelName() {

if (CLEARANCE_TOPSECRET == 0) { return "Normal"; } else if (this.CLEARANCE_TOPSECRET == 1) { return "Classified"; } else if (this.CLEARANCE_TOPSECRET == 2) { return "Secret"; } else { return "Topsecret"; } }

}

Please help me fix my code to replicate these sample results:

image text in transcribedimage text in transcribed

My results with my current code:

image text in transcribed

public class Homework4Program { public public static void main(String [] args) // All that main() does is execute the driver program. driver(); } public static void driver() ManagementUnit govtOnly, primary; Governmentorgunit spyAgency, forestryAgency; Orgunit database, spreadsheet, server; // Build the management units, first. govtOnly = new ManagementUnit("Government-Special Projects", "Oversight: Government Security", 15, 500000); primary = new ManagementUnit("Regular projects", "Oversight: regular projects", 12, 250000); // Now build the normal units that work on projects. database = new OrgUnit("Database Unit", "Database Upgrade", 20, 12500); spreadsheet = new OrgUnit("Spreadsheet Unit", "Spreadsheet development", 30, 50000); server = new OrgUnit("Server extension", "Web server program", 35, 90000); // Build government units to work on government projects. spyAgency = new Governmentorgunit("Spy Projects", "Spy Satellite Software", 150, 2000000, "Dept. of Navy"); forestryAgency = new Governmentorgunit("Forest Projects", "Tree Information Software", 25, 500000, "National Parks Service"); // Set the spy agency project to 'TOP SECRET // Set the spy agency project to 'TOP SECRET' spyAgency.setClearance (Governmentorgunit.CLEARANCE_TOPSECRET); // Assemble the organization, so that every Orgunit here is either // a management unit, or under a management unit. primary.addSupervisedUnit(database); primary.addSupervisedUnit (spreadsheet); primary.addSupervisedUnit(server); primary.addSupervisedUnit(forestryAgency); govtOnly.addSupervisedunit (spyAgency); * // OUTPUT 1 - Brief information on all units System.out.println("*** BRIEF INFO - ALL UNITS ***"); printQuickDetails (govtOnly); smal LSep(); printQuickDetails(primary); smallSep(); printQuickDetails(spyAgency); smallSep(); printQuickDetails(forestryAgency); smallSep(); printQuickDetails(database); smallSep(); printQuickDetails(spreadsheet); smallSep(); printQuickDetails(server); LargeSep(); // OUTPUT 2 - Information on management units System.out.println("*** Management unit information ****, System.out.println(primary.toString(); smallSep(); System.out.println(govtOnly.toString(); Large Sep(); // OUTPUT 3 - Information on ALL units, as relayed by // the ManagementUnits that supervise them ); // OUTPUT 3 - Information on ALL units, as relayed by // the ManagementUnits that supervise them System.out.println("*** Detailed Information ***" *"); System.out.print(primary.getAllManagedDetails()); smallSep(); System.out.print(govtOnly.getAllManagedDetails(); Largesep(); // Final message, and then exit driver() System.out.println("* ("*** END OF COMPANY STATUS REPORT ***"); } private static void smallSep() { // Small separator, nothing especially // important, helps with formatting. I System.out.println("***"); } private static void largesep() { // Large separator. System.out.println("- -"); } private static void printQuickDetails(Orgunit unit) { // Outputs the results of reportQuickDetails) // using System.out.println System.out.println(reportQuickDetails(unit)); } private static String reportQuickDetails (Orgunit currentUnit) { return currentUnit.toString(); } Homework #4 MIS 120 In this homework assignment, we will develop several classes in order to create an organizational hierarchy. This will use inheritance and interfaces so that the classes can use polymorphism in order to minimize the amount of code that is rewritten. . This exercise provides an opportunity to work with: Inheritance Polymorphism Interfaces SCENARIO: You have been asked to write several classes to help organize a management hierarchy in a technology development firm. This organizes the various company units to help track projects, assignments, and responsibility on a unit level. There are two types of business units that we will be dealing with regular organization units (Org Unit) which generally actually develop technology, as well as units that are focused more on managing other units (Management Unit). A Management Unit is built on an OrgUnit and enhances it for this task. Our firm also concerns itself with government projects that may be considered classified or secret in some other manner. We need to enhance these units in such a way that they can store and process additional information, such as the level of clearance required. For this assignment, we are using a common practice in software engineering - the use of drivers. Drivers are not in this context, the pieces of software that allow hardware to function. Drivers, in software testing, are used to "run" a class so that it can be tested without writing the entire program. As such, a driver is being provided by the instructor. You are expected to include this in your submission, and use it for testing. This also means that no user input needs to be gathered, since the driver produces all the input for your classes. CLASS: OrgUnit . The OrgUnit class serves as the basis of the other organizational unit classes in the program. It is also useful by itself. An OrgUnit keeps track of the following: The name of the unit (String) The current main project of the unit (String) The number of employees currently assigned to the unit (int) The amount of cash the unit currently has direct access to (double) . The constructor for OrgUnit must accept a parameter for each one of these and initialize its instance variables with these parameters. Each instance variable must have a corresponding instance variable, as well as a corresponding getter method, named getUnitName(), getMainProject(), getCashAvailable() and getNumEmployees(), as appropriate. As in most organizations, OrgUnits can change, sometimes substantially. As such, we need functions that will alter or process the data stored within an OrgUnit. The most frequent change will be the level of cash. As such we have two methods - addCash() and reduceCash(). Both of these accept a double-type value as a parameter (the amount of money to add or remove), and return a boolean to indicate if the operation succeeded or failed. reduceCash() first checks to make certain that the amount passed by parameter is non-negative, so we cannot accidentally add cash when we are reducing it. We also check that the subtraction does not send the cash balance into the negative, e.g. if you try to reduce the cash by $20 and you only have $10, then it will not work. If both of these conditions are satisfied, then the parameter is subtracted from the current amount of cash, and reduceCash() returns true. If not, the calculation is not completed and reduceCash() returns false. addCash() is a little simpler; it checks to make certain that the amount passed by the parameter is non- negative. If it is negative, addCash() returns false. Otherwise, the amount is added to the current cash amount and addCash() returns true. The number of employees are also subject to change, and are changed in a manner extremely similar to reduceCash() and addCash(). It uses methods named addEmployees() and removeEmployees(), both of which accept an int parameter (the number of employees to add or remove), and return a boolean value that reports whether the operation succeeded or failed. Similar to addCasho, addEmployees() checks to make sure that the parameter to be added is non- negative; if the parameter is non-negative, then the parameter is added to the current number of employees, and addEmployees() returns true. If the parameter is negative, addEmployees() returns false. Similar to reduceCash(), removeEmployees() checks to make sure the parameter specifying the number of employees to be removed is non-negative, and that the subtraction would not send the number of employees into the negative. If the parameter is non-negative, and the subtraction would not result in a negative number of employees, then the calculation is performed on the instance variable and the function returns true. Otherwise, the function returns false. The OrgUnit class also has a simple setter, setMainProject(), which accepts a String as a parameter and sets the main project instance variable to that parameter. This method is void type, so it returns nothing Finally, the Org Unit overrides toString(). It makes toString() return a single String, listing the unit name, number of employees, amount of cash, and main project name, IN THAT ORDER. The toString) method ABSOLUTELY MUST use the object's getter methods, or else it will not work properly for what we have planned. Do NOT directly reference the instance variables from toStringO. CLASS: Management Unit Management Unit builds on Org Unit. However, it does something OrgUnit cannot do - it keeps track of OTHER Org Units. It is built on OrgUnit in an inheritance relationship. ManagementUnit is the subclass of OrgUnit, because it is a specialized version of Org Unit. Using inheritance in this assignment is absolutely required for credit. As Management Units are responsible for keeping track of other Org Units, it needs a way to store that. This is done with an ArrayList named 'unit', which carries instances of OrgUnit. This list should be initialized in the constructor, although at this point it is an empty list. The constructor should also run the constructor for the superclass. Management Unit should have a method, addSupervisedUnit(), which takes an OrgUnit as its parameter. It then adds it to the ArrayList holding instances of Org Unit. The OrgUnits that a Management Unit keeps track of must occasionally be reported to the user. As such, there is a method, getSupervisedUnits(), which returns a String and accepts no parameters. This function iterates through the ArrayList. It then reports, each on its own separate line, the name of every OrgUnit in the ArrayList, and (on the same line) its main project. It should note which is the unit name and which is the project name. It should return the resulting String that has compiled all of these details. A method, getNumManaged(), should return the number of units that are currently in the ArrayList, as an int. It accepts no parameters. Displaying ALL the information a ManagementUnit has, along with all the information of its corresponding OrgUnit instances, will require a new method. As such, we will write the getAll Managed Details() method, which accepts no parameters and returns a String. This will start by printing out a String that gives the name of the Management Unit (using getUnitName(), and then notes that it is responsible for the units that follow. It then puts out a newline. From that point, it then iterates through the entire ArrayList structure, adding in the output of the toString() method for each and every OrgUnit in the ArrayList. These should have sufficient spacing to make it readable (see the sample output). The getUnitName() method from OrgUnit must be overridden to append the string "(MANAGEMENT)" to the getUnitName() located in the superclass. As such, any attempt to output a ManagementUnit will result in it being marked as a management unit. INTERFACE: Government Unit The Government Unit interface is designed to allow you to specify a specialized version of an OrgUnit that is capable of working on specialized government projects. The GovernmentUnit interface adds the following: . A series of constants denoting four levels of classification normal (0), classified (1), secret (2), and top secret (3). These are all ints. A method, getClearance(), which returns an int and accepts no parameters. This should not have a body for the method. The body should be implemented by the class that implements Government Unit. This method is intended to return the clearance level of the unit. A method, getAgency, which returns a String and accepts no parameters. This also should not include a body for the method. The body should be implemented by the class that implements Government Unit. This method is intended to return the agency the unit will work for. A method, setClearance(), which returns a boolean and accepts an int parameter. This method is intended to set the clearance level. The int parameter is the clearance level you want for the particular object you are working with. It should be one of the constants declared in the interface. SetClearance returns true if successful and false if not. . A static method, is ValidClearance, which takes an int and returns a boolean. Static methods/functions in an interface must have their code included as part of the interface. As such, you must provide a body for this one. It should check to make sure that the parameter is one of the constants that represent clearance levels. If it is, then the clearance level from the parameter is considered valid, so the return value is true. If not, then the clearance level is NOT valid so the return value is false. A default method, getClearance LevelName(), which accepts no parameters and returns a String. Because it is a default method it is possible to override it, but in the absence of an override, the default code is used. The default code is included as a body for the method if it is not overridden by the implementing class. The default method uses the getClearance() method to get the current clearance level of the current object. It then compares the current clearance level against the constants, and returns "Top Secret if it is consistent with the Top Secret constant, "Secret" if it is the same as the Secret constant, "Classified" if it is the same as the Classified constant, and "Normal" if it is anything else. NOTE: Even though we provide no getClearance() code, getClearance() is specified as part of the interface. As such, the compiler will require any class that implements the interface to override getClearance(). This means that we (and the compiler) can be certain that the default code will always have access to valid code for getClearance() whenever it is used. CLASS: Government OrgUnit This class both inherits from OrgUnit and implements the Government Unit interface. Most of the work is already done at this point for OrgUnit, but you still need to implement GovernmentUnit. Remember - an interface cannot hold instance variables. So GovernmentOrg Unit has to keep track of its own information. As such, there are two instance variables included - a String for the name of the agency the unit works with, and an int for the clearance level. The constructor must accept all of the parameters the constructor for OrgUnit accepts. It must also have a String parameter for the agency that it is supposed to represent, and it should set the instance variable to equal this parameter. It should set the clearance level to the value of the Normal clearance level constant (from Government Unit). As GovernmentOrg Unit implements Government Unit, there are at least THREE METHODS that it ABSOLUTELY MUST implement: getClearance(), setClearance and getAgency. The exact specifications for these are in the Government Unit interface. getClearance() returns the current clearance level. getAgency() returns the String used for the agency the GovernmentOrg Unit works with. setClearance() takes the desired clearance level as an int, and returns a boolean. This function should use the 'is Valid Clearance('static function to determine if the parameter is a valid clearance level or not. If it is valid, then the clearance level instance variable should be set to the value in the parameter, and it should return true. If it is not valid, then the return value should be false with no change. This class must also override toString. It should take the toString() from the superclass, and add several pieces of information to it. First, it should should concatenate the output with a label, GOVERNMENT CLEARANCE:", and then add getClearance LevelName(). This calls the default method in the interface and uses it to give the user an actual text representation of what the clearance should be, e.g. "Top Secret", "Secret", etc. Note that if you wrote getClearance LevelName() correctly it should require no further parameters or anything else, because it is using the interface specification to call the getClearance Level() method and bases its decision on that. Second, another label should be added, ", coordination with ", followed by the agency name of the agency. Finally, this should all be ended with a newline character. This should result in toString() having all of the information in toString() from Org Unit, plus an additional line that has the name of the clearance level and the name of the government agency that the GovernmentOrgUnit instance is working with. CLASS: Homework4Program This is the class that contains the driver code, found in main(). It is not intended to be instantiated. In fact, much of the code is already written for you. There are a few support functions included. However, there is one function that you must write. This is the reportQuick Details() static function. The header for reportQuickDetails() is supplied. It returns an instance of String, and takes an instance of Org Unit as a parameter, named current Unit. In its current form, it is a "stub" - a placeholder that will let you compile and test. Your job here is to replace it with something that actually does something useful. reportQuickDetails() needs to build a String instance to return. It always starts that string out, on its own line, the label of "NAME:" and the name of currentUnit. However, it is not all it does. It also detects whether or not currentUnit is a Management Unit or a Government Unit. . If currentUnit is a Government Unit, then it will add a line to the output string, which has the label "Clearance Level:" and follow it with the clearance level name, taken from the appropriate method in the Government Unit interface. If currentUnit is a Management Unit, then it will add a line to the output string, which has the label "Number of units managed: " followed by the number of units it is managing. That number is obtained using methods from the Management Unit object. If currentUnit is neither a ManagementUnit or a Government Unit, then it adds a line to the output string, stating "Normal project unit" and nothing else. Once this is complete, reportQuick Details() returns the resulting String object. TIPS AND NOTES: Read the entire assignment prompt CAREFULLY. Do not be afraid to review a certain part in detail if you are having trouble. It would be best to read it and evaluate your program at least once before turning it in. You will need to know what inheritance is. In particular, you need to remember that an instance of a subclass is also an instance of its superclass, and also how overrides work. You will need to know what interfaces are and what they do and do not do. Remember the differences between inheritance and interfaces. Also remember the similarities. Keep in mind that interfaces often act as "fill in the blank" structures, so you are going to have to fill in the blank with appropriate methods. But also remember that there are some methods that you do NOT have to "fill in the blank" with in interfaces, and you will need to know the difference. Although your code will have a driver program to work with, it will be expected to be generally functional. This means that it should not be written in a manner that it depends on the driver program to work. These classes are intended to be used in another software project, or at least that is what we are supposing for the purposes of this assignment, and as such they need to be modular so that they can be moved to another development project with little to no trouble. Failure to do so will likely result in a penalty. This will probably not be a major problem for you unless you do some very unusual things, so do not be too concerned, but keep it in mind. Think about what you are doing, and why you are doing it. In most cases you should not blindly follow the specifications; think about whether you are doing something that actually makes sense. For instance, setting up a class to inherit from another class, and then writing completely new code without the slightest relationship to the original class is probably not correctly using inheritance, and your approach should be reconsidered. This can result in severe loss of credit. Also remember that how you do something is important, not just what you are doing. This class is intended to teach object-oriented programming, so your code should follow an object- oriented paradigm and correctly use related features of the programming language. Feel free to modify the driver program for debugging purposes, especially when you are testing your code but only have parts of it written. However, the driver program should be unmodified for the final submission. Only modify the driver program if you intend to comment out parts because they were not complete so you can obtain partial credit. Because this program takes in no user input, it has only one correct output. This is intentional. However, DO NOT ATTEMPT TO PRODUCE "FAKE" CORRECT OUTPUT. Any attempt to do this will be subject to an EXTREMELY large penalty, even if the rest of the program is correct. Up to 50% credit may be lost for one instance, and all credit will be lost for two or more instances. All programs are read by the instructor during grading; do not expect to "get away" with this. I would rather see a program that only implements part of the specifications than one that pretends to fulfill specifications but does not GRADING: Program properly implements inheritance and all related code, as specified: 40% Program properly implements interfaces and all related code, as specified: 30% Program otherwise fulfills specifications: 30% NOTE: You are expected to use skills developed in prior assignments and units. Incorrectly using skills from prior assignments, e.g. ArrayList and classes, may be counted against the applicable category/categories. Also note that the "right" answer implements the code in the way specified. Simply producing "correct" output is not enough. . NECESSARY MATERIALS: The driver program is available on Canvas for download. Be sure to use it. Sample output is available on Canvas in a separate file. Refer to it as needed. DELIVERABLES: Turn in a single .ZIP file containing only the java files by the due date. Do NOT turn in a project from a Java IDE or any.class files. *** BRIEF INFO - ALL UNITS *** Unit: Government-Special Projects (MANAGEMENT) Number of units managed: 1 Unit: Regular projects (MANAGEMENT) Number of units managed: 4 Unit: Spy Projects Clearance level: Top Secret Unit: Forest Projects clearance level: Normal Unit: Database Unit Normal project unit Unit: Spreadsheet Unit Normal project unit Unit: Server extension Normal project unit *** Management unit information UNIT: Regular projects (MANAGEMENT) # EMPLOYEES: 12 CASH AVAILABLE: $250000.0 MAIN PROJECT: Oversight: regular projects + UNIT: Government-Special Projects (MANAGEMENT) # EMPLOYEES: 15 CASH AVAILABLE: $500000.0 MAIN PROJECT: Oversight: Government Security *** Detailed Information *** Management Department: Regular projects (MANAGEMENT), Responsible for these units: 0) UNIT: Database Unit EMPLOYEES: 20 CASH AVAILABLE: $12500.0 MAIN PROJECT: Database Upgrade 1) UNIT: Spreadsheet Unit # EMPLOYEES: 30 CASH AVAILABLE: $50000.0 MAIN PROJECT: Spreadsheet development 2) UNIT: Server extension # EMPLOYEES: 35 CASH AVAILABLE: $90000.0 MAIN PROJECT: Web server program 3) UNIT: Forest Projects # EMPLOYEES: 25 CASH AVAILABLE: $ 500000.0 MAIN PROJECT: Tree Information Software GOVERNMENT CLEARANCE: Normal, coordination with: National Parks Service Management Department: Government-Special Projects (MANAGEMENT), Responsible for these units: 0) UNIT: Spy Projects # EMPLOYEES: 150 CASH AVAILABLE: $2000000.0 MAIN PROJECT: Spy Satellite Software GOVERNMENT CLEARANCE: Top Secret, coordination with: Dept. of Navy *** END OF COMPANY STATUS REPORT *** *** *** BRIEF INFO - ALL UNITS *** Government-Special Projects Oversight: Government Security 15.0 500000 Regular projects Oversight: regular projects 12.0 250000 Spy Projects Spy Satellite Software 150.0 2000000 **** *** *** Forest Projects Tree Information Software 25.0 500000 Database Unit Database Upgrade 20.0 12500 *** Spreadsheet Unit Spreadsheet development 30.0 50000 Server extension Web server program 35.0 90000 *** Management unit information *** Regular projects Oversight: regular projects 12.0 250000 ***** Government-Special Projects Oversight: Government Security 15.0 500000 *** Detailed Information *** Regular projects Oversight: regular projects 12.0 250000 Database Unit Database Upgrade 20.0 12500 Spreadsheet Unit Spreadsheet development 30.0 50000 Server extension Web server program 35.0 90000 Forest Projects Tree Information Software 25.0 500000 ***** Government-Special Projects Oversight: Government Security 15.0 500000 Spy Projects Spy Satellite Software 150.0 2000000 *** END OF COMPANY STATUS REPORT ***

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions

Question

Summarize the impact of a termination on the employee.

Answered: 1 week ago