Create an abstract Division class with fields for a company\'s division name and
ID: 3540184 • Letter: C
Question
Create an abstract Division class with fields for a company's division name and account number, and an abstract display() method that will be defined in the subclasses. Use a constructor in the superclass that requires values for both fields. Create two subclasses named InternationalDivision andDomesticDivision. The InternationalDivision class includes a field for the country in which the division is located, a field for the language spoken, and a constructor that requires all fields when created. The DomesticDivision class include a field for the state in which the division is located and a constructor that requires all fields when created. Write an application named UseDivision that creates two instances of each of these concrete classes (4 total instances). Save the files as:
Division, InternationalDivision, DomesticDivision, and UseDivision.
Please write in the simplest form of java.
Explanation / Answer
public abstract class Division {
//abstract class has method names declared in it but method body is not defined unless its a constructor.
// you can see Division(String divisionName, String accountNumber) is a constructor so its defined. Its body is there.
// where as display() method has no body. It is just declared.
protected String divisionName;
//declaring a String variable of name divisionName
protected String accountNumber;
//declaring a String variable of name accountNumber
//Parameterized constructor
public Division(String divisionName, String accountNumber) {
// super is callled to inherit the properties of parent class (if any). here there is no parent of Division class
super();
this.divisionName = divisionName;
this.accountNumber = accountNumber;
}
// declaring the display method.
//this is done so that class extending the Division class will be forced to define the message body of this method.
abstract String display();
}
//second class is InternationalDivision which extends the Division class
// in other terms, Division class is parent class of InternationalDivision class
public class InternationalDivision extends Division{
//declaring a String variable named countryName
private String countryName;
//declaring a String variable named languageSpoken
private String languageSpoken;
//parameterized constructor
public InternationalDivision(String divisionName, String accountNumber,String countryName, String languageSpoken) {
// it takes divisionName and accountNumber from the parent class. These fields are not defined here but just
// because InternationDivision is child of Division class, it can use these fields.
super(divisionName, accountNumber);
this.countryName = countryName;
this.languageSpoken = languageSpoken;
}
//Display method is defined here. If you do not, it ll show error because this class extends the Division class.
//Override annotation is used because, this method is declared in the abstract class Division. And we are defining it here.
// In other words we are overriding the definition of the definition method
//the return type of the method is String.
// This string ll combine all the details into one String and send it to main method to print it.
//anything inside " " double inverted commas is plain text. it can be anything. and rest is the fields declared before.
@Override
public String display() {
return " Division Name= " + divisionName +" Account Number= "
+ accountNumber+ " Country Name= " + countryName
+ " Language Spoken=" + languageSpoken ;
}
}
//third class is DomesticDivision which extends the Division class
// in other terms, Division class is parent class of DomesticDivision class
public class DomesticDivision extends Division {
//declaring a String type variable named stateName
private String stateName;
//parameterized constructor
// it takes divisionName and accountNumber from the parent class. These fields are not defined here but just
// because DomesticDivision is child of Division class, it can use these fields.
public DomesticDivision(String divisionName, String accountNumber,
String stateName) {
super(divisionName, accountNumber);
this.stateName = stateName;
}
//Display method is defined here. If you do not, it ll show error because this class extends the Division class.
//Override annotation is used because, this method is declared in the abstract class Division. And we are defining it here.
// In other words we are overriding the definition of the definition method
//the return type of the method is String.
// This string ll combine all the details into one String and send it to main method to print it.
//anything inside " " double inverted commas is plain text. it can be anything. and rest is the fields declared before.
@Override
public String display() {
return " Division Name= " + divisionName +" Account Number= "
+ accountNumber+ " State Name= " + stateName ;
}
}
//this is the fourth class containing the main method
// main is the method which is run very first when the code runs
public class UseDivision {
public static void main(String[] args) {
//making objects of DomesticDivision named domestic1 and domestic2 with our data.
//where first field is divisionname= "North Divsion"
//second field is accountNumber = "123345"
//third field is stateName = "WashingtonDC"
DomesticDivision domestic1 = new DomesticDivision("North Divison", "123345", "WashingtonDC");
// similarly it is done for domestic2
DomesticDivision domestic2 = new DomesticDivision("South Division", "45678", "New York");
System.out.println(" Printing Domestic division results");
// calling the display method described in DomesticDivision class which ll return the String containing all the above details
System.out.println(domestic1.display());
// Same is done for second object domestic2 of DomesticDivision class
System.out.println(domestic2.display());
//Similarly, 2 objects named international1 and international2 are made of InternationalDivision class
// where first field is divisionName
// second field is accountNumber
//third field is countryName
// fourth field is language spoken
//Important to tell you that the sequence is important. if you replace the sequence, feilds ll be interchanged. which will make your output go wrong.
InternationalDivision international1 = new InternationalDivision("East Division", "234342342", "Germany", "german");
InternationalDivision international2 = new InternationalDivision("West Division", "453453425", "United States of America", "English");
System.out.println(" Printing International Division results");
//calliing the display method defined in InternationalDivision class
System.out.println(international1.display());
System.out.println(international2.display());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.