1-Implementing A Java Program
1-Implementing A Java Program
Remember that, before we begin creating the program, the Java Development Kit (JDK) must be
properly installed on our system and also path will be set.
Creating Program
We can create a program using Text Editor (Notepad) or IDE (NetBeans)
class Test
{
public static void main(String []args)
{
System.out.println("My First Java Program.");
}
};
D:\javac Test.java
If everything is OK, the “javac” compiler creates a file called “Test.class” containing
byte code of the program.
d:\java Test
Now the interpreter looks for the main method in the program and begins execution from
there. After execution program successfully the output will be as follows,
Output:
My First Java Program.
static : static is again a keyword used to make a function static. To execute a static function you do not
have to create an Object of the class. The main() method here is called by JVM, without creating any
object for class.
void : It is the return type, meaning this function will not return anything.
main : main() method is the most important method in a Java program. This is the method which is
executed, hence all the logic must be inside the main() method. If a java class is not having a main()
method, it causes compilation error.
String[] args : This is used to signify that the user may opt to enter parameters to the Java Program at
command line. We can use both String[] args or String args[]. Java compiler would accept both forms.
System.out.println : This is used to print anything on the console like “printf” in C language.
CLASSPATH
1. CLASSPATH describes a location where all required files are available which is used in our
application.
2. Java compiler and JVM will use CLASSPATH to locate required files.
3. If we do not set CLASSPATH then Java compiler will not able to find required files hence you
will get error.
PATH
1. PATH variable is set to provide path for all java tools like javac, java, appletviewer.
3. If we do not set PATH then our system will not be able to find where javac is; hence it will not
work. It is mandatory to set path.
If you are saving the java source file inside the jdk/bin directory, path is not required to be set because all
the tools will be available in the current directory.
But If you are having your java file outside the jdk/bin folder, it is necessary to set path of JDK.
1. Temporary
2. Permanent
To set the temporary path of JDK, you need to follow following steps:
For Example:
For setting the permanent path of JDK, you need to follow these steps:
Go to MyComputer properties ->Click on Advanced system settings -> select Advance Tab
->click on environment variables -> click on new button of user variable
write
-> write path of bin folder in variable value -> ok -> ok -> ok
Variable Name: path
class Sample {
public static void main(String args[])
{
int a,b,sum=0,product=0;
a=10;
b=20;
sum=a+b;
product=a*b;
System.out.println("Sum="+sum);
System.out.println("Product="+product);
}
};
Q: Write a Java program to accept two numbers from the user and calculate sum and
product.
import java.util.*;
class Sample {
public static void main(String args[])
{
int a,b,sum=0,product=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the First No:");
a=scan.nextInt();
System.out.println("Enter the Second No:");
b=scan.nextInt();
sum=a+b;
product=a*b;
System.out.println("Sum="+sum);
System.out.println("Product="+product);
}
}
Q: Write a Java program to accept two number from the user and calculate the average.
import java.util.*;
public class Sample {
public static void main(String args[])
{
double a,b,sum=0,avg=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the First No:");
a=scan.nextDouble();
System.out.println("Enter the Second No:")
b=scan.nextDouble();
sum=a+b;
avg=sum/2;
System.out.println("Average="+avg);
}
}
Q: Write a java program to convert fahrenheit to celsius and celsius to Fahrenheit using
formula c = (5.0/9.0)*(f-32).
import java.util.Scanner;
public class Fahrenheit_Celsius
{
public static void main(String[] args)
{
double c, f;
Scanner s = new Scanner(System.in);
System.out.print("Enter temperature in Fahrenheit:");
f = s.nextDouble();
c = (5.0/9.0)*(f-32);
System.out.println("Temperature in Celsius:"+c);
}
}
Output:
Enter temperature in Fahrenheit:15
Temperature in Celsius:-9.444444444444445
Q: Write a Java program to calculate the area of triangle using three sides.
import java.lang.*;
import java.util.Scanner;
public class Area
{
public static void main(String[] args)
{
int a,b,c;
double s,area;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the three sides:");
a=scan.nextInt();
b=scan.nextInt();
c=scan.nextInt();
s=(a+b+c)/2;
area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.err.println("Area of Triangle:"+area);
}
}
import java.util.Scanner;
import java.text.DecimalFormat;
public class Sample
{
public static void main(String[] args)
{
int a,b,c;
double s,area;
Scanner scan = new Scanner(System.in);
Yes.