Object Oriented Programming-1 (Lab Sesion-2)


 Lab Sesion-2

Basic JAVA Pogramming

1. Write JAVA program that reads a temperature value from the user in Celsius and converts it to Fahrenheit and Kelvin and prints the result.

(Tf = Tc(9/5) + 32 , Tk = Tc + 273.15)

Program Code : 


import java.util.Scanner;

public class Celsius_Fahrenheit {

    public static void main(String[] args){

      double celsius,fahrenheit,kelvin;

      Scanner s = new Scanner(System.in);

      

      System.out.println("Enter temperature in clesius :");

      celsius = s.nextDouble();


      fahrenheit = (celsius * 9/5) +32;

      System.out.println("Temperature in Fahrenheit :"+fahrenheit);

      kelvin = celsius + 273.15;

      System.out.println("Temperature in Kelvin :"+kelvin);

      

    }

}

OUTPUT :






    2. Write a program that takes three numbers (integers) from the user and display the integers decreasing order. (User should give space separated input for three numbers (Ex. 5 15 10). User should not be asked three times to enter the number.)

    Program Code : 


import java.util.Scanner;

public class number {

    public static void main(String[] args){

        int x,y,z;

      System.out.println("Enter three integers :");

      Scanner m = new Scanner(System.in);

      int a = m.nextInt(); 

      int b = m.nextInt(); 

      int c = m.nextInt();  

      if (a > b && a > c && b > c)

      System.out.println( "Decreasing order =" +a + " " + b + " " + c); 

      else if ( b > a && b > c && a > c) 

      System.out.println( "Decreasing order =" +b + " " + a + " " + c); 

      else if ( c > b && c > a && b > a) 

      System.out.println( "Decreasing order =" +c + " " + b + " " + a); 

      else if ( a > c && a > b && c > b) 

      System.out.println( "Decreasing order =" +a + " " + c + " " + b); 

                else if ( b > c && b > a && c > a) 

      System.out.println( "Decreasing order =" +b + " " + c + " " + a); 

      else if ( c > a && c > b && a > b) 

      System.out.println( "Decreasing order =" +c + " " + a + " " + b);    

    }

}

       

        OUTPUT :

                


    3.Write a program that asks the user to enter a letter and check whether a letter is a vowel or constant.

     Program Code:

             import java.util.Scanner;

          public class VowelOrConstant {

        public static void main(String[] args){

            System.out.println("Enter a character :");

            Scanner s = new Scanner(System.in);

            char ch = s.next().charAt(0);

        if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch==' '){

            System.out.println("Given character is a vowel");

        }else{

            System.out.println("Given character is constant");

        }

    }

    }


        OUTPUT :

                


                                    


            

           4.Assume a vehicle plate number consists of three uppercase letters followed by four digits separated by ‘ - ’ (Ex. AGM-1326 ) . Write a program to generate ten plate numbers at a time. Output should display ten number plates in new lines.


          Program Code:


                               import java.util.Random;

                 public class Plat {

                                 public static void main(String[] args){

            for(int i = 0; i<=10; i++){

            int letter1 = 65 + (int)(Math.random() * (90 - 65));

            int letter2 = 65 + (int)(Math.random() * (90 - 65));

            int letter3 = 65 + (int)(Math.random() * (90 - 65));


            //Generate four random digits.

           

            int number1 = (int)(Math.random() * 10);

            int number2 = (int)(Math.random() * 10);

            int number3 = (int)(Math.random() * 10);

            int number4 = (int)(Math.random() * 10);


            //Display number plate

            System.out.println("" + (char)(letter1) + ((char)(letter2))

             + ((char)(letter3)) + "-" + number1 + number2 + number3 + number4);

                    }

            

         }


                           OUTPUT :


                    

            

    5.Java Program to Check Whether a Number is Even or Odd.

    Program Code :

        import java.util.Scanner;


public class EvenOdd {


    public static void main(String[] args) {


        Scanner reader = new Scanner(System.in);


        System.out.print("Enter a number: ");

        int num = reader.nextInt();


        if(num % 2 == 0)

            System.out.println(num + " is even");

        else

            System.out.println(num + " is odd");

    }

}

  

                   OUTPUT :

                                        


                                                





Download assignment doc file here....





1 comment: