chapter(review(for(( midterm(exam( - east carolina university ·...
TRANSCRIPT
Chapter Review for Midterm Exam
ENGR 2710U (Object Oriented Programming and Design Dr. Kamran SarHpi
Faculty of Engineering and Applied Science UOIT
• What are different components of a computer system?
• CPU, Memory (ROM, RAM), I/O devices (Hard disk, CD/DVD, Monitor, Keyboard, Mouse), Computer Buses (Address, Data, Control)
• How does a CPU execute a program? • It compiles the code wriZen in a high-‐level language (C, C++, Java, Fortran, etc.) into assembly code (machine code) and stores it as an executable code. The object code is loaded in memory and CPU executes the machine code program.
Chapter 1: Computer Program
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Schematic Diagram of a Computer
• What are the contents of “MyCode.java” and “MyCode.class” files, and how are they generated?
• “myCode.java” file contains Java code and is created by user using a text editor. “myCode.class” file contains codes for Java Virtual machine and created by Java compiler.
• What are compile-‐Hme and run-‐Hme errors? • Compile-‐Hme error is due to syntax-‐error
• Run-‐Hme error is due to program’s logic error. – System.ou.println("Hello, World!);
– System.out.println(1/0);
• What is Pseudocode? • An informal descripHon of an algorithm
Chapter 2: Objects
• What is a Type, and give some examples?
• A type defines a set of values and the operaHons that can be carried out on the values. – Number literal (is not object): 23 (Int), 56.44 (Double) – String: “Hello” is an object. – A class is a type and objects are instances
• What is wrong with 45.println() ?
• 45 is not an object
• Which variable names are according to Java convenHons? HelloWorld, hello world, hello_world, 2000Simcoe, public, $helloWorld
• Only hello_world • How many Classes can be instanHated from an Object?
• Nothing. • What are different elements of a Class?
• Instance Variables, Constants, Constructors, Methods.
• What are interfaces of an object? • Those with “access specifier” public • How a user of an object can manipulate the instance variables?
• By using public methods, including constructors. • What is overloaded method? • When a class declares two methods with the same name, but different parameters. Usually constructors
• What is the value of string river at the end? • String river = "Mississippi"; – String bigRiver = river.toUpperCase();
• ”Mississippi”, since toUpperCase creates a new object
• What are implicit and explicit parameters? • Implicit: the object on which a method is invoked (can be made explicit by using this). Explicit: all parameters except implicit par.
• What are accessor and mutator methods • Accossor used to retrieve the value of instance variable, mutator is the only way to change the value of an instance variable.
• How do you use Java API library? • Search for the name of the desired funcHonality and the library gives you the path
• How do you implement a tester program? • Use a main method, inside main construct one or more objects of the target class and pass the test cases to the objects. Print the results and compare them with expected results.
• What is an object reference?
• A variable with the same type of the object that is used to access the object’s interfaces.
• What does the following program do?
• 9 public class RectangleComponent extends JComponent • 10 { • 11 public void paintComponent(Graphics g) • 12 {
• 13 • 14 Graphics2D g2 = (Graphics2D) g; • 15 • 16 • 17 Rectangle helloWorld = new Rectangle(5, 10, 20, 30); • 18 g2.draw(helloWorld);
• 20 • 21 helloWorld.translate(15, 25); • 22 • 23 • 24 g2.draw(helloWorld); • 25 }
• 26 }
• How do you draw a component? 1. Construct a frame. Construct an object of your component class. Add the component to the frame. Make the frame visible
Chapter 3: ImplemenHng Classes
• What is EncapsulaHon or InformaHon Hiding? • Answer:
• The process of hiding object data and providing methods for data access
• To encapsulate data, declare instance variables as private and declare public methods that access the variables
• EncapsulaHon allows a programmer to use a class without having to know its implementaHon
• InformaHon hiding makes it simpler for the implementor of a class to locate errors and change implementaHons
What is wrong with the following code?
public class Testing { public static void main(String[] args) { BankAccount mySavings = new BankAccount(1000); mySavings.balance = 0; } }
The balance instance variable is accessed from the main method of Testing. The compiler will report an error because balance has private access in BankAccount.
Chapter 4: Data Types
• What is the size in bytes of the following types: int, byte, boolean, float, long, char, double, short.
• int (4) .. byte (1) .. boolean (1bit) .. float (4) .. long (8) .. char (2) .. double (8) .. short (2)
• What is Rounding Error? • when an exact conversion between numbers is not
possible. f = 4.35…but .. f * 100 434.999999 • Is this correct? double balance = 13.75; int dollars = balance;
• NO, double can not be assigned to integer, but the reverse is correct.
• What is difference between final and staHc final? What is the proper naming and access specifier for them?
• Final defines constants in method; staHc final defines constants in class. The name must be all capital leZers. And public staHc final
• Why do you use Scanner class? And how?
• Scanner in = new Scanner (System.in); int quanHty.nextInt(); …. nextLine, next ..
Chapter 5: Decisions
• Is this correct? if (amount <= balance) newBalance = balance - amount; balance = newBalance;
• No. the body of if is only one statement. • How do you compare floaHng point numbers? • Using |x -‐ y| < epsilon = 10E-‐14 • How do you compare strings? • String1.equals(string2) • For objects, use == to test object references; use equals to
test the contents
The BankAccount class has three interfaces public void deposit(double amount)
public void withdraw(double amount)
public double getBalance()
• harrysChecking is an object of BankAccount and has some balance. How do you empty the account?
• harrysChecking.withdraw(harrysChecking.getBalance())
• What is unit tesHng? • TesHng a module (object) in isolaHon. It needs a driver (main method) and stubs which are called by the module (object) under the test.
• How do you design a stub? • Different ways: i) lookup table; ii) a method that returns a single value; iv) human playing the role of a method.
• What is local variable and how it differs from instance variable?
• Local variable belongs to a method; it is destroyed when the method terminates. Instance variable belongs to a class and it is destroyed when the object is not referenced any more by other objects.
• Can tesHng prove that the program is correct? • No, tesHng is done to find bugs. It can not guarantee that the program is correct.
• What is whitebox tesHng?
• Take internal structure into account when designing tests. TesHng all statements, branches, and compound branches.
• What is blackbox tesHng?
• Test funcHonality without consideraHon of internal structure of implementaHon
21
Example: statement coverage Design a minimal test for this code
read (x); read (y);"if x > 0 then "" write ("1");"else "" write ("2");"end if;"
if y > 0 then "" write ("3");"else "" write ("4");"end if;" "
Test set covers all statements:"{<x = 2, y = 3>, <x = - 13, y = 51>, " <x = 97, y = -17>, <x = - 1, y = - 1>}"
Test set is minimal"{<x = - 13, y = 51>, ��� <x = 2, y = - 3> }
22
Generate test cases such that every edge (branch) of the control flow is exercised at least once
{<x = 0, z = 1>, <x = 1, z = 3>} causes the execuHon of all edges
if x ≠ 0 then "" y := 5; "else "" z := z - x; "end if;"
if z > 1 then "" z := z / x; "else "" z := 0; "end if;
Weakness of Edge Coverage It fails to expose the risk of a division by zero {<x = 0, z = 3> <x = 1, z = 1>}
Edge Coverage
23
• Based on “playing the computer” operaHons
• Recommended prescripHons – Small number of people (three to five)
• ParHcipants: designer, moderator, and a secretary
– ParHcipants receive wriZen documentaHon from the designer a few days before the meeHng
– Predefined duraHon of meeHng (a few hours)
– Focus on the discovery of errors, not on fixing them
– Foster cooperaHon; no evaluaHon of people • Experience shows that most errors are discovered by the designer during the
presentaHon, while trying to explain the design decisions to other people.
24
• OrganizaHonal aspects similar to code walk-‐through
• A reading technique aiming at error discovery
• Based on checklists; e.g.: – use of uniniHalized variables; – jumps into loops; – Non-‐terminaHng loops; – array indexes out of bounds; – mismatch between actual / formal parameters
• WriHng a procedure that modifies a formal parameter • Calling the procedure with a constant value as the actual parameter;
Chapter 6: lteraHon
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons.
All rights reserved.
• A while statement executes a block of code repeatedly
• A condiHon controls how o{en the loop is executed while (condition) statement
• Most commonly, the statement is a block statement (set of statements delimited by { })
while Loops
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons.
All rights reserved.
Execution of a while Loop
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons.
All rights reserved.
• Example:
for (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; }
• Use a for loop when a variable runs from a starHng value to an ending value with a constant increment or decrement
for Loops
• Explain the syntax of for loop: for (i=0; i <= 8; i++) { loop-‐body}
• “i” is iniHalized before the loop, the condiHon is checked to see if the body will be executed or not. “i” will be incremented a{er the loop-‐body is executed.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons.
All rights reserved.
Execution of a for Loop
Chapter 7: Arrays • How do you insert an element to an Array? • First shi{ all elements from that locaHon to the right and put the element in that locaHon.
• How do you remove an element from an UnOrdered Array?
• Just move the last item of Array to that locaHon.
• How do you insert an element to an ArrayList?