Create a program with the following OO structure - you do not have to implement
ID: 3765373 • Letter: C
Question
Create a program with the following OO structure - you do not have to implement any of the methods other than constructors to fill in some default data. You should implement the rest of the methods with a Console.WriteLine to indicate that it has been called. Within a UNIVERSITY there are 4 DEPARTMENTS MATH, ENGLISH, GEOGRAPHY, COMPUTERSCIENCE Each DEPARTMENT has up to 10 STAFF There is a DEAN, PROFESSORs, ADMINISTRATORs and RESEARCHERs All Staff should have a name and salary Professors should have a Class Researchers should have a ResearchSpeciality which is a fixed set of research areas represented by an enumeration Deans, Professors and Researchers should implement an ITeachable interface which contains the Teach() method Deans and Administrators should implement an IAdmin interface which contains the Administrate() method Pay special attention to the IS-A relationships and the HAS-A relationships and implement things correctly. You should determine reasonable types for all properties not otherwise specified and initialise things with sensible defaults. Your constructors should have reasonable parameters to set up the type where you cannot simply use constants. var u = new University(); Console.WriteLine(u.Department[2].Staff[3].Name); u.Department[1].Staff[0] = new Researcher("Andy"); You should create additional test code to make sure that all of the properties and methods are available
Explanation / Answer
Above piece of code begins with comments. Now come to class declaration, the line public class Book declares a class Book of public access. Keyword publicmakes this class accessible from anywhere inside or outside the package. If the access modifier is omitted then a class by default remains accessible within the package in which the class has been defined.
Usually, Java classes are defined in separate files where one file contains one class and the file name is identical to the class name having .java extension. But more than one class can also be written in a file; and in that case, only one class can be declared public and remaining will be of default access. File name will be in the name of public class if there is more than one class coded in one file.
Conclusively, the best practice is to keep all class or enum or interface definitions in separate files. It is assumed clean and unambiguous approach of code writing.
Entering into the body of the Java class Book we see three data members named asbookTitle, bookAuthor, and numOfPages, where none of them are declared static. So there are no class level members. All members declared inside class body will be object level members. They all declared private to the class and therefore cannot be accessed outside the class. Now come to constructors.
Java Class: Parameterized Constructor
In the body of Java class Book below the declaration of data members you see function-definition like structures with the same name as the class name. These are constructors. Constructors have same name as the class name. Here, for Bookclass two constructors have been defined - one without parameters (zero argument or with no argument), and second with parameters (parameterized constructor or constructor with parameters). If you define a parameterized constructor in a class then it is mandatory to define zero argument constructor in order to create an object with no argument in object creation expression (usingnew operator).
A constructor runs when an object of a Java class is created. A constructor can be seen as a method definition that has no return type, and that's name is identical to the class name. Java Language Specification gives the following syntax to define a constructor in a class:
Java constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding. A Java constructor is invoked at following three occasions:
Note that Java constructors are never invoked by method invocation expressions (object-name.constructor-name). Unlike methods, a constructor cannot beabstract, static, final, native, strictfp, or synchronized.
In body of a Java constructor data members are initialized. The first statement of a Java constructor's body may be an explicit invocation of another Java constructor of the same class or of the direct superclass.
There will be a compile-time error for a Java constructor to directly or indirectly invoke itself through a series of one or more explicit constructor invocations involving this.
If a Java constructor's body does not begin with an explicit constructor invocation and the constructor being declared is not part of the class Object, then the constructor body implicitly begins with a superclass constructor invocationsuper();, an invocation of the constructor of its direct superclass that takes no arguments.
A return statement with no return value may be used in the body of a constructor.
Java Class: Default Constructor
Sometimes, when you don't feel a need to initialize data members by particular values then you can skip writing constructors at all. In that case the Java compiler automatically creates a constructor with zero arguments that does nothing. This default constructor allocates memory and initializes instance variables by default values.
Here, it is important to know that once programmer defines one or more parameterized constructors then Java does not internally create default constructor. In that situation developers have to create a zero argument constructor at their own.
Java Class: Accessors and Mutators or Getters and Setters
While writing new Java classes we pay much attention to information hiding, where we ensure the data members should be declared private if programmers don't feel an explicit need to declare them public or default. As we know privatemembers of a class cannot be accessed outside the class yet you can allow other classes to find out what their values are by providing public accessor methods. Class Book defines three accessors or getters for all three members asgetBookTitle(), getBookAuthor(), getNumOfPages(), and three mutators or setters assetBookTitle(String bookTitle)(), setBookAuthor(String bookAuthor), andsetNumOfPages(int numOfPages).
Not all fields need individual field accessors and mutators. It will depend on the business logic written in a class.
Now, let's quickly create a class BookDemo to demonstrate object creation and data member access.
In class BookDemo when the statement Book b = new Book("James Gosling", "The Java Language Specification", 670); is executed it creates an object in memory by calling the parameterized constructor of class Book then newly created object is assigned to the reference b. Values of all data members are then printed on console by invoking accessors defined in Book.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.