Download Chapter 9: Thinking in Objects

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Science Notes
Chapter 9
Page 1 of 6
Chapter 9: Thinking in Objects
These notes are meant to accompany Introduction to Java Programming: Brief Version, seventh edition by Y.
Daniel Lang.
Programming Skills in a Nutshell:
At the end of this chapter you should have the following programming skills:
1. To design and implement your own darn classes!
Book’s Statement of Skills:
1. To create immutable objects from immutable classes to protect the contents of objects. (9.2)
2. To determine the scope of variables in the context of a class. (9.3)
3. To use the keyword this to refer to the object calling itself. (9.4)
4. To apply class abstraction to develop software. (9.5)
5. To explore the differences between the procedural paradigm and the object-oriented paradigm. (9.6)
6. To design programs using the object-oriented paradigm. (9.7 – 9.9)
Computer Science Notes
Chapter 9
Page 2 of 6
Group Project: Write a program that “does” fractions.
1. When you are presented with a programming problem like this, you first need to decide what it is
the program should do.
a. Here is a list of what a fraction program should do:
i. run
ii. change between an improper fraction like 3/2 and a whole number like 1 1/2
iii. keep on looping for more calculations
iv. return a decimal approximation
v. convert to a percentage
vi. multiply fractions
vii. simplify fractions (reduce)
viii. add fractions
ix. subtract fractions
x. divide fractions
xi. find lowest common denominator
xii. determine corresponding angle on a circle
xiii. convert to an equivalent fraction
xiv. take reciprocal of a fraction
xv. read data in from user
xvi. convert decimal to fraction
xvii. output the fraction
2. Next, you should think of how information and processes can be divided up into classes, and how
to separate what the user sees as an interface from what each class should be responsible for
doing. (Hint: Output to a console window should not be done by an object except for main objects; also
input from the keyboard)
a. Here is a list of the classes in the fraction program:
i. Fraction – represent a fraction: numerator, denominator, whole number part; also the
Fraction class should have the arithmetic functionality built into it
ii. FractionUserInterface – handles getting input from the keyboard and sending output to
the monitor
iii. Circle class??? – for the corresponding angle… save for another day
Computer Science Notes
Chapter 9
Page 3 of 6
b. Here is how the functionality of the fraction program should be split up between the classes.
i. Fraction
1. change between an improper fraction like 3/2 and a whole number like 1 ½
2. return a decimal approximation
3. convert to a percentage
4. multiply fractions
5. simplify fractions (reduce)
6. add fractions
7. subtract fractions
8. divide fractions
9. find lowest common denominator
10. determine corresponding angle on a circle
11. convert to an equivalent fraction
12. take reciprocal of a fraction
13. convert decimal to fraction
ii. FractionUserInterface
1. keep on looping for more calculations
2. read data in from user
3. output the fraction
3. Next, you should “design” the class by generating a UML diagram for its data fields, constructors,
and methods.
a. Here are UML diagrams for the fraction program classes:
Fraction
-numerator: int
-denominator: int
Fraction(n: int, d: int)
Fraction(w: int, n: int, d: int)
Fraction(w: int)
Fraction()
Fraction(x: double)
Fraction(f: Fraction)
The Fraction class represents a ratio of integers. The
ratio may be an improper fraction whose whole
number part will be calculated “on the fly.” The
Fraction class is immutable, which means that its
values can not be changed after it is created.
The numerator of this Fraction. It may be negative.
The denominator of this Fraction. It may be negative.
Constructs a Fraction object with a numerator of n and
a denominator of d.
Constructs a Fraction object with a whole number part
of w, a numerator of n, and a denominator of d. Note:
this will have to be converted to an improper Fraction,
given the design of our data fields.
Constructs a Fraction object with a whole number part
of w. Note: this will have to be converted to an
improper Fraction, given the design of our data fields.
Constructs a default Fraction object of 0 / 1.
Constructs a Fraction object that is the closest
fractional equivalent of the real number x. Since
integers are used for the data fields, the Fraction object
will be the closest fraction with a denominator of
2,000,000,000.
Constructs a Fraction object that is equal to a given
Fraction object f.
Computer Science Notes
Chapter 9
convertFromImproperToMixed(): Fraction
convertFromMixedToImproper()
getDecimalApproximation(): double
getPercentApproximation(): double
multiply(f: Fraction): Fraction
reduce(): Fraction
add(f: Fraction): Fraction
subtract(f: Fraction): Fraction
divide(f: Fraction): Fraction
lcm(n1: int, n2: int): int
gcd(n1: int, n2: int): int
getCircleAngle(): double
convertToFraction(x: double): Fraction
getReciprocal(): Fraction
getEquivalentFraction(n1: int): Fraction
toString(): String
getReducedWholeNumberPart(): int
getReducedImproperNumerator(): int
getReducedProperNumerator(): int
getReducedDenominator(): int
Page 4 of 6
We will not implement this method, since our original
Fraction object does not have a whole number part.
We will not implement this method, since our original
Fraction object does not have a whole number part.
Returns a decimal approximation of this Fraction
Returns a percent approximation of this Fraction
Returns a Fraction object that is the product of this
Fraction and another given Fraction f.
Returns a Fraction that is the reduce Fraction of this
Fraction.
Returns a Fraction object that is the sum of this
Fraction and another given Fraction f.
Returns a Fraction object that is the difference of this
Fraction and another given Fraction f.
Returns a Fraction object that is the quotient of this
Fraction and another given Fraction f.
Returns the least common multiple of n1 and n2. (a
static method…)
Returns the greatest common divisor of n1 and n2. (a
static method…)
Returns the corresponding angle on a circle of this
Fraction
Returns a Fraction object that is the equivalent of the
decimal number x.
Returns a Fraction object that is the reciprocal of this
Fraction.
Returns a Fraction object that is equivalent to this
Fraction when numerator and denominator are
multiplied by n1.
Returns a String representation of this Fraction object.
Returns the whole number part of the simplified form
of this Fraction. i.e., if someone constructed a Fraction
of -2 + (-14)/(-4), this method would return +1.
Returns the numerator of the simplified form of this
Fraction when the fraction is thought of as an
improper fraction. i.e., if someone constructed a
Fraction of -2 + (-14)/(-4), this method would return
+3.
Returns the numerator of the simplified form of this
Fraction when the fraction is thought of as a mixed
number. i.e., if someone constructed a Fraction of -2 +
(-14)/(-4), this method would return +1.
Returns the numerator of the simplified form of this
Fraction when the fraction is thought of as a mixed
number. i.e., if someone constructed a Fraction of -2 +
(-14)/(-4), this method would return +2.
Computer Science Notes
Chapter 9
ClassName
DataField1
DataField2
DataFieldEtc
Constructor1
Constructor2
ConstructorEtc
Method1
Method2
Method3
MethodEtc
4. Lastly, you implement your design as source code.
a. (Do this for the fraction program in eclipse now…)
Page 5 of 6
Computer Science Notes
Chapter 9
Page 6 of 6
Section 9.1: Introduction
This chapter focuses on designing and implementing your own classes as opposed to using classes that have
already been implemented.
Section 9.2: Immutable Objects and Classes
Section 8.2.11 Problem: Checking Palindromes

See www.cs.armstrong.edu/liang/intro7e/book/CheckPalindrome.java
Section 9.3: The Scope of Variables
Section 9.4 The this Reference
Section 9.5 Class Abstraction and Encapsulation

See www.cs.armstrong.edu/liang/intro7e/book/TestLoanClass.java

See www.cs.armstrong.edu/liang/intro7e/book/Loan.java
Section 9.6 Object-Oriented Thinking

See www.cs.armstrong.edu/liang/intro7e/book/BMI.java
Section 9.7 Designing the Course Class

See www.cs.armstrong.edu/liang/intro7e/book/TestCourse.java

See www.cs.armstrong.edu/liang/intro7e/book/Course.java
Section 9.8 Designing a Class for Stacks

See www.cs.armstrong.edu/liang/intro7e/book/TestStackOfIntegers.java

See www.cs.armstrong.edu/liang/intro7e/book/StackOfIntegers.java
Section 9.9 Designing the GuessDate Class

See www.cs.armstrong.edu/liang/intro7e/book/UseGuessDateClass.java

See www.cs.armstrong.edu/liang/intro7e/book/GuessDate.java