63 Matching Annotations
  1. May 2024
    1. data structure

      A data structure is a way of organizing and storing data in a computer so that it can be accessed and used efficiently.

    2. arrayname[index]

      Array accessing syntax.

    3. Figure 4: A primitive array and an object array

      A Primitive array with initial values; Space is allocated in the default way primitive types are treated in the memory.

      An Object array with initial values; Space is allocated in the memory for two things, one for the object references and the other on for the object itself. (refer the image.)

    4. int[ ] highScores = {99,98,98,88,68}; String[ ] names = {"Jamal", "Emily", "Destiny", "Mateo", "Sofia"};

      Array with Initializer List Syntax by Example.

    5. initializer list

      List of initialising data attached to an array by the programmer manually in the code.

      In this method of creating an array, the programmer doesn't need to specify the size of the array as it will be automatically determined (you still need to specify the data type of the data list).

    6. Note Array elements are initialized to default values like the following. 0 for elements of type int 0.0 for elements of type double false for elements of type boolean null for elements of type String

      Array initialisation according to the assigned data type.

    7. // create the array highScores = new int[5]; // declare and create array in 1 step! String[] names = new String[5];

      Syntax for Creation of an array using New keyword

    8. // Declaration for an array of ints int[] scores;

      Array Declaration syntax (Doesn't create the array though)

    1. Static variables and methods

      Static means that the variable or method marked as such is available at the class level. In other words, you don't need to create an instance of the class to access it.

    1. Call by Value

      Calling a method by passing actual parameters to it.

    2. some of the main reasons to use multiple methods in your programs: Organization and Reducing Complexity: organize your program into small sections of code by function to reduce its complexity. Divide a problem into subproblems to solve it a piece at a time. Reusing Code: avoid repetition of code. Reuse code by putting it in a method and calling it whenever needed. Maintainability and Debugging: smaller methods are easier to debug and understand than searching through a large main method.

      Some of the main reasons to use multiple methods in your programs

    3. Procedural Abstraction

      Procedurally abstracting the details of how stuff works behind the scenes.

      It basically a word for the process of using the concept of functions on code.

    1. Notice the difference between setters and getters in the following figure. Getters return an instance variable’s value and have the same return type as this variable and no parameters. Setters have a void return type and take a new value as a parameter to change the value of the instance variable. Figure 1: Comparison of setters and getters

      Comparison of setters and getters

    2. // Setter method template public void setVarName(typeOfVar newValue) { varName = newValue; }

      Setter method syntax

    1. 5.4.1. toString¶

      need to read

    2. You don’t need to write a getter for every instance variable in a class but if you want code outside the class to be able to get the value of one of your instance variables, you’ll need to write a getter that looks like the following. class ExampleTemplate { // Instance variable declaration private typeOfVar varName; // Accessor (getter) method template public typeOfVar getVarName() { return varName; } } Notice that the getter’s return type is the same as the type of the instance variable and all the body of the getter does is return the value of the variable using a return statement.

      Making Private instance variables available for access for methods outside the class by creating a public getter method to return the value in the private instance variable.

    1. Unified Modeling Language (UML) for drawing these diagrams. For example, here is a UML class diagram for the Turtle class. The - in front of the attributes indicate that they are private, and the + in front of the methods indicate that they are public. Here is a tutorial on class diagrams that explains it in more detail if you are curious (Class diagrams are not on the AP CSA exam). If you want to draw your own, app.diagrams.net or Creately.com are good free online drawing tools for UML class diagrams.

      UML Stuff

    2. Note Methods define what the object can do. They typically start with public then a type, then the name of the method followed by parentheses for optional parameters. Methods defined for an object can access and use its instance variables!

      Method Syntax

    3. public class Person { // instance variables // constructors // methods }

      Basic Class syntax

    1. Note The first character in a string is at index 0 and the last characters is at length -1.

      What is important here is that you can access only the last x number character by using the length function and subtracting 1 from that.

    2. Here is a list of common mistakes made with Strings. Thinking that substrings include the character at the last index when they don’t. Thinking that strings can change when they can’t. They are immutable. Trying to access part of a string that is not between index 0 and length -1. This will throw an IndexOutOfBoundsException. Trying to call a method like indexOf on a string reference that is null. You will get a null pointer exception. Using == to test if two strings are equal. This is actually a test to see if they refer to the same object. Usually you only want to know if they have the same characters in the same order. In that case you should use equals or compareTo instead. Treating upper and lower case characters the same in Java. If s1 = "Hi" and s2 = "hi" then s1.equals(s2) is false.

      Crazy important stuff here…

    3. Figure 2: compareTo returns a negative or positive value or 0 based on alphabetical order

      "CompareTo" method syntax and set of outputs based on set of input conditions.

      The comparison is made based on which string comes earlier in an alphabetical progression.

    4. This method is inherited from the Object class, but is overridden which means that the String class has its own version of that method.

      Can't tell if this is something worth remembering... But I sure know it is something important.

    1. Note Two common patterns in for-loops are to count from 0 up to an number (using <) or count from 1 to the number including the number (using <=). Remember that if you start at 0 use <, and if you start at 1, use <=. The two loops below using these two patterns both run 10 times. The variable i (for index) is often used as a counter in for-loops. // These loops both run 10 times // If you start at 0, use < for(int i = 0; i < 10; i++) { System.out.println(i); } // If you start at 1, use <= for(int i = 1; i <= 10; i++) { System.out.println(i); }

      Useful when you don't like to initialise the counter variable from zero but instead prefer to start with 1.

    2. for (initialize; test condition; change) { loop body }

      For Loop Syntax

    1. Common Errors with Loops

      Infinite Loop - Self explanatory... Off-by-one-error - Incorrect operator used, resulting in an incorrect number of iterations.

    2. Figure 5: A trace table showing the values of all of the variables each time through the loop. Iteration 0 means before the loop.

      Looping trace table format.

      The implementation is exactly the same as a regular trace table. :P

    3. Figure 2: Comparing Snap! or Scratch Repeat Until Loop to Java while loop

      Difference between a Repeat Until loop and a While Loop:

      Repeat Until Loop: Executes the code as long as the given condition is False, once the condition turns to True, it stops.

      While Loop: Executes the code as long as the given condition is True, once the condition turns to False, it stops.

    4. // The statements in a while loop run zero or more times, // determined by how many times the condition is true while (condition) { statements; }

      While loop Syntax

    5. control structures

      Meaning features of a language that are used to essentially control the flow of execution. (If-else conditions and loops).

    1. Note Only use == with primitive types like int or to test if two strings (or objects) refer to the same object. Use equals, not ==, with strings to test if they are equal letter by letter.

      Essentially saying that the == operator only compares the references stored in the variables, which are basically memory locations and they are always different unless both the given variables are storing the same reference. Making it pretty unreliable when it comes to strings. BUT, you can use it without a hitch when working with primitive types.

      The Equals method however is actually going to the end of that reference and then comparing the strings.

    1. !(c == d) is equivalent to c != d !(c != d) is equivalent to c == d !(c < d) is equivalent to c >= d !(c > d) is equivalent to c <= d !(c <= d) is equivalent to c > d !(c >= d) is equivalent to c < d

      A layman's guide.

    1. In fact many experienced Java programmers always use curly braces, even when they are not technically required to avoid this kind of confusion.

      My point exactly.

    2. If statements can be nested inside other if statements. Sometimes with nested ifs we find a dangling else that could potentially belong to either if statement. The rule is that the else clause will always be a part of the closest unmatched if statement in the same block of code, regardless of indentation.

      Good to know. No. Really important to know.

      Better to always use curly braces. As shown in the next highlight.

    3. // A single if/else statement if (boolean expression) Do statement; else Do other statement;

      If-Else Condition Syntax

    1. // A single if statement if (boolean expression) Do statement; // Or a single if with {} if (boolean expression) { Do statement; } // A block if statement: { } required if (boolean expression) { Do Statement1; Do Statement2; ... Do StatementN; }

      If Condition Syntax

    1. >

      There should be a != here instead of a >.

      Since a negative odd number would fetch the remainder -1 and thus using the > relational operator would bring about faulty results.

    1. Note Math.random() returns a random number between 0.0-0.99. (int)(Math.random()*range) + min moves the random number into a range starting from a minimum number. The range is the (max number - min number + 1). Here are some examples that move a random number into a specific range. // Math.random() returns a random number between 0.0-0.99. double rnd = Math.random(); // rnd1 is an integer in the range 0-9 (including 9). int rnd1 = (int)(Math.random()*10); // rnd2 is in the range 1-10 (including 10). The parentheses are necessary! int rnd2 = (int)(Math.random()*10) + 1; // rnd3 is in the range 5-10 (including 10). The range is 10-5+1 = 6. int rnd3 = (int)(Math.random()*6) + 5; // rnd4 is in the range -10 up to 9 (including 9). The range is doubled (9 - -10 + 1 = 20) and the minimum is -10. int rnd4 = (int)(Math.random()*20) - 10;

      Good section for proper understanding of how ranging works here.

    2. Easy enough—casting a double to an int will throw away any values after the decimal point. For example, // rnd is an integer in the range 0-9 (from 0 up to 10). int rnd = (int)(Math.random()*10);

      Getting random integers instead of random doubles.

    1. These wrapper classes (defined in the java.lang package) are also useful because they have some special values (like the minimum and maximum values for the type) and methods that you can use.

      So that's where the min-max number nonsense is coming from...

    2. Wrapper Classes

      Wrapper classes provide a way to use primitive data types as objects.

      Useful when the programmer can only use objects and is restricted from utilising the primitive types as they are.

    1. backslash escape sequence

      Overriding the compiler's default reaction to special characters such as quotation marks and backslashes, that are used in the code with programming language specific meaning, to allow the programmer to display these special characters included with the string literals as a part of it.

    2. 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.

      Really important to remember the first sentence and the rest is obvious.

    1. declared return type

      The return type is the same as the method's type, as defined in the method signature of the method.

    1. Methods inside the same class can call each other using just methodName(), but to call non-static methods in another class or from a main method, you must first create an object of that class and then call its methods using object.methodName().

      Gotta know this quirk...

    2. Methods inside the same class can call each other using just methodName(), but to call non-static methods in another class or from a main method, you must first create an object of that class and then call its methods using object.methodName().

      Really important is what I can tell, but where is it used???

    3. Activity: CodeLens 2.3.1.2 (songviz1)

      Gotta put this down...

      So I had this question/doubt about the difference between returning a value and printing a value. What that doubt was exactly is that I didn't know that returning a value meant returning it to the object/method call, and not just being thrown into a reference pool to just be found by something or whatever.

      Takeaway: Returning a value means returning it to the object/method call and not flinging it into the arbitrary abyss of the RAM.

    4. Execution in Java always begins in the main method in the current class.

      I don't know how and why but this line made something in my head click and realise that the rule the main/starting/current class's name should be the exact same as the file name.

      And that is to let the complier know which is the class that it should start executing from and which ones are the other classes that are to be used as supplementaries to the main class.

    5. Figure 1: A Student class showing instance variables, constructors, and methods

      A definite keeper for a concise example of the various sections in a class.

    6. Procedural abstraction allows a programmer to use a method and not worry about the details of how it exactly works.

      Why not call it something much more memorable, like functions maybe?

    7. method signature (or method header), which is the method name followed by a parameter list

      This should have been mentioned/clarified earlier in the signature section earlier.

    1. Figure 4: A Date class showing attributes and constructors

      Example class

    2. signatures

      Basically a set of data that lists the name and parameters of the constructors and methods under a particular class.

      An individual signature refers to the name and parameters of a specific constructor or method.

    3. The code Turtle t1 = null; creates a variable t1 that refers to a Turtle object, but the null means that it doesn’t refer to an object yet. You could later create the object and set the object variable to refer to that new object (t1 = new Turtle(world1)). Or more commonly, you can declare an object variable and initialize it in the same line of code (Turtle t2 = new Turtle(world1);). World world1 = new World(); Turtle t1 = null; t1 = new Turtle(world1); // declare and initialize t2 Turtle t2 = new Turtle(world1);

      Summary: - You can declare object variables without assigning to it a new object. The assignment can be done at a later stage in code. - Null here is just Null, no special contextual meaning here. It literally just means nothing's here.

    4. object variable

      Literally a variable that just stores an object.

    5. one reason why programming languages have data types – to allow for error-checking.

      *another reason

    6. // To create a new object and call a constructor write: // ClassName variableName = new ClassName(parameters); World habitat = new World(); // create a new World object Turtle t = new Turtle(habitat); // create a new Turtle object

      There's a really important thing mentioned here and its that a constructor's usage isn't limited to a single object and that it too can be utilised by multiple objects.

      MIND YOU this is DIFFERENT than Constructor Overloading.

    1. class diagram that shows some of the attributes and methods in the class Turtle.

      This is an important key term to find comprehensive lists of classes online.

    2. And just like the Java compiler will only let you do things with the values of int variables that make sense (like adding and multiplying them), it will only let you do things with values of a Turtle variable that make sense to do with turtles, namely accessing the instance variables and methods defined in the Turtle class. The following picture has lots of cats (objects of the type cat). They are all different, but they share the same attributes and behaviors that make up a cat. They are all instances of cat with different values for their attributes. Name some of the attributes and behaviors of the cats below. For example, the color (attribute) of the first cat is black (attribute value) and it is playing (behavior).

      A Good explanation to make sense of what is the difference between attributes and behaviours.

      Attributes are like the magazine configurations of a gun or the scope strength or even the type of the gun.

      Behaviours for a gun are the different attack functions a gun can give to you, some can only give a single function of shooting, some can give you spray shooting options and some can give good melee damage features and much more.

      Another thing to mention is that the gun class here contains only a basic set of attributes and behaviours, when can be modified and added upon when new objects of it are created, thus making the class a framework/blueprint/template of sorts.

  2. Mar 2023