knowt ap exam guide logo

Using Classes and Objects

Objects and Classes Overview

  • Object- A data type that is created in Java.

  • There is a class that is used to build more objects in Java specifically.

  • Classes have instance fields or data that belong to objects and methods that are used to help change the data.

Math Class

  • This store's common numbers used in mathematical calculations and methods to have mathematical functions.

  • These methods are static.

  • Note: Static methods, belong to a class, but it doesn’t belong to any instances of that specific class. This type of method can be called without the class's instance or object.*

  • We don’t need to create the objects in this class.

  • Here are the main ones used on the AP Exam:

    • Ex: Say you want to calculate the amount of candy each person gets amongst your group of 5 friends. You have 14 chocolates, and you didn’t feel like doing the math so you decide on using your amazing IDE to help you out. What line of code would best fit your needs?

a. Math.abs(14/5);

b.Math.pow(14,5);

c. double chocAmount = 14.0/5.0;

//chose between these 3 options

Answer: C would be the only option that fits your needs because you don’t want an absolute value for your number of chocolates. It has to be accurate, and you don’t need exponents either. You just want to find out how to split the chocolate amount. So, this would be your answer.

String Class

  • A string is also an object data type, which means it has a class with many methods that describe the behavior of an object.

  • Here are some commonly used methods for the AP Exam:

    • Ex: Consider the following code segment, What will be the value of n?

String s = “This is the beginning”;

String t = s.substring(5);

int n = t.indexOf(“the”);

Answer: The value of n will be 3. The question is asking for the value of n, which is the indexOf “the” in String t, which is essentially just a substring of string s. Determine t first, which is the substring that begins at the index 5 of s. Start at 0 and count the spaces as different characters. Index 5 is the letter i, at the beginning of the word is. Since string t is a substring of s, the sentence for this substring would be “is the beginning”. the index of “the” is 3 because it begins at index 3.

  • There are a lot more methods that are in the String class. However, these are just the main ones.

  • Remember that the data in the String class is just an array of characters. This means that String’s have similar functions to arrays.

  • IndexOutOfBoundsException’s occur when the index that you use in any of the methods above goes beyond the index of the String.

  • The String class is an immutable class. This means that if you would like to change the values or content of a String object the new contents need to be rearranged to help find the variable.

  • Note: When using methods found in any class, you should remember that parameters that are used to call the method need to match the data types of the arguments that are found in that specific method.

  • Make sure that parameters match the correct data types so there are no issues with the execution of the code.

Using Classes and Objects

Objects and Classes Overview

  • Object- A data type that is created in Java.

  • There is a class that is used to build more objects in Java specifically.

  • Classes have instance fields or data that belong to objects and methods that are used to help change the data.

Math Class

  • This store's common numbers used in mathematical calculations and methods to have mathematical functions.

  • These methods are static.

  • Note: Static methods, belong to a class, but it doesn’t belong to any instances of that specific class. This type of method can be called without the class's instance or object.*

  • We don’t need to create the objects in this class.

  • Here are the main ones used on the AP Exam:

    • Ex: Say you want to calculate the amount of candy each person gets amongst your group of 5 friends. You have 14 chocolates, and you didn’t feel like doing the math so you decide on using your amazing IDE to help you out. What line of code would best fit your needs?

a. Math.abs(14/5);

b.Math.pow(14,5);

c. double chocAmount = 14.0/5.0;

//chose between these 3 options

Answer: C would be the only option that fits your needs because you don’t want an absolute value for your number of chocolates. It has to be accurate, and you don’t need exponents either. You just want to find out how to split the chocolate amount. So, this would be your answer.

String Class

  • A string is also an object data type, which means it has a class with many methods that describe the behavior of an object.

  • Here are some commonly used methods for the AP Exam:

    • Ex: Consider the following code segment, What will be the value of n?

String s = “This is the beginning”;

String t = s.substring(5);

int n = t.indexOf(“the”);

Answer: The value of n will be 3. The question is asking for the value of n, which is the indexOf “the” in String t, which is essentially just a substring of string s. Determine t first, which is the substring that begins at the index 5 of s. Start at 0 and count the spaces as different characters. Index 5 is the letter i, at the beginning of the word is. Since string t is a substring of s, the sentence for this substring would be “is the beginning”. the index of “the” is 3 because it begins at index 3.

  • There are a lot more methods that are in the String class. However, these are just the main ones.

  • Remember that the data in the String class is just an array of characters. This means that String’s have similar functions to arrays.

  • IndexOutOfBoundsException’s occur when the index that you use in any of the methods above goes beyond the index of the String.

  • The String class is an immutable class. This means that if you would like to change the values or content of a String object the new contents need to be rearranged to help find the variable.

  • Note: When using methods found in any class, you should remember that parameters that are used to call the method need to match the data types of the arguments that are found in that specific method.

  • Make sure that parameters match the correct data types so there are no issues with the execution of the code.

robot