Create a class called \'Enterprize\', it has one private member variable called
ID: 3820424 • Letter: C
Question
Create a class called 'Enterprize', it has one private member variable called 'category' (of character type, its value is either 'F' (for food), or 'C' (for clothing), or 'H' (for housing)). This class should have constructor and accessor functions. b) Create a derived class (of Enterprize) called 'not_for_profit' having one private member variable called 'sponsor' (of character type, its value is either 'P' (for private), or 'G' (for government)). This derived class should also have constructor and accessor functions. c) Create another derived class (of Enterprize) called 'for_profit' having one private member variable called "revenue" (of integer type). This derived class should also have constructor and accessor functions. d) Define all the functions (no need of default constructors). c) In the main function, declare one object (called Salvation army) of class "not_for_profit' that has private sponsor and deals with clothing. Declare another object (called HEB) of class 'for_profit' that has SIM revenue and deals with food. f) Write statements to check if Salvation_army and HEB are in the same category of Enterprize or not (food, clothing, or housing) and print appropriate messages.Explanation / Answer
Here is your code below.I have written this in Java and I have added comments to help you understand this better.
MainDriver.java
public class MainDriver {
public static void main(String[] args) {
not_for_profit Salvation_army = new not_for_profit('P', 'C'); //setting private as sponser and clothing as category
for_profit HEB = new for_profit(1, 'F'); // setting $1M as revenue and Food as category
if (Salvation_army.getCategory() == HEB.getCategory()) { //Using getCategory method of parent class.
System.out.println("Both Salvation Army and HEB have same category which is:" + HEB.getCategory());
} else {
System.out.println("Salvation Army and HEB have different categories.");
}
}
}
for_profit.java
public class for_profit extends Enterprize {
private Integer revenue;
public for_profit(Integer rev, Character cat) {
super(cat);// calling parent constructor and setting category
this.revenue = rev;
}
public Integer getRevenue() {
return revenue;
}
}
not_for_profit.java
public class not_for_profit extends Enterprize {
private Character sponser; // 'P' for private, 'G' for government
public not_for_profit(Character spon, Character cat) {
super(cat);// calling parent constructor and setting category
this.sponser = spon;
}
public Character getSponser() {
return sponser;
}
}
Enterprize.java
public class Enterprize {
private Character category; // 'F' for food, 'C' for clothing, 'H' for
// housing
public Enterprize(Character c) {
this.category = c;
}
public Character getCategory() { // accessor function
return category;
}
}
Output:
Salvation Army and HEB have different categories.
Output Explanation
Here we are using parent method getCategory() which is inherited by both classes not_for_profit and for_profit to check if both categories are same. Note that while creating objects of both classes we are actually passing th category character in there respective constructors and there we are calling super() to set the category which in place calls constructor of Enterprize class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.