Unit+1+Pop+Review


 * Open Note Pop Review **  


 *  1.    What is the line of code that makes a class into a program that you can run?   **

public static void main(String[] args)  a)    an amount of money ** double double String
 *  2.    What is the best data type for the following variables?
 *  b)     a distance **
 *  c)    a book title **
 *  d)    a zip code

**int //pattern: type varName// ex) String word1; int x; Cat bob;
 *  3.    Give an example of __declaring__ a variable. **

//pattern: varName = value;// ex) word1 = "Hello"; x = 5; bob = new Cat //must use the constructor for object type variable **// pattern: type varName = value; //ex) String word1 = "Hello"; int x = 5; Cat bob = new Cat;
 *  4.    Give an example of __initializing__ a variable. **
 *  5.    Give an example of both __declaring and initializing__ a variable.

Implicit means //implied //parameters (ex. System.out.println////;) while explicit means the parameters are told (ex. System.out.println//(“Hello World!”)//;). Implicit parameters use information that is outside of the method but held by the object, such as from instance variables.  **    A method needs explicit parameters rather than implicit when the information that is required for a method is not outside the method. (Ex. Mutator methods commonly need explicit parameters because they change information outside the method with the parameters given. The parameters are not implied because the program would not know what the outside data would be changed to.)
 *  6.    Explain the difference in implicit and explicit parameters. **
 * 7.**   ** When does a method need explicit rather than implicit parameters? What type of method needs only explicit parameters?

Static methods need only explicit parameters, which is why they are designed to function from a class name rather than being called by an object.  a.    Write an accessor method for this instance variable **
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> 8.    Within a class, you have a String instance variable called Cheese.

<span style="FONT-FAMILY: 'Courier New', Courier, monospace">public String getCheese <span style="FONT-FAMILY: 'Courier New', Courier, monospace">{ return Cheese };

<span style="FONT-FAMILY: 'Courier New', Courier, monospace">public void setCheese(String newCheese) <span style="FONT-FAMILY: 'Courier New', Courier, monospace">{ Cheese = newCheese }; ** <span style="FONT-FAMILY: 'Courier New', Courier, monospace">public void printCheese <span style="FONT-FAMILY: 'Courier New', Courier, monospace">{ System.out.println(Cheese)}; <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> a.    truncation ** <span style="FONT-SIZE: 11pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Calibri','sans-serif'; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA">Truncation occurs when mathematical calculations are done with two ints and the result would normally have decimals. In Java, these decimals are cut off/rounded DOWN and an int is given as a result ** to combine two Strings into one. In Java, concatenation is done using the ‘+’ symbol. Ex. String newWord= “Base”+”ball”;
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> b.    Write a mutator method for this instance variable **
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> c.    Write a method that prints the value of the instance variable on the screen.
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> 9.    Explain the following terms:
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> b.    concatenation

**Type casting= commonly known as casting, type casting is how a programmer moves form a larger piece of data to a smaller one, such as from a double to an int. Because casting can cause data loss, it is not always right to use. Ex. <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">Int x= 5; <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">Double y= 8.8; <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">Int q= (int) y; <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">Int q= 8; **<span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">something that when given a piece of information, yields a predictable result, but its inner workings are a mystery. When methods are written, they are created as black boxes. (See encapsulation) <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> ** e. **  **encapsulation** <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri">   <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">Encapsulation= the process of making a black box. To encapsulate, certain things are made private rather than public. Because these things are private, a programmer needs to provide information that would be in the API for the user via comments. **<span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">Something should be public if the programmer wants others to be able to use it. Other things should be private if the programmer wants the users not to mess with it. Generally… <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'"> <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">Public: Class, Methods (that the programmer wants users to use), Constants <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">Private: Instance variables, Helper methods (methods that are called by real methods. These should never be used alone)
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> c.    type casting
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> d.    black box
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> 10.    When should something be public or private?

**<span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">The word ‘final’ is used to indicate that the value of a variable cannot change, and thus that variable is a constant. Constant variables also are completely capitalized for differentiation between instance variables and constants. **<span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri">   <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">A static method is a method that does not require an object in order to be used. Static methods are instead called with their class name and are usually very simple, executing only very specific tasks. <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">Ex. Static method <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'"> Math.pow(x,y); <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'"> //Raises ‘x’ to the power of ‘y’// <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">Ex. Static constant <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'"> Math.PI <span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'"> Equals= 3.14159 (pi)
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> 11.    What reserved word makes a regular instance variable into a constant?
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> 12.    What is a static method? Give an example of a static method and a static constant.

<span style="FONT-SIZE: 11pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Calibri','sans-serif'; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA">Non-void methods return a value, specified by the return type (public type methodname) What types can be returned? primitives and object types Void methods return nothing, but still take action within the program (public void methodname) **<span style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'">A default constructor contains preset variables values and does not need parameters. A custom, or non-default, constructor requires parameters from the user to fill in the variables with provided values. <span style="FONT-SIZE: 11pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Calibri','sans-serif'; mso-fareast-font-family: 'Malgun Gothic'; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: KO; mso-bidi-language: AR-SA"> int y = 2 double a = 1.5 double b = 2.75 String s = “Cats” String t = “Rule” ** <span style="FONT-SIZE: 11pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Calibri','sans-serif'; mso-fareast-font-family: 'Malgun Gothic'; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: KO; mso-bidi-language: AR-SA"> <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> ** a. **   **a + y / x –b = - 1.25** a + 0 - b 1.5 - 2.75 = -1.25
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> 13.    Explain the difference between a void method and a non-void method. **
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> 14.    Explain the difference between a default and non-default constructor.
 * <span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> 15.    What will the value of each of the following expressions be after the code is executed?  **
 * int x = 5

<span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> ** b. **  ** Math.floor(b) / y = 1.0 2.0/2 (double/int = double)

<span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> c.    **** x + a – b * y = 1.0 x + a - 5.5 5 + 1.5 - 5.5 = 1.0 ****

<span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> d.    **** Math.sqrt(a) * Math.sqrt(a) = 1.5 a (or 1.5)

<span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> e.    **** Math.sqrt(s.length ) = 2 Math.sqrt(4) = 2.0

<span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> f.    **** String k = t.substring(0,1) + s.substring(1) = "Rats" String k = "R' + "ats" = "Rats"

<span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> g.    **** x++ / y / a = 2 (6 / y )/ a 3 / 1.5 = 2.0

<span style="mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"> h.    Math.pow(x, y) ** = 25.0

1. Is it public or private? 2. Is it void or should if have a return? If return, what type? 3. What should I name it? 4. Should it have explicit parameters? Do we need information from outside the object? 5. How does it work? (write the body)
 * Steps to take when writing a method:**