Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-31-2004, 07:20 AM   #1 (permalink)
davidmccabe
Registered User
 
Join Date: Mar 2004
Posts: 7
davidmccabe is on a distinguished road
calculator applet

hi,

i have been asked to create a calculator applet which includes basic functions (i.e add, subtract, multiply, divide)
But aswell square root, recipricol, positive/negative number switch.

It also has to be able to save the contents of a display field to a memory location, retrieve this data and clear the memory.

The code below is what i have managed so far, but i have no clue about the rest of the functions that are to be included i.e.sqrt etc.

Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator extends Applet implements
ActionListener
{ 	
        Label displayLabel, enterLabel;
	TextField displayBox, enterBox;
	Button addButton, subtButton, multButton, divButton;
	Button clearButton, exitButton;
	
	Button sqrButton;
	
	double display_total, new_value;
	String displayString, inputString;
	
	public void init()
	{ 	displayLabel = new Label("Display");
		displayBox = new TextField(12);
		add(displayLabel);
		add(displayBox);
		displayBox.setEditable(false);
		display_total = 0.0;
		displayString = Double.toString(display_total);
		displayBox.setText(displayString);
		
		enterLabel = Label("Enter");
		enterBox = new TextField(12);
		add(enterLabel);
		add(enterBox);
		
		addButton = new Button("+");
		add(addButton);
		addButton.addActionListener(this);

		subtButton = new Button("-");
		add(subtButton);
		subtButton.addActionListener(this);
		
		multButton = new Button("*");
		add(multButton);
		multButton.addActionListener(this);

		divButton = new Button("/");
		add(divButton);
		divButton.addActionListener(this);

		clearButton = new Button("Clear");
		add(clearButton);
		clearButton.addActionListener(this);

		exitButton = new Button("Exit");
		add(exitButton);
		exitButton.addActionListener(this);
		
} end init

public void actionPerformed(ActionEvent event)
{	 	
if	(arg.equals("Exit"))
// Exit button clicked
	{ 	Graphics g = this.getGraphics();
		g.dispose();
		System.exit(0);
	}	//exit
else if (arg.equals("Clear"))
// Clear button clicked
	{ 	display_total = 0.0;
		displayString = Double.toString(display_total);
		displayBox.setText(displayString);
	} 	//clear
	
else
{ 	try
	{ inputString = enterBox.getText();
	new_value =	Double.valueOf(inputString).doubleValue();
	showStatus("");
	if(arg.equals("+"))
		display_total = display_total + new_value;
	else if (arg.equals("-"))
		display_total = display_total - new_value;
	else if (arg.equals("*"))
		display_total = display_total * new_value;
	else if (arg.equals("/"))
		display_total = display_total / new_value;
	
	else if (arg.equals("sqr"))
		display_total = display_total * display_total;
		
}//try
catch (NumberFormatException entry)
{ 	showStatus("Error: Invalid Input");
	enterBox.setText("");
}//catch

}//else
}//ActionPerformed
}//Calculator
ANY help apreciated.

Thanks
davidmccabe is offline   Reply With Quote
Old 05-06-2006, 01:49 PM   #2 (permalink)
Drift
Registered User
 
Drift's Avatar
 
Join Date: May 2006
Posts: 7
Drift is on a distinguished road
} end init

public void actionPerformed(ActionEvent event)

in line 58 there was an error,how can ı solve that?compiler says ";" expected but it doesn't work.Could you help me?
Drift is offline   Reply With Quote
Old 05-06-2006, 04:01 PM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,148
Belisarius is on a distinguished road
You need to comment out "end init". Java is interpreting this as a statement, which is why it's complaining about the lack of ";"
__________________
GitS
Belisarius is offline   Reply With Quote
Old 05-07-2006, 12:52 AM   #4 (permalink)
Drift
Registered User
 
Drift's Avatar
 
Join Date: May 2006
Posts: 7
Drift is on a distinguished road
belisarius;
Could you explain your answer,can we solve that problem?
Drift is offline   Reply With Quote
Old 05-07-2006, 02:10 PM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,148
Belisarius is on a distinguished road
Code:
} end init
The "}" ends a statement in Java. It now begins processing the next statement, which happens to be "end init". This is obviously a comment that wasn't actually commented out. It's a syntatical error - Java tries to interpret it as best as it can, but because it doesn't make any sense it throws back it's best guess as to what is wrong with it - that is, the statement "end init" is lacking an ending semi-colon.

To resolve the error, simply comment out "end init".
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 10:52 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting