Professional Documents
Culture Documents
Workshop 3
Workshop 3
Workshop 3
CAR.JAVA:
// Class fields
// Default constructor
public Car() {
this.color = color;
this.enginePower = enginePower;
this.convertible = convertible;
this.parkingBrake = parkingBrake;
// Getters
return color;
}
public int getEnginePower() {
return enginePower;
return convertible;
return parkingBrake;
// Setters
this.color = color;
this.enginePower = enginePower;
this.convertible = convertible;
this.parkingBrake = parkingBrake;
}
// Method to simulate pressing the start button
TESTER.JAVA
c.pressStartButton();
c.pressAcceleratorButton();
c.output();
c2.pressStartButton();
c2.setColor("black");
c2.output();
PART 2:
2. Inventory class: This class will hold and manage a collection of Guitar objects.
Now, let's visualize this in a UML class diagram. Since I can't draw directly here, I'll describe how to
construct the UML diagram:
The Guitar class would be represented as a rectangle divided into three sections:
The middle section lists the fields with their visibility (- for private) and type:
- serialNumber: String
- price: double
- builder: String
- model: String
- backWood: String
- topWood: String
The bottom section lists the methods with their visibility (+ for public) and return type:
+ createSound(): void
The Inventory class would be represented similarly:
- list: List<Guitar>
PART 3:
GUITAR.JAVA
// Private fields
// Default constructor
public Guitar() {
this.serialNumber = null;
this.price = 0;
this.builder = null;
this.model = null;
this.backWood = null;
this.topWood = null;
}
// Constructor with parameters
public Guitar(String serialNumber, int price, String builder, String model, String backWood, String
topWood) {
this.serialNumber = serialNumber;
this.price = price;
this.builder = builder;
this.model = model;
this.backWood = backWood;
this.topWood = topWood;
// Getters
return serialNumber;
return price;
return builder;
return model;
return topWood;
// Setters
this.serialNumber = serialNumber;
this.price = price;
this.builder = builder;
this.model = model;
this.backWood = backWood;
}
public void setTopWood(String topWood) {
this.topWood = topWood;
TESTER.JAVA
System.out.println("State of obj1:");
obj1.createSound();
System.out.println("State of obj2:");
obj2.createSound();
System.out.println("set price = 3000 of obj1");
obj1.setPrice(3000);
PART 4:
In Java, memory is divided into several sections, such as the stack, heap, and method area (which
includes the static heap). Here's a breakdown:
1. Static Heap: Stores static variables and class information (metadata, the code for methods, and
the static variables).
2. Heap (Dynamic Heap): Stores all the objects created during the runtime of the application, as
well as the instance variables contained within those objects.
3. Stack: Stores the local variables and the call stack (which includes method call frames with local
variables and reference variables).
Now, let's answer the questions based on the given Java program:
Static Heap: Class metadata for the Guitar and Tester classes, and any static variables if
they were present (none are present in the given code).
Stack: The main method's frame including the local variables args, obj1, and obj2.
Dynamic Heap: The objects created by new Guitar() and new Guitar("G123", 2000,
"Sony", "Model123", "hardWood", "softWood").
The objects in the program are instances of the Guitar class: obj1 and obj2.
obj1: Initially, all fields are in their default state (null for String, 0 for int). After the
method call obj1.setPrice(3000), its price is 3000 and the rest remain in their default
state.
obj2: Initialized with the given values: serialNumber is "G123", price is 2000, builder is
"Sony", model is "Model123", backWood is "hardWood", and topWood is "softWood".
What is the current object when the program runs to the line obj2.createSound()?
In the method main, can you use the keyword "this" to access all fields of obj2? Why?
No, you cannot use the keyword "this" in a static context (such as the main method) to
refer to instance variables or methods. The keyword "this" is used within an instance
method to refer to the current object, and since main is static, there is no current
instance associated with it.
To illustrate a memory map, one would typically draw a diagram showing the stack frame for main with
pointers to the two Guitar objects in the heap. However, as I cannot draw here, you might imagine it as
described: the stack contains references (addresses) to objects in the heap, where the actual Guitar
objects with their state reside.