Midterm Superdeck Flashcards
What is procedural programming?
Methods/functions calling each other.
Downside of procedural programming?
It can become unmanageable/harder to manage as they become larger.
How does OOP handle complexity?
Decomposition. Design objects to contain data and related behaviors.
Benefits of OOP?
Maintainability and extensibility.
What is a class.
A blueprint or instruction on how to make an object.
What is an object.
An instance of a class that has been created.
What is a field?
An attribute. Data relevant to the systems purpose.
What is an entity?
Models a real “thing”.
What is a container?
A data structure to hold entities.
What is an interface?
Communicates with the “outside world”.
What is a control object?
Organizes computation.
I.e. The main program.
Types of entitites?
Concrete objects. People, buildings, etc.
Conceptual objects. Organizations, agreements, etc.
Event and state-change objects. I.e. purchase, sale, birth.
Types of methods.
Accessors/getters.
Mutators/setters.
Hybrid. Access and mutate, but this is discouraged.
5 stages to software development.
Requirements.
Design.
Implementation.
System testing and verification.
Maintenance.
UMLs describe…
What it does, not how.
What is a primary actor?
Triggers a use case to happen.
What is a supporting actor?
Indirectly involved with a use case. I.e. A user (primary actor) tries to withdraw money. The bank (supporting actor) verifies there’s enough money.
Extends. (non-class uml)
Base use case may conditionally execute the associated use case. Dashed line, solid arrow, «extend» in the middle
Include.
The base case will always execute the associated use case. Dashed line, solid arrow, «include» in the middle.
Generalization. (non-class uml)
A child use case inherits from parent. Solid line, hollow arrow, from child to parent.
Visibility symbols in UML.
+ public
- private
# protected
Class UML inheritance.
Solid line, hollow arrow, child to parent.
Class UML generalization.
Dashed line, hollow arrow, “child to parent”. “Class B implements A”.
Class UML association.
A and B call eachother.
Solid line.
Class UML one way association.
B can call A. Arrow from B to A, solid line, arrow is not complete triangle.
Class UML reflexive association.
A class may have multiple functions or responsibilities. A smaller box in the lower right underneath.
Aggregation.
A contains B, B can survive if A is deleted. Solid line from B to A. Hollow diamond.
Composition.
A contains B. B cannot exist without A. Solid line from B to A. Filled diamond.
When are multiplicities used in class UML?
For association, aggregation, composition.
I.e. 1 teacher may call at least 1 student ( 1..* ).
A classroom (1) holds at least one student (1..*).
Encapsulation.
Group related variables and functions. Reduce complexibility and increase reusability.
Abstraction.
Hide details and show the essentials. Isolate impacts of change.
Inheritance benefit.
Eliminate redundant code.
Polymorphism.
Refactor ugly if and switch/case statements.
What is the substitution principle?
You can always use a subclass object when a superclass object is expected.
I.e. Cat is an Animal, so if you ask for an Animal, I can give you a Cat.
What is dynamic method lookup?
Methods called are from the actual objects, not the variable type. I.e. You have a bunch of Animals, and you want them to make noises. Calling something like “makeNoise()” on each animal checks the actual object, so you might get a Cat, a Dog, a Bird making different noises, even though the variable type is just Animal.
What is an interface made for?
To group related methods with empty bodies, deferring implementation to subclasses. Something that implements an interface must implement it’s methods. This way, if you know an object implements an interface, you know what functions it will have from that.
What is an abstract class?
Cannot be instantiated. However, its subclasses can. I think this means we could have an abstract “Animal” class, since you don’t want to have general “Animals”, but you can have “Cats” or “Dogs” that inherit from it and can be instantiated.
What is static typing?
Expressions are checked at compile time. Code must be fixed before it can be ran.
Illegal expression - Compile-time error
What is dynamic typing?
Expressions are checked at run time.
You can run the program, but it may crash.
Illegal expression - Run-time error
Java. main function syntax.
public static void main(String[] args)
What are Java’s numeric types similar to?
Very similar to C/C++.
What are Strings in java?
A class, not a primitive.
Where does java keep primitive types?
On the stack.
Where does java keep objects?
On the heap.
Constants in java.
Use the “final” keyword in the beginning.
Note that by convention, final variables are in all caps.
Does java let you type cast float to integer?
Only if you use the cast operator.
I.e. (int) some_double;
This truncates the decimal.
What is the lifetime of a variable?
Within its scope. ( the curly braces {} )
Java if, elif, else, syntax.
if (condition)
{
{
else if (condition)
{
}
else
{
}
Java for loop.
Just like in C.
Java do while?
do
{
stuff;
} while(condition);
Java console output.
System.out.println(“hello world”);
Note that println automatically adds a newline.
How to get keyboard input in java?
import java.util.Scanner;
Scanner in = new Scanner(System.in);
in.next();
in.nextInt();
in.nextDouble();
etc…
How to parse strings to other data types?
Integer.parseInt(String s);
Float.parseFloat(String s);
Byte.parseByte(String s);
String comparison.
== is for objects. Two strings may have the same value but won’t evaluate to true here.
Use s1.equals(s2)
Testing is a _____ process.
proactive
They help us identify bugs.
Debugging is a _____ process.
Reactive. There is a problem so you act to fix it.
What is the goal to testing?
To detect faults in our code.
Types of errors.
Commission. You made errors in your code. Syntax mistakes. Minor logic errors. Stuff is there, but not working.
Omission. You didn’t prepare for some behavior. Stuff is missing entirely.
What is a fault?
The appearance/form/manifestation of an error.
Incorrect output,
Run-time errors,
Failure to achieve the stated purpose
What does the presence of a fault mean?
It implies the presence of an error.
Note that one error may create many faults.
What is debugging?
Deducing the error from the fault.
What are the testing levels?
Unit testing: On each method individually.
Integration testing: Some methods work together. Limited combinations.
System testing: The whole application. Very testing. Do the previous two as much as possible before to find most of the faults.
What is test driven developmetn?
Design stubs, parameters, return, effect.
Design test cases.
Implement methods.
All test cases should fail initially.
Work on method implementation until all test cases pass.
What is bottom-up testing?
Start with things that don’t use other units.
Repeatedly test the integration of modules that only use already tested module.
Continue until whole system tested.
What is top-down testing?
Implement top-level methods first.
Write test cases for the top-level.
Leave low-level methods as stubs.
Top down or bottom up?
Bottom-up for well understood modules or well structured designs (like assignments).
Top-down for nebulous modules (or open-ended free-form projects).
Inheritance relationship description.
“is-a”
What is polymorphism?
Lets subtypes be treated as their supertypes.
Lets classes be treated as interface types that they implement.
What is cohesion?
How many things or concepts does something do/represent? Low cohesion is when classes do too many things, which is bad.
High cohesion would have each class be doing a specific and limited number of things well.
What is coupling?
How many calls between classes are there?
High coupling is when classes call each other too much, which is bad. Low coupling is when the calls between classes is reduced, which is good.
Useful methods and operators of Object as given in class?
toString()
getClass()
Equals()
instanceof operator
Java arrays.
Fixed single block of memory. Accessed by index. All values the same type. Primitive or reference types. Duplicate values permitted. NOT part of Java Collections Framework.
Remember to use new keyword.
i.e. int[] someArray = new int[99];
ArrayList
Accessed by index. Variable size. All the same type. Only reference types. Duplicate lists permitted.
LinkedList
A sequence of values. Accessed in order. All the same type. Reference types only. Exactly as much space as needed.
Maps
key-value pairs. Organized by key to access the values.
HashMap - array-like
TreeMap - tree-like
Creating a new map in Java.
Map<keytype, valuetype> varname = new HashMap<keytype, valuetype>();
Adding, removing, and getting entries from a map.
.put(key, value)
.get(key)
.remove(key)
What is a benefit of Hash Maps?
I.e. A bunch of 10 digit ids. It would be far too costly to make space for all possible, so make an array for roughly the right size. Use a function to convert each id into an index. (if there’s duplicates, have strategies to try elsewhere, but beyond cmpt270).
Sets
Unordered collection. Can store, remove, and check for value. Like a map that only stores keys. HashSet and TreeSet
What factors into choosing a collection type?
How do you want to access values?
What types or key/value types are needed?
Does order matter?
What operations need to be fast?
Do you need to implement .equals() and .hashCode() for Hash Sets/Maps?
Do you need to implement the Comparable interface for trees?
Generics.
Let you avoid specifying a type.
I.e.
public class Zoo<i> {
This lets us omit the specific type until we make an instance.</i>
Can you restrict generics?
Yes.
I.e. <i></i>
What are exceptions within the context of Java?
Objects. Created when an event is detected, passed to the JVM, and halting the normal execution of code (passed upwards until something deals with it).
What makes exceptions different from error codes?
Error codes could be ignored. They were somewhat informal.
What ways can exceptions be used?
To detect and handle exceptions that can be managed by the application. I.e. user enters bad data. A file is missing. Etc.
To signal errors that need to be debugged and fixed. These are the programmers’ fault and need to be fixed to solve them.
Java try catch statement.
try
{
// code goes here
}
catch (NameOfException exception)
{
// do something about it
}
catch (OtherNameOfException exception)
{
// do something about it
}
finally
{
// do this thing always (i.e. close a file).
}
‘exception’ replaced by things like ‘e’ normally or ‘ignored’ when testing to see if it’s thrown like in our assignments.
Throws clause
If a method could throw a checked exception, it must be declared in the method.
i.e.
public String readData(String filename)
throws FileNotFoundException, NumberFormatException
Note that this clause is not required if the method catches the exception.
How do you throw an exception?
I.e.
throw new NameOfException(“message to accompany exception”);
A UML Use Case diagram is a type of…
behavioral diagram
Is a UML Class diagram a type of behavioral diagram?
No.
A house and a wall would have a ____ relationship?
Composition.
A hammer and nail would have a ___ relationship.
Association.
What principle lets you use one method to have different behaviors based on the class?
Polymorphism.
How do you show polymorphism in a UML class diagram?
You don’t.
Polymorphism helps to…
Refactor ugly if and switch/case statements.
Can you use single quotes to denote a string literal?
No.
Validation helps to…
Increase confidence in the correctness of a system.
An error is…
The cause of an undesired behavior.
Debugging is…
Inferring the cause of undesired behavior or incorrect results.
A fault is…
An undesired behavior or incorrect result.
Can there be fault’s without errors?
No.
What keywords indicates inheritance in Java?
extends
Benefits to polymorphism.
Reduces the amount of code you need to write.
Makes your code simpler and more concise.
Reduces coupling.
Can make your code more flexible/adaptable.
What keyword is used to define the use of an interface in Java?
implements
How do you let your custom classes be comparable with ‘<’ and ‘>’ operators?
You can’t.
Where should exceptions be caught and handled?
The method that has the enough information to most effectively handle the problem.
Can you compile code that doesn’t acknowledge unchecked exceptions?
No.