Wednesday, August 21, 2019

Java Programs

Simple Java Programs  

public class prg2 {

    public static void main(String[] args) {

        
        System.out.println("You entered:");
    }
}


public class prg1{

    public static void main(String[] args) {
        String str = "Welcome to Beginnersbook";
        String reversed = reverseString(str);
        System.out.println("The reversed string is: " + reversed);
    }

    public static String reverseString(String str)
    {
        if (str.isEmpty())
            return str;
        //Calling Function Recursively
        return reverseString(str.substring(1)) + str.charAt(0);
    }
}

public class prg3{

    public static void main(String[] args) {

        double number = 12.3;

        // true if number is less than 0
        if (number < 0.0)
            System.out.println(number + " is a negative number.");

        // true if number is greater than 0
        else if ( number > 0.0)
            System.out.println(number + " is a positive number.");

        // if both test expression is evaluated to false
        else
            System.out.println(number + " is 0.");
    }
}

public class prg5{

    public static void main(String[] args) {

        float first = 1.20f, second = 2.45f;

        System.out.println("--Before swap--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + second);

        // Value of first is assigned to temporary
        float temporary = first;

        // Value of second is assigned to first
        first = second;

        // Value of temporary (which contains the initial value of first) is assigned to second
        second = temporary;

        System.out.println("--After swap--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + second);
    }
}

public class prg6{
      public static void main(String args[]) {
         int a=5;
   while(a<10)   //while condition
         {
         System.out.println("value of a" +a);
         a++;
   System.out.println("aa");
         }
    }
}

public class prg7{
   public static String myClassVar="class or static variable";
   public static void main(String args[]){
      prg7 obj = new prg7();
      prg7 obj2 = new prg7();
      prg7 obj3 = new prg7();
      
      //All three will display "class or static variable"
      System.out.println(obj.myClassVar);
      System.out.println(obj2.myClassVar);
      System.out.println(obj3.myClassVar);

      //changing the value of static variable using obj2
      obj2.myClassVar = "Changed Text";

      //All three will display "Changed Text"
      System.out.println(obj.myClassVar);
      System.out.println(obj2.myClassVar);
      System.out.println(obj3.myClassVar);
   }
}

public class prg8{
      public static void main(String args[]) {
         int a=5;
   while(a<10)   //while condition
         {
         System.out.println("value of a" +a);
         a++;
   System.out.println("");
         }
    }
}

public class prg9{
    public static void main(String[] args) {
     int a=10;
     int b=5;
  
if(a>b)
      {  // if condition
     System.out.println(" A is greater than B");
      }
else
      {     // else condition
      System.out.println(" B is greater");
      }
}
}

public class prg10{ 
    public static void main(String[] args) {
     int week=2;
     String weeknumber;

switch(week){    // switch case
case 1:
          weeknumber="Monday";
       break;
     
case 2:
          weeknumber="tuesday";
       break;

case 3:
          weeknumber="wednesday";
       break;

default:        // default case
          weeknumber="invalid week";
       break;
     }
  System.out.println(weeknumber);
     } 
}


public class prg11{
      public static void main(String args[]){
          int count=1;
do {                        // do statement
     System.out.println("count is:"+count);
     count++;
   }
 while (count<10);
       }
  }

public class prg12{
      public static void main(String args[]) {
          for(int i=0; i<=10; i++)  // for condition  
          {
          System.out.println(i);
          }
     }
}


import java.util.Scanner;
class prg13{
    public static void main(String[] args) {
    
     Scanner input = new Scanner(System.in);
    
     System.out.print("Enter an integer: ");
     int number = input.nextInt();
     System.out.println("You entered " + number);
    }
}



10 comments:

  1. Programs are very useful....

    ReplyDelete
  2. programs are simple and easy to unterstand

    ReplyDelete
  3. Switch case and other conditions and swapping programs are very useful sir thank you sir..

    ReplyDelete
  4. These are all Working Fine Without Error...

    ReplyDelete
  5. programs are simple and easy to unterstand sir

    ReplyDelete
  6. The simple java programs are really very usful sir

    ReplyDelete
  7. All programs are very nice and very easily understandable sir...

    ReplyDelete
  8. As the same of my first comment I can understand the programs in short span of time sir and too very easy to understand sir even though in books and some websites they will give program but it will be difficult to understand sir and also it can't get exact output sir.But the programs which you gave that is very usefull sir .. thank you sir

    ReplyDelete