0% found this document useful (0 votes)
2 views

Quiz1

The document is a note packet for CSE 12 - Basic Data Structures by Paul Cao at UC San Diego, focusing on Java generics, data abstraction, and testing methodologies. It covers topics such as generic classes, interfaces, black box and white box testing, and JUnit testing practices. Additionally, it includes code examples and exercises related to generics and testing concepts.

Uploaded by

dsylviaaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Quiz1

The document is a note packet for CSE 12 - Basic Data Structures by Paul Cao at UC San Diego, focusing on Java generics, data abstraction, and testing methodologies. It covers topics such as generic classes, interfaces, black box and white box testing, and JUnit testing practices. Additionally, it includes code examples and exercises related to generics and testing concepts.

Uploaded by

dsylviaaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CSE 12 – Basic Data Structures Paul Cao UC San Diego

CSE 12 – Basic Data Structures


Note Packet
Author: Paul Cao

Last update: March 2025


1
CSE 12 – Basic Data Structures Paul Cao UC San Diego
Chapter 2 - Java Review
2.1 - Generics
The purpose of generics in Java is to separate data types from algorithms that apply on data types.
Generics is in general associated with data structures (CSE 12) and algorithms.
Generic Class
Create a class which has multiple generic methods (like our ArrayList, Set, HashMap classes)

public class MyGList<T> { @Override


private T[] nums; public String toString() {
public MyGList() { String str = "";
nums=null; for (T n : nums) {
} if (n instanceof Object){
public MyGList(int size) { str += (n.toString() + ", ");
this.nums = (T[])new Object[size]; }
} }
public int size() { return str;
return this.nums.length; }
} public static void main(String[] args){
public void update(int ind, T data) { MyGList<Integer> vals = new
nums[ind] = data; MyGList<Integer>(3);
} System.out.println(vals);
vals.update(2, 7);
vals.update(0, 4);
vals.update(1, 3);
System.out.println(vals);
}
}

public interface Collection<E> extends Iterable<E>


What does the <E> mean in the above code?
A. That this collection can only be used with objects of a built-in Java type called E
B. That an object reference that implements Collection can be instantiated to work with any object type
C. That a single collection can hold objects of different types

Will the main method compile?


public class Generics<T> {
private ArrayList<T> elements;
public Generics() {…}
public T add(T element) {…}
public int getNumElements() {…}
public T getLastElement() {…}
public static void main(String[] args) {
Generics<T> rr = new Generics<T>();
rr.add(1);
System.out.println("Last elem was " + rr.getLastElement());
}
}

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>{ ………}

• Important for data structures in general


public class MyList<E>{
//codes that use E }
• Type erasure during compile time
• Compiler checks if generic type is used properly. Then replace them with Object
• Runtime doesn’t have different generic types
MyList<String> ref1 = new MyList<String>();
MyList<Integer> ref2 = new MyList<Integer>();
Compile time

Runtime

Generics Worksheet
import java.io.*;
import java.util.*;
public class GenericWorksheet<E>{
public E[] data;
public GenericWorksheet(){

________________________________//Initialize data to null


}
public GenericWorksheet(E[] data){
//complete a shallow copy
________________________________________________

//what if we want to have a deep copy?


}
public E findMedian(){

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

True or False: Array is an ADT, not a data structure


A. True B. False

True or False: ADT and API are the same


A. True B. False

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

Test driven development


• first writing tests, and then writing the software that will be tested
• Increases quality of code, can increase productivity (Bissi, W., Neto, A. G. S. S., & Emer, M. C. F. P.
(2016). The effects of test driven development on internal quality, external quality and productivity: A
systematic review. Information and Software Technology, 74, 45-54)

Manual Testing Automated Testing


Executing a test cases manually without any tool support Taking tool support and executing the test cases by using an
is known as manual testing. automation tool is known as automation 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;

public CSE12() { public class CSE12Tester {


numStudents = 0; private CSE12 ref;
largeClass = true;
} @Before
public CSE12(int num) { public void setup() throws Exception{
numStudents = num; ref = new CSE12();
if (numStudents > 100) { }
largeClass = true; @Test
} else { public void ctor_default() {
largeClass = false; assertEquals(0, ref.getNum());
} }
}
public int getNum() { @Test
return numStudents; public void ctor_2() {
} ref.setNum(406);
public void setNum(int num) { assertEquals(406, ref.getNum());
numStudents = num; assertTrue("Should be a large class",
if (numStudents > 100) { ref.getLargeClass());
largeClass = true; }
} else {
largeClass = false; @Test
} public void testOfficeHour() {
} ref.setNum(90);
public boolean getLargeClass(){ assertEquals(90, ref.getNum());
return largeClass; assertFalse("Should be a small class",
} ref.getLargeClass());
public int officeHour() { assertEquals(90 / 8, ref.officeHour());
return numStudents / 10; }
} }
}

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

Please write a tester about the class.


@Test
public void testDefaultCtor(){
Person ref = new Person();

@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

You might also like