How Do I Set or Change The PATH System Variable?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

How do I set or change the PATH system variable?

Setting Path on Windows


For Windows XP:

1. Start -> Control Panel -> System -> Advanced


2. Click on Environment Variables, under System Variables, find PATH, and click on it.
3. In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have
the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the
value.
4. Close the window.
5. Reopen Command prompt window, and run your java code.

For Windows Vista:

1. Right click “My Computer” icon


2. Choose “Properties” from context menu
3. Click “Advanced” tab (“Advanced system settings” link in Vista)
4. In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have
the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the
value.
5. Reopen Command prompt window, and run your java code.

Setting Path on Solaris and Linux


To find out if the java executable is in your PATH, execute:
% java -version

This will print the version of the java executable, if it can find it. If you get error java: Command not found. Then path
is not properly set.

To find out which java executable the first one found in your PATH, execute:
% which java

Below are the steps to set the PATH permanently,


Note: We are here giving instructions for two most popular Shells on Linux and Solaris.
Please visit link below if you are using any other shells. 
Path Setting Tutorial

For bash Shell: 

1. Edit the startup file (~/ .bashrc)


2. Modify PATH variable: 
PATH=/usr/local/jdk1.6.0/bin
3. export PATH
4. Save and close the file
5. Open new Terminal window
6. Verify the PATH is set properly
% java -version

For C Shell (csh):

1. Edit startup file (~/ .cshrc)


2. Set Path 
set path=(/usr/local/jdk1.6.0/bin )
3. Save and Close the file
4. Open new Terminal window
5. Verify the PATH is set properly
% java –version

Programmes
1)Calculate Circle Area using Java Example

1. import java.io.BufferedReader;
2. import java.io.IOException;
3. import java.io.InputStreamReader;
4.  
5. public class CalculateCircleAreaExample {
6.  
7. public static void main(String[] args) {
8.  
9. int radius = 0;
10. System.out.println("Please enter radius of a circle");
11.  
12. try
13. {
14. //get the radius from console
15. BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
16. radius = Integer.parseInt(br.readLine());
17. }
18. //if invalid value was entered
19. catch(NumberFormatException ne)
20. {
21. System.out.println("Invalid radius value" + ne);
22. System.exit(0);
23. }
24. catch(IOException ioe)
25. {
26. System.out.println("IO Error :" + ioe);
27. System.exit(0);
28. }
29.  
30. /*
31. * Area of a circle is
32. * pi * r * r
33. * where r is a radius of a circle.
34. */
35.  
36. //NOTE : use Math.PI constant to get value of pi
37. double area = Math.PI * radius * radius;
38.  
39. System.out.println("Area of a circle is " + area);
40. }
41. }

o/p

1. /*
2. Output of Calculate Circle Area using Java Example would be
3. Please enter radius of a circle
4. 19
5. Area of a circle is 1134.1149479459152
6. */

2) Calculate Circle Perimeter using Java Example


1. import java.io.BufferedReader;
2. import java.io.IOException;
3. import java.io.InputStreamReader;
4.  
5. public class CalculateCirclePerimeterExample {
6.  
7. public static void main(String[] args) {
8.  
9. int radius = 0;
10. System.out.println("Please enter radius of a circle");
11.  
12. try
13. {
14. //get the radius from console
15. BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
16. radius = Integer.parseInt(br.readLine());
17. }
18. //if invalid value was entered
19. catch(NumberFormatException ne)
20. {
21. System.out.println("Invalid radius value" + ne);
22. System.exit(0);
23. }
24. catch(IOException ioe)
25. {
26. System.out.println("IO Error :" + ioe);
27. System.exit(0);
28. }
29.  
30. /*
31. * Perimeter of a circle is
32. * 2 * pi * r
33. * where r is a radius of a circle.
34. */
35.  
36. //NOTE : use Math.PI constant to get value of pi
37. double perimeter = 2 * Math.PI * radius;
38.  
39. System.out.println("Perimeter of a circle is " + perimeter);
40. }
41. }

o/p

1. /*
2. Output of Calculate Circle Perimeter using Java Example would be
3. Please enter radius of a circle
4. 19
5. Perimeter of a circle is 119.38052083641213
6. */

3) Reverse Number using Java


1. public class ReverseNumber {
2.  
3. public static void main(String[] args) {
4.  
5. //original number
6. int number = 1234;
7. int reversedNumber = 0;
8. int temp = 0;
9.  
10. while(number > 0){
11.  
12. //use modulus operator to strip off the last digit
13. temp = number%10;
14.  
15. //create the reversed number
16. reversedNumber = reversedNumber * 10 + temp;
17. number = number/10;
18.  
19. }
20.  
21. //output the reversed number
22. System.out.println("Reversed Number is: " + reversedNumber);
23. }
24. }

4) Even Odd Number Example


1. public class FindEvenOrOddNumber {
2.  
3. public static void main(String[] args) {
4.  
5. //create an array of 10 numbers
6. int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};
7.  
8. for(int i=0; i < numbers.length; i++){
9.  
10. /*
11. * use modulus operator to check if the number is even or
odd.
12. * If we divide any number by 2 and reminder is 0 then the
number is
13. * even, otherwise it is odd.
14. */
15.  
16. if(numbers[i]%2 == 0)
17. System.out.println(numbers[i] + " is even number.");
18. else
19. System.out.println(numbers[i] + " is odd number.");
20.  
21. }
22.  
23. }
24. }

o/p

1. /*
2. Output of the program would be
3. 1 is odd number.
4. 2 is even number.
5. 3 is odd number.
6. 4 is even number.
7. 5 is odd number.
8. 6 is even number.
9. 7 is odd number.
10. 8 is even number.
11. 9 is odd number.
12. 10 is even number.
13. */

5) Calculate Rectangle Perimeter using Java Example


1. import java.io.BufferedReader;
2. import java.io.IOException;
3. import java.io.InputStreamReader;
4.  
5. public class CalculateRectPerimeter {
6.  
7. public static void main(String[] args) {
8.  
9. int width = 0;
10. int length = 0;
11.  
12. try
13. {
14. //read the length from console
15. BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
16.  
17. System.out.println("Please enter length of a rectangle");
18. length = Integer.parseInt(br.readLine());
19.  
20. //read the width from console
21. System.out.println("Please enter width of a rectangle");
22. width = Integer.parseInt(br.readLine());
23.  
24.  
25. }
26. //if invalid value was entered
27. catch(NumberFormatException ne)
28. {
29. System.out.println("Invalid value" + ne);
30. System.exit(0);
31. }
32. catch(IOException ioe)
33. {
34. System.out.println("IO Error :" + ioe);
35. System.exit(0);
36. }
37.  
38. /*
39. * Perimeter of a rectangle is
40. * 2 * (length + width)
41. */
42.  
43. int perimeter = 2 * (length + width);
44.  
45. System.out.println("Perimeter of a rectangle is " + perimeter);
46. }
47.  
48. }
49.  
50. /*
51. Output of Calculate Rectangle Perimeter using Java Example would be
52. Please enter length of a rectangle
53. 10
54. Please enter width of a rectangle
55. 15
56. Perimeter of a rectangle is 50
57. */

6) Java Class example


1. public class JavaClassExample{
2. /*
3.   Syntax of defining memebers of the java class is,
4.   <modifier> type <name>;
5.   */
6. private String name;
7. /*
8.   Syntax of defining methods of the java class is,
9.   <modifier> <return-type> methodName(<optional-parameter-list>) <exception-
list>{
10.   ...
11.   }
12.   */
13. public void setName(String n){
14. //set passed parameter as name
15. name = n;
16. }
17. public String getName(){
18. //return the set name
19. return name;
20. }
21. //main method will be called first when program is executed
22. public static void main(String args[]){
23. /*
24.   Syntax of java object creation is,
25.   <class-name> object-name = new <class-constructor>;
26.   */
27. JavaClassExample javaClassExample = new JavaClassExample();
28. //set name member of this object
29. javaClassExample.setName("Visitor");
30. // print the name
31. System.out.println("Hello " + javaClassExample.getName());
32. }
33. }
34.  
35. /*
36. OUTPUT of the above given Java Class Example would be :
37. Hello Visitor
38. */

7) Find Largest and Smallest Number in an Array Example


1. public class FindLargestSmallestNumber {
2.  
3. public static void main(String[] args) {
4.  
5. //array of 10 numbers
6. int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
7.  
8. //assign first element of an array to largest and smallest
9. int smallest = numbers[0];
10. int largetst = numbers[0];
11.  
12. for(int i=1; i< numbers.length; i++)
13. {
14. if(numbers[i] > largetst)
15. largetst = numbers[i];
16. else if (numbers[i] < smallest)
17. smallest = numbers[i];
18.  
19. }
20.  
21. System.out.println("Largest Number is : " + largetst);
22. System.out.println("Smallest Number is : " + smallest);
23. }
24. }
25.  
26. /*
27. Output of this program would be
28. Largest Number is : 98
29. Smallest Number is : 23
30. */

8)Java Factorial Example

1. public class NumberFactorial {


2.  
3. public static void main(String[] args) {
4.  
5. int number = 5;
6.  
7. /*
8. * Factorial of any number is !n.
9. * For example, factorial of 4 is 4*3*2*1.
10. */
11.  
12. int factorial = number;
13.  
14. for(int i =(number - 1); i > 1; i--)
15. {
16. factorial = factorial * i;
17. }
18.  
19. System.out.println("Factorial of a number is " + factorial);
20. }
21. }
22.  
23. /*
24. Output of the Factorial program would be
25. Factorial of a number is 120
26. */

9) Java Interface example


1. interface IntExample{
2.  
3. /*
4.   Syntax to declare method in java interface is,
5.   <modifier> <return-type> methodName(<optional-parameters>);
6.   IMPORTANT : Methods declared in the interface are implicitly public and
abstract.
7.   */
8.  
9. public void sayHello();
10. }
11. }
12. /*
13. Classes are extended while interfaces are implemented.
14. To implement an interface use implements keyword.
15. IMPORTANT : A class can extend only one other class, while it
16. can implement n number of interfaces.
17. */
18.  
19. public class JavaInterfaceExample implements IntExample{
20. /*
21.   We have to define the method declared in implemented interface,
22.   or else we have to declare the implementing class as abstract class.
23.   */
24.  
25. public void sayHello(){
26. System.out.println("Hello Visitor !");
27. }
28.  
29. public static void main(String args[]){
30. //create object of the class
31. JavaInterfaceExample javaInterfaceExample = new JavaInterfaceExample();
32. //invoke sayHello(), declared in IntExample interface.
33. javaInterfaceExample.sayHello();
34. }
35. }
36.  
37. /*
38. OUTPUT of the above given Java Interface example would be :
39. Hello Visitor !
40. */

10) Java Factorial Using Recursion Example


1. import java.io.BufferedReader;
2. import java.io.IOException;
3. import java.io.InputStreamReader;
4.  
5. public class JavaFactorialUsingRecursion {
6.  
7. public static void main(String args[]) throws NumberFormatException,
IOException{
8.  
9. System.out.println("Enter the number: ");
10.  
11. //get input from the user
12. BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
13. int a = Integer.parseInt(br.readLine());
14.  
15. //call the recursive function to generate factorial
16. int result= fact(a);
17.  
18.  
19. System.out.println("Factorial of the number is: " + result);
20. }
21.  
22. static int fact(int b)
23. {
24. if(b <= 1)
25. //if the number is 1 then return 1
26. return 1;
27. else
28. //else call the same function with the value - 1
29. return b * fact(b-1);
30. }
31. }
32.  
33. /*
34. Output of this Java example would be
35.  
36. Enter the number:
37. 5
38. Factorial of the number is: 120
39. */

11) Swap Numbers Java Example


1. public class SwapElementsExample {
2.  
3. public static void main(String[] args) {
4.  
5. int num1 = 10;
6. int num2 = 20;
7.  
8. System.out.println("Before Swapping");
9. System.out.println("Value of num1 is :" + num1);
10. System.out.println("Value of num2 is :" +num2);
11.  
12. //swap the value
13. swap(num1, num2);
14. }
15.  
16. private static void swap(int num1, int num2) {
17.  
18. int temp = num1;
19. num1 = num2;
20. num2 = temp;
21.  
22. System.out.println("After Swapping");
23. System.out.println("Value of num1 is :" + num1);
24. System.out.println("Value of num2 is :" +num2);
25.  
26. }
27. }
28.  
29. /*
30. Output of Swap Numbers example would be
31. Before Swapping
32. Value of num1 is :10
33. Value of num2 is :20
34. After Swapping
35. Value of num1 is :20
36. Value of num2 is :10
37. */

12) Swap Numbers Without Using Third Variable Java Example


1. public class SwapElementsWithoutThirdVariableExample {
2.  
3. public static void main(String[] args) {
4.  
5. int num1 = 10;
6. int num2 = 20;
7.  
8. System.out.println("Before Swapping");
9. System.out.println("Value of num1 is :" + num1);
10. System.out.println("Value of num2 is :" +num2);
11.  
12. //add both the numbers and assign it to first
13. num1 = num1 + num2;
14. num2 = num1 - num2;
15. num1 = num1 - num2;
16.  
17. System.out.println("Before Swapping");
18. System.out.println("Value of num1 is :" + num1);
19. System.out.println("Value of num2 is :" +num2);
20. }
21.  
22.  
23. }
24.  
25. /*
26. Output of Swap Numbers Without Using Third Variable example would be
27. Before Swapping
28. Value of num1 is :10
29. Value of num2 is :20
30. Before Swapping
31. Value of num1 is :20
32. Value of num2 is :10
33. */

13) Find absolute value of float, int, double and long using Math.abs
1. public class FindAbsoluteValueExample {
2.  
3. public static void main(String[] args) {
4.  
5. int i = 8;
6. int j = -5;
7.  
8. /*
9.   * To find absolute value of int, use
10.   * static int abs(int i) method.
11.   *
12.   * It returns the same value if the agrument is non negative value,
otherwise
13.   * negation of the negative value.
14.   *
15.   */
16.  
17. System.out.println("Absolute value of " + i + " is :" + Math.abs(i));
18. System.out.println("Absolute value of " + j + " is :" + Math.abs(j));
19.  
20. float f1 = 10.40f;
21. float f2 = -50.28f;
22.  
23. /*
24.   * To find absolute value of float, use
25.   * static float abs(float f) method.
26.   *
27.   * It returns the same value if the agrument is non negative value,
otherwise
28.   * negation of the negative value.
29.   *
30.   */
31. System.out.println("Absolute value of " + f1 + " is :" + Math.abs(f1));
32. System.out.println("Absolute value of " + f2 + " is :" + Math.abs(f2));
33.  
34. double d1 = 43.324;
35. double d2 = -349.324;
36. /*
37.   * To find absolute value of double, use
38.   * static double abs(double d) method.
39.   *
40.   * It returns the same value if the agrument is non negative value,
otherwise
41.   * negation of the negative value.
42.   *
43.   */
44. System.out.println("Absolute value of " + d1 + " is :" + Math.abs(d1));
45. System.out.println("Absolute value of " + d2 + " is :" + Math.abs(d2));
46.  
47. long l1 = 34;
48. long l2 = -439;
49. /*
50.   * To find absolute value of long, use
51.   * static long abs(long l) method.
52.   *
53.   * It returns the same value if the agrument is non negative value,
otherwise
54.   * negation of the negative value.
55.   *
56.   */
57. System.out.println("Absolute value of " + l1 + " is :" + Math.abs(l1));
58. System.out.println("Absolute value of " + l2 + " is :" + Math.abs(l2));
59.  
60. }
61. }
62.  
63. /*
64. Output would be
65. Absolute value of 8 is :8
66. Absolute value of -5 is :5
67. Absolute value of 10.4 is :10.4
68. Absolute value of -50.28 is :50.28
69. Absolute value of 43.324 is :43.324
70. Absolute value of -349.324 is :349.324
71. Absolute value of 34 is :34
72. Absolute value of -439 is :439
73. */

Find maximum of two numbers using Math.max

1. public class FindMaxOfTwoNumbersExample {


2.  
3. public static void main(String[] args) {
4.  
5. /*
6.   * To find maximum of two int values, use
7.   * static int max(int a, int b) method of Math class.
8.   */
9.  
10. System.out.println(Math.max(20,40));
11.  
12. /*
13.   * To find minimum of two float values, use
14.   * static float max(float f1, float f2) method of Math class.
15.   */
16. System.out.println(Math.max(324.34f,432.324f));
17.  
18. /*
19.   * To find maximum of two double values, use
20.   * static double max(double d2, double d2) method of Math class.
21.   */
22. System.out.println(Math.max(65.34,123.45));
23.  
24. /*
25.   * To find maximum of two long values, use
26.   * static long max(long l1, long l2) method of Math class.
27.   */
28.  
29. System.out.println(Math.max(435l,523l));
30. }
31. }
32.  
33. /*
34. Output would be
35. 40
36. 432.324
37. 123.45
38. 523
39. */

Round Java float and double numbers using Math.round

1. public class RounFloatDoubleNumbersExample {


2.  
3. public static void main(String[] args) {
4.  
5. /*
6.   * To round float number, use
7.   * static int round(float f) method of Java Math class.
8.   *
9.   * It returns closest int number to the argument.
10.   * Internally, it adds 0.5 to the argument, takes floor value and casts
11.   * the result into int.
12.   *
13.   * i.e. result = (int) Math.floor( argument value + 0.5f )
14.   */
15.  
16. //returns same value
17. System.out.println(Math.round(10f));
18.  
19. // returns (int) Math.floor(10.6) = 10
20. System.out.println(Math.round(20.5f));
21.  
22. //returns (int) Math.floor(20.5 + 0.5) = 30
23. System.out.println(Math.round(20.5f));
24.  
25. //returns (int) Math.floor(-18.9) = 19
26. System.out.println(Math.round(-19.4f));
27.  
28. //returns (int) Math.floor(-23) = -23
29. System.out.println(Math.round(-23.5f));
30.  
31. /*
32.   * To round double numbers, use
33.   * static long round(double d) method of Java Math class.
34.   * It returns long.
35.   */
36. }
37. }
38.  
39. /*
40. Output would be
41. 10
42. 21
43. 21
44. -19
45. -23
46. */

Find largest number

import java.util.Scanner; //scanner class import declaration


public class LargestValue
{
public void determineLargestValue()
{
Scanner input = new Scanner(System.in);

int intCounter = 0;
int total = 0;
int number;
int largest;
System.out.println("Enter number: ");
largest = input.nextInt();

total += largest;

while (intCounter < 9)


{
System.out.println("Enter number: ");
number = input.nextInt();
if(number > largest)
{
largest = number;
}
total += number;
intCounter++;
}

}//end if

}//end while

System.out.printf("The total is : %d\n", total);


System.out.printf("\nThe largest of all 10 integers is: %d\n", largest, number);
}
}

class LargestValueTest
{
public void main(String args[])
{
LargestValue LargestValue1 = new LargestValue();//create object of LargestValue class to call
method
LargestValue1.determineLargestValue();//find the largest integer in 10
}//end main
}//end class

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy