Part 4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Explain step by step what happened when the program runs:

1. The program finds main method


2. Obj1 is accessed to class Guitar()
3. Obj2 is accessed to class Guitar(String String serialNumber, int price, String
builder, String model, String backWood, String topWood)
4. Program prints out String : “ State of Obj1:”
5. Program uses method createSound() to print out state of obj1 and it prints
out null, 0, null, null, null and null because we don’t transmit input to Guitar
6. Programs print out String : “State of Obj2:”
7. Program uses method createSound() to print out state of obj2 and it prints
out G123, 2000, Sony, Model123, hardwood and softwood because we
transmit input to Guitar to tell program that we need to use that input to print
out information.
8. Next, program prints out String: “set price = 3000 of obj1”
9. We transmit 3000 to method setPrice in obj1
10.Because of transmitting 3000 to price of Obj1, program prints out value of
method getPrice of obj1

Answer some questions:

- What is stored in the static heap, stack, dynamic heap?


+ Static Heap: No data is stored in the Static Heap.
+ Stack: In the main method of Tester class, each local variable like obj1
and obj2 is stored on Stack.
+ Dynamic Heap: Guitar objects created in the main method of Tester class
will be stored on Dynamic Heap.
- What are objects in the program?
Objects in the Program: Objects are an instance of a class, in this case, the
objects are guitars created from the Guitar class.
- What is the state of obj1, obj2 ?
obj1: No information has been assigned to obj1 yet, because it is initialized
with the default constructor.
obj2: Already initialized with information serialNumber: "G123", price:
2000, builder: "Sony", model: "Model123", backWood: "hardWood",
topWood: "softWood".
- Do you access all fields of obj1 in the class Tester.java? Why?
In the Tester class, only the public methods of the Guitar class can be
accessed, so the private fields of obj1 cannot be accessed directly.
- What is the current object when the program runs to the line
“obj2.createSound();”?
Obj2
- In the method main, can you use the keyword “this” to access all fields of
obj2? Why?
In the main method, the "this" keyword cannot be used to access obj2's
fields because main is not part of the Guitar object, but is a static method.
The "this" keyword is used to refer to the current object, and can only be
used in methods of the current class.

You might also like