Sample Java Code

  • Share
  • SocialTwist Tell-a-Friend
  • Share
  • SocialTwist Tell-a-Friend

Ito na po ung request nyo na code..

Please share with other ComElec1 students..

Below are two java codes, the first one SampleOne.java don’t have a main method, but the method inside this class is being called on the second java file, how? You just need to instantiate the 1st file on the 2nd file. Thats it!
SampleOne.java



//SampleOne.java
public class SampleOne
{
public int sumOfTwoNumbers(int x,int y)
	{
	int sum = x+y;
	return sum;
	}

}

SampleOneMain.java

//SampleOneMain .java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SampleOneMain extends JFrame implements ActionListener
{
	//declare all gui components
	JButton compute,reset;
	JTextField num1,num2;

	SampleOne s = new SampleOne();
	//constructor

	public SampleOneMain()
	{
		super("Webinar ni sir joell");
		setLayout(new GridLayout(2,2,5,5));// rows and cols
		compute = new JButton("Compute Sum");
		reset = new JButton("reset");

		num1 = new JTextField(5);
		num2 = new JTextField(5);

		add(num1);
		add(num2);
		add(compute);
		add(reset);

		compute.addActionListener(this);
		reset.addActionListener(this);

		setSize(250,250);
		setVisible(true);

	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==compute)
		{
			int temp1=0,temp2=0;
			String msg="";
			temp1=Integer.parseInt(num1.getText());
			temp2=Integer.parseInt(num2.getText());
			msg="Sum is :"+s.sumOfTwoNumbers(temp1,temp2);
			//msg = String + int = ?
			JOptionPane.showMessageDialog(null,msg);
			//my mga tanong ba?
			//hehehe..

		}
	}

	public static void main(String args[])
	{
		new SampleOneMain().setDefaultCloseOperation(EXIT_ON_CLOSE);

	}

}
Related Posts with Thumbnails

WooThemes - Made by Designers

Related posts:

This entry was posted on Sunday, July 18th, 2010 at 9:50 pm and is filed under class. You can follow any responses to this entry through the RSS 2.0 feed.


2 Responses to “Sample Java Code”

  1. jason Jul 20, 2010 at 8:46 pm #

    sir kasama po sa exam to? jason alvero here B)

  2. Jairus Polintan Aug 10, 2010 at 7:44 pm #

    Sir bat may nlabas na error dun sa SampleOne s = new SampleOne ();

Leave a Reply