Search This Blog

Showing posts with label method. Show all posts
Showing posts with label method. Show all posts

Java Program to Find Power of a Number

This is a java program to calculate power of a given number. The inputs are the number and its exponent (power). For example, if you give m and n, the result is m^n. You may use built-in function Math.pow() or hard code it.

Using Built-in Function

import java.util.Scanner;

public class Power{

public static void main(String[] args)
{
int m,n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number and its power");
m=sc.nextInt();
n=sc.nextInt();
System.out.printf("m^n = %.0f",(float)Math.pow(m,n));
}
}

Without using Built-in function

import java.util.Scanner;

public class Power{

public static void main(String[] args)
{
int m,n,i,p=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number and its power");
m=sc.nextInt();
n=sc.nextInt();
for(i=0;i<n;i++)
    p*=m;
System.out.printf("m^n = %d",p);
}
}

How to Remove Title bar from JFrame of Java Application

To remove the title bar completely, there is a method called setUndecorated() in java. It takes a boolean variable. If the boolean is true, the title bar will be removed. The following one is an example. The method setUndecorated() invoked with a true boolean argument, will remove the title bar from the JFrame MyJFrame. The method is called here from the constructor. You may call it anywhere from within the class scope.

How to Change Image in JButton in Java - Set Icon of JButton

In this post we are discussing how to change the image in button in Java. There may be situations in
change jbutton image java jbutton image jbutton background image jbutton image icon how to add image to jbutton in java swing jbutton picture adding image to jbutton add image to jbutton
Jbutton with Image set
programming where we want to know how to set JButton image in java or to change button icon in java. The java code to change the icon of a Jbutton is so short. We use the
setIcon()
method in JButton class. The setIcon method takes an ImageIcon object as an argument. I will show you a sample java code in which the icon of  button is set. First of all,  you have to copy the icon image (jpg, bmp, png or gif image) to the src folder in the project directory. You may also place it in some folder inside the src folder. If you are using netbeans IDE, you can simply drag and drop the image file into the desired folder in the projects pane. After copying the icon image to src folder (or any subfolder in it), use the following code to set it as the button's icon.
playbtn.setIcon(new ImageIcon(getClass().getResource("/manhaj/images/play.png")));