JF_7_1_Practice
JF_7_1_Practice
Lesson Objectives:
• Recognize the correct general form of a class
• Create an object of a class
• Create methods that compile with no errors
• Return a value from a method
• Use parameters in a method
• Create a driver class and add instances of Object classes
• Add a constructor to a class
• Apply the new operator
• Describe garbage collection and finalizers
• Apply the this reference
• Add a constructor to initialize a value
Vocabulary:
Identify the vocabulary word for each definition below.
An instance of a class.
A built-in function of the Java VM that frees memory as objects are no longer needed or referenced.
A method that returns information about an object back to the calling program.
A procedure (changes the state of an object) or function (returns information about an object) that is
encapsulated as part of a class.
A verb used to describe the act of creating a class object using the keyword new.
An optional method that is called just before an object is removed by the garbage collector.
Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
The name of a variable that is associated with an object.
2. Identify the key parts of the Java Class below. Put asterisks next to all the instance variables. Place a box around each
constructor. Circle the signature of methods other than the constructor method. Place triangles around the parameters.
Underline the return types of methods.
public class Animal {
int weight, height;
double speed;
Animal() {
weight = 50;
height = 4;
speed = 2; //miles per hour
}
Animal(int w, int h, int s ) {
weight = w;
h = height;
speed = s
}
public double getTime(double miles) { //gets the number of hours to go these
miles
return miles/speed;
}
public int getWeight() {
return weight;
}
public int getHeight() {
return height;
}
public double getSpeed() {
return speed;
}
}
Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. 2
3. Write code to create two instances of the Animal class template listed in problem #2. Be sure to use each of the two
constructors provided. Then add Java code that will print the following:
a. Animal #1 has a speed of ___.
b. Animal #2 has a speed of ___.
Be sure that the blanks are automatically filled in with the actual speeds. Use the methods provided to access the speeds.
4. Write a class Student. It should have the following instance variables for the name, credits, grade point average (GPA), and
quality Points. Create a constructor method. Create two other methods as follows:
a. A method that will return the current grade point average which will be the quality points divided by the credits.
b. A method that will take in the credits for a class or semester along with the quality points. It should update the
credits, the quality points, and the GPA.
5. Using the class you created in #4, create three instances of the Student Class from the table below:
Mary Jones 14 46
Ari Samala 31 69
6. Using the instance variables created in #5, add 13 credits and 52 quality points to the student “Ari Samala”.
7. Using the Card class from the slides and test the program to make sure it works. Add a second random Card. Code is
included below:
public class Card{
String suit,name;
int points;