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

The program shown does not compile. Fix all the syntax errors // Phone.java // P

ID: 3648494 • Letter: T

Question

The program shown does not compile. Fix all the syntax errors

// Phone.java
// Program creates a GUI that resembles a phone with functionality.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Phone extends JFrame
{
private Jbutton keyJButton[];
private JPanel keyJPanel;
private JPanel lcdJPanel;
private JTextArea lcdJTextArea;
private String lcdOutput = "";
private int count;

// constructor sets up GUI
public Phone()
{
super( "Phone" );

lcdJTextArea = new JTextArea( 4, 15 );
lcdJTextArea.setEditable( false );
lcdJPanel.add( lcdJTextArea );

keyJButton = new Jbutton[ 15 ];

// initialize all digit key Buttons
for ( int i = 3; i <= 11; i++ )
keyJButton[ i ] = new Jbutton( String.valueOf( i - 2 ) );

// initialize all non-digit key Buttons
keyJButton[ 0 ] = new Jbutton( "Send" );
keyJButton[ 1 ] = new Jbutton( "clr" );
keyJButton[ 2 ] = new Jbutton( "End" );
keyJButton[ 12 ] = new Jbutton( "*" );
keyJButton[ 13 ] = new Jbutton( "0" );
keyJButton[ 14 ] = new Jbutton( "#" );

keyJButton[ 0 ].addActionListener(

public void actionPerformed( ActionEvent e )
{
lcdOutput = "Calling... " + lcdOutput;
lcdJTextArea.setText( lcdOutput );
} // end method actionPerformed
} // end new ActionListener
) // end addActionListener call

keyJButton[ 1 ].addActionListener(

new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
if ( lcdOutput.length() == 0 ||
lcdOutput.substring( 0, 1 ).equals( "C" ) )
return;
else
{
lcdOutput = lcdOutput.substring( 0, ( lcdOutput.length() - 1 ) );
lcdJTextArea.setText( lcdOutput );
} // end else
} // end method actionPerformed
} // end object ActionLstener
); // end addActionListener call

keyJButton[ 2 ].addActionListener(

new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
lcdJTextArea.setText( " " );
lcdOutput = "";
} // end method actionPerformed
} // end new ActionListener
); // end ActionListener call

for ( int i = 3; i <= 14; i++ )
{
keyJButton[ i ].addActionListener(

new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
lcdOutput += e.getActionCommand();

if ( lcdOutput.substring( 0, 1 ).equals( "C" ) )
return;

lcdJTextArea.append( e.getActionCommand() );
} // end method actionPerformed
} // end new ActionListener
); // end addActionListener call
} // end for loop

// set keyJPanel layout to grid layout
keyJPanel = new JPanel();
keyJPanel.setLayout( new GridLayout( 5, 3 ) );

// add buttons to keyJPanel
for ( int i = 0; i <= 14; i++ )
keyJPanel.add( keyJButton[ i ] );

// add components to container
add( lcdOutput, BorderLayout.NORTH );
} // end Phone constructor
} // end class Phone

// PhoneTest.java
// Program creates a GUI that resembles a phone with functionality.
import javax.swing.JFrame;

public class PhoneTest
{
// execute application
public static void main( String args[] )
{
Phone application = new Phone();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.setSize( 200, 300 );
application.setVisible( true );
} // end main
} // end class PhoneTest


Explanation / Answer

It should not be Jbutton. Rename it as JButton. everything works fine