8.1 Choosing Classes Other classes are abstractions of real-life entities. BankAccount Cash Register...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
8.1 ■ Choosing Classes Other classes are abstractions of real-life entities. • BankAccount • Cash Register 337 For these classes, the properties of a typical object are easy to understand. A Rectangle object has a width and height. Given a BankAccount object, you can deposit and withdraw money. Generally, concepts from the part of the universe that a program concerns, such as science, business, or a game, make good classes. The name for such a class should be a noun that describes the concept. Some of the stan- dard Java class names are a bit strange, such as Ellipse2D.Double, but you can choose better names for your own classes. Another useful category of classes can be described as actors. Objects of an actor class do some kinds of work for you. Examples of actors are the Scanner class of Chapter 4 and the Random class in Chapter 6. A Scanner object scans a stream for numbers and strings. A Random object generates random numbers. It is a good idea to choose class names for actors that end in "-er" or "-or". (A better name for the Random class might be RandomNumber Generator.) Very occasionally, a class has no objects, but it contains a collection of related static methods and constants. The Math class is a typical example. Such a class is called a utility class. Finally, you have seen classes with only a main method. Their sole purpose is to start a program. From a design perspective, these are somewhat degenerate exam- ples of classes. What might not be a good class? If you can't tell from the class name what an object of the class is supposed to do, then you are probably not on the right track. For example, your homework assignment might ask you to write a program that prints paychecks. Suppose you start by trying to design a class PaycheckProgram. What would an object of this class do? An object of this class would have to do everything that the homework needs to do. That doesn't simplify anything. A bet- ter class would be Paycheck. Then your program can manipulate one or more Paycheck objects. Another common mistake, particularly by students who are used to writing pro- grams that consist of functions, is to turn an action into a class. For example, if your homework assignment is to compute a paycheck, you may consider writing a class ComputePaycheck. But can you visualize a "ComputePaycheck" object? The fact that "ComputePaycheck" isn't a noun tips you off that you are on the wrong track. On the other hand, a Paycheck class makes intuitive sense. The word "paycheck" is a noun. You can visualize a paycheck object. You can then think about useful meth- ods of the Paycheck class, such as computeTaxes, that help you solve the assignment. SELF CHECK 1. What is the rule of thumb for finding classes? 2. Your job is to write a program that plays chess. Might ChessBoard be an appropriate class? How about MovePiece? 8.1 ■ Choosing Classes Other classes are abstractions of real-life entities. • BankAccount • Cash Register 337 For these classes, the properties of a typical object are easy to understand. A Rectangle object has a width and height. Given a BankAccount object, you can deposit and withdraw money. Generally, concepts from the part of the universe that a program concerns, such as science, business, or a game, make good classes. The name for such a class should be a noun that describes the concept. Some of the stan- dard Java class names are a bit strange, such as Ellipse2D.Double, but you can choose better names for your own classes. Another useful category of classes can be described as actors. Objects of an actor class do some kinds of work for you. Examples of actors are the Scanner class of Chapter 4 and the Random class in Chapter 6. A Scanner object scans a stream for numbers and strings. A Random object generates random numbers. It is a good idea to choose class names for actors that end in "-er" or "-or". (A better name for the Random class might be RandomNumber Generator.) Very occasionally, a class has no objects, but it contains a collection of related static methods and constants. The Math class is a typical example. Such a class is called a utility class. Finally, you have seen classes with only a main method. Their sole purpose is to start a program. From a design perspective, these are somewhat degenerate exam- ples of classes. What might not be a good class? If you can't tell from the class name what an object of the class is supposed to do, then you are probably not on the right track. For example, your homework assignment might ask you to write a program that prints paychecks. Suppose you start by trying to design a class PaycheckProgram. What would an object of this class do? An object of this class would have to do everything that the homework needs to do. That doesn't simplify anything. A bet- ter class would be Paycheck. Then your program can manipulate one or more Paycheck objects. Another common mistake, particularly by students who are used to writing pro- grams that consist of functions, is to turn an action into a class. For example, if your homework assignment is to compute a paycheck, you may consider writing a class ComputePaycheck. But can you visualize a "ComputePaycheck" object? The fact that "ComputePaycheck" isn't a noun tips you off that you are on the wrong track. On the other hand, a Paycheck class makes intuitive sense. The word "paycheck" is a noun. You can visualize a paycheck object. You can then think about useful meth- ods of the Paycheck class, such as computeTaxes, that help you solve the assignment. SELF CHECK 1. What is the rule of thumb for finding classes? 2. Your job is to write a program that plays chess. Might ChessBoard be an appropriate class? How about MovePiece?
Expert Answer:
Answer rating: 100% (QA)
1 The rule of thumb for finding classes is to look for reallife entities or concepts and to try to name the class based on a noun that describes the c... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
Case Study: Quick Fix Dental Practice Technology requirements Application must be built using Visual Studio 2019 or Visual Studio 2017, professional or enterprise. The community edition is not...
-
The Crazy Eddie fraud may appear smaller and gentler than the massive billion-dollar frauds exposed in recent times, such as Bernie Madoffs Ponzi scheme, frauds in the subprime mortgage market, the...
-
Is astrology scientific? The University of Chicago's General Social Survey (GSS) is the nation's most important social science sample survey. The GSS asked a random sample of adults their opinion...
-
Is there any truth in the saying that the price of a good is a reflection of its quality?
-
Property Failures Find a subset of R2 fitting each description. 1. Closed under vector addition but not under scalar multiplication? 2. Closed under scalar multiplication but not under vector...
-
What is an external stakeholder? Provide an example of a situation where pressure from external stakeholders has pushed managers to engage with environmental issues.
-
From the data in Table 14-2 in the text calculate the average annual inflation rate as measured by the CPI for the following years: (a) End of 1973 to end of 1982 (b) End of 1980 to end of 1989 (c)...
-
NIOSH Lifting Guide. Considering the work of airport baggage handlers working on the tarmac who are responsible for loading luggage from a trolley (start of the lift, Condition A) onto the conveyor...
-
On March 1, 2008, Ginny Tyler established Seltzer Realty. Ginny completed the following transactions during the month of March: a. Opened a business bank account with a deposit of $30,000 from...
-
Spade Millhone Detective Agency performs investigative work for a variety of clients. Recently, Alban Insurance Company asked Spade Millhone to investigate a series of suspicious claims for whiplash....
-
An integration method in which the computation of \(x_{i+1}\) is based on the equilibrium equation at \(t_{i}\) is known as a. explicit method b. implicit method c. regular method
-
Fill in the Blanks. Numerical methods that require the use of a time step \((\Delta t)\) smaller than a critical value \(\left(\Delta t_{\text {cri }} ight)\) are said to be ______________ stable.
-
Enid operates an animal shelter as a sole proprietor which generates net revenue of $88,000. Enid has also made a self-employed health insurance payment of $6,000. What is Enids qualified business...
-
Does statelessness exist in wealthy countries such as the United States? How does the status of the "Dreamers" (people who entered the United States illegally as young children) compare with that of...
-
Assess the changes in our understanding of gender in the past decades and relate them to changes in family and economy.
-
40 What is true about the relationship between performance evaluation and motivation? Choose 1 answer Performance evaluations have little potential for impacting motivation. The relationship does not...
-
Why is inventory management important for merchandising and manufacturing firms and what are the main tradeoffs for firms in managing their inventory?
-
In the article "Waves of Rural Brides: Female Marriage Migration in China" (Annals of the Association of American Geographers, Vol. 88(2), pp. 227-251), C. Fan and Y. Huang reported on the reasons...
-
In the paper Reassessment of TL Age Estimates of Burnt Flint from the Paleolithic Site of Tabun Cave, Israel (Journal of Human Evolution, Vol. 45, Issue 5, pp. 401409), N. Mercier and H. Valladas...
-
In a Zogby International Poll, conducted in early 2006 in conjunction with Le Moyne Colleges Center for Peace and Global Studies, roughly 29% of the 944 military respondents serving in Iraq in...
-
Why is communications management so important during project execution? What is the difference between formal and informal communications? Why are nonverbal communications so important?
-
What is involved in managing quality, and how does it affect project execution? What are tools and techniques used in performing quality assurance? How did Kristins team use one of these tools to...
-
Find an example of a large project that took more than a year to complete. Describe some of the planning work completed for the project as part of project integration and scope management. Summarize...
Study smarter with the SolutionInn App