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, 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 roachesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started