Question: USING JAVA Using inner class discussed in class ,simulate that you are playing Pega4: The program should continue running until it happensone of these two
USING JAVA
Using inner class discussed in class ,simulate that you are playing Pega4:
- The program should continue running until it happensone of these two situations :
- 1. Press Ok on the JOptionPane ( if you press the button it should say " You lost !!!").
- 2. If the number entered comes out on Paste4, it must say You won !.
They must implement an interface called ask () . Which will ask for the desired number.
The interval that is every 1 second and that makes a "beep" . Show how many rolls were made.
Note : To investigate how to generate numbers at the chance . Remember that Pega4 goes from 1000 to 9999.
The output would be as follows :
-------------------------------------------------- -------------------------------------------------
Enter desired number: 3267
07/09/2019 09:50:23:
shot # 1: 4392
roll #2: 8391
You lost !!!
-------------------------------------------------- -------------------------------------------------
Enter desired number: 4912
07/09/2019 09:50:23:
shot # 1: 4931
roll #2: 9450
roll #3: 4912
Won !!!
------------------------------------------------------------------------------------------------------------------------------------------
This is what i currently have. The computer "beeps" and shows me the time and date every second. I need to implement an ask() interface, generate a random number and have the output as given above.
-----------------------------------------------------------------------------------------------------------------------------------------
package edu.pupr.InnerClass; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import java.util.Scanner; public class InnerClass { public static void main(String[]args) { TalkingClock clock = new TalkingClock(1000, true); clock.start(); } } class TalkingClock { private int interval; private boolean beep; private Timer timer; public TalkingClock(int interval, boolean beep) { this.interval = interval; this.beep = beep; } public class TimerPrinter implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Date now = new Date(); System.out.println("At the tone, the time is: " + now); if(beep) { Toolkit.getDefaultToolkit().beep(); } } } public void start() { ActionListener listener = new TimerPrinter(); timer = new Timer(interval, listener); timer.start(); JOptionPane.showMessageDialog(null, "Exit Inner Class?"); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
