CS 180 Problem Solving and OO Programming Fall 2010

advertisement
CS 180 Problem Solving and Object Oriented
Programming
Fall 2011
http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2011/
Notes for Week 3:
September 5-9, 2011
Aditya Mathur
Department of Computer Science
Purdue University
West Lafayette, IN, USA
This week:
1. Quiz
2. Questions/Review
3. Writing simple programs
to solve simple problems
Readings and Self-help Exercises for Week 3
Readings:
Chapter 3: 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7
Self help Exercises [Not to be turned in]:
3.1, 3.2, 3.3, 3.7, 3.8, 3.23.
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
2
Announcements
Feast with faculty: To resume on September 14.
Monday lecture by Dr Korb. Type conversions and handling
strings.
Project 1 deadline Friday September 23, 201.
Late submission is not in your best interest.
Programs in the textbook are now available for you
to download and play with. Go to the course web
site and click on the link marked Textbook Programs
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
3
Programming Competition
Please send me (apm@cs.purdue.edu) mail to
register. I need names of the team members and of
the team leader.
Winning team gets a free:
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
4
Questions?
Frequency Code for your iClicker: AA
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
5
Quiz
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
6
Q1. The value of a will be 0 after which one
of the following assignments?
(a) float a=(float)1/2;
(b) float a=1/2;
(c ) int a=1/2;
Correct answer: c
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
7
Q2. Which one of the following is not a
primitive type in Java?
(a) int
(b) Float
(c) float
(d) double
Correct answer: b
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
8
Q3. What is the value of the expression 7%2?
(a) 1
(b) 0
(c) 2
(d) 3
Correct answer: a
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
9
Q4. Given int a=4; what is the value of the
expression a<<2?
(a) 8
(b) 16
(c) 32
(d) 64
Correct answer: b
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
10
Let us write a simple Java program:
The problem
Write a Java program to compute the
net sale in dollars given the price of
each ticket in dollars and the number
of tickets sold.
Assume that a randomly selected
discount of between 3 to 7% is given
on any sale.
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
11
Problem: Understanding
This is an easy problem!
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
12
Problem: Design of solution
Keep this step independent of Java!
Step 1: Get data [price of a ticket and total tickets sold]
Step 2: Compute total sale[multiply the items input]
Step 3: Find discount to be given [3 to 7%]
Step 4: Compute discounted sale price [=total sale-(total
sale*discount)].
Step 5: Display the discounted sale price.
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
13
Problem: Refine solution: Get data
Keep this step independent of Java!
Step 1.1: Prompt for price
Step 1.2: Read price
Step 1.3: Prompt for number of tickets sold
Step 1.4: Read number of tickets sold
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
14
Problem: Refine solution: Compute total sale
Keep this step independent of Java!
Step 2: Compute
Total sale=price of a ticket * number of tickets sold
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
15
Problem: Refine solution: Compute discount
Keep this step independent of Java!
Step 3: Generate a random number (say, r) between 0 and 1
and convert this to a number (say, d) between 3 and 7.
d=mr+c [Remember the straight line y=mx+c?]
d
7
m= ?
c= ?
3
0
9/7/2011
1
©Aditya Mathur. CS 180. Fall 2011. Week 3
r
16
Problem: Refine solution: Compute discounted
price
Keep this step independent of Java!
Step 4: Compute
discounted price= total price-(total
price*discount)
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
17
Problem: Refine solution: Display discounted price
Keep this step independent of Java!
Step 5: Display discounted price (as easy as
that!!).
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
18
Code the solution in Java.
Test it.
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
19
Week 3: September 5-9, 2011
Hope you enjoyed this week!
Questions?
Contact your recitation instructor. Make
full use of our office hours.
9/7/2011
©Aditya Mathur. CS 180. Fall 2011. Week 3
20
Download