Basic Java
Basic Java
1. /*
2. Java Hello World example.
3. */
4.
5. public class HelloWorldExample{
6.
7. public static void main(String args[]){
8.
9. /*
10. Use System.out.println() to print on console.
11. */
12. System.out.println("Hello World !");
13.
14. }
15.
16. }
17.
18. /*
19.
20. OUTPUT of the above given Java Hello World Example would be :
21.
22. Hello World !
23.
24. */
1. /*
2. Java Class example.
3. This Java class example describes how class is defined and being used
4. in Java language.
5.
6. Syntax of defining java class is,
7. <modifier> class <class-name>{
8. // members and methods
9. }
10. */
11.
12. public class JavaClassExample{
13. /*
14. Syntax of defining memebers of the java class is,
15. <modifier> type <name>;
16. */
17. private String name;
18. /*
19. Syntax of defining methods of the java class is,
20. <modifier> <return-type> methodName(<optional-parameter-list>) <exception-
list>{
21. ...
22. }
23. */
24. public void setName(String n){
25. //set passed parameter as name
26. name = n;
27. }
28. public String getName(){
29. //return the set name
30. return name;
31. }
32. //main method will be called first when program is executed
33. public static void main(String args[]){
34. /*
35. Syntax of java object creation is,
36. <class-name> object-name = new <class-constructor>;
37. */
38. JavaClassExample javaClassExample = new JavaClassExample();
39. //set name member of this object
40. javaClassExample.setName("Visitor");
41. // print the name
42. System.out.println("Hello " + javaClassExample.getName());
43. }
44. }
45.
46. /*
47. OUTPUT of the above given Java Class Example would be :
48. Hello Visitor
49. */
50.
1. /*
2. Find Largest and Smallest Number in an Array Example
3. This Java Example shows how to find largest and smallest number in an
4. array.
5. */
6. public class FindLargestSmallestNumber {
7.
8. public static void main(String[] args) {
9.
10. //array of 10 numbers
11. int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
12.
13. //assign first element of an array to largest and smallest
14. int smallest = numbers[0];
15. int largetst = numbers[0];
16.
17. for(int i=1; i< numbers.length; i++)
18. {
19. if(numbers[i] > largetst)
20. largetst = numbers[i];
21. else if (numbers[i] < smallest)
22. smallest = numbers[i];
23.
24. }
25.
26. System.out.println("Largest Number is : " + largetst);
27. System.out.println("Smallest Number is : " + smallest);
28. }
29. }
30.
31. /*
32. Output of this program would be
33. Largest Number is : 98
34. Smallest Number is : 23
35. */
1. /*
2. Java Factorial Example
3. This Java Factorial Example shows how to calculate factorial of
4. a given number using Java.
5. */
6.
7. public class NumberFactorial {
8.
9. public static void main(String[] args) {
10.
11. int number = 5;
12.
13. /*
14. * Factorial of any number is !n.
15. * For example, factorial of 4 is 4*3*2*1.
16. */
17.
18. int factorial = number;
19.
20. for(int i =(number - 1); i > 1; i--)
21. {
22. factorial = factorial * i;
23. }
24.
25. System.out.println("Factorial of a number is " +
factorial);
26. }
27. }
28.
29. /*
30. Output of the Factorial program would be
31. Factorial of a number is 120
32. */
1. /*
2. Calculate Rectangle Area using Java Example
3. This Calculate Rectangle Area using Java Example shows how to calculate
4. area of Rectangle using it's length and width.
5. */
6.
7. import java.io.BufferedReader;
8. import java.io.IOException;
9. import java.io.InputStreamReader;
10.
11. public class CalculateRectArea {
12.
13. public static void main(String[] args) {
14.
15. int width = 0;
16. int length = 0;
17.
18. try
19. {
20. //read the length from console
21. BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
22.
23. System.out.println("Please enter length of a
rectangle");
24. length = Integer.parseInt(br.readLine());
25.
26. //read the width from console
27. System.out.println("Please enter width of a
rectangle");
28. width = Integer.parseInt(br.readLine());
29.
30.
31. }
32. //if invalid value was entered
33. catch(NumberFormatException ne)
34. {
35. System.out.println("Invalid value" + ne);
36. System.exit(0);
37. }
38. catch(IOException ioe)
39. {
40. System.out.println("IO Error :" + ioe);
41. System.exit(0);
42. }
43.
44. /*
45. * Area of a rectangle is
46. * length * width
47. */
48.
49. int area = length * width;
50.
51. System.out.println("Area of a rectangle is " + area);
52. }
53.
54. }
55.
56. /*
57. Output of Calculate Rectangle Area using Java Example would be
58. Please enter length of a rectangle
59. 10
60. Please enter width of a rectangle
61. 15
62. Area of a rectangle is 150
63. */
1. /*
2. Calculate Circle Area using Java Example
3. This Calculate Circle Area using Java Example shows how to calculate
4. area of circle using it's radius.
5. */
6.
7. import java.io.BufferedReader;
8. import java.io.IOException;
9. import java.io.InputStreamReader;
10.
11. public class CalculateCircleAreaExample {
12.
13. public static void main(String[] args) {
14.
15. int radius = 0;
16. System.out.println("Please enter radius of a circle");
17.
18. try
19. {
20. //get the radius from console
21. BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
22. radius = Integer.parseInt(br.readLine());
23. }
24. //if invalid value was entered
25. catch(NumberFormatException ne)
26. {
27. System.out.println("Invalid radius value" + ne);
28. System.exit(0);
29. }
30. catch(IOException ioe)
31. {
32. System.out.println("IO Error :" + ioe);
33. System.exit(0);
34. }
35.
36. /*
37. * Area of a circle is
38. * pi * r * r
39. * where r is a radius of a circle.
40. */
41.
42. //NOTE : use Math.PI constant to get value of pi
43. double area = Math.PI * radius * radius;
44.
45. System.out.println("Area of a circle is " + area);
46. }
47. }
48.
49. /*
50. Output of Calculate Circle Area using Java Example would be
51. Please enter radius of a circle
52. 19
53. Area of a circle is 1134.1149479459152
54. */
1. /*
2. Reverse Number using Java
3. This Java Reverse Number Example shows how to reverse a given number.
4. */
5.
6. public class ReverseNumber {
7.
8. public static void main(String[] args) {
9.
10. //original number
11. int number = 1234;
12. int reversedNumber = 0;
13. int temp = 0;
14.
15. while(number > 0){
16.
17. //use modulus operator to strip off the last digit
18. temp = number%10;
19.
20. //create the reversed number
21. reversedNumber = reversedNumber * 10 + temp;
22. number = number/10;
23.
24. }
25.
26. //output the reversed number
27. System.out.println("Reversed Number is: " +
reversedNumber);
28. }
29. }
30.
31. /*
32. Output of this Number Reverse program would be
33. Reversed Number is: 4321
34. */
1. /*
2. Swap Numbers Without Using Third Variable Java Example
3. This Swap Numbers Java Example shows how to
4. swap value of two numbers without using third variable using java.
5. */
6.
7. public class SwapElementsWithoutThirdVariableExample {
8.
9. public static void main(String[] args) {
10.
11. int num1 = 10;
12. int num2 = 20;
13.
14. System.out.println("Before Swapping");
15. System.out.println("Value of num1 is :" + num1);
16. System.out.println("Value of num2 is :" +num2);
17.
18. //add both the numbers and assign it to first
19. num1 = num1 + num2;
20. num2 = num1 - num2;
21. num1 = num1 - num2;
22.
23. System.out.println("Before Swapping");
24. System.out.println("Value of num1 is :" + num1);
25. System.out.println("Value of num2 is :" +num2);
26. }
27.
28.
29. }
30.
31. /*
32. Output of Swap Numbers Without Using Third Variable example would be
33. Before Swapping
34. Value of num1 is :10
35. Value of num2 is :20
36. Before Swapping
37. Value of num1 is :20
38. Value of num2 is :10
39. */