Vocabulary Part 2 Post
Vocabulary part 2 post for AP CSA
- Comparing Numbers: To compare 2 numbers in Java, we mainly use the >, <, and == operators. == Checks if 2 numbers are equal to each others. > checks if the first number is greater than the second number. < checks if the second number is greater than the first number.
- Comparing Strings: To compare 2 strings in Java, we mainly use the equals() and compareTo() method. The equals() method checks if 2 strings are equal to each other, and the compareTo() method compares string values lexicographically.
- Comparing Objects: To compare 2 objects in Java, we mainly use the equals() method to check for equality between 2 objects.
- For Loop and Enhanced For Loop: A for loop basically iterates through a given group of statements multiple times, and keeps running as long as the set condition evaluates to true. A for loop generally has some kind of incrementing in one of its parameter. An enhanced for loop as also known as a for-each loop, which is a type of control flow statement that iterates through each element of a data collection (e.g. arrays).
- While Loop vs. Do-While Loop: A while loop executes a given group of statements multiple times as long as the boolean condition is met, and a do-while loop is also used to do the same thing, although it executes at least once, even if the boolean condition is not met.
- Nested Loop: A nested loop is a loop statement inside another loop statement, or in other words, an inner loop statement within the body of an outer loop statement. It is typically used to print dimensions as in rows and loops.
- Class Creation: In Java, a class is user-defined blueprint that is used to create objects. To create a class, write "class ClassName {}." The naming convention for creating classes in Java is generally to capitalize the first letter of each word.
- Constructor: A constructor is a special method in Java that is used to initialize values in objects, as it is called whenever an object of a class is created. To create a constructor, write "public ClassName(){}."
public class VocabTwo {
public static void main(String[] args) {
System.out.println("Comparing Numbers:");
int a = 5;
int b = 10;
int c = 3;
System.out.println(a == a);
System.out.println(a < b);
System.out.println(a > c);
System.out.println("");
System.out.println("Comparing Strings:");
String string1 = "Hello World";
String string2 = "Hello!";
System.out.println(string1.equals(string2));
System.out.println(string1.compareTo(string2));
System.out.println("");
System.out.println("Comparing Objects");
Integer int1 = new Integer(7);
Integer int2 = new Integer(7);
Integer int3 = new Integer(8);
System.out.println(int1.equals(int2));
System.out.println(int1.equals(int3));
System.out.println("");
System.out.println("For Loop and Enhanced For Loop:");
for (int i = 0; i < 10; i++) {
System.out.print(i + " ");
}
System.out.println("");
int[] forLoopArray = {0, 1, 2, 3, 4, 5, 6, 7};
for (int element : forLoopArray) {
System.out.print(element + " ");
}
System.out.println("");
System.out.println("While Loop vs. Do-While Loop:");
int w = 0;
while(w < forLoopArray.length) {
System.out.print(forLoopArray[w] + " ");
w++;
}
System.out.println("");
int z = 0;
do {
System.out.println(z);
} while (z < 0);
System.out.println("");
System.out.println("Nested Loop:");
int[][] multiDimensionalArray = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(multiDimensionalArray[i][j] + " ");
}
System.out.println("");
}
System.out.println("");
}
}
VocabTwo.main(null);
// Class Creation
public class Main {
// Variable x is declared
int x;
// Constructor
public Main() {
// Variable x is assigned a value 7
x = 7;
}
public void printMessage() {
System.out.println("Hello, World!");
}
}
class Tester {
public static void main(String[] args) {
// Create an object with an attribute "x" immediately initialized due to the constructor.
Main myObj = new Main();
System.out.println(myObj.x);
myObj.printMessage();
}
}
Tester.main(null);