Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:57:23+00:00 2026-06-04T20:57:23+00:00

My code is for an applet that expands a binomial. I haven’t finished it,

  • 0

My code is for an applet that expands a binomial. I haven’t finished it, but it’ll only accept an exponent between 1 and 10, inclusive. I got it to 7, but like I said, I haven’t finished. My problem is this: in the first textField, it’ll accept a number and a variable (like 4a, or something). And the code I wrote can’t take into account that number (it’s supposed to only accept a variable). So…How do I get it so that the first textField can’t allow there to be an int? THANKS!

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*; 
import java.applet.*; 
import javax.swing.*; 
import java.text.DecimalFormat; 
import java.util.ArrayList; 
import javax.swing.Action;

public class BinomialExpander extends JApplet implements ActionListener
{
  JLabel welcome;
  JLabel directions;
  JLabel example;

  JLabel instructions;
  JLabel startOfBinomial;
  JLabel plusSign;
  JLabel forExponent;
  JLabel endOfBinomial;

  JTextField firstVar;
  JTextField txtSecond;
  JTextField exp;


  JLabel lblExpanded; 
  JLabel outputExpanded;  
  FlowLayout layout;

  JButton compute;

  private int[] pascal1 = {1,1};
  private int[] pascal2 = {1,2,1};
  private int[] pascal3 = {1,3,3,1};
  private int[] pascal4 = {1,4,6,4,1};
  private int[] pascal5 = {1,5,10,10,5,1};
  private int[] pascal6 = {1,6,15,20,15,6,1};
  private int[] pascal7 = {1,7,21,35,35,21,7,1};
  private int[] pascal8 = {1,8,28,56,70,56,28,8,1};
  private int[] pascal9 = {1,9,36,84,126,84,36,9,1};
  private int[] pascal10 = {1,10,45,120,210,120,45,10,1};

  private String a;
  private int y; 
  private int expo;
  private String x;

  public void init() 
  {
      Container c = getContentPane(); 
      c.setBackground(Color.pink);

      layout = new FlowLayout();
      layout.setAlignment(FlowLayout.LEFT);
      c.setLayout(layout);
      setSize(500,175);

      welcome = new JLabel("Welcome to the Binomial Expander Applet!");
      welcome.setFont(new Font("Copperplate Gothic Bold", Font.PLAIN, 16));
      directions = new JLabel("Enter binomial ONLY in the form: (VARIABLE + NUMBER)^EXPONENT");
      directions.setFont(new Font("Times New Roman", Font.PLAIN, 14));
      example = new JLabel("EXAMPLE: (x + 2)^2.");
      instructions = new JLabel("Enter the variable of your binomial:");
      startOfBinomial = new JLabel(" (");
      firstVar = new JTextField(4);
      plusSign = new JLabel(" + ");
      txtSecond = new JTextField(4);
      endOfBinomial = new JLabel(")");
      forExponent = new JLabel("^");
      forExponent.setFont(new Font("Times New Roman", Font.PLAIN, 10));
      exp = new JTextField(2);
      compute = new JButton("Compute!");
      compute.addActionListener(this);
      lblExpanded = new JLabel("Your expanded binomial is: ");
      outputExpanded = new JLabel("");
      c.add(welcome);
      c.add(directions);
      c.add(example);
      c.add(instructions);
      c.add(startOfBinomial);
      c.add(firstVar);
      c.add(plusSign);
      c.add(txtSecond);
      c.add(endOfBinomial);
      c.add(forExponent);
      c.add(exp);
      c.add(compute);
      c.add(lblExpanded);
      c.add(outputExpanded);
  }
  public void actionPerformed(ActionEvent event) 
  {
      a = firstVar.getText();
      y = Integer.parseInt(txtSecond.getText());
      expo = Integer.parseInt(exp.getText());

      outputExpanded.setText(expand()); 
  }
  public String expand()
  {  
     if (expo < 1 || expo > 10)
     {
        x = "ERROR: Exponent value must be between 1 and 10, inclusive.";
        return x;
     }
     else if (expo == 1)
     {
        x = a + "+" + y;
        return x;  
     }
     else if (expo == 2)
     {
        x = a + "^" + expo + " + " + (pascal2[1] * y) + a + " + " + (pascal2[2] * Math.pow(y, expo));
        return x;
     }
     else if (expo == 3)
     {
        x = a + "^" + expo + " + " + (pascal3[1] * y) + a + "^" + (expo-1) + (pascal3[2] * Math.pow(y, expo-1)) + a + " + " + (Math.pow(y, expo));
        return x;
     }
     else if (expo == 4)
     {
        x = a + "^" + expo + " + " + (pascal4[1] * y) + a + "^" + (expo-1) + " + " + (pascal4[2] * Math.pow(y, expo-2)) + a + " ^ " + (expo-2) + " + " + (pascal4[3] * 
        Math.pow(y, expo-1)) + a + "+" + (Math.pow(y,expo));
        return x;
     }
     else if (expo == 5)
     {
        x = a + "^" + expo + " + " + (pascal5[1] * y) + a + "^" + (expo-1) + " + " + (pascal5[2] * Math.pow(y,expo-3)) + a + "^" + (expo-2) + " + " + (pascal5[3] * 
        Math.pow(y, expo-2)) + a + "^" + (expo-3) + " + " + (pascal5[4] * Math.pow(y, expo-1)) + a + " + " + (Math.pow(y, expo));
        return x;
     }
     else if (expo == 6)
     {
        x = a + "^" + expo + " + " + (pascal6[1] * y) + a + "^" + (expo-1) + " + " + (pascal6[2] * Math.pow(y, expo-4)) + a + "^" + (expo-2) + " + " + (pascal6[3] *
        Math.pow(y, expo-3)) + a + "^" + (expo-3) + " + " + (pascal6[4] * Math.pow(y, expo-2)) + a + "^" + (expo-4) + " + " + (pascal6[5] * Math.pow(y, expo-1)) +
        a + " + " + (Math.pow(y,expo));
        return x;
     }
     else if (expo == 7)
     {
        x = a + "^" + expo + " + " + (pascal7[1] * y) + a + "^" + (expo-1) + " + " + (pascal7[2] * Math.pow(y, expo-5)) + a + "^" + (expo-2) + " + " + (pascal7[3]
        * Math.pow(y, expo-4)) + a + "^" + (expo-3) + " + " + (pascal7[4] * Math.pow(y, expo-3)) + a + "^" + (expo-4) + " + " + (pascal7[5] * Math.pow(y, expo-2))
        + a + "^" + (expo-5) + (pascal7[6] * Math.pow(y, expo-1)) + a + " + " + (Math.pow(y, expo));
        return x;
     }
     return x; 
  }
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:57:24+00:00Added an answer on June 4, 2026 at 8:57 pm

    I would recommend using String's match() function to see if the value in a is a valid answer (I don’t know what is valid for you). and then if it’s not valid you can use a JDialog to show an error message rather than computing the binomial.

    It would looks something like this:

      public void actionPerformed(ActionEvent event) 
      {
          a = firstVar.getText();
          y = Integer.parseInt(txtSecond.getText());
          expo = Integer.parseInt(exp.getText());
    
          if(a.matches(/*Some REGEX*/)) {
              outputExpanded.setText(expand());
          }
          else {
              //Use the JDialog
          } 
      }
    

    Here are the pages:

    String

    JDialog

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a java applet that loads some pre-installed native code extensions to display
I have an Java applet that loads native code through JNI. Everything worked just
i have following code in applet that basically writes some data to a file
I have a small bit of code that runs in an applet that contains
I read that you could call JavaScript code from a Java Applet by calling
In Apple sample code project: MoviePlayer , I want to realize that, when the
I've used the reachability code provided by apple and created a UIAlert that displays
I have an Applet that makes a request to a Servlet. On the servlet
I'm writing an Applet that makes some JSON-RPC calls. I'm using the Google JSON
I have a simple java applet that is invoked from gwt application. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.