CS 49J - Lecture 4

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
- Codecheck uses “expected style”:
System.out.println(bug.getPosition());
System.out.println("Expected: 10");
- Two disadvantages.
- When a test fails with an exception, all other tests are skipped since the test program has terminated.
- Nobody outside SJSU uses them.
- The more standard choice is JUnit.
- Write one or more test methods, prefixed by the “annotation”
@Test
. - 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));
}
}
- To add JUnit to your project, right-click on the project. Select Properties → Java Build Path → Libraries → Add Library → JUnit → Next → JUnit 4 → Finish.
- To run test class, right-click and select “Run as JUnit Test”.
- Upon success, you get a green bar
- Upon failure, red bar
- Still pretty good—now you have a reproducible failure