Question

java CLASS DESIGN GUIDELINES 1. Cohesion • [✓] A class should describe a single entity, and...

java

CLASS DESIGN GUIDELINES

1. Cohesion • [✓] A class should describe a single entity, and all the class operations should logically fit together to support a coherent purpose. • [✓] A single entity with many responsibilities can be broken into several classes to separate the responsibilities.

2. Consistency • [✓] Follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods. A popular style is to place the data declaration before the constructor and place constructors before methods. • [✓] Make the names consistent. It is not a good practice to choose different names for similar operations. • [✓] In general, you should consistently provide a public no-arg constructor for constructing a default instance. If a class does not support a no-arg constructor, document the reason. If no constructors are defined explicitly, a public default no-arg constructor with an empty body is assumed. • [✓] If you want to prevent users from creating an object for a class, you can declare a private constructor in the class, as is the case for the Math class.

3. Encapsulation • [✓] A class should use the private modifier to hide its data from direct access by clients. This makes the class easy to maintain. • [✓] Provide a getter method only if you want the data field to be readable, and provide a setter method only if you want the data field to be updateable.

4. Clarity • [✓] Cohesion, consistency, and encapsulation are good guidelines for achieving design clarity. Additionally, a class should have a clear contract that is easy to explain and easy to understand. • [✓] Users can incorporate classes in many different combinations, orders, and environments. Therefore, you should design a class that imposes no restrictions on how or when the user can use it, design the properties in a way that lets the user set them in any order and with any combination of values, and design methods that function independently of their order of occurrence. • [✓] Methods should be defined intuitively without causing confusion. • [✓] You should not declare a data field that can be derived from other data fields.

5. Completeness • [✓] Classes are designed for use by many different customers. In order to be useful in a wide range of applications, a class should provide a variety of ways for customization through properties and methods.

6. Instance vs. Static • [✓] A variable or method that is dependent on a specific instance of the class must be an instance variable or method. A variable that is shared by all the instances of a class should be declared static. • [✓] Always reference static variables and methods from a class name (rather than a reference variable) to improve readability and avoid errors. • [✓] Do not pass a parameter from a constructor to initialize a static data field. It is better to use a setter method to change the static data field. • [✓] Instance and static are integral parts of object-oriented programming. A data field or method is either instance or static. Do not mistakenly overlook static data fields or methods. It is a common design error to define an instance method should have been static. • [✓] A constructor is always instance, because it is used to create a specific instance. A static variable or method can be invoked from an instance method, but an instance variable or method cannot be invoked from a static method.

7. Inheritance vs. Aggregation • [✓] The difference between inheritance and aggregation is the difference between an is-a and a has-a relationship.

8. Interfaces vs. Abstract Classes • [✓] Both interfaces and abstract classes can be used to specify common behavior for objects. How do you decide whether to use an interface or a class? In general, a strong is-a relationship that clearly describes a parentchild relationship should be modeled using classes. A weak is-a relationship, also known as an is-kind-of relationship, indicates that an object possesses a certain property. A weak is-a relationship can modeled using interfaces. • [✓] Interfaces are more flexible than abstract classes, because a subclass can extend only one superclass but can implement any number of interfaces. However, interfaces cannot contain concrete methods. The virtues of interfaces and abstract classes can be combined by creating an interface with an abstract class that implement it. Then you can use the interface or the abstract class, whichever is convenient.

Please choose 5 guidelines and discuss them in depth. For each guideline, use at least half a page for your discussion.

Homework Answers

Answer #1

Hi, Please find the solution & Rate the answer.

Cohesion: Modern programmers have realized something called as a "single responsibility principle". This concept is best understood by an example. Lets say we have a class called FileOperations. It makes a lot of sense to have all file related operations in that single class only. Some methods in the file can be "open","close","flush","save","saveCSVtoSpaceSeperated", where the last method converts comma seperated values(for some reason) to space seperated values. It would simply create havoc in the team, if there was even another class which would handle, another set of same kind of file operations.
Methods also need to have a single responsibility and not more. Example, we can never expect a method named "open" doing more than just returning a handle to a file on disk, or returning an error if it was not possible to open that file.
Similarly, the work of "flush" method should only be, flush all the data in the buffer to the HDD before closing the file. If the methods would do anything more or less than what they are supposed to, it would simply create havoc/confusion in the team.

Encapsulation: This is one of the most important principles of object oriented programming. When C language was invented, it was the procedure which was more important and not the data. Data was passed around according to the requirement of the methods. Slowly the flaws of such an approach were realized and software community moved to object oriented programming(OOP). In OOP, the object can decide how it reacts to the external environment. This is best understood by an example.

Lets say we have a variable which needs to keep track of number of passes issued at a gate. This means that, whenever a new object is created, that variable should not have a new instance of the same, which further means it should be static. Now we dont want it to get modified anywhere apart from the constructor of that object, where it is incremented every time a new object of that type is created. This means it should not be accessible outside the class. Hence, this variable needs to be private. We should also have a way of reading, how many passes were issued. Hence we need to have a getter method which simply returns the value of the object. This is the way of maintaining confidentiality of data in the program. Encapsulation makes sure what we can hide, and what we can show to the world. This is verily applicable in real life also.

Inheritance Vs Aggregation: Consider the following sentences: "Car is a vehicle, Dog is an animal, Laptop is a computer". All these are examples of "IS-A" relationships. Consider these sentences: "Car has a tyre, car has a steering wheel, dog has teeth, laptop has a processor". All these are examples of HAS-A relationships. Here is a very important point. HAS-A relationships tend to become member variables of the class for example, the car class will have a variable called tyre, and it can be an array. All the IS-A relationships tend to become child classes, example Car class will become the child class of vehicle class where vehicle class will have attributes which are common to all the vehicles, like seating capacity. So a class called Vehicle HAS-A seating capacity of "some number", otherwise there is no sense to any kind of vehicle.
class Vehicle{
private float seatingCapacity;//assume setters and getters are present as required
CapacityType type;
enum CapacityType{
LITRES,PERSONS,CC;//you can imagine
}
}
class Car extends Vehicle{//since car is a vehicle
public Car(){
setType(CapacityType.PERSONS)
//.. some other code
}
}
In the above Car is a vehicle and HAS-A seatingCapacity.
Interfaces vs Abstract classes:
As we know in Java we can Inherit from one class only, but there is no limit to the number of interfaces a class can implement. This is best explained by an example. Let's say we have an inheritance tree called Animal and Vehicle in our program. There are many animals and vehicles in the program. Let's say for example we have a method called "run" which is present in both trees. If we come up with a requirement in our program where we want to identify all the objects in our program which can simply "run", instead of creating an Abstract class and making Animal and Vehicle inherit it, it would be better if objects which can run implement an interface called "Runnable"(not java.lang.Runnable), which has a method "run". Interface is a contract to what a class can do. If some object implements an interface, it means it is capable of doing that functionality. Where as an Abstract class is an IS-A relationship. We should be very clear, when to use right concepts for better maintenance of s/w for its lifetime. Otherwise, programs don't remain maintainable.

Instance Vs Static: Instance variable is one which can have different run time states for different objects, depending upon how they are used by the program. On the contrary, static variable will have only one different states depending upon all the variables of all the objects created of the single Object. This is best explained by an example.
class Bill{
int quantity;
int pricePerQuantity;//for simplicity.
static int billsPrinted;
public printBill(){
...
billsPrinted++;//adding to the number of bills printed
return;
}
}
In the above class, the number of bills printed depends upon the number of times printBill method was called, it need not have a new value per object of Bill type created in the program.

Completeness: Let's say we have a class called FileOperations which which has methods like below.

class FileOperations{
public Handle open(){
...
}
public Data read(){
...
}
public boolean write(String data){
...
}
...
}
In the above example, we can see that the write method can only support String data, and what if the program needs to store images. In the requirements collection phase and the design phase care should be taken that classes are designed with Single responsibility principle in mind, so that a class can meet complete requirements of the software.


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a...
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a sub class's constructor? Select one: a. It must be the first statement in the constructor b. If you don't include it Java will c. If you don't include it you must have a 0 parameter constructor coded in the super class or no constructors coded at all in the super class d. The sub class constructor containing the super call and the called super...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods....
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure...
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1....
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. // code from Q1: interface Vehicle { void Start();...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Create a program that the simulates a thermometer and displays its temperature using different units. 1-...
Create a program that the simulates a thermometer and displays its temperature using different units. 1- Temperature Interface (Temperature.java) This interface should declare the following two abstract methods: • getTemperature      o Returns a String o Accepts no arguments • setTemperature      o Returns void o Accepts one double argument 2- Abstract Thermometer Class (Thermometer.java) This abstract class must implement the Temperature interface and contain: • One private field (a double) named degrees • One constructor that accepts a double...
Problem Description: Problem Description: (The Account class) Design a class named Account that contains: A private...
Problem Description: Problem Description: (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor...
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and...
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and make a java program for your bank. However, there is another type of customer that may not have money in the bank and may not in fact have a back account with this bank at all, but may have a loan here. Make another class in the application you are making called CarLoan. Carloan's should have a name, amount owed, a rate, and a...
c++ program. can you please do commenting. Question 1 [25]. (The Account Class) Design a class...
c++ program. can you please do commenting. Question 1 [25]. (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator...