Notes

advertisement
COMP 121
Week 01
Agenda
• Introductions
• Review policies, procedures, and
expected outcomes
• Learning Activities
• Introduce homework problems
Introductions
Policies, Procedures
and Expected Outcomes
Administrivia
Course Outcomes
1. Apply the advanced object-oriented principles
of inheritance and polymorphism to analyze
real-world problems and design, implement,
and test solutions.
2. Detect and correct runtime program errors
using exceptions.
3. Read and write random access and sequential
files of text and binary data.
Administrivia
Course Outcomes (continued)
4. Analyze space and time complexity of algorithms.
5. Design, implement, test, debug, and document linear
data structures.
6. Use lists, stacks, and queues to solve problems.
7. Recognize and apply the design patterns of Strategy,
Template Method, Iterator, and Adapter in objectoriented designs.
8. Discuss relationships between the disparate topic
areas addressed in this course.
Administrivia
Texts
Big Java 3rd Edition, Cay Horstmann, ISBN: 9780-470-10554-2 (2nd Edition is acceptable)
Head First Design Patterns, Eric Freeman and
Elisabeth Freeman, ISBN: 0-596-00712-4
Objects, Abstraction, Data Structures, and Design
Using Java, Elliot Koffman and Paul Wolfgang,
ISBN: 0-471-46756-1
Administrivia
Points breakdown
Pct
Type
Count
Each
Total
10
15
150
35% Labs
5
70
350
45% Exams
3
150
450
5%
1
50
50
15% Homework
Reflection Paper
1000
Weekly Activities
1.
2.
3.
4.
5.
6.
7.
Check course announcements and bulletin board
postings
Read the associated sections from the text books
and key points
Complete the weekly learning activities in class
Read and consider the weekly homework problems
Complete the homework assignment
Work on any scheduled lab assignments
Note significant learning for reflection paper
Weekly Activities
Exam
Lab
Homework
Discussion
Learning activities
Readings
• Course is carefully
designed such that
each activity lays the
foundation for the
next
• Skipping activities is
considered harmful
• I will assume the
lower layers when
discussing the higher
layers
Major Themes
• Object-oriented design
• Design patterns
• Test-driven development
• Linear data structures
This Week’s Outcomes
• Review the fundamental object-oriented
principles of encapsulation, composition, and
abstraction.
• Review the algorithmic building blocks of
methods, selection, and repetition.
• Review the properties, operations, and use of
primitive data types, strings, and arrays.
• Use an integrated development environment to
edit, compile, run, submit, and correct a Java
program.
Guided Learning
Activity Solutions for Week 01
Learning Activities
Activity 1-1
• Outcome: Review the fundamental
object-oriented principles of
encapsulation, composition, and
abstraction.
Learning Activities
Q: Consider the following problem description, and decide
what classes you should use to implement it. For each
class, also list its methods and instance fields.
A user places coins in a vending machine and selects a
product by pushing a button. If the inserted coins are
sufficient to cover the purchase price of the product,
the product is dispensed and change is given.
Otherwise, the inserted coins are returned to the user.
The user also has an option to cancel the sale and
have his or her coins returned.
Learning Activities
CoinCollector
Product
-price : double
+getPrice() : double
+setPrice() : void
-numQuarters : int
-numDimes : int
-numNickels : int
+getTotal() : double
+dispenseCoins(in amount : double) : void
+insertCoin(in value : int) : void
Controller
+dispenseProduct() : void
+cancelSale() : void
Learning Activities
Q: Employees receive their paychecks biweekly.
Hourly workers are paid their hourly rates for
each hour worked; however, anyone who
works more than 40 hours per week is paid
overtime at 150% of the regular wage.
Salaried employees simply get the two-week
portion of their yearly salary regardless of how
many hours they work.
Learning Activities
HourlyEmployee
-hourlyWage : double
-hoursWorked : int
+getHourlyWage() : double
+getHoursWorked() : int
+calculatePay() : double
ExemptEmployee
-salary : int
+getSalary() : int
+calculatePay() : double
Learning Activities
Q: Customers order products from a store.
Invoices are generated to list the items,
payments received, and amount still due.
Products are shipped to the shipping
address of the customer, and invoices
are sent to the billing address.
Learning Activities
Address
-streetAddr : string
-city : string
-state : string
-zip : string
+getStreetAddr() : string
+getCity() : string
+getState() : string
+getZip() : string
Customer
-name : string
-mailingAddr : Address
-billingAddr : Address
+getName() : string
+getBillingAddr() : Address
+getBillingAddr()
Invoice
-customer : Customer
-lineItems : LineItem
-orderTotal : double
-paymentsReceived : double
-amountOwed : double
+getCustomer() : Customer
+getLineItem() : LineItem
+getOrderTotal() : double
+getPaymentsReceived() : double
+getAmountOwed() : double
LineItem
Product
-product : Product
-qty : int
+getProduct() : Product
+getQty() : int
-name : string
-unitPrice : double
+getName() : string
+getUnitPrice() : double
Learning Activities
Activity 1-2
Outcome: Review the algorithmic building
blocks of methods, selection, and
repetition.
Q: Describe the selection control structures
available within Java.
Learning Activities
A:
if (condition) {
…
} else {
…
}
switch (selector) {
case label: statements; break;
case label: statements; break;
default: statements;
}
Q: Describe the repetition control structures available within Java.
Learning Activities
A:
while (condition) {
…
}
do {
…
} while (condition);
for (initialization; condition; update) {
…
}
Q: In the following expression, what happens if num is 0?
(num != 0 && num / num)
Learning Activities
A: It looks like an ArithmeticException should be
thrown because of division by zero, but
because of short-circuiting, the second half of
the expression is never evaluated.
Q: What is the purpose of a break statement?
What is the purpose of a continue statement?
Learning Activities
A: The purpose of a break statement is to
end execution of a loop immediately.
The purpose of a continue statement is
to end execution of the current iteration,
and begin the next iteration immediately.
Learning Activities
Activity 1-3
Outcome: Review the properties,
operations, and use of primitive data
types, strings, and arrays.
Q: List and describe the primitive types in Java.
Learning Activities
A:
Data type
Range
Size
byte
-128 to 127
1 byte
short
-32768 to 32768
2 bytes
int
-2147483648 to 2147483647
4 bytes
long
-9223372036854775808 to 922337203685477807
8 bytes
float
-3.4E38 to 3.4E38 with 7 digits accuracy
4 bytes
double
-1.7E308 to 1.7E308 with 17 digits accuracy
8 bytes
char
0 to 65535
2 bytes
boolean
true/false
Q: In Java, how do declare a variable to be constant?
Learning Activities
A: With the keyword final.
Q: What does each of the following
operators do?
Learning Activities
Operator
Use/Meaning
[]
Indexes into arrays
()
Controls order of operations
.
Accesses members of objects
++
Increment
--
Decrement
!
Logical NOT
new
Creates new objects
*
Multiplication
/
Division
%
Modulo Division
<
Less Than
>=
Greater Than or Equal To
==
Compare For Equality
=
Assignment
!=
Not Equal To
&&
Logical AND
||
Logical OR
Learning Activities
Q: Given the following three statements:
String greeting = “hello”;
String welcome = greeting;
String salutations = “hello”;
What are the truth-values of
(greeting == welcome) and
(greeting == salutations)?
Learning Activities
A: As it turns out, they’re both true because of
the specific details of how the String class
works. However, you should not use == to
compare the value of objects.
Q: If you had a long String that contained an
entire paragraph of text, how would you go
about breaking it up into individual words?
Learning Activities
A:
String [] words = text.split(“ “);
Learning Activities
Q: Write tests for the flight control
system, and repair the parts that
have been sabotaged.
Homework Problems
BlueJ Debugger Demo
Homework 01
• No homework this week!
• Install and review the software
• Java Development Kit (JDK)
• JDK Documentation
• BlueJ IDE
• Activity Files
Question and Answer Session
Download