Question

In: Computer Science

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.

Solutions

Expert Solution

Java Class Designing Guidelines:

Class is a basic principle in object-oriented programming. class is an encapsulation of data values and the methods used for those values. By which we can bind them together to get the security, reusability and also extensibility of the objects.

Class Design principles help the programmer to design a class in a way to achieve principles like reusability, extensibility, correctness and easy maintenance of the class.

Here, we are discussing the following proinciples:

Cohesion:

  • cohesion means uniting all substances together.
  • in class, the attributes and the methods must cohesive together to achieve the goal.
  • i.e., the functions or the methods in the class should cooperate together to attain the purpose.
  • if the methods have not cooperated with each other the class is said to be less cohesive. then the class is said to be poorly designed.
  • it is property describes the boundness of the methods. strong cohesion is a desirable phenomenon to achieve well-defined abstraction.
  • to achieve cohesion the class should contain ess functionality.c
  • cohesion itself gives the data hiding feature. the class with high cohesion also has a strongly bonded function and also attributes that are having default visibility.

Encapsulation:

  • "Class itself is binding of attributes and methods" it is a basic concept for every designing principle.
  • Encapsulation also knew as information hiding in a closed module. which is also refers to designing a class in a way to bond the attributes and methods securely within a closed module which can be accessible by a class object only.
  • encapsulation can be achieved using the private, protected and public keywords.
  • these are the access specifiers that restrict the usage of class members.
  • let us discuss the below example,

class Student{

public int no;

public string name;

}

the class members can be accessible by the object of the class like,

Student s;

s.no=10;

s.name="steven";

here the class variables have direct access in other classes. i.e., the information is not hidden properly. the public members of the class can be accessed by anyone.

Now see the above example in another way.

class Student

{

private int no;

private String name;

public void getNo(){ return no;}

public int setNo(int num)

{

no=num;

}

public int setName(int nm)

{

name=nm;

}

public void grtName(){ return Name;}

}

Student s;

s.no=10;// not allowed

s.name="steven";//not allowed shows error

s.setNo(10);

s.setName("steven");

in the above example, the private members of the class cannot be accessed directly. to access them the getter and setter methods will help.

Consistency:

  • Following the naming conventions allows the compilers to understand the program clearly.
  • The readability of the program also increases. and it is helpful for future programmers who are want to enhance the coding.
  • Naming conventions give consistency in the coding.some of the conventions are listed below.
  • class name must start with a capital letter. every word in the class name must starts with a capital letters. for example, ClassName, Classname,
  • every variable must start with the alphabet only. for ex, var1,stdno.s1.
  • 1s,2no are not allowed.
  • every method name's first letter must be a small letter. the following words the first letter can be capital. for ex, getNo(), setName().

Inheritance:

  • Inheritance is the process of using the one class properties in another class.
  • the class which is inherited is known as child class or subclass.
  • the class from which the child class inherited is known as parent class or superclass.
  • all the members of the parent class can be accessed by its child class except the private members.
  • Inheritance is used to enhance the base class properties and also for the reusability of the already defined class.
  • the example is given below.

class Student //Base class

{

private int no;

private String name;

public void getNo(){ return no;}

public int setNo(int num)

{

no=num;

}

public int setName(int nm)

{

name=nm;

}

public void grtName(){ return Name;}

}

in the above example we can add the student marks or for student marks we can use the already existed student class information.

class StudentMarks extends Student

{

private float marks[6];

}

we can access the getter and setter methods of parent class in base class also.

Clarity:

  • As mentioned in the question, cohesion, completeness, consistency, and encapsulation are enough to achieve the clarity.
  • also the program should be well formatted and commented.
  • the comments are useful to understand the programmer's view to the user's.
  • the design also labelled properly.
  • the shortcuts of the words will give confusion.if we used a shortcut.ther should be a comment which describes it properly.

Related Solutions

JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the following instance variables: employee’s name reportID: this should be unique. The first reportID must have a value of 1000 and for each new reportID you should increment by 10. hourly wage Include a suitable collection of constructors, mutator methods, accessor methods, and toString method. Also, add methods to perform the following tasks: Compute yearly salary - both the gross pay and net pay Increase...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
Define what is coupling and cohesion. From the design perspective, what should be the goals with...
Define what is coupling and cohesion. From the design perspective, what should be the goals with regard to coupling and cohesion? Explain with your own examples highlighting the difference between them
Write a java program using the information given Design a class named Pet, which should have...
Write a java program using the information given Design a class named Pet, which should have the following fields (i.e. instance variables):  name - The name field holds the name of a pet (a String type)  type - The type field holds the type of animal that a pet is (a String type). Example values are “Dog”, “Cat”, and “Bird”.  age - The age field holds the pet’s age (an int type) Include accessor methods (i.e. get...
Write a Java application, and an additional class to represent some real-world entity such as a...
Write a Java application, and an additional class to represent some real-world entity such as a technology item, an animal, a person, a vehicle, etc. Keep in mind that a class is a model in code of something real or imagined, which has attributes (member variables) and behaviors (member methods). The class will: Create a total of 5 member variables for the class, selecting the appropriate data types for each field. For example, a class to represent a lamp might...
In Java: Design a class that checks if a String is made of tokens of the...
In Java: Design a class that checks if a String is made of tokens of the same data type (for this, you may only consider four data types: boolean, int, double, or char). This class has two instance variables: the String of data and its delimiter. Other than the constructor, you should include a method, checkTokens, that takes one parameter, representing the data type expected (for example, 0 could represent boolean, 1 could represent int, 2 could represent double, and...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT