Question: Hi, I need help with this program I have to make for a Java programming class I need to make an applet that plays a

Hi, I need help with this program I have to make for a Java programming class

I need to make an applet that plays a simple guessing game and give it two buttons labeled Odd and Even. The user needs to guess whether a secret number is odd or even by clicking one of these buttons. After a guess is made, the applet should display either Congratulations, you are correct! or Sorry, you are wrong. In either case, also display The secret number was: followed by the secret number. Use labels for these three messages.

The applet will have a private instance variable secretNumber of type long that holds the secret number. You will need to set it in the init method using the following line of code: secretNumber = java.util.Calendar.getInstance().getTimeInMillis() % 100; In the actionPerformed method, check which button was pressed and make the appropriate response label visible. After that, make the two buttons invisible (only one guess is allowed) and the label containing the secret number visible.

here is what I have wrote for code.

import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JLabel; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

public class GuessGame extends JApplet { private JLabel line1; private JLabel line2; private JLabel number; private Container contentHolder; int secretNumber = (int) (secretNumber = java.util.Calendar.getInstance().getTimeInMillis() % 100); String str = "Number is" + Integer.toString(secretNumber); public void start () { contentHolder=getContentPane(); contentHolder.setBackground(Color.green); final JButton jbt = new JButton("odd number"); final JButton jbt1 = new JButton("even number"); line1 = new JLabel("you guessed right"); line1.setVisible(false); line2 = new JLabel("you guessed wrong"); line2.setVisible(false); number=new JLabel (str); number.setVisible(false); contentHolder.setLayout(new FlowLayout()); contentHolder.add(jbt); contentHolder.add(jbt1); contentHolder.add(line1); contentHolder.add(line2); contentHolder.add(number);

jbt.addActionListener (new ActionListener(){ public void actionPerformed(ActionEvent e) { if(secretNumber%2!=0) line1.setVisible(true); else if (secretNumber%2!=0); line2.setVisible(true); jbt.setVisible(false); jbt1.setVisible(false); number.setVisible(false); } }); jbt1.addActionListener (new ActionListener(){ public void actionPerformed(ActionEvent e) { if(secretNumber%2!=0) line2.setVisible(true); else if(secretNumber%2!=0); line1.setVisible(true); jbt.setVisible(false); jbt1.setVisible(false); number.setVisible(true); } }); } }

When I try to compile it I get this 1 lone error

GuessGame.java:19: error: incompatible types: possible lossy conversion from long to int (secretNumber = java.util.Calendar.getInstance().getTimeInMillis() % 100);

and it shows an arrow under the % as well in that error notice.

I was wondering if someone could help me trouble shoot this.

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!