# Notes

# Object Oriented Design

contains fields and methods.
are related items that are stored within an object of the class, and define the object’s state.
are actions or behaviors that may be performed by an object of the class.

When developing an object-oriented application, one of your first tasks is to identify the classes that you will need to create (your domain). Typically, your goal is to identify the different types of real-world objects that are present in the problem, and then create classes for those types of objects within your application.

Over the years, software professionals have developed numerous techniques for finding the classes in a given problem. One simple and popular technique involves the following steps:

  1. Get a written description of the problem domain.
  2. Identify all the nouns (including pronouns and noun phrases) in the description.
    Each of these is a potential class.
  3. Refine the list to include only the classes that are relevant to the problem.

# What to include in a class?

If a noun represents a type of item that would not have any identifiable fields or methods, then it can probably be eliminated from the list. To help determine whether a noun represents an item that would have fields and methods, ask the following questions about it:

  • Would you use a group of related values to represent the item’s state?
  • Are there any obvious actions to be performed by the item?

# Example of a problem domain

Suppose we are programming an application that the manager of Joe’s Automotive Shop will use to print service quotes for customers. Here is a description that an expert, perhaps Joe himself, might have written:

Joe’s Automotive Shop services foreign cars, and specializes in servicing cars made by Mercedes, Porsche, and BMW. When a customer brings a car to the shop, the manager gets the customer’s name, address, and telephone number. Then the manager determines the make, model, and year of the car, and gives the customer a service quote. The service quote shows the estimated parts charges, estimated labor charges, sales tax, and total estimated charges.

The problem domain description should include any of the following:

  • Physical objects such vehicles, machines, or products
  • Any role played by a person, such as manager, employee, customer, teacher, student, etc
  • The results of a business event, such as a customer order, or in this case a service quote
  • Recordkeeping items, such as customer histories and payroll record

Identifying All of the nouns (possible classes!!)

The next step is to identify all of the nouns and noun phrases. (If the description contains pronouns, include them too.) Here’s another look at the previous problem domain description. This time the nouns and noun phrases appear in bold.

Joe's Automotive Shop services foreign cars , and specializes in servicing cars made by Mercedes , Porsche , and BMW. When a customer brings a car to the shop , the manager gets the customer's name , address , and telephone number. Then the manager determines the make , model , and year of the car , and gives the customer a service quote. The service quote shows the estimated parts charges , estimated labor charges , sales tax , and total estimated charges.

Notice above that some of the nouns are repeated.

List now nouns without duplication. Refine list as well. Eliminate redundancies. Eliminate possible primitive types.

addressforeign carsPorsche
BMWJoe's Automotive Shopsales tax
carmakeservice quote
customerMercedestelephone number
estimated labor chargesmodeltotal estimated charges
estimated parts chargesnameyear

True class attributes!! Needed classes remain as follows…

addressforeign carsPorsche
BMWJoe's Automotive Shopsales tax
carmakeservice quote
customerMercedestelephone number
estimated labor chargesmodeltotal estimated charges
estimated parts chargesnameyear

# Review

Chapter 6 OOP

  1. Methods that operate on an object's fields are called

    • Private methods
    • Instance methods
    • Instance variables
    • Public methods


    • Private 方法
    • 实例方法 ✔️
    • 实例变量
    • Public 方法
  2. A constructor

    • Has return type of void
    • Always accepts two arguments
    • Always has an access specifier of private
    • Has the same name as the class


    • 返回类型为 void
    • 一律接受两个参数
    • 始终具有私有的访问权限
    • 与类同名 ✔️
  3. A method that gets a value from a class's field but does not change it is known as a mutator method.

    从类的字段获取值,但不更改它的方法称为 mutator 方法。

  4. Given the code below, what is the value of finalAmount when it is displayed?

    public class Order {    
       private int orderNum;
       private double orderAmount;
       private double orderDiscount;    
       public Order(int orderNumber, double orderAmt,double orderDisc) {    
          orderNum = orderNumber;
          orderAmount = orderAmt;
          orderDiscount = orderDisc;
       public int getOrderAmount() {
          return orderAmount;
       public int getOrderDisc() {
          return orderDiscount;
    public class CustomerOrder {        
       public static void main(String[] args) {    
          int ordNum = 1234;
          double ordAmount = 580.00;
          double discountPer = .1;
          Order order;
          double finalAmount = order.getOrderAmount() -
                 order.getOrderAmount() * order.getOrderDisc();
          System.out.println("Final order amount = $" + 
    • 580.00
    • 528.00
    • There is no value because the object order has not been created
    • There is no value because the constructor has an error

    给定以下代码, finalAmount 显示时,它的值是什么?
    没有值,因为尚未创建 Order 对象

  5. In UML diagrams, this symbol indicates that a member is private.

    • #
    • -
    • +
    • \
  6. What is Data Hiding, how why is it important, in other words, why consider it for any instance fields of a class?

    • What is Data Hiding:
      Allows direct access and any allowed changes to the objects internal data by the class object itself.
      Create private fields to allow for data hiding.
    • how why is it important?
      Data hiding is important because classes are typically used as components in large software systems, involving a team of programmers.
      Data hiding helps enforce the integrity of an object's internal data.
  7. What is stale data and how would avoid it?

    • What is stale data?
      Stale data is simply data that may not be recent and hence may not be good for any calculations involving up to date information from the given data source (ex. instance variable (field)).
    • how would avoid it?
      To avoid stale data, it is best to calculate the value of that data within a method rather than store it in a variable.
  8. Distinguish between a no-argument constructor and a default constructor. How are constructors distinguished from normal methods of a class?

    A constructor that does not accept arguments is known as a no-arg constructor. It usually intializes class level variables.
    不接受参数的构造函数称为无参数构造函数。 它通常初始化类级别的变量。
    The default constructor is a constructor that the Java compiler adds to your code if no explicit constructor is available. The default constructor is consider a no-arg constructor.
    默认构造函数是 Java 编译器在没有显式构造函数可用时,将其添加到您的代码中的构造函数。 默认构造函数被视为无参数构造函数。
    Ex. no-arg constructor programmer defined
    例如 无参数构造函数程序员定义

    public Rectangle() {
      length = width = 1.0;
  9. What are overloaded methods? Why may they be used in a program?

    • What are overloaded methods?
      It is where two or more methods (incl. the constructor) in a class have the same name but differ in their parameter lists.
    • Why may they be used in a program?
      They are popular because of the increased readability of the code and thus cleaniness of the code base.
      Moreover method overloading is important because sometimes you need several different ways to perform the same operation (ex. adding two doubles versus two ints).
      此外,方法重载很重要,因为有时您需要几种不同的方式来执行同一操作(例如,添加两个 double 和两个 int)。
  10. Given the code below how would a compiler now which method to call at runtime? Hint- check out what binding and method signatures mean in the chapter.

    public int add(int num1, int num2)
        int sum = num1 + num2;
        return sum;
    public String add (String str1, String str2)
        String combined = str1 + str2;
        return combined;

    给定下面的代码,编译器现在将如何在运行时调用哪个方法? 提示 - 在本章中了解什么是绑定和方法签名。
    The compiler knows which version of the overloaded method to trigger at run time by the differing metho signature, that is by the differences in the parameter list.
    Ex. of differing parameter lists of method calls
    例如 方法调用的不同参数列表
    add(int, int)
    The process of matching a method call with the correct method is known as binding.
    Again the compiler uses the method signature to determine which version of the overloaded method to bind the call to.