unit+1


 * Unit 1, Concepts, Definitions and Review**

Objects and Primitives

//State// is what the object is and contains __variables__ to define it. [ex. chair is blue, short] //Behavior// (__methods__) is what the object is able to do. [ex. chair can roll, break]
 * Objects in java have two things: State and Behavior. Explain.**

String, System.out, Scanner
 * So far, we have used several objects in our code. List 2 of them:**


 * List the 8 primitives in order of size, noting what type of data they can hold.**

boolean- 1 bit, yes/no (1/0); //byte//- 1 byte, whole number (+/- 127) //short//- 2 bytes, whole number; //char//- 2 bytes, single alphanumeric character; //int//- 4 bytes, whole number; //float//- 4 bytes, floating point number; //long//- 8 bytes, whole number; //double//- 8 bytes, floating point number;

A double can hold and int, but an int can't hold a double unless you cast it. ex) double x = 7.5; int y = 4; double z = 4; //OK int q = x; //Error in compiler int q = (int) x; will make q equal to 7 because it __truncates.__

An //object// can hold values and variables and have/use methods and a //primitive//, while it can be a value or variable, cannot hold them and cannot have/use methods.
 * Explain the difference in an object and a primitive.**

You would want to import a library when you need a specific object, such as Scanner for when you want the computer to read keystrokes from the keyboard. Many basic classes and primitives are automatically available without importing.
 * When do you need to import a library?**

String; because a name is a series of letters int or double; int if you want the age to be whole [ex. 42]; double if you want the exact age [ex. 42.5] int; because a grade level is a whole number double; because GPA is normally not a whole number, but with fractions or decimals
 * You need to represent the following information using variables. For each, state which type of variable you would use and why:**
 * **Name**
 * **Age**
 * **Grade Level**
 * **GPA**

.replace(String toBeReplaced, String Replacement); -- replaces texts with another .toUpperCase; -- shifts to uppercase .toLowerCase; -- shifts to lowercase .length; -- measures the length and displays it as an int .substring(int iStart, int iEnd); -- cuts out and returns part of the word, beginning with the character in index iStart and end at iEnd - 1. The original string is unchanged ex) String myCat = "Aphrodite"; System.out.println(myCat.substring(2,6)); this code will print hrod .substring(int index); -- cuts out and returns part of the word beginning with the character in "index" and going through the end. The original string is unchanged. ex) String myDog = "Rover"; System.out.println(myDog.substring(3)); this code will print "er"
 * List the methods of the String class and indicate what each one does.**
 * remember that the index of the string starts with 0 and goes through length - 1**


 * What is the pattern for calling a method?**

objectName.methodName(parameter1, parameter2); OK: myCat.length; OK: myDog.substring(4); NOT OK: myDog.substring; //substring must have parameters to function

NOT OK: String.length;// Must use object name not class name.

.next; -- scans for the next word the user enters .nextLine; -- scans for the next line .nextInt; -- scans for the next int .nextDouble; -- scans for the next double
 * What are the 4 methods of the Scanner and what are they used for?**


 * How do we create an object?**

Must Import the right library

Type objectName = new Type(parameter);

ex) Scanner input = new Scanner(System.in);


 * What two processes are part of creating a variable?**

Declaration - defining variable with name and type ex) int myAge; Initialization - assigning first value ex) myAge = 103;

This can also happen in one step: ex) int myAge = 103;