Preludes+Apendices+Chapter 1-4+Mid-term Notes

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

(T/F)Assuming the BagInterface<T> interface as we discussed the following statement is legal: BagInterface<String> L = new BagInterface<String>();

FALSE - We cannot make objects of interfaces

when an array is passed as a parameter, it is possible to

alter the contents of the array

To get objects out of the array list, use the

get method; For example, accounts.get(2) retrieves the account with index 2

If we use composition to create a new Java class utilizing old classes, the new class has a (has a, is a)________ relationship with the old class

has a

REVIEW THIS< IT IS AMAZING

https://quizlet.com/44766304/java-ch-22-lists-stacks-queues-and-priority-queues-flash-cards/

REVIEW THESE QUIZLET"S THEY ARE AWESOME AND I DIDN"T HAVE ENOUGH TIME TO TOTALLY GO THROUGH THEM

https://quizlet.com/45521432/java-data-structures-final-flash-cards/

Java interface VS implemntation

https://stackoverflow.com/questions/18777989/how-should-i-have-explained-the-difference-between-an-interface-and-an-abstract

How do you create arrayList?

https://www.youtube.com/watch?v=e7U-wWdsp78

protected

indicates data fields and methods that are accessible only from within the class that inherit from it

The push method of the stack can be compared with the method ____ for a general list.

insertFirst

the DOT is used for? Example: System.out what about System.out.prinlin

it is a method, it basically calling a method

LIFO means?

last-in, first-out

Index values of an array range from?

0 to length-1

Applets & Application:

An applet is program which runs inside a browser (see more explanation in text book). An application program is a program that runs on your computer. In this course, we cover only application programs.

OBJECT AND CLASSES REVIEW: class is the blue print object contains ______ and has _______ that do actions

An object is a program construct that contains data (fields) and can perform certain actions (methods). A class is a blueprint or prototype from which objects are created. All objects in the same class have the same types of data and the same methods. Example: String str1= "hello"; String str2 = new String("world");

A refrence variable that contains null.....

... does not reference any object.

You use an identifier to....

... name a variable

A variable in program represents...

...a memory location that stores the data

How do you create javadoc style comments?

/** */

When an array is created, all values are initialized with

0 (for an array of numbers such as int[ ] or double[ ]), false (for a bolean[ ] array), or null (for an array of object references)

Suppose the Instructor asks the students to sit in consecutively numbered desks (Array bag is similiar to this) No say if student 8 drops class, that desk is empty, to fill that spot, the teacher has the student sitting in the last desk move there. That way all the desks are filled except at the back end 1. What is the advantage of moving students like this, so vacated desk does not remain vacant? 2. What is the advantage of leaving the desk vacant? 3. What if a student were to drop the courses, which one could do so without forcing another to change desk?

1.All desks are filled, only have to move one student to fill the desk instead all of them You don't have to keep track, of the empty desk 2. Saving time for not doing anything 3.The back, or the students with the highest seat (page 61 in text book)

What/how do you suppress warning? Why is this cast safe

@SupressWarnings("unchecked") T[] tempBag = T[])new Object[capacity]; bag = tempBag; numberOfEntries = 0; This is safe because the new array has null entires

Queue

A collection based on the first-in-first-out policy. Implemented using a Circular Array or Singly Linked List

Stack

A collection based on the last-in-first-out principle. Implemented using an Array or Singly Linked List

Linked List

A data structure where each element of the data structure has a pointer to the next element. WHAT DOES THIS EXACTLY MEAN?

Finally block

A finally block is ALWAYS executed, regardless of the cause of exit from the try block, or whether any catch block was executed (except if System.exit() is called). -Generally finally block is used for freeing resources, cleaning up, closing connections etc. ***If the finally block executes a control transfer statement such as a return or a break statement, then this control statement determines how the execution will proceed regardless of any return or control statement present in the try or catch.***

Packages notes

A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. A package is a folder which contains a collection of classes. The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the "Application Programming Interface" (API).

ArrayList

A resizable-array implementation of the List interface that may be used in place of array, when the array needs to be resized according to the input. Takes linear time.

ARRAY

A structured data type used when you want to reuse your data that can store as much data as you want and must all be of the same type. MORE ON THIS

Encapsulation POLYMORPHISM---MAKE A SLIDE FOR THIS CAUSE I DON"T GET THIS>... PROBABLY ADD NOTES FROM YOUR NOTES ON BEGGINGING SLIDES

A way of hiding details of implementation during the design of the class (I don't fully understand)

I am confused. System is a class and a object? is System.out, system is an object and out is a method or is out another object apart of the

Actually out is a static member in the System class , being an instance of PrintStream. And println is a normal (overloaded) method of the PrintStream class. See http://download.oracle.com/javase/6/docs/api/java/lang/System.html#out. Actually, if out/err/in were classes, they would be named with capital character (Out/Err/In) due to the naming convention (ignoring grammar).

push

Add element to the top of the stack. O(1)

Text inside Parathenses are called: Example: System.out.println("This get printed");

An Arguement. This is method from the System object In the parantheses is what arguement, the information needed for the method to carry out what it needs to do.

Applet vs application

An application can run as a stand alone program. An applet needs another program, like an internet browser. Typically, applets are sent somewhere in the Internet and run there. Applets have dependency issues

Are an instance and a object the same thing?

An instance is an object in memory. Basically you create object and instantiate them when you are using them. Here is a nice writeup on Classes Vs Objects Vs Instances, he is talking Java but it applies to all OO. http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

Interfaces

An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface.

Java interfaces https://www.youtube.com/watch?v=5Aef6vnAxR8&index=11&list=PLckPQEKYlbxewuI8YfxhKsZq-fX6_RU1P I don't full get. REVIEW

Basically, I beleive this means, creating objects from a class rather than inheriting a class. Because a Java class can only extend to one class(because if it could do more than one class, things might get confused on what commands to use from the two or more classses it inhereits) therefore, Java interfaces is creating an object from another class, and sorta extending to use that class in that way.

(T/F) InsertionSort cannot be effectively done on a linked list because of its linked nature

FALSE - It is actually simple to do InsertionSort of a linked list by manipulating the nodes

Type casting is..... WRite example of code of type casting... What is a type cast operator look like?

Changing the type of one thing to another thing. Changing the type of something. NOTE: you can easily convert up, but not down. Meaning you can easily convert int to double, but Java won't let you convert double to int unless you type cast. This is because double has more numbers, more memory than int. Remeber it goes: byte->short->int->float->long->double

Ennumerations--- WTF is this?

Dictionary definition: the action of mentioning a number of things one by one. Enumeration is actually a class Example: enum LetterGrade {A, B, C, D, F} LetterGrade grade; grade = LetterGrade.A; The last one assigns A to grade. BTW, enum line does not need a semi-colon.

Catch block(s)

Exceptions thrown during execution of the try block can be caught and handled in a catch block. -Order of catch blocks: A superclass type (e.g. Exception) catch block must appear after subclass type (e.g. IOException) catch block. -An exception will look for the matching catch block to handle the error. On exit from a catch block, normal execution continues and the finally block is executed. **If no exception occurs (or no matching catch block) the execution proceeds with the finally block.**

(T/F)The data in a singly linked list is contiguous

FALSE - A fundamental property of linked list is that t he data is NOT contiguous, but, rather, in arbitrary locations

Java Primitive types can be stored in ArrayBags (T/F)

FALSE - ArrayBags store only objects (and subclasses thereof). If you want to store primitive values, you must use wrapper classes. (ex. Integer)

top

Return the value of the top element of the stack without removing it. O(1)

WHY? (T/F) So that we do not waste memory, when resizing an array-based bag or stack in Java it is best to increase the size by a minimal amount (ex. making the new_length = (old_length + 1))

FALSE a small increment will case frequent resizing which has a lot of overhead

(T/F) To see if two different objects contain the same data (i.e. are equivalent) in Java, we use the predefined == operator.

FALSE the == operator compares addresses, not contents

Elements are deleted from a queue at one end, called the ____.

Front

Encapsulation

Hiding and making the code neat, so it cannot be hacked. Basically making variables so they cannot be changed easily by anyone.

Difference between java class and interface

Java interface begins like a class definition, except that you use the word interface instead of class. ____________________________________ An interface is a contract: The guy writing the interface says, "hey, I accept things looking that way", and the guy using the interface says "OK, the class I write looks that way". An interface is an empty shell. There are only the signatures of the methods, which implies that the methods do not have a body. The interface can't do anything. It's just a pattern. Implementing an interface consumes very little CPU, because it's not a class, just a bunch of names, and therefore there isn't any expensive look-up to do. It's great when it matters, such as in embedded devices. ___________________________________________ Abstract classes Abstract classes, unlike interfaces, are classes. They are more expensive to use, because there is a look-up to do when you inherit from them. Abstract classes look a lot like interfaces, but they have something more: You can define a behavior for them. It's more about a guy saying, "these classes should look like that, and they have that in common, so fill in the blanks!". ____________________________________________________________ Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs. Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (they are defined public by default). When inheriting an abstract class, a concrete child class must define the abstract methods, whereas an abstract class can extend another abstract class and abstract methods from the parent class don't have to be defined. Similarly, an interface extending another interface is not responsible for implementing methods from the parent interface. This is because interfaces cannot define any implementation. A child class can only extend a single class (abstract or concrete), whereas an interface can extend or a class can implement multiple other interfaces. A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility (public)

A stack is also called a ____data structure.

LIFO

(Java Basics)

Main Features: no pointers, automatic garbage collection, rich predefined class library etc

to use Math library

Math.method_name(arguments)

when to use Static method???

One rule-of-thumb: ask yourself "does it make sense to call this method, even if no Obj has been constructed yet?" If so, it should definitely be static. So in a class Car you might have a method double convertMpgToKpl(double mpg) which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But void setMileage(double mpg) (which sets the efficiency of one particular Car) can't be static since it's inconceivable to call the method before any Car has been constructed. (Btw, the converse isn't always true: you might sometimes have a method which involves two Car objects, and still want it to be static. E.g. Car theMoreEfficientOf( Car c1, Car c2 ). Although this could be converted to a non-static version, some would argue that since there isn't a "privileged" choice of which Car is more important, you shouldn't force a caller to choose one Car as the object you'll invoke the method on. This situation accounts for a fairly small fraction of all static methods, though.)

public method can be used by? Versus are private method can only be used by?

Public can be used by any class private can only be used by a class that defines it. Interestingly enough, u can use private variables in other classes if your are using the methods from the private class(DOUBLE CHECK THIS)

pop

Remove the top element of the stack. O(1)

difference between void and return method.

Return methods will require return type specification public int getValue() { return Value } Whereas void method will execute a task public void setValue { Value = 3; } Return - Stops execution and returns to the calling function. When a return statement is executed, the function is terminated immediately at that point, regardless of whether it's in the middle of a loop, etc. Void - Same as a value-returning function, except it does not return a value to the function or program that calls it. All methods in Java have a 'return' type. You can imagine that if a method in Java returns something it's as if that method was a sum and the returned value is the answer to that sum. A method with a return type of String returns a String, a return type of int returns an int etc. So say if a method is declared as: public static long getVector(long x1, long x2, long y1, long y2){ ... } then you know that it will return a long. You can capture the "answer" (return value) of this method by calling it like this: long myVariable = getVector(1,2,3,4); The return keyword generally comes at the end of the execution path of a method (though be aware this isn't always the last line of code in a method) and marks the value you wish to return; For example public static String getAString(){ String s1 = "what is"; String s2 = "a String?"; return s1; } Will return the String s1, the value of which is "what is"; A function declared with a void return type, eg public static void Main(String[] args){ ... } returns nothing. Void methods do not need to have a return statement (although they may have, but will simply be 'return;' and mark the end of an execution path in the method, returning program flow to the method's invocation point).

HOLLY SHIT INTRO TO POLYMORPHISM: People tuna = new Bucky();

So People is a super class of Bucky Bucky inherits the People class This is useful when working with arrays, because you can make an People array that stores objects of Bucky, Garrett, Derek, Alphie and more https://www.youtube.com/watch?v=0xw06loTm1k

wow tricky, think about this In Java, the equals() method of a class ArrayQueue should be declared to return a boolean, and to take on argument of type Object.

T

(T/F) Assuming the BagInterface<T> interface and the LinkedBad<T> class as we discussed, the following statement is legal: Bag Interface<String> L = new LinkedBag<String>();

TRUE

I DON"T GET THIS, LOOK INTO THIS!!! (T/F) A reference of some Java interface type can be used to store any object that implements that interface.

TRUE

Problem with declaring and intilizing in a for loop: example for(int i=2; i<=counter; counter++)

The problem is that variable is always, and only used in that loop. AKA the variable is not avaible after the loops completion.

Abstraction

The proccess of abstraction is focusing on the what rather than the how. I believe it is when you are designing a program, you basically, don't focus on the details, but are more meta. When creating a program, you are thinking of a skeleton, and not filling out the methods to how they work exactly.

Objects

Things that belong to a class. The actions preformed by objects are defined by methods.

A disadvantage of arrays compared to linked lists is that it takes more steps to insert a new element at the beginning of a long array than at the beginning of a long linked list.

True

Adding or pushing an element onto the stack is a two-step process. True or False?

True

Class invariants can be considered to be both pre- and post- conditions of every method of the class

True

Inheritance creates an 'is-a' relationship; composition creates a 'has-a' relationship.

True

True or false? Booleans cannot be converted to other types.

True

You can use stacks to convert recursive algorithms into nonrecursive algorithms. True or false?

True

@Override what is it? Why and when you use it.

Use it every time you override a method for two benefits. Do it so that you can take advantage of the compiler checking to make sure you actually are overriding a method when you think you are. This way, if you make a common mistake of misspelling a method name or not correctly matching the parameters, you will be warned that you method does not actually override as you think it does. Secondly, it makes your code easier to understand because it is more obvious when methods are overwritten. Additionally, in Java 1.6 you can use it to mark when a method implements an interface for the same benefits. I think it would be better to have a separate annotation (like @Implements), but it's better than nothing.

Abstract modifier

Use to create abstract classes and methods.

In class autmobile it creates objects Automobiles what is instantiated is BobsCar, DudesCar, SusansSUV what are those called?

Variables of Automoblile

LIST OF QUESTIONS I HAVE:

What is garbage collection? What is the difference of an instance variable and a reference variable? What is Polymorphism? How do you use it? How is this helpful/useful? What exactly is static? What exactly is Final?

What are all these components? Bucky tuna = new Bucky(); What is: 1) First Bucky 2) tuna 3) = sign in this senario? 4) new 5)Bucky #2 6) ()

What is: 1) First Bucky is the data type 2) tuna is a reference variable 3) = sign in this senario? 4) new 5)Bucky #2 is the object/class 6) ()

Keyword: assert

What it does: Basically a boolean statement in the program. It checks if the boolean statement is true or false. If true the program keeps running. If false, the program ends and terminals spits out: Exception in thread "main" java. lang.AssertionError Why:This is done to make sure the program is running properly. Basically helps when debugging a program. You know where things went wrong. It is sorta an extension of the idea of postconditions and preconditions to a method. How to use: assert sum > 0; Note: do not use them in replacement of if statement. They should be an aid , not apart the programming logic. BTW, you have to turn them on, otherwise they will be ignored by execution code.

What is a reference variable?

___________________________________ Description of reference variable: Reference variables are used to refer to an object. They are declared with a specific type which cannot be changed. _____________________________ Info on reference Variable: The only way you can access an object is through a reference variable. A reference variable is declared to be of a specific type and that type can never be changed. Reference variables can be declared as static variables, instance variables, method parameters, or local variables. A reference variable that is declared as final can't never be reassigned to refer to a different object. The data within the object can be modified, but the reference variable cannot be changed.

An array variable stores a reference to the array. Copying the variable yields

a second reference to the same array

Use this to find the length of an array

a.length

Use this to find the size of an Array list

a.size()

You use this to add an object to the end of an array list

add method; The size of the array increases after each call to the add method. The size method yields the current size of the ArrayList.

Making a static data field, once chaged, it changes for _____ objects.

all

Benefits of Array lists

array lists can grow and shrink as needed, the ArrayList class supplies methods for many common tasks such as inserting and removing elements

WHat are reference types? How are they manipulated?

arrays classes Now that we have discussed the syntax for working with objects and arrays, we can return to the issue of why classes and array types are known as reference types. As we saw in Table 2-2, all the Java primitive types have well-defined standard sizes, so all primitive values can be stored in a fixed amount of memory (between one and eight bytes, depending on the type). But classes and array types are composite types; objects and arrays contain other values, so they do not have a standard size, and they often require quite a bit more memory than eight bytes. For this reason, Java does not manipulate objects and arrays directly. Instead, it manipulates references to objects and arrays. Because Java handles objects and arrays by reference, classes and array types are known as reference types. In contrast, Java handles values of the primitive types directly, or by value. A reference to an object or an array is simply some fixed-size value that refers to the object or array in some way.[4] When you assign an object or array to a variable, you are actually setting the variable to hold a reference to that object or array. Similarly, when you pass an object or array to a method, what really happens is that the method is given a reference to the object or array through which it can manipulate the object or array. [4] Typically, a reference is the memory address at which the object or array is stored. However, since Java references are opaque and cannot be manipulated in any way, this is an implementation detail.

Primitive Types :

boolean Boolean value true or false char holds one character (16-bit Unicode) byte 8-bit signed integer short 16-bit signed integer int 32-bit signed integer long 64-bit signed integer float floating-point number (32-bit) double floating-point number (64-bit)

Accessing a nonexistent element in an array results in

bounds error

use this method to copy elements of an array

clone method double[] prices = (double[]) data.clone();

how to grow an array that has run out of space

create a new larger array, copy all elements into the array, store the reference to the new array in the array variable. double[] newData = new double[2*data.length]; System.arraycopy(data,0, newData, 0, data.length); data=newData;

public class Name{ private String last; private String first; } What is: private String last; private String first; called? What does private mean here? What are the words public and private called?

data fields, instance variables or data members. The word private that precedes the declaration of each data dield means that only the methods within the class can refer to the data fields by their names first and last. Access modifiers. They specify where a class, data field or method can be used.

Difference of deceleration VS Initialization

deceleration = int n; Initialization = n =1 declaring that n exists is deceleration n=1 is the initial assignment of an object/variable which is initialization

Holly shit, I am madly behind in this class. And I totally ****ed up and missed class. **** shits. Time to do some mad cramming and studying to catch up. deceleration is? Initialization is?

declaration is int =n; initialization n=10;

variable declartaion

declares what type it is and the data it will hold

How you type cast double to int. In the following double distance = 9.0; int revDistance; What is the rule to type casting, like what order you have to go to?

double distance = 9.0; int revDistance = (int)distance; Remember that the value is not rounded but discarded. Basically you can go in both orders, but when going to larger number to smaller, you have to use (int) while small to big I believe you can do int a =10; double d=a;

Declaration of an array variable

double[ ] data= new double[10];

the = does not mean___ example: int dogs; dogs = dogs-2;

equal, Make the variable equal to what follows

What is the difference between interfaces and inheritance? Give examples of both Polymorphism

extends is for extending a class. implements is for implementing an interface The difference between an interface and a regular class is that in an interface you can not implement any of the declared methods. Only the class that "implements" the interface can implement the methods. Also java doesn't support multiple inheritance for classes. This is solved by using multiple interfaces. public interface ExampleInterface{ public void do(); public String doThis(int number); } public class sub implements ExampleInterface{ public void do(){ //specify what must happen } public String doThis(int number){ //specfiy what must happen } } ______________________________________________ now extending a class ________________________________________ public class SuperClass{ public int getNb(){ //specify what must happen return 1; } public int getNb2(){ //specify what must happen return 2; } } public class SubClass extends SuperClass{ //you can override the implementation @Override public int getNb2(){ return 3; } } __________________________________ in this case Subclass s = new SubClass(); s.getNb(); //returns 1 s.getNb2(); //returns 3 SuperClass sup = new SuperClass(); sup.getNb(); //returns 1 sup.getNb2(); //returns 2

The info part of a node cannot be a reference variable.

false

To delete a given item from an ordered linked list, you do not need to search the list to see whether the item to be deleted is in the list.

false

array length is ________________

fixed (if you decide you need to add additional elements, then you need to make a new array)

The ArrayList class is a ______ class

generic; ArrayList<T> collects objects of type T

All objects of the same kind are.....

of the same class

What is the method of re-writing the toString method called?

overiding a method?... I honestly don't get the question overloading is basically making multiple methods with the same name and have different parameters and return types. 1. Definitions Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class. 2. Overriding vs. Overloading Here are some important facts about Overriding and Overloading: 1). The real object type in the run-time, not the reference variable's type, determines which overridden method is used at runtime. In contrast, reference type determines which overloaded method will be used at compile time. 2). Polymorphism applies to overriding, not to overloading. 3). Overriding is a run-time concept while overloading is a compile-time concept.

he method ____ from the Java class Stack returns and removes the top element of the stack.

pop

Wow, How would you make a double of PI, but make it so it is a constant and unchanging?

public static final double PI = 3.14159;

In a stack, the ____ operation adds an element onto the stack.

push

An interface type is a __________ type? What does this means?

reference type ________ (I don't know what this means? LOok this up)?

reference type

represents a reference, or memory address, instead of an actual item stored at that address.

The Node class that we use in our linked lists has an instance variable of class Node. For this reason Node is called a __________ data type

self referential

To set an array list element to a new value, use the

set method; For example, accounts.set(2, anAccount); sets position two of the accounts array list to anAccount, overwriting whatever value was there before

Try Blocks

statements that you think may produce an exception is placed within a try block for a suitable catch block to handle the error. If an exception is generated within the try block, the remaining statements in the try block are not executed. **Must be followed by either at least one catch block or one finally block**

Every class has a method toString that java uses to get a string representation of any object. If you don not define toString for a class you write......WHAT HAPPENS?

the default toString will returna representation fo an object's location in memory. Thus, you generally should provide your own toString method when you define a class..

The end at which the insertions and deletions occur in a stack is called the ____ of the stack.

top

In a queuing system, the term ____represents the time it takes to serve a customer.

transaction time

Every node in a linked list has two components.

true

The data of a variable is called

value

local variable is

when a variable is declared in a method. Then it can only be used in that method. That is why this. is important and used. Say if two methods have the same variable name, because they are different methods, they are different even though they have the same name.

To treat primitive type values as objects, you use a

wrapper class


Set pelajaran terkait

Chapter 11: Liabilities and Payroll dynamic study

View Set

Modern Physics - Multiple Choice Questions

View Set

AP Stats Extra Practice Chapter 6

View Set

Barron's Dictionary of Legal Terms

View Set