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

JAVA__________ The keypad on your microwave is not working. The digits 1, 4 and

ID: 3771380 • Letter: J

Question

JAVA__________

The keypad on your microwave is not working. The digits 1, 4 and 7 don't work correctly.

If you enter a temperature with any of those digits you will get a message saying that the keypad does not work properly.

It will also give you the closest temperature below the entered temperature that will work and the closest temperature above the entered temperature, that will work.

If all digits entered are good you will get a message saying the temperature will be set to, and the entered temperature.

Temperatures allowed must be in the range 1 to 999 (inclusive)

this is to continue running until a temperature of 0 is entered

Name the class keyboard.

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.CookingTime;
import javax.swing.*;
import javax.swing.border.*;
public class keyboard extends JFrame
{
   private JPanel windowJPanel;
   private JPanel controlJPanel;
   private JTextField displayJTextField;
   private JButton firstJButton;
   private JButton secondJButton;
   private JButton thirdJButton;
   private JButton fourthJButton;
   private JButton fivethJButton;
   private JButton sixthJButton;
   private JButton seventhJButton;
   private JButton eightthJButton;
   private JButton ninethJButton;
   private JButton zerothJButton;
   private JButton startJButton;
   private JButton clearJButton;
   private Timer clockTimer;
   private String timeToDisplay = "";
   private microwaveTime = new CookingTime( 0, 0 );
   private DecimalFormat timeFormat = new DecimalFormat( "00" );
   public keyboard()
   {
      createUserInterface();
   }
     private void createUserInterface()
   {
      Container conPane = getContentPane();
      conPane.setLayout( null );
      windowJPanel11 = new JPanel();
      windowJPanel11.setBounds( 16, 16, 328, 205 );
      windowJPanel11.setBorder( new LineBorder( Color.BLACK ) );
      conPane.add( windowJPanel );
      controlJPanel11 = new JPanel();
      controlJPanel11.setBounds( 368, 16, 149, 205 );
      controlJPanel11.setBorder( new LineBorder( Color.BLACK ) );
      controlJPanel11.setLayout( null );
      conPane.add( controlJPanel );
      displayJTextField11 = new JTextField();
      displayJTextField11.setBounds( 7, 5, 135, 42 );
      displayJTextField11.setText( "The Microwave Oven" );
      displayJTextField11.setHorizontalAlignment( JTextField.CENTER );
      displayJTextField11.setEditable( false );
      controlJPanel11.add( displayJTextField );
      firstJButton = new JButton();
      firstJButton.setBounds( 13, 59, 41, 24 );
      firstJButton.setText( "1" );
      controlJPanel11.add( firstJButton );
      firstJButton.addActionListener(
      new ActionListener()
         {
          public void actionPerformed( ActionEvent event )
            {
               firstJButtonActionPerformed( event );
            }
         }
      );
      secondJButton = new JButton();
      secondJButton.setBounds( 54, 59, 41, 24 );
      secondJButton.setText( "2" );
      controlJPanel11.add( secondJButton );
      secondJButton.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent event )
            {
               secondJButtonActionPerformed( event );
            }

         }

      );
      thirdJButton = new JButton();
      thirdJButton.setBounds( 95, 59, 41, 24 );
      thirdJButton.setText( "3" );
      controlJPanel11.add( thirdJButton );
      thirdJButton.addActionListener(
         new ActionListener()
         {
         
            public void actionPerformed( ActionEvent event )
            {
               thridJButtonActionPerformed( event );
            }

         }

      );

   
      fourthJButton = new JButton();
      fourthJButton.setBounds( 13, 83, 41, 24 );
      fourthJButton.setText( "4" );
      controlJPanel.add( fourthJButton );
      fourthJButton.addActionListener(

         new ActionListener()
         {
       
            public void actionPerformed( ActionEvent event )
            {
               fourthJButtonActionPerformed( event );
            }

         }

      );

  
      fivethJButton = new JButton();
      fivethJButton.setBounds( 54, 83, 41, 24 );
      fivethJButton.setText( "5" );
      controlJPanel.add( fivethJButton );
      fivethJButton.addActionListener(

         new ActionListener()
         {
          
            public void actionPerformed( ActionEvent event )
            {
               fivethJButtonActionPerformed( event );
            }

         }

      );

   
      sixthJButton = new JButton();
      sixthJButton.setBounds( 95, 83, 41, 24 );
      sixthJButton.setText( "6" );
      controlJPanel.add( sixthJButton );
      sixthJButton.addActionListener(

         new ActionListener()
         {
         
            public void actionPerformed( ActionEvent event )
            {
               sixthJButtonActionPerformed( event );
            }

         }

      );

  
      seventhJButton = new JButton();
      seventhJButton.setBounds( 13, 107, 41, 24 );
      seventhJButton.setText( "7" );
      controlJPanel.add( seventhJButton );
      seventhJButton.addActionListener(

         new ActionListener()
         {
        
            public void actionPerformed( ActionEvent event )
            {
               seventhJButtonActionPerformed( event );
            }

         }

      );

    
      eightthJButton = new JButton();
      eightthJButton.setBounds( 54, 107, 41, 24 );
      eightthJButton.setText( "8" );
      controlJPanel.add( eightthJButton );
      eightthJButton.addActionListener(

         new ActionListener()
         {
       
            public void actionPerformed( ActionEvent event )
            {
               eightthJButtonActionPerformed( event );
            }

         }
      );

       ninethJButton = new JButton();
      ninethJButton.setBounds( 95, 107, 41, 24 );
      nineJButton.setText( "9" );
      controlJPanel.add( ninethJButton );
      ninethJButton.addActionListener(

         new ActionListener()
         {
         
            public void actionPerformed( ActionEvent event )
            {
               ninethJButtonActionPerformed( event );
            }

         }

      );
    
   
      zerothJButton = new JButton();
      zerothJButton.setBounds( 54, 131, 41, 24 );
      zeroJButton.setText( "0" );
      controlJPanel.add( zerothJButton );
      zerothJButton.addActionListener(

         new ActionListener()
         {
         
            public void actionPerformed( ActionEvent event )
            {
               zerothJButtonActionPerformed( event );
            }

         }

      );


      startJButton = new JButton();
      startJButton.setBounds( 7, 171, 64, 24 );
      startJButton.setText( "Start" );
      controlJPanel11.add( startJButton );
      startJButton.addActionListener(

         new ActionListener()
         {
        
            public void actionPerformed( ActionEvent event )
            {
               startJButtonActionPerformed( event );
            }

         }

      );

  
      clearJButton11 = new JButton();
      clearJButton11.setBounds( 79, 171, 64, 24 );
      clearJButton11.setText( "Clear" );
      controlJPanel11.add( clearJButton11 );
      clearJButton11.addActionListener(

         new ActionListener()
         {
        
            public void actionPerformed( ActionEvent event )
            {
               clearJButtonActionPerformed( event );
            }

         }

      );

    
      ActionListener timerActionListener =

         new ActionListener()
         {
         
            public void actionPerformed( ActionEvent event )
            {
               clockTimerActionPerformed( event );
            }

         };

  
      clockTime11= new Timer( 1000, timerActionListener );

    
      setTitle( "The Microwave Oven" );
      setSize( 536, 261 );  
      setVisible( true );     

   }


   private void firstJButtonActionPerformed( ActionEvent event )
   {
      displayTime( "1" );

   }


   private void secondButtonActionPerformed( ActionEvent event )
   {
      displayTime( "2" );

   }


   private void thirdJButtonActionPerformed( ActionEvent event )
   {
      displayTime( "3" );

   }


   private void fourthJButtonActionPerformed( ActionEvent event )
   {
      displayTime( "4" );

   }


   private void fivethJButtonActionPerformed( ActionEvent event )
   {
      displayTime( "5" );

   }


   private void sixthJButtonActionPerformed( ActionEvent event )
   {
      displayTime( "6" );
   }


   private void seventhJButtonActionPerformed( ActionEvent event )
   {
      displayTime( "7" );

   }

   private void eightthJButtonActionPerformed( ActionEvent event )
   {
      displayTime( "8" );
   }


   private void ninethJButtonActionPerformed( ActionEvent event )
   {
      displayTime( "9" );

   }


   private void zerothJButtonActionPerformed( ActionEvent event )
   {
      displayTime( "0" );

   }


   private String formatTime()
   {

      String currTime = timeToDisplay;
      for ( int i = currTime.length(); i < 4; i++ )
      {
         currTime = "0" + currTime;
      }
      if ( currTime.length() > 4 )
      {
      
         currTime = currTime.substring( 0, 4 );
      }

      return currTime;

   }
    private void startJButtonActionPerformed( ActionEvent event )
   {
   
      String fourthDigitTime = formatTime();
      String min = fourDigitTime.substring( 0, 2 );
      String sec = fourDigitTime.substring( 2 );
      microwaveTime.setMinute( Integer.parseInt( min ) );
      microwaveTime.setSecond( Integer.parseInt( sec ) );
         displayJTextField.setText( timeFormat.format(
         microwaveTime.getMinute() ) + ":" + timeFormat.format(
         microwaveTime.getSecond() ) );
      timeToDisplay = "";
   
      clockTimer.start();    
      windowJPanel.setBackground( Color.YELLOW );

   }


   private void clearJButtonActionPerformed( ActionEvent event )
   {
   
      clockTimer.stop();
      displayJTextField.setText( "The Microwave Oven" );
      timeToDisplay = "";
      windowJPanel.setBackground( new Color( 204, 204, 204 ) );

   }


   private void displayTime( String digit11 )
   {
    
      timeToDisplay += digit11;
      String fourthDigitTime = formatTime();
      String min = fourDigitTime.substring( 0, 2 );
      String sec = fourDigitTime.substring( 2 );
      displayJTextField.setText( min + ":" + sec );

   }

   private void clockTimerActionPerformed( ActionEvent event )
   {
    
      microwaveTime.tick();
      if ( !microwaveTime.isDone() )
      {
      
         displayJTextField.setText( timeFormat.format(
            microwaveTime.getMinute() ) + ":" + timeFormat.format(
            microwaveTime.getSecond() ) );
      }
      else
      {
         clockTimer.stop();
         displayJTextField.setText( "Done!" );
         windowJPanel.setBackground( new Color( 204, 204, 204 ) );

      }
    
   }


   public static void main( String args[] )
   {
      MicrowaveOven app= new MicrowaveOven();
      app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   }

}