Lesson 2 – Algorithms and Pseudocode

When you come in
- Connect to the Internet
- Log in to Piazza
- Start Bluej
- Navigate to laughton.com/obrien/sjsu/cs46a/CRT_lessons/ and open this lesson
Concepts - Algorithms
A sequence of instructions taken to solve a problem
An algorithm must be:
- unambiguous
- executable
- terminating
Concepts - Pseudocode
- A combination of English and Java-like code
- Used in designing an algorithm
- Easier to think about the steps involved before worrying about the exact syntax
Activity - Reading Pseudocode
Record your participation in Piazza clicker question Lesson2 Q1
Here is pseudocode to determine if a student passed or failed
If student's grade is greater than or equal to 73
Print "passed"
else
Print "failed"
What is printed when the grade is 73, 72, 71? Mark the correct answer in Piazza Lesson2 Q1
- passed, passed passed
- failed, failed, failed
- passed, passed, failed
- passed, failed, failed
Activity - Designing an Algorithm in Pseudocode
Record your participation in Piazza clicker question Lesson2 Q2
Design an algorithm to build this Duplo staircase using pseudocode.
Write it on a piece of paper or in a text file

Activity - Using Methods of the Picture class
Record your participation in Piazza clicker question Lesson2 Q3
- open redwood project from last class period
- if you did not get it then, download it from here
- open RedwoodViewer
- change the file name back to "redwood.png" if you need to
- we want to make the tree 50 pixels wider and twice as tall.
- look at the documentation for Picture class for grow method
- what should the parameter for width be? for height?
- how could you find the height?
- type in the code
- we want to move the image to (30, 50)
- in the constructor, the Picture object is created at coordinates (0 ,0)
- look at the documentation for Picture class
- in translate method, what parameters should we use to move the picture to 30, 50?
- type in the code
Activity - Using the Day class
Record your participation in Piazza clicker question Lesson2 Q4
Copy the Day class from here
From the Day class, we can make Day objects and call their methods
Now write an application using Day objects:
- Create a new project
- Add a class Day
- Delete all content and paste in the code from Day class above
- Add a class, perhaps called DayPrinter
- add a main method
- how can you find the header for the main method?
- Create an new Day object representing today
- look at the constructors in the API documentation
- Calculate and print how many days until Labor Day
- Make a new Day object representing today
- Make a Day object representing Labor Day (September 2, 2019)
- What method should you use to find how many days Labor Day is from today?
- Calculate the date 10 days from today and print it
- look at the Day class documentation for an appropriate method
- The values should be different if I run the program tomorrow
Activity - Variable names
Record your participation in Piazza clicker question Lesson2 Q5
Which of the following are valid variable names and follow the Java conventions? Check all that apply.
- taxRate
- tax rate
- 7Up
- public
- day
- Removed
- t
- taxrate
Activity - Pseudocode
Record your participation in Piazza clicker question Lesson2 Q6
- Write the pseudocode to read 10 numbers from the keyboard and print the average.
- Write pseudocode to read numbers until the user is done entering and print the average