Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Help Programming Calculator.java


  • Please log in to reply
1 reply to this topic

#1 eduardo0

eduardo0

  • Members
  • 5 posts
  • OFFLINE
  •  
  • Local time:09:13 PM

Posted 11 February 2008 - 08:04 AM

hi im eduardo0 i have problem to compile my project it is java file please help me to do this this is my codes but i have many errors in this codes please help me :D
Calculator.java:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JPanel implements ActionListener
{
  private JtextField Display = new JtextField("0");
  private String Button = "789/456*123-0.=+";
  private double result = 0;
  private String operator = "=";
  private boolean calculating = true;
  private boolean decimal = false;

  public Calculator()
  {
  setLayout(new BorderLayout());
 
  display.set.Editable(false);
  add(display, "North");

  JPanel p = new JPanel();
  p.setLayout(new GridLayout(4, 4));

  for (int i = 0; i < button.length(); i++)

  {
  JButton p = new JButton(button.subsstring(i, i + 1));
  p.add(B);
  b.addActionListener(this);

  }

  add(p, "center");
  }

  public void actionPerformed(ActionEvent evt)
  {
  String cmd = evt.getActionCommand();
  if ('0' <= cmd.charAT(0) && cmd.charAT(0) <= '9' || cmd.equals("."))
  {
		   System.out.println("Entered Here");

		   if (calculating){
				System.out.println("Entered Calculating..." + cmd.
charAT(0));
		   display.setText(cmd);

		}
	   else
		  display.setText(display.getTest() + cmd);
	   calculating = false;
	 }

	 else
	 {
	   if (calculating)
	   {
		 if (cmd.equals("-"))
		 {
			display.setText(cmd);
		   calculating = false;
		 }
		 else
			 operator = cmd;
	   }
		 else
	   {
		try{
				 double x = Double.parseDouble(display.getText());
				 calculate(x);
		operator = cmd;
		calculating = false;
				}catch(NumberFormatExeption e){
					display.setText("Illegal Number Format");
					calculating = false;

				}
				calculating = true;

		 


		   }
		 }
		  }

		  private void calculate(double n)
		  {
		  if (operator.equals("+"))
				result += n;
		  else if (operator.equals("-"))
				result -= n;
		  else if (operator.equals("*"))
				result *= n;
		  else if (operator.equals("/"))
				result /= n;
		  else if (operator.equals("="))
				result = n;
		  display.setText("" = result);
		   }

		   public static viod main(String [] args)
		   {
		  JFrame Frame = new JFrame();
		  frame.setTitle("Calculator");
		  frame.setBounds(250, 250, 200, 200);
		  frame.addWindowsListener(new WindowAdapter();
		  {  
			public void windowClosing(WindowEvent e);
		   
			  } System.exit(0);
			
		 }
		}

		Container contentPane = frame.getContent.Panel();
		ContentPanel.add(new Calculator());
		frame.show();
		 }
  }


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:08:13 PM

Posted 11 February 2008 - 08:23 AM

Just from a quick glance, you have void spelled wrong in the man method. Specifically, what errors are you having?? You're code is not formatted well, and there are no comments as to what you think each step should be doing.

EDIT: that Calculator.java business at the top is wrong also. I assume that when you pasted in the code that you missed the slashes.

EDIT@: I just took another quick look...where are you getting this code from? There is no such thing as a JtextFiels, you want a JTextField. You declare an array of Button labels, but you theen try to use button instead. There is no set.Editable method. You want setEditable. You use the statement p.add(B);, but you do not declare anything to be B. You try to add an action listener to b.addActionListener(this), but there is no object named b.

That's only the first 20 lines. You need to go back and start over and correct all of your spelling errors, maybe start with a much simpler project.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users