Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Object is a program-along lab. It consists of an enum called Size that has t

ID: 665529 • Letter: L

Question

Lab Object is a program-along lab.

It consists of an enum called Size that has the following enum constants: XS, S, M, L, and XL, a class Balloon (see video), and a class BalloonApp.

BalloonApp compares 2 identical Balloon objects, 2 separate objects with the same content, and 2 different objects.
If you missed class I recommend to look at those different options and to see how the implementation of equals and hashCode affects the outcome of the comparison.

As part of the lab we also create a gui generates 2 random balloon objects and compares them with each other ( see video)

https://www.youtube.com/watch?v=cBFOf7zdd7U&list=UUPaQ9GNO5Ivo4al4OKMh9dQ (Links to an external site.)

Explanation / Answer

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class UseBalloon extends Jframe

             implements ActionListener {

    private JButton growButton, moveButton;

    private JPanel panel;

    private Balloon balloon;

    public static void main(String[] args) {

        UseBalloon demo = new UseBalloon();

        demo.setSize(200,220);

        demo.createGUI();

        demo.setVisible(true);

    }

    private void createGUI() {

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container window = getContentPane();

        window.setLayout(new FlowLayout());

        panel = new JPanel();

        panel.setPreferredSize(new Dimension(150, 150));

        panel.setBackground(Color.white);

        window.add(panel);

        moveButton = new JButton("move");

        window.add(moveButton);

        moveButton.addActionListener(this);

        growButton = new JButton("grow");

        window.add(growButton);

        growButton.addActionListener(this);

        balloon = new Balloon();

    }

    public void actionPerformed(ActionEvent event) {

        Graphics paper = panel.getGraphics();

       if (event.getSource() == moveButton) {

            balloon.moveRight(20);

        }

        else {

            balloon.changeSize(20);

        }

        paper.setColor(Color.white);

        paper.fillRect(0, 0, 150, 150);

        balloon.display(paper);

    }

}

At the head of the class UseBalloon, we declare instance variables as usual, including a variable named balloon:

private Balloon balloon;

Within the class UseBalloon, we perform any necessary initialization, including creating a new instance of the class Balloon. This is the crucial step where we create an object from our own class.

balloon = new Balloon();

Now the code to respond to button clicks. If the move button is clicked, then the method moveRight is called. Otherwise changeSize is called.

    public void actionPerformed(ActionEvent event) {

        Graphics paper = panel.getGraphics();

        if (event.getSource() == moveButton) {

            balloon.moveRight(20);

        }

        else {

            balloon.changeSize(20);

        }

        paper.setColor(Color.white);

        paper.fillRect(0, 0, 150, 150);

        balloon.display(paper);

    }

This concludes the coding for the class UseBalloon. Writing this code helps us to clarify how a balloon object will be used, enabling us to see what methods need to be provided by class Balloon, as well as the nature of any parameters. This leads us to write the code for class Balloon:

public class Balloon {

    private int x = 50;

    private int y = 50;

    private int diameter = 20;

  public void moveRight(int xStep) {

        x = x + xStep;

    }

    public void changeSize(int change) {

        diameter = diameter + change;

    }

    public void display(Graphics paper) {

        paper.setColor(Color.black);

        paper.drawOval(x, y, diameter, diameter);

    }

}

The heading of a class starts with the key word class, and gives the class name, followed by a brace. The complete class is terminated with a brace. A class is labeled as public so that it can be used widely. The Java convention (and in most OO languages) is that the name of a class starts with a capital letter. The body of a class consists of declarations of variables and methods. Note how the readability of the class is enhanced using blank lines and indentation. In the next few sections of this chapter we will look in detail at each of the ingredients in the above class for balloons.