- A Class is a blueprint for creating object with the same behavior and defined attributes
- An Object is a specific entity and instances of classes named by variables, made from a class, that we can manipulate in our program
- Each object has behaviors and attributes that are defined by the class which does not affect to the behavior of other objects
-The changes on the object won't affect the other object that is defined under the same class so after a change on a object, the others will be unchanged
- A signature consists of the constructor name and the parameter list
- The parameter list lists the types of the values that are passed and the variable names
- We can have more than one constructor, which means overloading and no-argument constructor has no parameters and it sets the instance variables for the object to default values
- The actual parameters that are passed to a constructor must be compatible with the types identified in the parameter list
- Java will match the data types of the constructors with the parameters that we have given
- Parameters are passed using call by value which initializes the formal parameters with copies of the actual parameters
- Every object is creating using the keyword new followed by a call to one of the class’s constructors
- A class contains constructors that are invoked to create objects. They have the same name as the class
- Existing classes and class libraries can be utilized as appropriate to create objects
- Parameters allow values to be passed to the constructor to establish the initial state of the object
- The keyword null is a special value used to indicate that a reference is not associated with any object
- The memory associated with a variable of a reference type holds an object reference value or, if there is no object,null. This value iss the memory address of the referenced object
- An object’s behavior refers to what the object can do and is defined by method
- Procedural abstraction allows a programmer to use a method by knowing what the method does even if they do not know how the method is written
- A method signature for a method without parameters consists of the method name and an empty parameter list which will set the values as default
- A method or constructor call interrupts the sequential execution of statements, causing the program to first execute the statements in the method or constructor before continuing
- Non-static methods are called through objects of the class
- The dot operator is used along with the object name to call non-static methods
- Void method do not have to return and are therefore not called as part of an expression
- Using a null reference to call a method or access an instance variable causes a NullPointerException to be thrown
- A method signature for a method with parameters consists of the method name and an ordered list of parameter types
- Values provided in the parameter list need to correspond to order and type in the method signature
- Methods are said to be overloaded when there are multiple method with the same name but different signature
- Non void methods are methods with actual types that we can return the output on another method so if we are using non void we can only output just one and single data types but if we are using void since void methods don’t have to return a value then we don’t need declare a type for the method
- To use the return value when calling a non-void method, it must be stored in a variable or used as part of an expression
- Class names in Java, like String, begin with a capital letter. All primitive types: int, double, and boolean, begin with a lowercase letter. This is one easy way to tell the difference between primitive types and class types.
- Every class knows its parent class. But, in Java, a class can only have one parent. A class can inherit object fields and methods from a parent class.
- Strings can be added to each other to create a new string using the + or += operator. This is also called appending or concatenating. You can also add any other kind of value to a String with + or += and the other value will be converted to a String automatically.
- Note that spaces are not added between strings automatically. If you want a space between two strings, then add one using + “ “ + and variables are never put inside the quotes (“”) since this would print the variable name out letter by letter instead of its value.
- If you are appending a number to a string it will be converted to a string first before being appended.
- Escape sequences start with a backslash \ and have special meaning in Java. Escape sequences used in this course include \", \\, and \n to print out a quote, backslash, and a new line.
- int length() method returns the number of characters in the string, including spaces and special characters like punctuation.
- String substring(int from, int to) method returns a new string with the characters in the current string starting with the character at the from index and ending at the character before the to index (if the to index is specified, and if not specified it will contain the rest of the string).
- int indexOf(String str) method searches for the string str in the current string and returns the index of the beginning of str in the current string or -1 if it isn’t found.
- int compareTo(String other) returns a negative value if the current string is less than the other string alphabetically, 0 if they have the same characters in the same order, and a positive value if the current string is greater than the other string alphabetically.
- boolean equals(String other) returns true when the characters in the current string are the same as the ones in the other string. This method is inherited from the Object class, but is overridden which means that the String class has its own version of that method.
- Remember that substring(from,to) does not include the character at the to index! To return a single character at index i, use str.substring(index, index + 1).
- We can compare primitive types like int and double using operators like == and < or >, which you will learn about in the next unit. However, with reference types like String, you must use the methods equals and compareTo, not == or < or >.
- The method compareTo compares two strings character by character. If they are equal, it returns 0. If the first string is alphabetically ordered before the second string (which is the argument of compareTo), it returns a negative number. And if the first string is alphabetically ordered after the second string, it returns a positive number.
- The substring method returns the string starting at the first index and not including the last index. The method indexOf returns the index of the first place the string occurs.
- Java will actually return the maximum integer value if you try to subtract one from the minimum value. This is called underflow. And, Java will return the minimum integer value if you try to add one to the maximum. This is called overflow.
- Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double.
- Unboxing is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double.
- Math.random() method is used to generate a random number.
- int abs(int) : Returns the absolute value of an int value.
- double abs(double) : Returns the absolute value of a double value.
- double pow(double, double) : Returns the value of the first parameter raised to the power of the second parameter.
- double sqrt(double) : Returns the positive square root of a double value.
- double random() : Returns a double value greater than or equal to 0.0 and less than 1.0 (not including 1.0!).
- Math.abs takes a single argument, either a double or an int and returns a value of the same type which is the absolute value of the argument. The absolute value is the positive value of the number without its sign or its distance from 0.
- Math.pow takes two arguments, both doubles and returns a double which is the first argument raised to the power of the second argument.
- Math.sqrt takes a double argument and returns a positive double value which is the square root of the argument. For example, the square root of 9 is 3 because 3 squared is 9.
- If you put (int) before the method Math.random then it will just take integer numbers between the range a to b (b is not included) and if we want to include b, then we should add +1 at the end of the random method.
Question 8: On question I selected B as answer thinking that w / 2 would cause an type error but after looking twice since the two number were actually integer then the division would be rounded up making up an integer so it would just work fine
Question 19: On this question I selected E as an answer thinking that the uses are valid but since void cant initiliaze a thing, we cannot assign something by using a void method and also since void doesnt contain a variable itself, it cannot again print something out so I was wrong
Question 32: On this question, I selected the answer D again thinking that 3 / 2 would turn out to be a double making 1.5 but I was again wrong it would round it up to 1 and subtract 17 by 1 which would make 16
public class CarDemo {
private String modelDemo;
private int horsepDemo;
private int yearDemo;
public CarDemo(String model, int horse, int year) {
modelDemo = model;
horsepDemo = horse;
yearDemo = year;
}
public static void main(String[] args) {
CarDemo supra = new CarDemo("model", 11, 11);
System.out.println("Model: " + supra.modelDemo + ", Horsepower: " + supra.horsepDemo + ", Year: " + supra.yearDemo);
}
}