By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

illegal start of type expression error

P: 1
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);
addWindowListener(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;
CheckboxMenuItem scientific;
CheckboxGroup cbg;
Checkbox radians, degrees;
TextField display;
Button key[] = new Button[20];
Button clearAll, clearEntry, round;
Button scientificKey[] = new Button[10];
boolean addButtonPressed, subtractButtonPressed, multiplyButtonPressed;
boolean divideButtonPressed, decimalPointPressed, powerButtonPressed;
boolean roundButtonPressed = false;
double initialNumber;
double currentNumber = 0;
int decimalPlaces = 0;

public static void main (String args[])
{

Calculator calc = new Calculator();
calc.makeCalculator();
}

public void makeCalculator()
{

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

scientific=new CheckboxMenuItem("Scientific");
scientific.addItemListener(this);
bold = new MenuItem("Arial Bold");
bold.addActionListener(this);
regular = new MenuItem("Arial Regular");
regular.addActionListener(this);
author = new MenuItem("Author");
author.addActionListener(this);
view.add(scientific);
font.add(bold);
font.add(regular);
about.add(author);
mb.add(view);
mb.add(font);
mb.add(about);
fr.setMenuBar(mb);

fr.setLayout(null);

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


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

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

key[0] = new Button("0");
key[0].addActionListener(this);
key[0].setBounds(30,210,BWIDTH,BHEIGHT);
key[0].setBackground(Color.yellow);
fr.add(key[0]);

key[10] = new Button(".");
key[10].addActionListener(this);
key[10].setBounds(60,210,BWIDTH,BHEIGHT);
key[10].setBackground(Color.yellow);
fr.add(key[10]);

key[11] = new Button("=");
key[11].addActionListener(this);
key[11].setBounds(90,210,BWIDTH,BHEIGHT);
key[11].setBackground(Color.yellow);
fr.add(key[11]);

key[12] = new Button("*");
key[12].addActionListener(this);
key[12].setBounds(120,120,BWIDTH,BHEIGHT);
key[12].setBackground(Color.yellow);
fr.add(key[12]);

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

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

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


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

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

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

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

clearAll = new Button("CA");
clearAll.addActionListener(this);
clearAll.setBounds(30, 240, BWIDTH+20, BHEIGHT);
clearAll.setBackground(Color.yellow);
fr.add(clearAll);

clearEntry = new Button("CE");
clearEntry.addActionListener(this);
clearEntry.setBounds(80, 240, BWIDTH+20, BHEIGHT);
clearEntry.setBackground(Color.yellow);
fr.add(clearEntry);

round = new Button("Round");
round.addActionListener(this);
round.setBounds(130, 240, BWIDTH+20, BHEIGHT);
round.setBackground(Color.yellow);
fr.add(round);

display = new TextField("0");
display.setBounds(30,90,150,20);
display.setBackground(Color.white);

scientificKey[0] = new Button("Sin");
scientificKey[0].addActionListener(this);
scientificKey[0].setBounds(180, 120, BWIDTH + 10, BHEIGHT);
scientificKey[0].setVisible(false);
scientificKey[0].setBackground(Color.yellow);
fr.add(scientificKey[0]);

scientificKey[1] = new Button("Cos");
scientificKey[1].addActionListener(this);
scientificKey[1].setBounds(180, 150, BWIDTH + 10, BHEIGHT);
scientificKey[1].setBackground(Color.yellow);
scientificKey[1].setVisible(false);
fr.add(scientificKey[1]);

scientificKey[2] = new Button("Tan");
scientificKey[2].addActionListener(this);
scientificKey[2].setBounds(180, 180, BWIDTH + 10, BHEIGHT);
scientificKey[2].setBackground(Color.yellow);
scientificKey[2].setVisible(false);
fr.add(scientificKey[2]);

scientificKey[3] = new Button("Pi");
scientificKey[3].addActionListener(this);
scientificKey[3].setBounds(180, 210, BWIDTH + 10, BHEIGHT);
scientificKey[3].setBackground(Color.yellow);
scientificKey[3].setVisible(false);
fr.add(scientificKey[3]);

scientificKey[4] = new Button("aSin");
scientificKey[4].addActionListener(this);
scientificKey[4].setBounds(220, 120, BWIDTH + 10, BHEIGHT);
scientificKey[4].setBackground(Color.yellow);
scientificKey[4].setVisible(false);
fr.add(scientificKey[4]);

scientificKey[5] = new Button("aCos");
scientificKey[5].addActionListener(this);
scientificKey[5].setBounds(220, 150, BWIDTH + 10, BHEIGHT);
scientificKey[5].setBackground(Color.yellow);
scientificKey[5].setVisible(false);
fr.add(scientificKey[5]);

scientificKey[6] = new Button("aTan");
scientificKey[6].addActionListener(this);
scientificKey[6].setBounds(220, 180, BWIDTH + 10, BHEIGHT);
scientificKey[6].setBackground(Color.yellow);
scientificKey[6].setVisible(false);
fr.add(scientificKey[6]);

scientificKey[7] = new Button("E");
scientificKey[7].addActionListener(this);
scientificKey[7].setBounds(220, 210, BWIDTH + 10, BHEIGHT);
scientificKey[7].setBackground(Color.yellow);
scientificKey[7].setVisible(false);
fr.add(scientificKey[7]);

scientificKey[8] = new Button("todeg");
scientificKey[8].addActionListener(this);
scientificKey[8].setBounds(180, 240, BWIDTH + 10, BHEIGHT);
scientificKey[8].setBackground(Color.yellow);
scientificKey[8].setVisible(false);
fr.add(scientificKey[8]);

scientificKey[9] = new Button("torad");
scientificKey[9].addActionListener(this);
scientificKey[9].setBounds(220, 240, BWIDTH + 10, BHEIGHT);
scientificKey[9].setBackground(Color.yellow);
scientificKey[9].setVisible(false);
fr.add(scientificKey[9]);
cbg = new CheckboxGroup();
degrees = new Checkbox("Degrees", cbg, true);
radians = new Checkbox("Radians", cbg, false);
degrees.addItemListener(this);
radians.addItemListener(this);
degrees.setBounds(185, 75, 3 * BWIDTH, BHEIGHT);
radians.setBounds(185, 95, 3 * BWIDTH, BHEIGHT);
degrees.setVisible(false);
radians.setVisible(false);
fr.add(degrees);
fr.add(radians);
fr.add(display);
fr.setVisible(true);
}

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

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


if(decimalPointPressed) {
for (int i=1;i <=decimalPlaces; ++i)
currentNumber *= 10;
currentNumber +=(int)buttonText.charAt(0)- (int)'0';


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

++decimalPlaces;
display.setText(Double.toString(currentNumber));
}
else if (roundButtonPressed) {
int decPlaces = (int)buttonText.charAt(0) - (int)'0';
for (int i=0; i< decPlaces; ++i)
displayNumber *=10;
displayNumber = Math.round(displayNumber);

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



display.setText(Double.toString(displayNumber));
roundButtonPressed = false;
}


else {
currentNumber = currentNumber * 10 + (int)buttonText.charAt(0)-(int)'0';
display.setText(Integer.toString((int)currentNumbe r));
}
}

if(buttonText == "+") {
addButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}

if (buttonText == "-") {
subtractButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}

if (buttonText == "/") {
divideButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}

if (buttonText == "*") {
multiplyButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}

if (buttonText == "1/x") {
display.setText(reciprocal(displayNumber));
currentNumber = 0;
decimalPointPressed = false;
}

display.setText(changeSign(displayNumber));
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "x!") {
display.setText(factorial(displayNumber));
currentNumber = 0;
decimalPointPressed = false;
}

if (buttonText == "x^n") {
powerButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}

if (buttonText == "Sin") {
if (degrees.getState())
display.setText(Double.toString(Math.sin(Math.PI * displayNumber/180)));


else {
display.setText(Double.toString(Math.sin(displayNu mber)));
currentNumber = 0;
decimalPointPressed = false;
}

}




if (buttonText == "Cos") {
if (degrees.getState())
display.setText(Double.toString(Math.cos(Math.PI * displayNumber/180)));
else{
display.setText(Double.toString(Math.cos(displayNu mber)));
currentNumber = 0;
decimalPointPressed = false;
}



}

if (buttonText == "Tan") {
if (degrees.getState())
display.setText(Double.toString(Math.tan(Math.PI * displayNumber/180)));


else {
display.setText(Double.toString(Math.tan(displayNu mber)));
currentNumber = 0;
decimalPointPressed = false;
}
}




if (buttonText == "aSin") {
if (degrees.getState())
display.setText(Double.toString(Math.asin(displayN umber)* 180/Math.PI ));


else {
display.setText(Double.toString(Math.asin(displayN umber)));
currentNumber = 0;
decimalPointPressed = false;
}


}




if (buttonText == "aCos") {
if (degrees.getState())
display.setText(Double.toString(Math.acos(displayN umber)* 180/Math.PI ));


else {
display.setText(Double.toString(Math.acos(displayN umber)));
currentNumber = 0;
decimalPointPressed = false;
}



}

if (buttonText == "aTan") {
if (degrees.getState())
display.setText(Double.toString(Math.atan(displayN umber)* 180/Math.PI ));


else {
display.setText(Double.toString(Math.atan(displayN umber)));
currentNumber = 0;
decimalPointPressed = false;
}

}

if (buttonText == "todeg")
display.setText(Double.toString(Math.toDegrees(dis playNumber)));
if (buttonText == "torad")
display.setText(Double.toString(Math.toRadians(dis playNumber)));
if (buttonText == "Pi") {
display.setText(Double.toString(Math.PI));
currentNumber =0;
decimalPointPressed = false;
}



if (buttonText == "Round")
roundButtonPressed = true;
if (buttonText == ".") {
String displayedNumber = display.getText();
boolean decimalPointFound = false;
int i;
decimalPointPressed = true;
for (i =0; i < displayedNumber.length(); ++i) {


if(displayedNumber.charAt(i) == '.') {
decimalPointFound = true;
continue;
}



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

resetAllButtons();
display.setText("0");
currentNumber = 0;
}
if (buttonText == "CE") {
display.setText("0");
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "E") {
display.setText(Double.toString(Math.E));
currentNumber = 0;
decimalPointPressed = false;
}

if (buttonText == "=") {
currentNumber = 0;
f(addButtonPressed)
display.setText(Double.toString(initialNumber + displayNumber));
if(subtractButtonPressed)
display.setText(Double.toString(initialNumber - displayNumber));

if (divideButtonPressed) {

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



else
display.setText(Double.toString(initialNumber/displayNumber));
}
if(multiplyButtonPressed)
display.setText(Double.toString(initialNumber * displayNumber));
if (powerButtonPressed)
display.setText(power(initialNumber, 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 itemStateChanged(ItemEvent ie) {

if (ie.getItem() == "Scientific") {
scientific.setState(true);
fr.setTitle("Scientific Calculator");
fr.setSize(270,270);
if (!scientificKey[0].isVisible()) {
for (int i=0; i < 10; ++i)
scientificKey[i].setVisible(true);
radians.setVisible(true);
degrees.setVisible(true);
}



}



}

public void resetAllButtons() {
addButtonPressed = false;
subtractButtonPressed = false;
multiplyButtonPressed = false;
divideButtonPressed = false;
decimalPointPressed = false;
powerButtonPressed = false;
roundButtonPressed = false;
}




public String factorial(double 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.toString(theNum);
}



}


public String reciprocal(double 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(double num)
{
return Double.toString(-num);
}



}




class MessageBox extends Dialog implements ActionListener
{
Button ok;
MessageBox(Frame 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.addActionListener(this);
southPanel.add(ok);
add(southPanel, "South");
pack();

addWindowListener (new WindowAdapter() {


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

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



}
Dec 10 '07 #1
Share this Question
Share on Google+
3 Replies


P: 9
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.
Dec 10 '07 #2

Expert 10K+
P: 11,448
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).
Dec 10 '07 #3

BigDaddyLH
Expert 100+
P: 1,216
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.
Dec 10 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.