Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I need help with my ohm's law applet code. can't figure it out!

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Thanked 0 Times in 0 Posts

    Default I need help with my ohm's law applet code. can't figure it out!

    Ok, here is the code that I wrote but there are couple things wrong and i dont know have figure it out.
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    public class OhmsLawApplet extends Applet implements ActionListener
    	// declare variables
    	int amps;
    	double volt, ohms, index;
    	Image logo; //declare as Image object
    	//construct components
    	Label companyLabel = new Label("Welcome to Ohm's Law Calculator - I=V/R");
    	Label voltLabel = new Label("Enter the voltage:");
    		TextField voltField = new TextField(10);
    	Label ohmsLabel = new Label("Enter the resistance:");
    		TextField ohmsField = new TextField(10);
    	Button calcButton = new Button("Calculate");
    	Label outputLabel = new Label("Click the calculate button to see the amps.");
    	public void init()
    	public void actionPerformed(ActionEvent e)
    	volt = Integer.parseInt(voltField.getText());
    	ohms = Integer.parseInt(ohmsField.getText());
    	volt = V / 39.36;
    	omhs = R / 2;
    	index = amps / Math.pow(ohms);
    	outputLabel.setText("Electrical current" + Math.round(index) + ".");
    Last edited by helloworld922; February 19th, 2010 at 06:04 PM.

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: I need help with my ohm's law applet code. can't figure it out!

    Generally in applets you don't want to initialize your instance variables that way. Instead, use the init() method (note that this shouldn't have too much of a performance component, but will make your code much more readable).

    The first problem I can see is that in your actionPerformed method, you try using the variables V and R, but they were never declared anywhere (I actually have no idea what they're being used for). You're reading in volts and ohms directly from the textfields, there's no need to calculate anything unless you're trying to convert units (can't think of any that would match those conversion factors, though).

    The second problem I can see is that you're only reading in integers for volts and ohms. That could be what you want, but I suspect it would make a lot more sense to read in doubles.

    volt = Double.parseDouble(voltField.getText());
    ohms = Double.parseDouble(ohmsField.getText());

    The last problem I can see is you forgot to put the exponent for the Math.pow() method. However, in my opinion here it would be quicker just to multiply ohms twice.

    Lastly, you're calculating the current incorrectly. The variable index is useless because you already have a variable called amps. Just use that variable. Personally I wouldn't round this value because it's quite unlikely you'll be getting current flow of amps (more commonly current is shown in milli-amps), but you can put the Math.round() back in if it's required.

    amps = volt / ohms;
    outputLabel.setText("Electrical current: " + amps + " amps");

Similar Threads

  1. Can not figure out my programs error!
    By mparkerj in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2010, 10:48 AM
  2. can't figure out how to sort an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 6th, 2010, 03:07 PM
  3. simple problem w/ appelets which i cant figure out
    By JavaGreg in forum Java Applets
    Replies: 7
    Last Post: August 15th, 2009, 07:22 PM
  4. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM
  5. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM