Workshop 3

You might also like

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

PART 1:

CAR.JAVA:

public class Car {

// Class fields

private String color;

private int enginePower;

private boolean convertible;

private boolean parkingBrake;

// Default constructor

public Car() {

this.color = ""; // Assuming empty value as default

this.enginePower = 0; // Assuming 0 as default engine power

this.convertible = false; // Assuming false as default for convertible

this.parkingBrake = true; // Assuming parking brake is engaged by default

// Constructor with parameters

public Car(String color, int enginePower, boolean convertible, boolean parkingBrake) {

this.color = color;

this.enginePower = enginePower;

this.convertible = convertible;

this.parkingBrake = parkingBrake;

// Getters

public String getColor() {

return color;

}
public int getEnginePower() {

return enginePower;

public boolean isConvertible() {

return convertible;

public boolean isParkingBrake() {

return parkingBrake;

// Setters

public void setColor(String color) {

this.color = color;

public void setEnginePower(int enginePower) {

this.enginePower = enginePower;

public void setConvertible(boolean convertible) {

this.convertible = convertible;

public void setParkingBrake(boolean parkingBrake) {

this.parkingBrake = parkingBrake;

}
// Method to simulate pressing the start button

public void pressStartButton() {

System.out.println("You have pressed the start button");

// Method to simulate pressing the accelerator button

public void pressAcceleratorButton() {

System.out.println("You have pressed the Accelerator button");

// Method to output the state of the car

public void output() {

System.out.println("Color: " + color);

System.out.println("Engine Power: " + enginePower);

System.out.println("Convertible: " + convertible);

System.out.println("Parking Brake: " + parkingBrake);

TESTER.JAVA

public class Tester {

public static void main(String[] args) {

// Create a new Car object with default constructor

Car c = new Car();

c.pressStartButton();

c.pressAcceleratorButton();

c.output();

// Create a new Car object with parameters


Car c2 = new Car("red", 100, true, true);

c2.pressStartButton();

c2.setColor("black");

System.out.println("Color of c2: " + c2.getColor());

c2.output();

PART 2:

1. Guitar class: This class will represent the details of a guitar.

 Fields: serialNumber, price, builder, model, backWood, topWood

 Methods: createSound() - This would represent creating a melodic sound, although in a


real application this might not be a literal implementation.

2. Inventory class: This class will hold and manage a collection of Guitar objects.

 Fields: list (an array or list that holds guitar objects)

 Methods: addGuitar() to add a new guitar to the inventory, searchGuitar() to find a


guitar by its serialNumber.

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 top section contains the class name "Guitar."

 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:

 The top section contains the class name "Inventory."

 The middle section lists the fields:

 - list: List<Guitar>

 The bottom section lists the methods:

 + addGuitar(guitar: Guitar): void

 + searchGuitar(serialNumber: String): Guitar

PART 3:

GUITAR.JAVA

public class Guitar {

// Private fields

private String serialNumber;

private int price;

private String builder;

private String model;

private String backWood;

private String topWood;

// Default constructor

public Guitar() {

// Assign all fields to empty values

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

public String getSerialNumber() {

return serialNumber;

public int getPrice() {

return price;

public String getBuilder() {

return builder;

public String getModel() {

return model;

public String getBackWood() {


return backWood;

public String getTopWood() {

return topWood;

// Setters

public void setSerialNumber(String serialNumber) {

if (serialNumber != null && !serialNumber.isEmpty()) {

this.serialNumber = serialNumber;

public void setPrice(int price) {

this.price = price;

public void setBuilder(String builder) {

this.builder = builder;

public void setModel(String model) {

this.model = model;

public void setBackWood(String backWood) {

this.backWood = backWood;

}
public void setTopWood(String topWood) {

this.topWood = topWood;

// Method to simulate creating a sound

public void createSound() {

// This is a placeholder for creating sound

System.out.println("Serial Number: " + getSerialNumber());

System.out.println("Price: " + getPrice());

System.out.println("Builder: " + getBuilder());

System.out.println("Model: " + getModel());

System.out.println("Back Wood: " + getBackWood());

System.out.println("Top Wood: " + getTopWood());

TESTER.JAVA

public class Tester {

public static void main(String[] args) {

Guitar obj1 = new Guitar();

Guitar obj2 = new Guitar("G123", 2000, "Sony", "Model123", "hardWood", "softWood");

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);

System.out.println("get price of obj1:" + obj1.getPrice());

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:

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

 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").

 What are objects in the program?

 The objects in the program are instances of the Guitar class: obj1 and obj2.

 What is the state of obj1, 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".

 Do you access all fields of obj1 in the class Tester.java? Why?


 Not directly. The createSound() method is called, which internally accesses all fields via
getter methods. Direct field access is not allowed because the fields are private.

 What is the current object when the program runs to the line obj2.createSound()?

 The current object is obj2, which is an instance of the Guitar class.

 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.

You might also like