CS 46B - Lecture 1

Cay S. Horstmann
CS 46B
- Second course in computer science
- Learn about algorithms and data structures
- Learn good software engineering habits
- For green sheet, contact information, etc., see http://horstmann.com/sjsu/fall2015/cs46b
Active Learning

- No couch potatos!
- You read the book before class
- Exercises during lectures
- You need your laptop for every lecture
- Mandatory lab
- You fail the class if you don't do the lab
- Lots of homework
- You only learn by doing
- You only learn if you do it yourself
What You Need to Succeed

- Prerequisites
-
- CS1 in Java
- Completion or concurrent enrollment in discrete math
Time
- You can't expect to learn complex skills by listening to lectures
Study habits
- Your brain needs time to learn. Don't try to do all work the night of the due date
Your Responsibilities

- Come to every class meeting
- And come to every lab meeting
- Do the assigned reading (and the quiz) before class.
- I won't lecture on the material that you can read yourself
- Submit two versions of each programming assignment
- I don't want you to start working the day before the due date
- Spend 12 hours per week for this class
- 2.5 hours in class
- 2.5 hours in the lab
- 7 hours (!) for preparation and homework
- Ask questions right away when you are stuck
- Use the online discussion group. You'll get a better grade if you ask lots of questions.
- Answer your peers' questions. You'll get a better grade if you do.
- Private or confidential questions—email or office hours
Homework

- Homework is due before every class meeting
- Every Tuesday and Saturday 6 pm
- Grace period for uploading until 11:59 pm
- Don't rely on it—get it done by 6 pm
- No mercy after midnight
- You submit to Canvas
- Programming assignments in two stages
- Draft: Something that does a part of the job (must compile)
- Final: The complete solution
- Sometimes, you need to upload an image too (such as in homework 1)
Lab

- You must register for a lab section
- You must attend the lab sections in order to pass the class
- Lab grade F ⇒ course grade F
- Lab grade A - D counts for 5% of the course
- Bring your laptop to the lab
- You will work with a buddy
- One of you writes code, the other types up answers
- Switch roles each week
- If your lab is scheduled for Tuesday, your first session is tomorrow
The Textbook

- Read the textbook before each class meeting
- Bring the textbook to class and labs
- You need the book now
- You need the correct edition (7th edition of Java Concepts or 5th edition of Big Java)
- The college bookstore has a slightly discounted custom edition for SJSU
- An ebook is fine (I recommend this version)
- Do not get an “international edition”—it is different.
Adding

- Do the prerequisite quiz and send me your answers.
- I will send you an add code when space becomes available. Use it within 24 hours, or it will become invalid
- Show up for all classes/labs even if you haven't received your code
- You MUST register for the lab that I assign to you, or you will be instructor-dropped from both the course and the lab
- In the meantime, respond to the invitation in Canvas/Piazza and do all the work
- You'll have to do the lab work on your own until you have an add code
Things To Do Today

- Log into Canvas and Piazza
- Upload a photo into your profile so I know who you are
- Get the textbook and start reading it
- Get a laptop and install the virtual machine for the course
- Look at Quiz 1 and Homework 1 draft in Canvas
- Important: In lieu of a roll call today, you must turn in Homework 1 draft by the deadline, or I will drop you from this course for lack of presence.
Inheritance Hierarchies
- Inheritance: the relationship between a more general class (superclass) and a more specialized class (subclass).
- The subclass inherits data and behavior from the superclass.
- Cars share the common traits of all vehicles
- Example: the ability to transport people from one place to another
Inheritance Hierarchies
- The class Car inherits from the class Vehicle
- The Vehicle class is the superclass
- The Car class is the subclass
Inheritance Hierarchies
- Inheritance lets you can reuse code instead of duplicating it.
- Two types of reuse
- A subclass inherits the methods of the superclass
- Because a car is a special kind of vehicle, we can use a Car object in algorithms that manipulate Vehicle objects
- The substitution principle:
- You can always use a subclass object when a superclass object is expected.
- A method that processes Vehicle objects can handle any kind of vehicle
Lecture 1 Clicker Question 1
Consider the method doSomething(Car c). Name the class whose objects cannot be passed to this method.

- Car
- Sedan
- SUV
- Vehicle
Inheritance Hierarchies
Example: Computer-graded quiz
- There are different kinds of questions
- A question can display its text, and it can check whether a given response is a correct answer.
- You can form subclasses of the Question class.

Implementing Subclasses

- To get a ChoiceQuestion class, implement it as a subclass of Question
- Specify what makes the subclass different from its superclass.
- Subclass objects automatically have the instance variables that are declared in the superclass.
- Only declare instance variables that are not part of the superclass objects.
- A subclass inherits all methods that it does not override.
Implementing Subclasses
- The subclass inherits all public methods from the superclass.
- You declare any methods that are new to the subclass.
- You change the implementation of inherited methods if the inherited behavior is not appropriate.
- Override a method: supply a new implementation for an inherited method
Implementing Subclasses
A ChoiceQuestion object differs from a Question object in three ways:
- Its objects store the various choices for the answer.
- There is a method for adding answer choices.
- The display method of the ChoiceQuestion class shows these choices so that the respondent can choose one of them.
Implementing Subclasses

- The ChoiceQuestion class needs to spell out the three differences:
public class ChoiceQuestion extends Question
{
// This instance variable is added to the subclass
private ArrayList<String> choices;
// This method is added to the subclass
public void addChoice(String choice, boolean correct) { . . . }
// This method overrides a method from the superclass
public void display() { . . . }
}
- The extends reserved word indicates that a class inherits from a superclass.
Implementing Subclasses
- Adding a new method: addChoice
public void addChoice(String choice, boolean correct)
{
choices.add(choice);
if (correct)
{
// Convert choices.size() to string
String choiceString = "" + choices.size();
setAnswer(choiceString);
}
}
- addChoice method can not just access the answer variable in the superclass:
- It must use the setAnswer method
- Invoke setAnswer on the implicit parameter:
setAnswer(choiceString);
OR
this.setAnswer(choiceString);
Lecture 1 Clicker Question 2
Which of the following statements is true?
Question
is a subclass of ChoiceQuestion
- The
ChoiceQuestion
class overrides the setText
method of Question
- The
ChoiceQuestion
class inherits the checkAnswer
method of Question
- The
ChoiceQuestion
method inherits the display
method of Question
Lecture 1 Clicker Question 3
Suppose q is an object of the class Question and cq an object of the class ChoiceQuestion. Which of the following calls is not legal?
- q.setAnswer(response)
- cq.setAnswer(response)
- q.addChoice(choice, true)
- cq.addChoice(choice, true)
Before Next Class

- Read sections 9.3 - 9.5, Special Topics 9.1, 9.2 and How To 9.1 of your textbook
- Make sure to do the quiz
- Tuesday lab starts tomorrow
- Make sure you do homework 1 draft—or you will be dropped