CS49J Lesson 4 – Classes

Ch 3: 3.1-3.5

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Eclipse or your IDE
  4. Navigate to laughton.com/obrien/sjsu/
    cs49j/lessons and open this lesson

Questions

Concept - Creating a Product Class

Activity - Starting the Product class

Record your participation in Piazza clicker question Lesson4 Q1

Let's write the class together

Activity - Accessors and Mutators

Record your participation in Piazza clicker question Lesson4 Q2

What is wrong with this sequence of statements?
Product toaster = new Product("Toaster", 39.99); 
System.out.println(toaster.reducePrice(10));
  1. There is nothing wrong
  2. reducePrice is an accessor method and can not be used as input to the println method
  3. reducePrice is an mutator method and can not be used as input to the println method
  4. Something else is wrong

Activity - Instance variables

Record your participation in Piazza clicker question Lesson4 Q3

Activity - Local variables

Record your participation in Piazza clicker question Lesson4 Q4

What are the local variables of the Product class increasePrice method? Click all that apply.

  1. percent
  2. price
  3. amountToIncrease
  4. there are no local variables

Activity - Initializing instance variables

Record your participation in Piazza clicker question Lesson4 Q5

What are instance variables initialized to by the compiler? Check all that apply.

  1. objects to null
  2. Strings to the empty string ("")
  3. numbers to 0
  4. instance variables are not initilized for you. You have to do it yourself or you will get a compile-time error

 

The this reference

Activity - The this Reference

Record your participation in Piazza clicker question Lesson4 Q6

What is wrong with this code for the constructor of the Product class?

    public Product(String name, double price)
    {
        price = price;
        name = name;  
    }

Activity - Write a Class

Record your participation in Piazza clicker question Lesson4 Q7

Write a Student class. A Student has a name and a total score (sum of all the points earned). It has a method to add a score to the total score, a method to get the total score and a methods to get and set the name.

Activity - private instance variables

Record your participation in Piazza clicker question Lesson4 Q8

In the previous activity, why should the total score variable be private? What might happen if it were public?