Quiz1
Quiz1
4
CSE 12 – Basic Data Structures Paul Cao UC San Diego
A. Yes B. No
A few notes
You are not allowed to use Generics as follows
• In creating an object of that type:
new T() // error
• In creating an array with elements of that type:
new T[100] // error
• As an argument to instanceof:
someref instanceof T // error
• To ensure that certain methods can be called, we can constrain the generic type to be subclass of an
interface or class
public class MyGenerics <E extends Comparable>{ ………}
Runtime
Generics Worksheet
import java.io.*;
import java.util.*;
public class GenericWorksheet<E>{
public E[] data;
public GenericWorksheet(){
5
CSE 12 – Basic Data Structures Paul Cao UC San Diego
public E findLast(){
}
public E removeFirst(){
}
public String toString(){
}
public static void main(String[] args){
Integer[] array = new Integer[]{5, 6, 33, 11, 99};
__________________________________________________________________
System.out.println(ref.findMedian());
System.out.println(ref.findLast());
System.out.println(ref.removeFirst());
System.out.println(ref);
//What if I have another class of my own and want to use it for this
generic class?
}
}
6
CSE 12 – Basic Data Structures Paul Cao UC San Diego
Data Abstraction
Abstract Data Types vs specific implementations
7
CSE 12 – Basic Data Structures Paul Cao UC San Diego
2.2 - Testing
Black box testing
• You don’t know (or you pretend you don’t know) how something is implemented
• You test only based on inputs and outputs
White box testing
• (Also known as “clear-box testing”)
• If you can look inside the black box and see how a method is implemented, you can do more detailed
testing
Time-consuming and tedious − Since test cases are Fast − Automation runs test cases significantly faster than
executed by human resources, it is very slow and tedious. human resources.
Huge investment in human resources − As test cases Less investment in human resources − Test cases are
need to be executed manually, more testers are required in executed using automation tools, so less number of testers are
manual testing. required in automation testing.
Less reliable − Manual testing is less reliable, as it has to More reliable − Automation tests are precise and reliable.
account for human errors.
Non-programmable − No programming can be done to Programmable − Testers can program sophisticated tests to
write sophisticated tests to fetch hidden information. bring out hidden information.
Junit Testing
• Test method: a method in a Java class that contains a single unit test.
• Test class: a Java class containing one or more test methods.
• Assertion: a statement that you include in a test method to check that the results of a test are as
expected.
• Test fixture: a class that is used to set up state for multiple tests; typically used when the set up routines
are “expensive” or take a long time to execute.
• Test suite: a grouping of test classes that are run together.
8
CSE 12 – Basic Data Structures Paul Cao UC San Diego
public class CSE12 { import static org.junit.Assert.*;
private int numStudents; import org.junit.Before;
private boolean largeClass; import org.junit.Test;
If I am testing CSE12 class’s getNum method, should I also test if the largeClass instance variable
has the correct value?
A. Yes B. No
9
CSE 12 – Basic Data Structures Paul Cao UC San Diego
2.3 - Junit Testing Worksheet
We write a Person class and we have the following methods
Fields: age (int), name (String), height (String in the format of f’ii) where f is a one digit
number and ii is a two digit number).
a. Default constructor: age is 0, name is null, height is null
b. A constructor that takes an age, name, and height and initialize all instance variables with parameter
values. If age < 0 or name or height isn’t in the correct format, throw an Exception with error message
“Wrong parameter!”
c. A function getNameLength that returns the number of characters in the name (not including spaces).
Throws an Exception if name is null
@Test
public void testCtor1(){
boolean exception = false;
try{
}
catch (Exception e){
exception = true;
}
assertEquals("Wrong parameters!", true, exception);
}
@Test
public void testCtor2(){
boolean exception = false;
try{
}
catch (Exception e){
exception = true;
}
10
CSE 12 – Basic Data Structures Paul Cao UC San Diego
@Test
public void testCtor3(){
boolean exception = false;
try{
}
catch (Exception e){
exception = true;
}
assertEquals("Wrong parameters!", true, exception);
}
@Test
public void testGetNameLength(){
boolean exception = false;
try{
}
catch (Exception e){
exception = true;
}
11