Question: This needs to be written in java. I have the below code, but I cannot seem to get it to work when I click the

This needs to be written in java. I have the below code,This needs to be written in java. I have the below code, but I cannot seem to get it to work when I click the button is never doubles for each click etc.

import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField;

// Roach Population Assignment public class Hw7Number1{ private static int FRAME_WIDTH = 400; private static int FRAME_HEIGHT = 100;

public static void main(String[] args){ JFrame frame = new JFrame(); JButton button = new JButton("Click to double the roach population!"); RoachPopulation roachPopulation = new RoachPopulation(2); JLabel label = new JLabel("population: " + roachPopulation.getPopulation()); JPanel panel = new JPanel(); panel.add(button); panel.add(label); frame.add(panel); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

public static class RoachPopulation{ public RoachPopulation(int initialPopulation){ pop = initialPopulation; } public void doublePopulation(){ pop = pop * 2; } public int getPopulation(){ return pop; } private int pop; } }

Write a program that demonstrates the growth of a roach population. Start with two roaches in your window and double the number of roaches with each button click. 1. When a the button is clicked generate a new random location for each roach. You can use a filled circle to represent a roach. You don't need to remember the position of each roach. You need to remember the number of roaches

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!