Lesson 6 – Classes

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Bluej
  4. Navigate to laughton.com/obrien/sjsu/cs46a/CRT_lessons/ and open this lesson

Questions

Tracing an object

Look at tracing using the Product class we wrote earlier

Activity - Instance variables

Record your participation in Piazza clicker question Lesson6 Q1

This class has a problem. What is it?

Circle

public class Circle
{
    private double radius;
    private double area;

    public Circle(double theRadius)
    {
        radius = theRadius;
        area = radius * radius * Math.PI;
    }

    public double area()
    {
        return area;
    }
    
    public void setRadius(double newRadius)
    {
        radius = newRadius;
    }
}
    

CircleTester

public class CircleTester
{
    public static void main(String[] args)
    {
        Circle c = new Circle(1);
        System.out.println(c.area());
        System.out.println("Expected: 3.141592653589793");
        
        c.setRadius(10);
        System.out.println(c.area());
        System.out.println("Expected: 314.1592653589793");
    }
}

 

So what is the problem?

Activity - Local variables

Record your participation in Piazza clicker question Lesson6 Q2

What are the local variables of the Product class increasePrice method?

    public void increasePrice(double percent)
    {
       double amountToIncrease = price * percent / 100;
       price = price + amountToIncrease;
    } 

Click all that apply.

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

Activity - Initializing local variables

Record your participation in Piazza clicker question Lesson6 Q3

What are local 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. local variables are not initilized for you. You have to do it yourself or you will get a compile-time error

 

Activity - Initializing instance variables

Record your participation in Piazza clicker question Lesson6 Q4

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 implicit and explicit parameters

Activity - The this Reference

Record your participation in Piazza clicker question Lesson6 Q5

What is wrong with this code?

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

Activity - Write a Class

Record your participation in Piazza clicker question Lesson6 Q6

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.

Here is a tester for the Student class. Copy it into your project. Does your code pass?