473,394 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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);
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
3 2641
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
JosAH
11,448 Expert 8TB
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
1,216 Expert 1GB
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

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

Similar topics

12
by: Chris Yates | last post by:
Why is this illegal: --(i++) // Error: -- needs an l-value and this is not: (--i)++ ??
6
by: BlueTrin | last post by:
Hello I was adapting a C version of SolvOpt in C++ to use it within a virtual class. However I am stuck with the overriding of evaluation and gradiant functions. cStepCurveEvaluator.cpp...
5
by: Roy Smith | last post by:
The following code appears to be illegal: while ((int c = getchar()) != EOF) { putchar (c); } I tried it on two different compilers (Sun workshop and gcc), and both give some variation on...
1
by: Manish | last post by:
The code is as ... $folderlistfile = $path."xmlfile.xml"; /* <list> <details id="2"> <name>Books</name>
2
by: dobbouk | last post by:
I'm getting these 2 errors and haven't got a clue whats causing them please java 43: illegal start of type for (i=0;i< Number_of_squares; i++) java 215 identifier expected } ...
4
by: rajesh619 | last post by:
I'm new to programming. I have created a servlet which retrieves values from the database after a value is put into the HTML page to which it is attached. But during compilation, it shows two errors....
2
by: mole40 | last post by:
Hello! I'm very new to writing java. And I keep getting the "Illegal Start of Type" error. I've tried everything to fix it. can anyone help me? import java.util.*; class ClockTalk { public...
2
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
3
by: MrHenry007 | last post by:
Hello! I'm fairly new to c++ but I have been following tutorials and have created functions before, but not one using a string. I can't work out what the problem is here. The function is supposed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.