0% found this document useful (0 votes)
80 views15 pages

Midterm Review

This document provides a review of concepts for a midterm exam, including: - Class fundamentals like constructors, methods, and variables - Method specifications and Big O notation - Variables, references, and primitive data types - Array cloning and references - IntArrayBag class including constructor, add, remove, and ensureCapacity methods - Linked lists including node constructor and methods for length, searching, positioning, and adding/removing nodes Example questions are provided for testing understanding of these concepts.

Uploaded by

Kevin Palmer
Copyright
© Attribution Non-Commercial (BY-NC)
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)
80 views15 pages

Midterm Review

This document provides a review of concepts for a midterm exam, including: - Class fundamentals like constructors, methods, and variables - Method specifications and Big O notation - Variables, references, and primitive data types - Array cloning and references - IntArrayBag class including constructor, add, remove, and ensureCapacity methods - Linked lists including node constructor and methods for length, searching, positioning, and adding/removing nodes Example questions are provided for testing understanding of these concepts.

Uploaded by

Kevin Palmer
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 15

Midterm

1 review

Class has : constructor methods variables.

Concepts
1. What items should be included in a complete method specica:on? (In Quiz1) (modifier, return value, method name, parameter list 2. What is a precondi:on? A condition that is supposed to be true when the method begins. A condition that is supposed to be true when the method ends. 3. What is a postcondi:on? 4. What is no-argument constructor? And what does it do? (In Quiz2) A no argument constructor has no arguments and effectively initializes the instance variables. 5. What is the invariant for IntArrayBag?
1. The number of elements in the bag is stored in the instance variable manyItems, which is no longer than data.length. 2. For an empty bag, we do not care what is stored in data[0] through data[manyItems - 1], and we don't care what is stored in the rest of the data.

Big-O

6. Given a formula, get Big-O nota:on (Quiz1 Q1) 7. 1+2+3++n, derive formula and get Big-O nota:on. 8. Compare Big O (Quiz1 Q4)

const O(x) Log O(log n) Linear O(n) quad O(n^2) exp O(n^n)
f(n) =

Variable
9. Test using variable for primi:ve data type as parameter public class Test{ public sta+c void incValue(int v){ v++; System.out.println(v); } public sta+c void main(String args[]){ int x = 1; System.out.println(x); Test.incValue(x); System.out.println(x); } }

10. Test using reference variable as parameter


public class Loca:on{ private double x; private double y; public Loca+on (double px, double py){x= px;y=py;} public String toString(){return (x+","+y);} public sta+c void reverseLoca+on(Loca+on l){ double tmp = l.x; l.x = l.y; l.y = tmp; } } public class Loca:onTest{ public sta:c void main(String args[]){ Loca:on loc = new Loca+on(1.0,2.0); System.out.println(loc); Loca:on.reverseLoca6on(loc); System.out.println(loc); } }

Equals, ==, clone, and =


11. Test the running results of equals, ==, or clone, or = (See Quiz2 Q2, Q3) 12. Implementa:on of equals method for class Loca:on (See Quiz2 Q4). 13. Implementa:on of equals method for class IntArrayBag. (Ref: lab 4) 14. Implementa:on of Clone method for class Loca:on 15. Implementa:on of clone method for class IntArrayBag (Ref: Quiz3 Q3) Class header Super.clone Data clone

Array clone, Array reference variable


16. Draw a picture of memory aeer these statements, or show the results of these statments int[ ] m = new int[2]; m[0] = 0; m[1] = 1; int[ ] p = (int[ ]) m.clone( ); System.out.println("[before]m:"+ m[0]+" "+m[1]); System.out.println("[before]p:"+ p[0]+" "+p[1]); p[0] = 4; p[1] = 5; System.out.println("[aAer]m:"+ m[0]+" "+m[1]); System.out.println("[aAer]p:"+ p[0]+" "+p[1]); [before]m:0 1 [before]p:0 1 [aeer]m:0 1 [aeer]p:4 5

IntArrayBag
17. IntArrayBag Constructor 18. Add method (3 example ques:ons) 19. Remove method (3 similar example ques:ons) 20. EnsureCapacity method(3 similar example ques:ons)

Example ques:on 1
Ref: Quiz 3, Complete codes public void add(int element){ if(manyItems==data.length){ EnsureCapacity((manyItems+1)*2); } data[manyItems]=element; manyItems++ }

Example ques:on 2
Give the add method public void add(int element){ if(manyItems==data.length){ EnsureCapacity((manyItems+1)*2); } data[manyItems]=element; manyItems++ } IntArrayBag bag = new IntArrayBag(2); bag.add(1); bag.add(2); bag.add(2); //what does the memory look like

Example ques:on 3
Debug public void add(int element) { if(manyItems<data.length){ data[manyItems] = element; manyItems++; }else{ ensureCapacity(manyItems*2+1); } } Any problem? Yes/No?

Linked List
21. IntNode constructor 22. IntNode ListLength (test two ways to loop a list) 23. IntNode listSearch (similar to 22) 24. IntNode listPosi:on (similar to 22) 25. IntNode addNodeAeer (Example ques:ons, see later notes) 26. IntNode removeNodeAeer (Example ques:ons, see later notes) 25-28 Func:ons in lab5: Reverse, removeDuplicate, sumLast,ndMiddle Example ques:ons: write one method

listLength
How to loop a list using for
public sta+c int listLength(IntNode head) { IntNode cursor; int answer; answer = 0; for (cursor = head; cursor != null; cursor = cursor.link) answer++; return answer; }

addNodeAeer
public void addNodeAPer(int item) { link = new IntNode(item, link); } Possible Ques:on 1: write this method Possible Ques:on 2: use this method to implement another method Create a list with n numbers Possible Ques:on 3: What error you may get Possible Ques:on 4: what is the :me complexity of this method?

removeNodeAeer
public void removeNodeAPer( ) { link = link.link; } Possible Ques:on 1: write this method Possible Ques:on 2: use this method to implement another method Empty a list removeDuplicate Possible Ques:on 3: What Excep:on you may get Possible Ques:on 4: what is the :me complexity of this method?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy