Final Exam Study Guide

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the result if you say 9 / 10? What is the result if you say 9.0 / 10.0? What is the result if you say 9L / 10L? What is the result if you say 9.0F / 10.0F?

9/10 where both values are integers, will return a an integer (a whole number.) 9.0/10.0 where both values are floating point values, will return a floating point value. (decimal) 9/10.0 one is an integer and the other is a floating point value, will a floating point value. (decimal). 9L / 10L turns the values into long integer. This will return a long integer (a whole number). 9.0F / 10.0F turns the values into floats (F = float). This will return a float value (decimal). pg 69-70 Lewis and Loftus; pg 40-42 Horstmann "Java for Everyone Late Objects"

When does the parent constructor run? When does the child constructor run?

A child's constructor is responsible for calling its parents constructor. pg 417 Lewis and Loftus

What is a constant? How do you create a constant in Java? What are they used for?

Constants hold a particular value for the duration of their existence. They are identifiers and are similar to variable, but they hold a specific value for the duration of their existence. In Java, any word preceded by *final will be made constant. Uppercase letters should be used when naming constants to distinguish them from regular variables. Constants prevent coding errors because only the initial value can be used. pg Lewis and Loftus 67-68

What is a get method? What is a set method? Why do we have them?

Get and Set methods are also known as Accessors adn Mutators. Get method isa method that provides read only access to a particular value. The set method allows changes in a particular value. Method names are such: getXYZ and setABC where XYZ is the value which it provides access, and ABC is the value they are setting. pg 159 Lewis and Loftus

What is a global variable?

Stored data that may be used by any part of the program. A variable is simply a storage location for a value that is known or may be changing often based on inputs. By making a variable "global" it means all parts of the program can access it. Only using global variable in large programs can create problems because how the data is used in subroutines might be slightly different.

How do you pull the first character off of a String?

String str = "Sally"; str = str.substring(1); results in "ally" pg 63 Hortsmann "Java for Everyone Late Objects"

How do you set up a method? What are the required parts?

The header of a method includes the type of the return value, the mothod name, and a list of parameters that the method accepts. The statement that makes up the body of the method are defined in a block delimited by braces. pg 160-164 Lewis and Loftus

What is protected access?

When a variable or method is declared with protected visibility, a derived class can reference it. And protected visibility allows the class to retain some encapsulation properties. pg 411-413 Lewis and Loftus

How do you find the smallest element in an array using a for loop?

double smallest = values[0]; for (int i = 1; i < values.length; i++) { if values[i] < smallest) { smallest = values[i]; } } pg 259 Horstmann "Java for Everyone Late Objects"

What does && mean in Java? What does || mean? How are they used in an if block or a while loop?

&& is a logical operator. It is the logical AND. True is a and b are both true and false otherwise. || is a logical operator. It is the logical OR. True is a or b or both are true and false otherwise. They are both binary operators since each uses two operands. if (temp > 0 && temp < 100) { System.out.println("liquid"); } if (temp <= 0 || temp >= 100) { System.out.println("not liquid"); } while (temp > 0 && temp < 80) { temp ++; } while (temp <= 0 || temp >= 100) { temp ++; } pg 194 -195 Lewis and Loftus

How do you create an array that is able to hold ten integers?

To create an array, the reference to the array must be declared. The array can be instantiated using the new (reserved word) operator. int[ ] height = new height[10]. int [ ] height sets up an array ([ ]) of integers (int) called height. height [10] sets the array to hold 10 elements. pg 357-360 Lewis and Loftus

What is a Boolean? How is it different than a String or an Integer?

A boolean variable is usually used to indicate whether a particular condition is true but it can also be use to represent any situation that has two states, such as a light bulb being turn on and off. Returns a value of true or false. Reserve word. Differs because it only returns true or false. It cannot be converted to any other data type, nor can any other data type be converted to a boolean value. True and False are also reserve words. pg 72 Lewis and Loftus

What is a character? How is it different than a String? How are they related?

A character is a fundamental type of data. Individual characters can be treated as separate data items, and they can be combined to form character strings. 'A' '#' 'b' is a character. java support the Unicode Charater set which uses 16 bits per character with 65,536 unique characters. pg 71-72 Lewis and Loftus

What is a class?

A class is a blueprint of an object. it represents the concept of an object, and any object created from that class is a realization of that concept. It establishes the kind of data an object of that type will hold and defines the methods that represent the behavior of such objects pg 46, 148 Lewis and Loftus

What is a class variable?

A class variable also known as a static variable, is shared among all instances of a class. . There is only one cop of a static variable for all objects of the class. Therefor, changing the value of a static variable in one object changes it for all of the others. The reserve word Static is used as a modifier to declare a static variable. pg 294 Lewis and Loftus

What is a class variable?

A class variable is a static variable. Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. There would only be one copy of each class variable per class, regardless of how many objects are created from it. Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final and static. Constant variables never change from their initial value. Static variables are stored in static memory. It is rare to use static variables other than declared final and used as either public or private constants. Static variables are created when the program starts and destroyed when the program stops. Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class. Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally values can be assigned in special static initializer blocks. Static variables can be accessed by calling with the class name ClassName.VariableName. When declaring class variables as public static final, then variables names (constants) are all in upper case. If the static variables are not public and final the naming syntax is the same as instance and local variables. Note: If the variables are access from an outside class the constant should be accessed as Employee.DEPARTMENT pg 294 Lewis and Loftus

What is a constructor? Can you have multiple constructors?

A constructor is similar to a method that is invoked when an object is instantiated. Yes, a class can have multiple constructors pg 169Lewis and Loftus

How do you set up a do loop? How many times does the loop run if you have count++ inside of the condition? How many times does the loop run if you have ++count inside of the condition?

A do statement is similar to the while statement except that its termination condition i s at the end of the loop body. "do { this } while ( this )" do { statement } while (expression); The body of the do loop continues until the while clause that contains the boolean condition that determines whether the loop body will be executed again. ++count and count++ will continue to run as long as the condition is true. Once false, the program will seek the while condition. pg 261-264 Lewis and Loftus

How do you call a method? How do you get a value back from a method?

A method declaration specifies the code that is executed when the method is invoked. When a method is called, the flow of control transfers to that method. the called method might be part of the same class as the calling method that invoked it. A method that returns a value must have a return statement. A method that does not return a value does not usually contain a return statement. The method automatically returns to the calling method when the end of the method is reached. It is usually good practice NOT to use more than one return statement in a methods even though it is possible to do so. pg160-162 Lewis and Loftus

How do you set up a while loop?

A while statement is a loop that evaluates a boolean condition just as an if statement does and executes a statement (called the body of the loop) if the condition is True. However, unlike he if statement, after the body is executed, the condition is evaluated again. If it is still true, then the body if executed again. The repetition continues until the condition becomes false. while(this is true) { do this } while (total > max) { total = total / 2; System.out.println("Current total: " + total); } pg 214-218 Lewis and Loftus

What are abstract classes? What are interfaces? What is the difference?

Abstract method is a method that does not have an implementation. Abstract classes represents a generic concept in a class hierarchy. An abstract class cannot be instantiated and usually contains one ore more abstract methods, which have no definitions. An abstract class is similar to an interface in some ways. However, unlike interfaces, an abstract class can contain methods that are not abstract. It can also contain data declarations other than constants. Abstract classes serve as placeholders in a class hierarchy. Interfaces is a term used to refer to the set of public methods through which we can interact with an object. A java interface is a collection of constants and abstract methods. Methods in interfaces have public visibility by defaults. A class implements an interface by providing method implementations for each of the abstract methods defined in the interface. Multiple classes can implement the same interface. A class can implement more than more interface. pg 310-317, 425-426 Lewis and Loftus

What does modulus mean? What is the modulus operator in Java? How do you determine if the number is even using the modulus operator?

According to the Third College Edition of Webster's New World Dictionary(yes it is old...1994), modulus is A) the absolute value of a complex number computed by adding the squares of each par and taking the positive square root of the sum . B) a quantity which gives the same remainders when it is the divisor of two quantities. C) a factor by which a logarithm to one base is multiplied to change it to a logarithm to another base. Modulus operator is also known as the remainder operator. The remainder operator (%) returns the remaineder after dividing the seond operatend into the first. The sign of the result of a remainder operation is the sign of the numerator. pg 73 Lewis and Loftus

What happens when an exception is thrown?

An exception is through by a program or the run-time environment and can be caught and handled appropriately if desired. If a program does not handle the exception at all, it will terminate abnormally and produce a message that describes what exception occurred and where it was produced. The information associated with an exception is often helpful in tracking down the cause of the problem. If an exception is not caught and handled where it occurs, control is immediately returned to the method that invoked the method that produced the exception. We can design our software so that the exception is caught and handled at this outer level. If it isn't caught there, control returns to the method that called it. This is called propagating the exception . it continues until the exception is caught and handled or until is passed out of the main method, which terminated the program and produces an exception message. pg 502-512 Lewis and Loftus

How do you create an interface with two required methods? How do you implement the interface in a class?

An interface is a collection of abstract methods that can not be instantiated. To create an interface with two required methods, you must do the following: public interface (name) { public void setNAME (data type and variable); public DATATYPE getNAME(); } You implement the interface by calling it from the main class public class MainJoint implements (name){ } pg Lewis and Loftus 311

What is an object? How do you create an object?

An object is a fundamental entity in a java program. Often represents a real object in our problem domain, such as a bank account. pg 146-47 Lewis and Loftus

What is an instance variable?

Each instance of the class has its own version of the variable Each object has a distinct memory space for each variable so that each object can have a distinct value for that variable. pg 294 Lewis and Loftus

What are exceptions? How do you handle exceptions? How do you throw exceptions?

Exceptions are an object that defines an unusual or erroneous situation. An exception is thrown by a program or the run-time environment and can be caught and handled appropriately is desired. Situations that cause exceptions to be thrown: Attempting to divide by 0, an array index that is out of bounds, a specified file that could not be found, a requested I/O operation that could not be completed normally, an attempt was made to follow a null reference, an attempt was made to execute to execute an operation that violates some kind of security measure. A program can be designed to process an exception in one of three ways: not handle the exception at all, handle the exception where it occurs, or handle the exception at another point in the program. Try catch statement identifies a block statement that may throw an exception. Most often, a finally clause is used to manage resources or to guarantee that particular parts of an algorithm are executed. If no exception is generated, the statements in the finally clause are executed after the try block is completed. if an exception is not caught and handled where it occurs, control is immediately returned to the method that invoked the method that produced the exception. We can design our software so that the exception is caught and handled at this outer level. IF it isn't caught there, control returns to the method that called it. This process is called propagating the exception. pg 502-512 Lewis and Loftus

How do you convert an if block into a switch statement?

If block is converted to a switch statement by setting each if to a case. if (num1 == 5) myChar = 'W'; else if (num1 == 6) myChar = 'X'; else if (num1 == 7) myChar = 'Y'; else myChar = 'Z'; switch(num1) { case5: myChar = 'W'; break case6: myChar = 'X'; break case7: myChar = 'Y'; break default: myChar = 'Z'; } pg 197-210, 256-260 Lewis and Loftus - example pg 260 SR 6.4 answer on pg 716

What is a local variable?

Local variables are declared in methods, constructors, or blocks. Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block. Access modifiers cannot be used for local variables. Local variables are visible only within the declared method, constructor or block. Local variables are implemented at stack level internally. There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use. pg 163-164 Lewis and Loftus

When discussing methods, what is the difference between overload and override?

Method overloading is when you can use the same method name with different parameter lists for multiple methods. Useful when you need to perform similar methods on different types of data. Method overriding is when a child class defines a method with the same name and signature as a method in the parent class. The need for overriding occurs often in inheritance situations. Method overriding is a key element in object - oriented design. It allows two objects that are related by inheritance to use the same naming conventions for methods that accomplish the same general test in different ways. It becomes even more important when it come to polymorphism. pg 331-333, 419-422 Lewis and Loftus

How many times does a System.out.println( ) run if it is contained in a for loop that is contained inside another for loop (nested for loop)?

Nested for loop is used mostly with tables. pg 172-175 Horstman" Java For Everyone Late Objects" However, with an nested loop, the inner loops loops x times per 1 loop of the outer loop. so if the inner loop is to execute 10 times and the outer loop is to execute 10 times, then you can multiple the loops to find the number of times the program will loop. in this case 10 X 10 = 100. pg 220, 265-266 Lewis and Loftus

When working with methods, what is "pass by value"? What is "pass by reference"? How are they different? Does Java favor one over the other?

Pass by value... All parameters are passed by value. The current value of the actual parameter (in the invocation) is copied into the formal parameter in the method header. When a parameter is passed, any changes made to the formal parameter do NOT affect the actual parameter. Pass by reference... When a parameter is passed, the memory location of the object referenced by the actual parameter is passed to the formal parameter, so that any changes made to that object by the formal parameter immediately affect the actual parameter, since both formal and actual parameters are referencing, or "pointing to", the same object in memory. pg 326 Lewis and Loftus

What is the difference between private, protected, and public access to variables and methods?

Private, protected and public are visibility modifiers. Public : can be directly referenced from outside of the object Private: can be used anywhere inside the class definition but cannot be referenced externally. Protected: relevant only in the context of inheritance. (chapter 9). Public variables violate encapsulation. Data declared as private can be accesses only by the methods of the class. A private method cannot be invoked from the outside the class. Its purpose is to help the other methods of the class do their job. (support method). Giving constants public visibility is generally considered acceptable. pg 158 Lewis and Loftus

How do you create a random number using the Math.random( ) function? Can you create a random number from 50 to 70 (inclusive)?

The Math class provides a large number of basic mathematical functions to help in making calculations. The math class is defined in the java.lang package. The java.lang.Math.random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. First you need to import java.lang.*;. Then you use Math.random( ); when you need a random number for something. int max = 70; int min = 50; int range = max - min + 1; int rand = (int) (Math.random()* range) + min; System.out.println(rand); pg 115-118 Lewis and Loftus

What is a child class?

The derived class that the original class uses. There is no limit to the number of children a class can have or to the number of levels to which a class hierarchy can extend. A child derived from one parent can be the parent of its own child class. The child should be a more specific version of its parent. pg 409, 422-423 Lewis and Loftus

How do you set up a for loop? What are the three sections and when are they called? Can you display even numbers from 50 to 100?

The for statement is another repetition statement that is particularly well suited for execuitng the body of a loop a specific number of times that can be determined before the loop is executed. There are three parts to the header of the for loop. The first part is the initialization. The second part is the boolean condition. The third part is the update condition. This is then followed by a statement to execute to which while the boolean condition is true. for (int i = 50; i <=100; i++) { if (i % 2 == 0) { System.out.println(i + " "); } } pg 265-268 Lewis and Loftus

What is the difference between count++ and ++count?

The increment and decrement operators can be applied after and before the variable. Before is prefix and after is postfix. When used alone in a statement , the prefix and postfix are functionally the same. However, when such a for is written as a statement by itself, it is usually written in postfix form. When the increment or decrement operator is used in a larger expression, it can yield different results depending on the form used. always yield to the side of readability. pg 79 Lewis and Loftus

What is a parent class?

The original class that is used to derive a new class. pg 409 Lewis and Loftus

What are overloaded methods? How does Java know which overload to use?

The use of the same method name with different parameters lists for multiple methods.Useful when you need to perform similar methods on different types of data. The compiler must be able to examine a method invocation to determine which specific method is being invoked. If you attempt to specify two method names with the same signature (A methods name, along with the number, type, and order of its parameters), the compiler will issue an appropriate error message and will not create an executable program. There can be no ambiguity. pg 331-333 Lewis and Loftus

How do you create a child class? What does the child get from the parent ?

Through the process of Aggregation and inheritance. Aggregation is known as "Has a" relationship. A Student has an Address. Inheritance is known as " is a" relationship. A Dog is a Animal. The child is derived from the parent. Student is the parent and Address is the child. Animal is the parent and Dog is the child. Therefore we use extends in the header of Address and Dog. A parent passes along a trait to a child class, and that child class passes it along to its children and so on. All methods and variables, even those that declared with private visibility, are inherited by the child class. Constructors however are not inherited. For a child class to refer to the constructor of the parent class, we use the super reference. pg 408-419 Lewis and Loftus

What is a sibling?

Two or more child classes from the same parent class. pg 423 Lewis and Loftus

How does scan.nextInt( ) work if the input given is 10 11 12

When the nextInt method is called, the program waits until the user types a number and presses the ENTER key. After the user supplies the input, the number is places into the variable, and the program continues. The program waits for the user input, then places the input into the variable. pg 49-50 Horstmann "Java for Everyone Late Objects"

How do you create and load an array in one single line?

You can use an initializer list to instantiate an array and provide the initial values for the elements of the array. The items in an initializer list are separated by commas and delimited by { }. When the initializer list is used, the new operator is not used. The size of the array is determined by the number of the items in the initializer list. int[ ] scores = {87, 98, 57, 65, 85, 75, 88} int [ ] scores sets up a new array ([ ]) of integers (int) called scores. The array has 7 total elements as listed in the { }. pg 365-366 Lewis and Loftus

When do variable get created in memory and when are they released from memory?

a variable declaration instructs the compiler to reserve a portion of memeory space large enough to hold a particular type of value and indicates the name by which we refer to that location. a variable can store only one vlaue of its declared type. Accessing (reading) data leave the values inmemory in tact, but writing data replaces the old data with new. pg 63-65 Lewis and Loftus

How do you set up an if block? How many branches can an if block contain? When are curly braces required?

an if block is an if statement, otherwise known as a condition statement. If statement starts with the reserve word if followed by a boolean expression in ( ) followed by { statement } to which a statement is executed if true or skipped to the next statement if false. "if (this) { do this. }" if (count > 20) { System.out.println("Count Exceed."); } A block statement is a collection of statements enclosed in braces. e.g. { System.out.println("That is not correct, sorry"); System.out.println("The number was " + answer); } An if block can contain multiple if statements within it however, you should use an else statement along with it. It is good practice to enclose your statements in { } though you do not have to. Indentation should be proper enough. pg 192-210 Lewis and Loftus

How do you convert a for loop into a do loop?

for(int count = 0; count <= 5; count ++) System.out.println(count); int count = 0 ; do { count++; System.out.println(count); } while (count <5); Part 1 int count = 0 is the first part of both the for and do. Part 2 Count <=5 in the for loop is part 4 of the do while loop. Part 3 count ++ of the for loop is part 2 of the do while loop. Part 4 System.out.println(count); of the for loop is part 3 of the do while loop. pg 261-268 Lewis and Loftus example pg 262 do while loop and pg 265 for statement

What does "a".compareTo("d") return? Why?

int compareTo(String str) returns an integer indicating if this string is lexically before (a negative return value) equal to (a zero return value), or lexically after (a positive return value), the string str. Therefor a is being compared to d and seeing if they are equal, less than, greater than each other and printing out some value assigned to it. pg Lewis and Loftus

How do you set up inheritance that has three layers: grandparent, parent, child?

pg 422 You use a class hierarchy. there is no limit to the amount of children a class can have or the number it extends. The only characters explicitly established in a child class are those that make the class distinct from the parent. a parent should pass a trait to a child.


Conjuntos de estudio relacionados

Chapter 4 Mastering Biology (Bio 1030)

View Set

Earth Science Chapter 14: Climate

View Set

Health Education in the Elementary Schools

View Set