# Notes

# Object Oriented Design

Class
contains fields and methods.
Fields
are related items that are stored within an object of the class, and define the object’s state.
Methods
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
carsmanagershop
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
carsmanagershop
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 = $" + 
                             finalAmount);
       }    
    }
    • 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)
    add(double,double)
    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.
    再次,编译器使用方法签名,来确定将调用绑定到哪个版本的重载方法。