CS 49J - Lecture 4

Cover page image

Cay S. Horstmann

Homework Demo/Discussion

Thinking About Instance Variables

Bug 1

Bug b = new Bug();
b.move(10);
b.move(-2);
int p = b.getPosition();

public class Bug
{
   private _____ _____;

   ...
}

Bug 2

Bug b = new Bug();
b.move(10);
b.turn(); // Turns 180 degrees
b.move(2);
int p = b.getPosition();

public class Bug
{
   private _____ _____;
   private _____ _____;

   . . .
}

Bug 3

Bug b = new Bug(10, 10, "East");
b.move(10);
b.turn(); // Turns 90 degrees clockwise
b.move(2);
int x = b.getX();
int y = b.getY();

public class Bug
{
   private _____ _____;
   private _____ _____;
   private _____ _____;
   private _____ _____;

   . . .
}

Student 1

Student s = new Student("000001729");
s.addScore(10);
s.addScore(20);
double total = s.getTotal(); // 30

public class Student
{
   private String id;
   private _____ _____;

   . . .
}

Student 2

Student s = new Student("000001729");
s.addScore(10);
s.addScore(20);
double average = s.getAverage(); // 15

public class Student
{
   private String id;
   private _____ _____;
   private _____ _____;

   . . .
}

Student 3

Student s = new Student("000001729");
s.addScore(10);
s.addScore(20);
double[] scores = s.getScores(); // [10, 20]

public class Student
{
   private String id;
   private _____ _____;
   private _____ _____;

   . . .
}

Pitfall

public class Employee
{
   private String name;
   private double salary;

   public void Employee() { name = "None"; salary = 0; }
   public String getName() { return name; }
   public void setName(String n) { name = n; }
   . . .
}

Employee e = new Employee();
int n = e.getName().length(); // What happens?

Pitfall

public class Employee
{
   private String name;
   private double salary;

   public  Employee(String n, double s)
   {
      String name = n;
      double salary = 0;
   }
   public String getName() { return name; }
   . . .
}

Employee e = new Employee("Fred", 100000);
int n = e.getName().length(); // What happens?

Code Smell

public class Employee
{
   private String name;
   private double salary;
   private double raise;
   . . .
   public void raiseSalary(double percent)
   {
      raise = salary * percent / 100;
      salary += raise;
   }
}

Code Smell

public class Letter
{
   private String recipient;
   private String sender;
   public Letter(String aRecipient, String aSender)
   {
      recipient = "John"; 
      sender = "Mary"; 
   }
   . . .
}

JUnit Demo

  1. Codecheck uses “expected style”:
    System.out.println(bug.getPosition());
    System.out.println("Expected: 10");
    
  2. Two disadvantages.
    1. When a test fails with an exception, all other tests are skipped since the test program has terminated.
    2. Nobody outside SJSU uses them.
  3. The more standard choice is JUnit.
  4. Write one or more test methods, prefixed by the “annotation” @Test.
  5. Call methods such as Assert.assertEquals, Assert.assertNull, or Assert.assertTrue:
    public class ChoiceQuestionTest
    {
       @Test public void testCorrect()
       {
          ChoiceQuestion q = new ChoiceQuestion();
          q.addChoice(. . .);
          . . .
          Assert.assertTrue(q.checkAnswer(2));
       }
    }
    
  6. To add JUnit to your project, right-click on the project. Select Properties → Java Build Path → Libraries → Add Library → JUnit → Next → JUnit 4 → Finish.
  7. To run test class, right-click and select “Run as JUnit Test”.
  8. Upon success, you get a green bar
  9. Upon failure, red bar