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

Write a Java Swing application that raises a floating-point number into an integ

ID: 3799714 • Letter: W

Question

Write a Java Swing application that raises a floating-point number into an integer power. The application should handle exceptions caused by invalid input and display warnings by using JOptionPane pop-up windows. In this case the result field should display "NaN" (Not a Number).Moreover, if the resulting number appears to be an integer (e.g., 2.03 = 8), it should be shown as an integer without the decimal point and the tailing zeros.

Swingtemplate provided

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;

public class Swingtemplate
{
public static void main(String[] args)
{
   SwingApp app = new SwingApp(400, 400);   // set app window size
   app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

class SwingApp extends JFrame //"extends"
{
private final DrawPanel panel;       // class variable
public SwingApp(int width, int height)   // class constructor
{
   super();               // call to super class constructor
   Container pane = super.getContentPane(); // create layout, set colors
   panel = new DrawPanel();       // add GUI components
   panel.setBackground(Color.yellow); // set the color yellow as the background
//------------------------- add other GUI components to the panel-----------------------------
   pane.add(panel); // add panel to the main app container
   Toolkit toolkit = Toolkit.getDefaultToolkit();   // optionally position JFrame
   Dimension screenSize = toolkit.getScreenSize(); // puts the popup in the middle of the screen
   super.setLocation((screenSize.width - width)/2, (screenSize.height - height)/2);  
   super.setTitle("Swing app");       // set desired window title
   super.setSize(width, height);       // set desired window size
   super.setVisible(true);   // make the app window visible
}
}

class DrawPanel extends JPanel           // main window panel
{
@Override
public void paintComponent(Graphics g)
{
   super.paintComponent(g);       // must be the 1st line
//----------------- add here other code for drawing on panel-----------------------------

}
}

Explanation / Answer

import java.util.Scanner; public class Quadratic_Equation { public static void main(String[] args) { int a, b, c; double root1, root2, d; Scanner s = new Scanner(System.in); System.out.println("Given quadratic equation:ax^2 + bx + c"); System.out.print("Enter a:"); a = s.nextInt(); System.out.print("Enter b:"); b = s.nextInt(); System.out.print("Enter c:"); c = s.nextInt(); System.out.println("Given quadratic equation:"+a+"x^2 + "+b+"x + "+c); d = b * b - 4 * a * c; if(d > 0) { System.out.println("Roots are real and unequal"); root1 = ( - b + Math.sqrt(d))/(2*a); root2 = (-b - Math.sqrt(d))/(2*a); System.out.println("First root is:"+root1); System.out.println("Second root is:"+root2); } else if(d == 0) { System.out.println("Roots are real and equal"); root1 = (-b+Math.sqrt(d))/(2*a); System.out.println("Root:"+root1); } else { System.out.println("Roots are imaginary"); } } }