illegal start of type expression error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gokulagopal
    New Member
    • Dec 2007
    • 1

    illegal start of type expression error

    I am sending the code please help me in finding the error and correct it.The code is below:

    import java.awt.*;
    import java.awt.event. *;
    class CalcFrame extends Frame
    {


    CalcFrame( String str)
    {
    super(str);
    addWindowListen er(new WindowAdapter()
    {


    public void windowClosing (WindowEvent we)
    {
    System.exit(0);
    }
    });

    }



    }
    public class Calculator11 implements ActionListener, ItemListener
    {
    CalcFrame fr;
    MenuBar mb;
    Menu view, font, about;
    MenuItem bold, regular, author;
    CheckboxMenuIte m scientific;
    CheckboxGroup cbg;
    Checkbox radians, degrees;
    TextField display;
    Button key[] = new Button[20];
    Button clearAll, clearEntry, round;
    Button scientificKey[] = new Button[10];
    boolean addButtonPresse d, subtractButtonP ressed, multiplyButtonP ressed;
    boolean divideButtonPre ssed, decimalPointPre ssed, powerButtonPres sed;
    boolean roundButtonPres sed = false;
    double initialNumber;
    double currentNumber = 0;
    int decimalPlaces = 0;

    public static void main (String args[])
    {

    Calculator calc = new Calculator();
    calc.makeCalcul ator();
    }

    public void makeCalculator( )
    {

    final int BWIDTH = 25;
    final int BHEIGHT = 25;
    int count =1;
    fr = new CalcFrame("Scie ntific Calculator");
    fr.setSize(200, 270);
    fr.setBackgroun d(Color.blue);
    mb = new MenuBar();
    view = new Menu("View");
    font= new Menu("Font");
    about = new Menu("About");

    scientific=new CheckboxMenuIte m("Scientific") ;
    scientific.addI temListener(thi s);
    bold = new MenuItem("Arial Bold");
    bold.addActionL istener(this);
    regular = new MenuItem("Arial Regular");
    regular.addActi onListener(this );
    author = new MenuItem("Autho r");
    author.addActio nListener(this) ;
    view.add(scient ific);
    font.add(bold);
    font.add(regula r);
    about.add(autho r);
    mb.add(view);
    mb.add(font);
    mb.add(about);
    fr.setMenuBar(m b);

    fr.setLayout(nu ll);

    for (int row = 0; row < 3; ++row) {


    for (int col = 0; col < 3; ++col) {

    key[count] = new Button(Integer. toString(count) );
    key[count].addActionListe ner(this);
    key[count].setBounds(30*( col + 1), 30*(row + 4),BWIDTH,BHEIG HT);
    key[count].setBackground( Color.yellow);
    fr.add(key[count++]);
    }
    }

    key[0] = new Button("0");
    key[0].addActionListe ner(this);
    key[0].setBounds(30,2 10,BWIDTH,BHEIG HT);
    key[0].setBackground( Color.yellow);
    fr.add(key[0]);

    key[10] = new Button(".");
    key[10].addActionListe ner(this);
    key[10].setBounds(60,2 10,BWIDTH,BHEIG HT);
    key[10].setBackground( Color.yellow);
    fr.add(key[10]);

    key[11] = new Button("=");
    key[11].addActionListe ner(this);
    key[11].setBounds(90,2 10,BWIDTH,BHEIG HT);
    key[11].setBackground( Color.yellow);
    fr.add(key[11]);

    key[12] = new Button("*");
    key[12].addActionListe ner(this);
    key[12].setBounds(120, 120,BWIDTH,BHEI GHT);
    key[12].setBackground( Color.yellow);
    fr.add(key[12]);

    key[13] = new Button("/");
    key[13].addActionListe ner(this);
    key[13].setBounds(120, 150,BWIDTH,BHEI GHT);
    key[13].setBackground( Color.yellow);
    fr.add(key[13]);

    key[14] = new Button("+");
    key[14].addActionListe ner(this);
    key[14].setBounds(120, 180,BWIDTH,BHEI GHT);
    key[14].setBackground( Color.yellow);
    fr.add(key[14]);

    key[15] = new Button("-");
    key[15].addActionListe ner(this);
    key[15].setBounds(120, 210,BWIDTH,BHEI GHT);
    key[15].setBackground( Color.yellow);
    fr.add(key[15]);


    key[16] = new Button("1/x");
    key[16].addActionListe ner(this);
    key[16].setBounds(150, 120,BWIDTH,BHEI GHT);
    key[16].setBackground( Color.yellow);
    fr.add(key[16]);

    key[17] = new Button("x^n");
    key[17].addActionListe ner(this);
    key[17].setBounds(150, 150,BWIDTH,BHEI GHT);
    key[17].setBackground( Color.yellow);
    fr.add(key[17]);

    key[18] = new Button("+/-");
    key[18].addActionListe ner(this);
    key[18].setBounds(150, 180,BWIDTH,BHEI GHT);
    key[18].setBackground( Color.yellow);
    fr.add(key[18]);

    key[19] = new Button("x!");
    key[19].addActionListe ner(this);
    key[19].setBounds(150, 210,BWIDTH,BHEI GHT);
    key[19].setBackground( Color.yellow);
    fr.add(key[19]);

    clearAll = new Button("CA");
    clearAll.addAct ionListener(thi s);
    clearAll.setBou nds(30, 240, BWIDTH+20, BHEIGHT);
    clearAll.setBac kground(Color.y ellow);
    fr.add(clearAll );

    clearEntry = new Button("CE");
    clearEntry.addA ctionListener(t his);
    clearEntry.setB ounds(80, 240, BWIDTH+20, BHEIGHT);
    clearEntry.setB ackground(Color .yellow);
    fr.add(clearEnt ry);

    round = new Button("Round") ;
    round.addAction Listener(this);
    round.setBounds (130, 240, BWIDTH+20, BHEIGHT);
    round.setBackgr ound(Color.yell ow);
    fr.add(round);

    display = new TextField("0");
    display.setBoun ds(30,90,150,20 );
    display.setBack ground(Color.wh ite);

    scientificKey[0] = new Button("Sin");
    scientificKey[0].addActionListe ner(this);
    scientificKey[0].setBounds(180, 120, BWIDTH + 10, BHEIGHT);
    scientificKey[0].setVisible(fal se);
    scientificKey[0].setBackground( Color.yellow);
    fr.add(scientif icKey[0]);

    scientificKey[1] = new Button("Cos");
    scientificKey[1].addActionListe ner(this);
    scientificKey[1].setBounds(180, 150, BWIDTH + 10, BHEIGHT);
    scientificKey[1].setBackground( Color.yellow);
    scientificKey[1].setVisible(fal se);
    fr.add(scientif icKey[1]);

    scientificKey[2] = new Button("Tan");
    scientificKey[2].addActionListe ner(this);
    scientificKey[2].setBounds(180, 180, BWIDTH + 10, BHEIGHT);
    scientificKey[2].setBackground( Color.yellow);
    scientificKey[2].setVisible(fal se);
    fr.add(scientif icKey[2]);

    scientificKey[3] = new Button("Pi");
    scientificKey[3].addActionListe ner(this);
    scientificKey[3].setBounds(180, 210, BWIDTH + 10, BHEIGHT);
    scientificKey[3].setBackground( Color.yellow);
    scientificKey[3].setVisible(fal se);
    fr.add(scientif icKey[3]);

    scientificKey[4] = new Button("aSin");
    scientificKey[4].addActionListe ner(this);
    scientificKey[4].setBounds(220, 120, BWIDTH + 10, BHEIGHT);
    scientificKey[4].setBackground( Color.yellow);
    scientificKey[4].setVisible(fal se);
    fr.add(scientif icKey[4]);

    scientificKey[5] = new Button("aCos");
    scientificKey[5].addActionListe ner(this);
    scientificKey[5].setBounds(220, 150, BWIDTH + 10, BHEIGHT);
    scientificKey[5].setBackground( Color.yellow);
    scientificKey[5].setVisible(fal se);
    fr.add(scientif icKey[5]);

    scientificKey[6] = new Button("aTan");
    scientificKey[6].addActionListe ner(this);
    scientificKey[6].setBounds(220, 180, BWIDTH + 10, BHEIGHT);
    scientificKey[6].setBackground( Color.yellow);
    scientificKey[6].setVisible(fal se);
    fr.add(scientif icKey[6]);

    scientificKey[7] = new Button("E");
    scientificKey[7].addActionListe ner(this);
    scientificKey[7].setBounds(220, 210, BWIDTH + 10, BHEIGHT);
    scientificKey[7].setBackground( Color.yellow);
    scientificKey[7].setVisible(fal se);
    fr.add(scientif icKey[7]);

    scientificKey[8] = new Button("todeg") ;
    scientificKey[8].addActionListe ner(this);
    scientificKey[8].setBounds(180, 240, BWIDTH + 10, BHEIGHT);
    scientificKey[8].setBackground( Color.yellow);
    scientificKey[8].setVisible(fal se);
    fr.add(scientif icKey[8]);

    scientificKey[9] = new Button("torad") ;
    scientificKey[9].addActionListe ner(this);
    scientificKey[9].setBounds(220, 240, BWIDTH + 10, BHEIGHT);
    scientificKey[9].setBackground( Color.yellow);
    scientificKey[9].setVisible(fal se);
    fr.add(scientif icKey[9]);
    cbg = new CheckboxGroup() ;
    degrees = new Checkbox("Degre es", cbg, true);
    radians = new Checkbox("Radia ns", cbg, false);
    degrees.addItem Listener(this);
    radians.addItem Listener(this);
    degrees.setBoun ds(185, 75, 3 * BWIDTH, BHEIGHT);
    radians.setBoun ds(185, 95, 3 * BWIDTH, BHEIGHT);
    degrees.setVisi ble(false);
    radians.setVisi ble(false);
    fr.add(degrees) ;
    fr.add(radians) ;
    fr.add(display) ;
    fr.setVisible(t rue);
    }

    public void actionPerformed (ActionEvent ae) {
    String buttonText = ae.getActionCom mand();
    double displayNumber = Double.valueOf( display.getText ()).doubleValue ();

    if((buttonText. charAt(0) >= '0') & (buttonText.cha rAt(0) <= '9')) {


    if(decimalPoint Pressed) {
    for (int i=1;i <=decimalPlaces ; ++i)
    currentNumber *= 10;
    currentNumber +=(int)buttonTe xt.charAt(0)- (int)'0';


    for (int i=1;i <=decimalPlaces ; ++i) {
    currentNumber /=10;
    }

    ++decimalPlaces ;
    display.setText (Double.toStrin g(currentNumber ));
    }
    else if (roundButtonPre ssed) {
    int decPlaces = (int)buttonText .charAt(0) - (int)'0';
    for (int i=0; i< decPlaces; ++i)
    displayNumber *=10;
    displayNumber = Math.round(disp layNumber);

    for (int i = 0; i < decPlaces; ++i) {
    displayNumber /=10;
    }



    display.setText (Double.toStrin g(displayNumber ));
    roundButtonPres sed = false;
    }


    else {
    currentNumber = currentNumber * 10 + (int)buttonText .charAt(0)-(int)'0';
    display.setText (Integer.toStri ng((int)current Number));
    }
    }

    if(buttonText == "+") {
    addButtonPresse d = true;
    initialNumber = displayNumber;
    currentNumber = 0;
    decimalPointPre ssed = false;
    }

    if (buttonText == "-") {
    subtractButtonP ressed = true;
    initialNumber = displayNumber;
    currentNumber = 0;
    decimalPointPre ssed = false;
    }

    if (buttonText == "/") {
    divideButtonPre ssed = true;
    initialNumber = displayNumber;
    currentNumber = 0;
    decimalPointPre ssed = false;
    }

    if (buttonText == "*") {
    multiplyButtonP ressed = true;
    initialNumber = displayNumber;
    currentNumber = 0;
    decimalPointPre ssed = false;
    }

    if (buttonText == "1/x") {
    display.setText (reciprocal(dis playNumber));
    currentNumber = 0;
    decimalPointPre ssed = false;
    }

    display.setText (changeSign(dis playNumber));
    currentNumber = 0;
    decimalPointPre ssed = false;
    }
    if (buttonText == "x!") {
    display.setText (factorial(disp layNumber));
    currentNumber = 0;
    decimalPointPre ssed = false;
    }

    if (buttonText == "x^n") {
    powerButtonPres sed = true;
    initialNumber = displayNumber;
    currentNumber = 0;
    decimalPointPre ssed = false;
    }

    if (buttonText == "Sin") {
    if (degrees.getSta te())
    display.setText (Double.toStrin g(Math.sin(Math .PI * displayNumber/180)));


    else {
    display.setText (Double.toStrin g(Math.sin(disp layNumber)));
    currentNumber = 0;
    decimalPointPre ssed = false;
    }

    }




    if (buttonText == "Cos") {
    if (degrees.getSta te())
    display.setText (Double.toStrin g(Math.cos(Math .PI * displayNumber/180)));
    else{
    display.setText (Double.toStrin g(Math.cos(disp layNumber)));
    currentNumber = 0;
    decimalPointPre ssed = false;
    }



    }

    if (buttonText == "Tan") {
    if (degrees.getSta te())
    display.setText (Double.toStrin g(Math.tan(Math .PI * displayNumber/180)));


    else {
    display.setText (Double.toStrin g(Math.tan(disp layNumber)));
    currentNumber = 0;
    decimalPointPre ssed = false;
    }
    }




    if (buttonText == "aSin") {
    if (degrees.getSta te())
    display.setText (Double.toStrin g(Math.asin(dis playNumber)* 180/Math.PI ));


    else {
    display.setText (Double.toStrin g(Math.asin(dis playNumber)));
    currentNumber = 0;
    decimalPointPre ssed = false;
    }


    }




    if (buttonText == "aCos") {
    if (degrees.getSta te())
    display.setText (Double.toStrin g(Math.acos(dis playNumber)* 180/Math.PI ));


    else {
    display.setText (Double.toStrin g(Math.acos(dis playNumber)));
    currentNumber = 0;
    decimalPointPre ssed = false;
    }



    }

    if (buttonText == "aTan") {
    if (degrees.getSta te())
    display.setText (Double.toStrin g(Math.atan(dis playNumber)* 180/Math.PI ));


    else {
    display.setText (Double.toStrin g(Math.atan(dis playNumber)));
    currentNumber = 0;
    decimalPointPre ssed = false;
    }

    }

    if (buttonText == "todeg")
    display.setText (Double.toStrin g(Math.toDegree s(displayNumber )));
    if (buttonText == "torad")
    display.setText (Double.toStrin g(Math.toRadian s(displayNumber )));
    if (buttonText == "Pi") {
    display.setText (Double.toStrin g(Math.PI));
    currentNumber =0;
    decimalPointPre ssed = false;
    }



    if (buttonText == "Round")
    roundButtonPres sed = true;
    if (buttonText == ".") {
    String displayedNumber = display.getText ();
    boolean decimalPointFou nd = false;
    int i;
    decimalPointPre ssed = true;
    for (i =0; i < displayedNumber .length(); ++i) {


    if(displayedNum ber.charAt(i) == '.') {
    decimalPointFou nd = true;
    continue;
    }



    }
    if (!decimalPointF ound)
    decimalPlaces = 1;
    }
    if(buttonText == "CA"){

    resetAllButtons ();
    display.setText ("0");
    currentNumber = 0;
    }
    if (buttonText == "CE") {
    display.setText ("0");
    currentNumber = 0;
    decimalPointPre ssed = false;
    }
    if (buttonText == "E") {
    display.setText (Double.toStrin g(Math.E));
    currentNumber = 0;
    decimalPointPre ssed = false;
    }

    if (buttonText == "=") {
    currentNumber = 0;
    f(addButtonPres sed)
    display.setText (Double.toStrin g(initialNumber + displayNumber)) ;
    if(subtractButt onPressed)
    display.setText (Double.toStrin g(initialNumber - displayNumber)) ;

    if (divideButtonPr essed) {

    if(displayNumbe r == 0) {
    MessageBox mb = new MessageBox ( fr, "Error ", true, "Cannot divide by zero.");
    mb.show();
    }



    else
    display.setText (Double.toStrin g(initialNumber/displayNumber)) ;
    }
    if(multiplyButt onPressed)
    display.setText (Double.toStrin g(initialNumber * displayNumber)) ;
    if (powerButtonPre ssed)
    display.setText (power(initialN umber, displayNumber)) ;
    resetAllButtons ();
    }

    if (buttonText == "Arial Regular")
    {
    for (int i =0; i < 10; ++i)
    key[i].setFont(new Font("Arial", Font.PLAIN, 12));
    }




    if (buttonText == "Arial Bold") {
    for (int i =0; i < 10; ++i)
    key[i].setFont(new Font("Arial", Font.BOLD, 12));
    }




    if (buttonText == "Author") {
    MessageBox mb = new MessageBox ( fr, "Calculator ver 1.0 beta ", true, "Author: Gokula Gopalakrishnan" );
    mb.show();
    }



    }
    public void itemStateChange d(ItemEvent ie) {

    if (ie.getItem() == "Scientific ") {
    scientific.setS tate(true);
    fr.setTitle("Sc ientific Calculator");
    fr.setSize(270, 270);
    if (!scientificKey[0].isVisible()) {
    for (int i=0; i < 10; ++i)
    scientificKey[i].setVisible(tru e);
    radians.setVisi ble(true);
    degrees.setVisi ble(true);
    }



    }



    }

    public void resetAllButtons () {
    addButtonPresse d = false;
    subtractButtonP ressed = false;
    multiplyButtonP ressed = false;
    divideButtonPre ssed = false;
    decimalPointPre ssed = false;
    powerButtonPres sed = false;
    roundButtonPres sed = false;
    }




    public String factorial(doubl e num) {
    int theNum = (int)num;


    if (theNum < 1) {
    MessageBox mb = new MessageBox (fr, "Facorial Error", true,
    "Cannot find the factorial of numbers less than 1.");
    mb.show();

    return ("0");
    }




    else {
    for (int i=(theNum -1); i > 1; --i)
    theNum *= i;
    return Integer.toStrin g(theNum);
    }



    }


    public String reciprocal(doub le num) {


    if (num ==0) {
    MessageBox mb = new MessageBox(fr," Reciprocal Error", true,
    "Cannot find the reciprocal of 0");
    mb.show();
    }



    else
    num = 1/num;
    return Double.toString (num);
    }




    public String power (double base, double index) {
    return Double.toString (Math.pow(base, index));
    }



    public String changeSign(doub le num)
    {
    return Double.toString (-num);
    }



    }




    class MessageBox extends Dialog implements ActionListener
    {
    Button ok;
    MessageBox(Fram e f, String title, boolean mode, String message)
    {
    super(f, title, mode);
    Panel centrePanel = new Panel();
    Label lbl = new Label(message);
    centrePanel.add (lbl);
    add(centrePanel , "Center");
    Panel southPanel = new Panel();
    ok = new Button ("OK");
    ok.addActionLis tener(this);
    southPanel.add( ok);
    add(southPanel, "South");
    pack();

    addWindowListen er (new WindowAdapter() {


    public void windowClosing (WindowEvent we)
    {
    System.exit(0);
    }

    });
    }
    public void actionPerformed (ActionEvent ae)
    {
    dispose();
    }



    }
  • vipersniper5
    New Member
    • Dec 2007
    • 9

    #2
    could you post again using the CODE tags? simply put I'm not going to try reading that without indentation. Also, without some kind of info as to the error your getting or the problem thats occurring there little hope of finding the bug in code that long.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by gokulagopal
      I am sending the code please help me in finding the error and correct it.The code is below:
      Don't dump all your code here and expect us to guess what might be wrong with it.
      Show us the actual error message and just the *relevant* part of the code.

      kind regards,

      Jos

      ps. and use those code tags (see the legend on the right side of the editing page).

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by JosAH
        Don't dump all your code here and expect us to guess what might be wrong with it.
        Show us the actual error message and just the *relevant* part of the code.
        And if it's a runtime error, the best thing to do is to write a short (<1 page) example program that demonstrates your error, and that forum members can copy and run.

        You see, the shorter your code listing, the more likely forum members will take a look at it (we're busy, too!) and *more* forum members will consider your post, too. It's important for us to see you are making an effort.

        Comment

        Working...