CSCI 1620 - Test 1

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

JUnit 4 Test

- Add suffix "Test" or "Tests" - @Test before test method (like @Override) - Always void & no parameters usually Ex: public void testEQualFractions() { assertionTrue(f1.equal(f2)); }

Formal Testing Strategies

- Black Box Testing - White Box Testing

Decomposition

- Classes represented a *single coherent entity* with clearly defined responsibilities - Methods serve a single, simply described purpose

Encapsulation & Information Hiding

- Classes should bring together all of the relevant data and behaviors for an entity - Member data of object should be controlled exclusively by the class itself

Over-Arching Goals of OOP

- Decomposition - Encapsulation & Information Hiding

Where to start when creating a Class?

- Incrementally, testing as you go

Inheritance Mechanism - Cont.

- Inheritance relationships may cascade in a hierarchy - Java only supports single inheritance between any two classes at a time So you can't do: 'public class C extends B, C; //this is illegal'

Classes

- Like a blue print - Allows us to create individual instances of the kind of thing - Each individual instance shares some prop. & behaviors - But each instances can have difference *values* for the prop. Ex: Every house has a color, but each house may have a difference color - Fraction f1 = new Fraction(1, 2); Fraction f2 = new Fraction(7, 10);

Complex Data Types

- Member data (fields) - Instance data - Class data

What is required to write Classes?

- Must be defined in a file that has the same name of the class with '.java' file extension (every '.java' file can have *one* public class defined)

JavaDoc - Tags

- Starts with @ - Are case-sensitive Ex: @return - IF the method returns a value, a single @return tag is used to describe what is returned @param - Each parameter of the method is giving its own @param tag with the name of the parameter and a description of what it is used for

Our Goal when Testing

- To create a test plan that exercises all paths of code execution, regardless of how common - Develop a suite of reusable test that can be easily re-run to verify our code along the way and at the end - All tests must be valid and compile

Static

- Underlined in UML, its class data - To access a static member, we use the class name (rather than a reference variable) on the left side of the dot operator Ex: Fraction.setSeperator(':'); Fraction f1 = new Fraction(1, 2);

JavaDoc - Comments

/** * a multiline JavaDoc Comment */

Reference Type Equality

10 = 10 will copy memory address, not make a copy use ".equals()" instead

Which of the following are important design goals for Object Oriented Design? (select all that apply) A) Classes should be structured to depend on as few other classes as possible. B) Our design should strive to have as few classes as possible. Small classes should be eliminated. C) Each class should have a clearly defined purpose that is distinct from others in the system. D) Setters and getters should be provided for every data member in a class.

A & C A) Classes should be structured to depend on as few other classes as possible. C) Each class should have a clearly defined purpose that is distinct from others in the system.

Which of the following are true about a datatype? (Check all that apply) A) To avoid compiler errors, values of a type must only be used in ways that are defined by the datatype. B) The 8 primitive types are the only datatypes provided by Java. C) A datatype defines the set of allowable values that can be stored. D) Classes in Java are datatypes.

A C D A) To avoid compiler errors, values of a type must only be used in ways that are defined by the datatype. C) A datatype defines the set of allowable values that can be stored. D) Classes in Java are datatypes.

Test Driver

A class is designed to help verify another class functions correctly - Informal, can get edited & deleted as you program

JUnit

A framwork for writing reusable unit tests - There is a lbray build on a series of "assertion methods" Ex: assertTrue(observedVal)

Dependency

A place where one class depends on ("Uses") another class in some way. - UML visualizes simple usage dependencies using a dotted arrow to indicate which class uses which. - "Use" Dependencies are the simplest relationship between classes - Can Occur between classes & Methods within a class - Understanding dep. can help us determine the order in which code should be implemented and tested - Minimizing the # of dep. between classes improves the robustness of the solution with respect to future changes Ex: User's Adopt method has a Pet parameter

Say you're designing an online quiz system. In the system there are QuizQuestions and QuizAnswers. Every QuizQuestion object can have one or more QuizAnswers. QuizAnswers are owned entirely by their corresponding QuizQuestions. Which best describes how the QuizQuestion and QuizAnswer classes relate? A) Compostion B) Aggregation C) Association

A) Compostion

Which of these statements is true about "has-a" relationships in between classes. A) Has-a relationships correspond to when one class contains instance variables for references to another. B) If class A "has-a" class B it means we can use them interchangeably in code. C) UML does not provide any mechanism to document has-a relationships. D) Has-a relationships are not considered dependencies.

A) Has-a relationships correspond to when one class contains instance variables for references to another.

Which of the following is not true about constructors involving inheritance? A) If the superclass does not define a default constructor, the compiler will automatically call the first constructor in the superclass when setting up a subclass object. B) To call a superclass constructor you use the syntax super(...); where ... contains the appropriate parameters. C) When a superclass constructor is called in the subclass it must be the first statement in the constructor. D) A constructor for the superclass must be executed in order to set up the inherited instance variables.

A) If the superclass does not define a default constructor, the compiler will automatically call the first constructor in the superclass when setting up a subclass object.

If you've been tasking with writing test cases before any of the code you're trying to test has been written yet, which of the following best describes what you've been asked to do. A) This would be a form of black-box testing. B) This would be white-box testing. C) This would be integration testing. D) This would be regression testing.

A) This would be a form of black-box testing. Yes! Since you don't have any insider information about how the code is written yet, this would be black-box testing. Further, it sounds like you've been asked to write "unit tests" for the functionality so that individual classes and methods can be tested as they get written. These tests would be based on the designed behavior to verify that the code does what said it should.

When reading a problem statement, what types of words are good indications of potential methods for your design? A) Verbs B) Adverbs C) Nouns D) Adjectives

A) Verbs

If you are trying to test a method with the signature: public static int mysteryMethod(String param) Which of the following test method calls would be invalid and not allowed as part of a test suite? (select all that apply) A) mysteryMethod("") B) mysteryMethod(8675309) C) mysteryMethod("TEST") D) mysteryMethod('@') E) mysteryMethod(null)

B & D B) mysteryMethod(8675309) That's right. This is a compiler error because the value given is an int, and thus not valid as a String. Since it won't compile, it can't be a test case in Java. D) mysteryMethod('@') Right! '@' is a char, which cannot be used in place of a String reference, which is what the method expects. This would lead to a compiler error, and is thus not a valid test in the first place in Java.

The process of breaking down a problem into individual classes and methods is called: A) Dependencies B) Decomposition C) Information Hiding D) Encapsulation

B) Decomposition

You're creating a class called QuizClass, the name of the file this class should be written in is: A) QuizClass B) QuizClass.java C) java.QuizClass D) The name of the file doesn't matter.

B) QuizClass.java

You're writing a method with the following header: public void methodName(int arg1, int arg2) The Javadoc for this method should include a description of the method as well as what tags? A) A @param tag for each parameter describing what they are used for and a @return tag stating the return is void B) A single @param tag describing both of the parameters C) A @param tag for each parameter describing what they are used for D) A single @param tag describing both of the parameters and a @return tag stating the return is void

C) A @param tag for each parameter describing what they are used for

Which method should we start with?

Cannot instantiate a class without a constructor

Assume that SimpleInput is a class whose JavaDoc contains information about the following method: ---------------------------- public static double readDouble() This method will display a graphical input box and prompt the user for a valid numeric value, which is returned. --------------------------- Which of the following code segments correctly invokes readDouble from a main method that is not part of the SimpleInput class? A) readDouble(SimpleInput); B) input.readDouble(); // where input is an instance of SimpleInput C) (static) SimpleInput.readDouble D) SimpleInput.readDouble();

D) SimpleInput.readDouble();

When overriding a method ... A) As long as the subclass method has the same name in the superclass, it is considered overridden. B) You cannot override a method that is indirectly inherited from the superclass of your superclass. C) You must put @Override before the method header. D) The @Override tag is optional.

D) The @Override tag is optional.

What will happen if you have the following assertion in a JUnit test case? String result = "THISISaTEST";assertEquals("THIS IS A TEST", result);//more code here... A) The test case will pass this assertion for equality since case and whitespace don't matter. B) The test case will produce an error on the console output from the assertEquals call. Any code following it in the method will not execute. C) This code produces a compiler error. D) The test case will fail at the assertEquals because the expected and observed values are not equal. Any code following in the method will not execute.

D) The test case will fail at the assertEquals because the expected and observed values are not equal. Any code following in the method will not execute.

You're creating a class called QuizClass, the class header should be: A) private class QuizClass B) public QuizClass class C) class QuizClass D) public class QuizClass

D) public class QuizClass

If I'd like to define class a Student to be a subclass of class Person, which of the following is the correct header for Student? A) public class Student super Person B) public class Student inherits Person C) public class Student extend Person D) public class Student extends Person

D) public class Student extends Person

The DRY Rule of OO Design

Don't Repeat Yourself

Member Methods

Example: equals

Has-A Relationship

Exists between two classes when objects of one class have references to other classes for instance variable - Stronger than "Use" association Ex: A car has an engine, tires, radio

All aggregation relationships are by definition also considered composition relationships. True or False?

False

The @Test annotation above a test case method header is optional in JUnit. True or False?

False Any methods that are not tagged with @Test will not be included in the test plan when you click the run button. This is how JUnit knows which code is a test case!

Inheritance Mechanism

If objects of B are also objects of A, we say: - A is a *superclass* of B - B is a *subclass* of A - B *inherits* the variables and methods defined in A

Variables

Named locations in memory where we can store values

JavaDoc

Provides a standardized format for documenting the expected behavior of classes. - Should be written when a new class is created so that others can quickly learn about how to use it - Typically, we read "Public JavaDoc," not private as 3rd party clients - Command Line Tool (Can generate HTML Document)

Degression Testing

Re-run unit and integration test when changes are made to existing code to verify your "fix" didn't accidentally break something else

JavaDoc - Methods

Should Include: - What a method returns - What a method takes as arguments - Describe what the method does (summary)

Black Box Testing

Testing functionally with no knowledge of internal implementation - Cannot see code/system

Unit Testing

Testing individual classes/methods as you implement to check completeness and correctness - Ideally, test cases are written first to be benchmark as you implement

White Box Testing

Testing the functionality with knowledge of internal implementation - More robust

Integration testing

Testing used for verifying the interactions of multiple modules/systems

Data Types

The range of acceptable values - A set of allowable operations - 8 primitive data types

A UML Diagram can be used to write a class "skeleton", but not necessarily the detailed implementation of the methods. True or False?

True

Assume that Robot is a class that defines: a constructor which takes a single String value corresponding to the Robot's name. a setName method which takes a single String value that can be used to update a Robot's name Consider the code below: Robot r1 = new Robot("Hal");Robot r2 = new Robot("Hal");Robot r3 = r1;r3.setName("Winston"); True or false: The name of the robot referred to by the variable r1 would be affected by the call to setName on line 4. True or False?

True

Multiplicity

UML allows us to indicate more details about *how many objects* of a given class are present in the relationship

Is-A Relationships - UML

Use up arrow

Object-Oriented Design Strategy

What kind of *objects* will we need? A: Look for nouns What kinds of *behaviors or methods* should each object be able to do? A: Look for verbs (methods are about doing things, so you would use a verb) What kinds of *data* should each object type store? A: Consider nouns and/or adjectives

Composition

When Objects of class A contains references to objects of Class B as instance variables and the objects of class B do not exist without A objects around Ex: If your car is broken than you assume so are the parts of the car

Is-A Relationship

When an object of one class can be considered logically also an object class - We leverage is-a relationships to extract commonalities thereby increasing reuse-ability and maintainability in our system Ex: - A student *is-a* person - A dog *is-a* pet

Aggregation

When objects of class A contain references to objects of class B as instance variables, but B objects may exist in the system without A objects around

To Override a method in a subclass

You just need to provide another method - Exact same name & parameter - Use '@Override'

Class - Syntax

public class ClassName { //everything inside the class }

When using a superclass in a subclass use:

public class ClassName *extends* NameSuperClass;

To invoke the constructor or superclass:

super(parameters) - Have to do this on the 1st line inside subclass constructor

To use Methods from superclass

super.method();

JavaDoc - Make it look like Code

{@code ____}


Kaugnay na mga set ng pag-aaral