Java CSD


There are more available on https://play.google.com/store/apps/details?id=com.master.askmastermaterial

String concatenation

import java. util.*;

class Authority{

    public  static void main (String[] args) {

        String s1,s2,res;

        Scanner sc=new Scanner(System.in);

        System.out.print("Inmate's name:");

        s1=sc.nextLine();

          System.out.print("\nInmate's father's name:");

        s2=sc.nextLine();

        if(!s1.matches("^[a-zA-Z]+[\\sa-zA-Z]*$")||!s2.matches("^[a-zA-Z]+[\\sa-zA-Z]*$"))

        {

            

            System.out.print("\nInvalid name");

            return;

        }

        

        

        

        s1=s1.toUpperCase();

        s2=s2.toUpperCase();

        res=s1.concat(" "+s2);

        //String res=(s1.toUpperCase()).concat(" "+s2.toUpperCase());

        System.out.print("\n"+res);

    }

    

    

    

}

Fuel.consumption code


import java.io.*;

import java.util.*;


class Main

{

    public static void main (String[] args) 

    {

     int x,y;

     double res1=2,res2,a,b;

     

     Scanner sc=new Scanner(System.in);

System.out.println("Enter the no of liters to fill the tank\n");

     x=sc.nextInt();

     if(x<=0)

     {

        System.out.print(x+ " is an Invalid Input");

        return;

     }

     

    System.out.print("Enter the distance covered\n");

     y=sc.nextInt();

     if(y<=0)

     {

        System.out.println(y+" is an Invalid Input");

        return;

     }

     

     //res1=(x*100);

     //res1=res1/y;

    res1=((float)x/y)*100;

      //   System.out.print(res1 +"second");

     a=(y*0.6214);

     b=(x*0.2642);

     res2=a/b;


     System.out.println("Liters/100KM\n");

       System.out.printf("%.2f",(float)res1);

         System.out.println("\nMiles/gallons\n");

          System.out.printf("%.2f",(float)res2);

          //System.out.print(res1 +"third");

        

        

        

        

    }

}


Least offer


import java.util.*;

public class Main{

    public static void main (String[] args) {

     Scanner sc=new Scanner(System.in);

     int i,pre=0,re,n1,n2,pos=0;

     

     //System.out.println("enter num");

     int n=sc.nextInt();

     String out[]=new String[n];

     int out2[]=new int[n];

     for(i=0;i<n;i++)

     {

         

    // System.out.println("enter str"+ i);

     String str=sc.next();

     String res[]=str.split(",",4);

     int k=0;

      String []d=new String[4];

     for(String j:res)

      {

          d[k]=j;

          k++;

          

      } 

     // out[0]=d[0];

      n1=Integer.parseInt(d[1]);

      n2=Integer.parseInt(d[2]);

      re=n1*n2;

      re=re/100;

      

     out[i]=d[0];

     out2[i]=re;

     //System.out.println(pos);

    // System.out.println(out[pos]);

    }

    int min=100007;

    for(i=0;i<n;i++)

    {

        

        if(out2[i]<min)

          min=out2[i];

    }

     for(i=0;i<n;i++)

    {

        if(out2[i]==min)

           System.out.println(out[i]);

   }

    }

}

display characters question


import java.util.*;

class AsciValue

{

    public static void main (String[] args)

    {

        

        int i,a[]=new int[4];

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter the digits:");

        for(i=0;i<4;i++)

        {

            a[i]=sc.nextInt();

            

        }

        char ch;

        for(i=0;i<4;i++){

            

         ch=(char)a[i];

         System.out.print(a[i]+"-"+ch +"\n");

        }

        

    }

    

}






 CONTROL STRUCTURES


1)Print customer details     

     import java.util.Scanner;

     

     public class Customer {

     


      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      System.out.println("Enter your name:");

      String name=sc.nextLine();

      System.out.println("Enter age:");

        int age=sc.nextInt();

        System.out.println("Enter gender:");

        String gender=sc.next();

        sc.nextLine();

        System.out.println("Hailing from:");

        String from=sc.nextLine();

        System.out.println("Welcome, " +name);

        System.out.println("Age:"+age);

        System.out.println("Gender:"+gender);

        System.out.println("City:"+from);

    }

    

    } 


2)Finding range of the number

     import java.util.Scanner;

     

     public class Main{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

            //Fill the code here

            System.out.print("Enter the number, ");

            int number=sc.nextInt();

            if(number>0 && number<=100)

            {

                System.out.printf("Range of the number %d is 0 to 100",number);

            }

            else if(number>100 && number<=200)

            {

                System.out.printf("Range of the number %d is 100 to 200",number);

            }

            else if(number>200 && number<=500)

            {

                System.out.printf("Range of the number %d is 200 to 500",number);

            }

            else

            System.out.printf("Entered number %d is not in the expected range",number);

            

            

            

        }

        

    }


3)Road signalling

    import java.util.Scanner;

    

    public class Main{

        

        public static void main(String[] args){

            

           Scanner sc=new Scanner(System.in);

           

            //Fill the code here

            System.out.println("Enter the color");

            String color=sc.nextLine();

            String a="green";

            String b="red";

            String c="yellow";

            if(a.equals(color))

            {

                System.out.print("go");

            }

            else if(b.equals(color))

            {

                System.out.print("stop");

            }

            else if(c.equals(color))

            {

                System.out.print("proceed with caution");

            }

            else

            {

                System.out.println("prepare to go");

            }

        }

        

    }

4)Electricity bill calculation

     import java.util.Scanner;

     

     public class Main{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

            //Fill the code here

            System.out.println("Enter the units consumed");

            int unit=sc.nextInt();

            if(unit<=20)

                    System.out.print("No charge");

            else if(unit>20&&unit<100)

            {

                System.out.println("You have to pay Rs."+(3.5*unit));

            }

            else{

                System.out.println("You have to pay Rs."+(5.0*unit));

            }

            

            

        }

        

    }


5)Checking budget of mobile

     import java.util.Scanner;

     

     public class Main{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

            //Fill the code here

            System.out.println("Enter the cost of the mobile");

            int cost=sc.nextInt();

            if(cost<=13000)

            {

                System.out.println("Mobile chosen is within the budget");

            }

            else

            System.out.println("Mobile chosen is beyond the budget");

        }

        

    }


6)Finding even number

     import java.util.Scanner;

     

     public class Main{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

            //Fill the code here

            System.out.print("Enter the number");

            int number=sc.nextInt();

            if(number%2==0)

            {

                System.out.printf("%d is an even number", number);

            }

        }

        

    }


7)Registration details

     import java.util.Scanner;

     

     public class RegistrationDetails{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

           System.out.print("Enter your name :");

           String name=sc.nextLine();

           System.out.print("Enter your age :");

           int age=sc.nextInt();

           System.out.print("Enter your phoneno : ");

           long phone=sc.nextLong();

           System.out.print("Enter your qualification :");

           String qualification=sc.next();

           sc.nextLine();

           System.out.print("Enter your email id[Please provide valid id, after registering your registration id will be mailed] :");

           String mail=sc.nextLine();

           System.out.print("Enter your noofexperience[if any] :");

           Float exp=sc.nextFloat();

           System.out.printf("Dear %s, Thanks for registering in our portal, registration id will be mailed to %s within 2 working days",name,mail);

        }

    }


8)Celcius to farenheit conversion

     import java.util.Scanner;

     

     public class CelsiusConversion{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

            //Fill the code here

            double Celsius=sc.nextDouble();

            double farenhit=((9*Celsius)/5)+32;

            System.out.println(farenhit);

            

        }

        

    }


9)Display characters

     import java.util.Scanner;

     

     public class AsciValue{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

            //Fill the code here

            {

                System.out.println("Enter the digits");

                int digit=sc.nextInt();

                int digit1=sc.nextInt();

                int digit2=sc.nextInt();

                int digit3=sc.nextInt();

                System.out.printf("%d-%c\n",digit,digit);

                System.out.printf("%d-%c\n",digit1,digit1);

                System.out.printf("%d-%c\n",digit2,digit2);

                System.out.printf("%d-%c\n",digit3,digit3);

            }

        }

        

    }


10)Bill generation

     import java.util.Scanner;

     

     public class SnacksDetails{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

            //Fill the code here

            System.out.print("Enter the no of pizzas bought:\n");

            int pizzas=sc.nextInt();

            System.out.print("Enter the no of puffs bought:\n");

            int puffs=sc.nextInt();

            System.out.print("Enter the no of cool drinks bought:\n");

            int drinks=sc.nextInt();

            int tot_pizzas=100*pizzas;

            int tot_puffs=20*puffs;

            int tot_drinks=10*drinks;

            int Total=tot_pizzas+tot_drinks+tot_puffs;

            System.out.print("Bill Details\n");

            System.out.printf("No of pizzas:%d\n",pizzas);

            System.out.printf("No of puffs:%d\n",puffs);

            System.out.printf("No of cooldrinks:%d\n",drinks);

            System.out.printf("Total price=%d\n",Total);

            System.out.printf("ENJOY THE SHOW!!!");

        }

        

    }


11)Find season

import java.util.Scanner;

     

     public class Season{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

            //Fill the code here

            System.out.print("Enter the month:");

            int month=sc.nextInt();

            switch(month)

            {

                case 1:System.out.print("Season:Winter");break;

                case 2:System.out.print("Season:Winter");break;

                case 12:System.out.print("Season:Winter");break;

                case 3:System.out.print("Season:Spring");break;

                case 4:System.out.print("Season:Spring");break;

                case 5:System.out.print("Season:Spring");break;

                case 6:System.out.print("Season:Summer");break;

                case 7:System.out.print("Season:Summer");break;

                case 8:System.out.print("Season:Summer");break;

                case 9:System.out.print("Season:Autumn");break;

                case 10:System.out.print("Season:Autumn");break;

                case 11:System.out.print("Season:Autumn");break;

                default:System.out.print("Invalid month");

            }

        }

        

    }


12)check for leap year

     import java.util.Scanner;

     

     public class LeapYear{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

            //Fill the code here

            System.out.println("Enter the Year");

            

            int yr=sc.nextInt();

            

            if(yr>999 && yr<10000)

            {

                if(yr%4==0)

                {

                    if(yr%100==0)

                    {

                        if(yr%400==0)

                        {

                            System.out.println("Leap Year");

                        }

                    else

                    {

                        System.out.println("Not a Leap Year");

                    }

                }

                else

                {

                    System.out.println("Leap Year");

                }

            }

            else

            {

                System.out.println("Not a Leap Year");

            }

        }

       else

       {

           System.out.println("Invalid Year");

       }

       }

    }


13)HIghest placement

     import java.util.Scanner;

     

     public class Placement{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

            //Fill the code here

            System.out.println("Enter the no of students placed in CSE:");

            int a=sc.nextInt();

            System.out.print("Enter the no of students placed in ECE:");

            int b=sc.nextInt();

            System.out.println("Enter the no of students placed in MECH:");

            int c=sc.nextInt();

            if(a<0 || b<0 || c<0)

            {

                System.out.println("Input is Invalid");

            }

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

            {

                System.out.println("None of the department has got the highest placement");

            }

            else if(a==b && c<a)

            {

                System.out.println("Highest placement");

                System.out.println("CSE");

                System.out.println("ECE");

                

            }

            else if(a==c && b<c)

            {

                System.out.println("Highest placement");

                System.out.println("CSE");

                System.out.println("MECH");

                

            }

            else if(b==c && a<c)

            {

                System.out.println("Highest placement");

                System.out.println("ECE");

                System.out.println("MECH");

                

            }

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

            {

                System.out.println("Highest placement \nCSE");

            }

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

            {

                System.out.println("Highest placement \nECE");

            }

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

            {

                System.out.println("Highest placement \nMECH");

            }

        }

        

    }


14)Increment calculation

     import java.util.Scanner;

     

     public class IncrementCalculation{

         

         public static void main(String[] args){

             

            Scanner sc=new Scanner(System.in);

            

            //Fill the code here

            System.out.println("Enter the salary");

            int s=sc.nextInt();

            System.out.println("Enter the performance appraisal rating");

            float r=sc.nextFloat();

            if(s<=0 ||r<1.0||r>5)

            {

                System.out.println("Invalid Input");

            }

            else if(r>=1&&r<=3)

            {

                s=s+(int)(0.1*s);

            System.out.println(s);    

            }

            else if(r>3&&r<=4)

            {

                s=s+(int)(0.25*s);

            System.out.println(s);    

            }

            else if(r>4&&r<=5)

            {

                s=s+(int)(0.3*s);

            System.out.println(s);    

            }

        }

        

    }


ITERATIONS


1)Display string

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int i;

      System.out.println("Enter the String");

    String name=sc.nextLine();

    System.out.println("Enter the number");

    int num=sc.nextInt();

    if(num>1)

    {

        for(i=1;i<=num;i++)

        {

            System.out.printf("%s\n",name);

        }

    }

    else

    System.out.printf("%d is not a valid input",num);

    }

    

    }


2)Reverse the number

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int a,rev=0;

      System.out.print("Enter the number");

    int number=sc.nextInt();

    while(number!=0)

    {

        a=number%10;

        rev=rev*10+a;

        number=number/10;

    }

    System.out.printf("%d", rev);

    }

    

    }


3)Prime factors

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int n=sc.nextInt();

      for(int i=2;i<n;i++)

    {

        while(n%i==0)

        {

            System.out.print(i+" ");

            n=n/i;

        }

    }

    if(n>2)

    System.out.print(n+" ");

    }

    

    }


4)Count of prime number

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int count=0;

      System.out.println("Enter starting range");

    int start=sc.nextInt();

    System.out.println("Enter ending range");

    int end=sc.nextInt();

    while(start<end)

    {

        boolean flag = false;

        for(int i = 2;i<=start/2; ++i)

        {

            if(start%i==0)

            {

                flag=true;

                break;

            }

        }

        if(!flag && start!=0 && start!=1)

        count++;

        ++start;

    }

    System.out.println(count);

   

    }

    

    }


5)Palindrome number

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int a,rev=0,i,n;

      System.out.println("Enter the starting range");

    int s=sc.nextInt();

    System.out.println("Enter the ending range");

    int e=sc.nextInt();

   

    for(i=s;i<=e;i++)

    {

        n=i;

        rev=0;

         while(n!=0)

         {

             a=n%10;

             rev=rev*10+a;

             n=n/10;

         }

         if(rev==i)

         {

             System.out.printf("%d ",i);

         }

    }

    }

    

    }


6)Display month/day

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      System.out.println("1.Display the Months");

      System.out.println("2.Display the Days");

    System.out.println("3.Exit");

    Ip:while(true)

    {

        int c=sc.nextInt();

        switch(c)

        {

            case 1:

                System.out.println("1.January");

                System.out.println("2.February");

                System.out.println("3.March");

                System.out.println("4.April");

                System.out.println("5.May");

                System.out.println("6.June");

                System.out.println("7.July");

                System.out.println("8.August");

                System.out.println("9.September");

                System.out.println("10.October");

                System.out.println("11.November");

                System.out.println("12.December");

                System.out.println("1.Display the Months");

                System.out.println("2.Display the Days");

                System.out.println("3.Exit");

                break;

           case 2:

                System.out.println("1.Monday");

                System.out.println("2.Tuesday");

                System.out.println("3.Wednesday");

                System.out.println("4.Thursday");

                System.out.println("5.Friday");

                System.out.println("6.Saturday");

                System.out.println("7.Sunday");

                System.out.println("1.Display the Months");

                System.out.println("2.Display the Days");

                System.out.println("3.Exit");

                break;

           case 3:

                break Ip;

        }

    }

    }

    

    }


7)Power of a number

     import java.util.Scanner;

     

     public class Power {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      System.out.println("Enter the number");

      int n=sc.nextInt();

    System.out.println("Enter the digit");

    int d=sc.nextInt();

    if(n<0||d<0)

    {

        System.out.println("Invalid input");

        System.exit(0);

    }

    long r=1;

    while(d!=0)

    {

        r*=n;

        --d;

    }

    System.out.println(r);

    }

    

    }


8)Factors of a number

     import java.util.Scanner;

     

     public class FindFactor {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int num=Math.abs(sc.nextInt());

      if(num==0)

    {

        System.out.println("No Factors");

    }

    else

    {

        System.out.println("1");

        for(int i=2;i<=num;i++)

        {

            if(num%i==0)

            {

                System.out.print(", "+i);

            }

        }

    }

   

    }

    

    }


9)Lucky number

     import java.util.Scanner;

     

     public class LuckyNum {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      System.out.println("Enter the car no:");

      int num=sc.nextInt();

    if(num<1000 || num>9999)

    {

        System.out.println(num+" is not a valid car number");

    }

    else

    {

        int sum=0;

        while(num>0)

        {

            sum+=num%10;

            num/=10;

        }

        if(sum%3==0 || sum%5==0 || sum%7==0)

        {

            System.out.println("Lucky Number");

        }

        else{

            System.out.println("Sorry its not my lucky number");

        }

    }

    }

    

    }


10)Number palindrome

     import java.util.Scanner;

     

     public class Palindrome {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int i=sc.nextInt();

     

    int n,rev=0;

    int temp=i;

    if(temp>=0)

    {

        while(i!=0)

        {

            n=i%10;

            rev=rev*10+n;

            i=i/10;

        }

        if(temp==rev)

        {

            System.out.println("Palindrome");

        }

        else

        System.out.println("Not a Palindrome");

    }

    else

    System.out.println("Invalid Input");

    }

    

    }


11)Find number for given factorial

     import java.util.Scanner;

     

     public class FindNumber {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int fact=1,i,k=0;

      int n=sc.nextInt();

    if(n>0){

    for(i=1;i<=n;i++)

    {

        if(n%i==0)

        {

            n=n/i;

            k++;

        }

        else

        break;

    }

    if(n==1)

    {

        System.out.println(k);

    }

    else

    {

        System.out.println("Sorry,The given number is not a perfect factorial");

    }

   

    }else if(n<=0)

    System.out.println("Invalid Input");

    }

    

    }


12)List of prime numbers

     import java.util.Scanner;

     

     public class PrimeNumbers {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int a=sc.nextInt();

      int b=sc.nextInt();

    int flag;

    if(a<=0||b<=0||a>=b)

    {

        System.out.println("Provide valid input");

    }

    else

    {

        while(a<=b)

        {

            if(a==2)

            {

                System.out.println(a+" ");

            }

            else if(a==1)

            {

                a++;

                continue;

            }

            else

            {

                flag=0;

                for(int i=2;i<=a/2;i++)

                {

                    if(a%i==0)

                    {

                        flag=1;

                        break;

                    }

                }

                if(flag==0)

                {

                    System.out.println(a+" ");

                }

            }

            a++;

        }

    }

     }    

     }

CLASSES AND OBJECTS

1)Book details

testbook.java

     import java.util.Scanner;

     

     public class TestBook {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      System.out.println("Enter the Book name:");

      String bookname=sc.nextLine();

      System.out.println("Enter the price:");

    int price=sc.nextInt();

    System.out.println("Enter the Author name:");

    String authorname=sc.nextLine();

    Book obj=new Book();

    obj.setBookName (bookname);

    obj.setBookPrice(price);

    obj.setAuthorName(authorname);

    System.out.println("Book Details");

    System.out.println("Book Name :"+obj.getBookName());

    System.out.println("Book Price :"+obj.getBookPrice());

    System.out.println("Author Name :"+obj.getAuthorName());

        // Fill the code here

    }

    

    }

book.java

     public class Book {

     

         // Fill the code here

         private String bookName;

         private int bookPrice;

         private String authorName;

         

         public void setBookName(String bookName)

        {

            this.bookName=bookName;

        }

        public String getBookName()

        {

            return this.bookName;

        }

        public void setBookPrice(int bookPrice)

        {

            this.bookPrice=bookPrice;

        }

        public int getBookPrice()

        {

            return this.bookPrice;

        }

        public void setAuthorName(String authorName)

        {

            this.authorName=authorName;

        }

        public String getAuthorName()

        {

            return this.authorName;

        }

    

    

    }


2)Student details - constructor

student.java

     public class Student {

     

         // Fill the code here

         private int studentId;

         private String studentName, studentAddress, collegeName;

         public Student(int studentId, String studentName, String studentAddress)

         {

             this.studentAddress=studentAddress;

            this.studentName=studentName;

            this.studentId=studentId;

            this.collegeName="NIT";

        }

        public Student(int studentId, String studentName, String studentAddress, String collegeName)

        {

            this(studentId, studentName, studentAddress);

            this.collegeName=collegeName;

        }

        public int getStudentId()

        {

            return this.studentId;

        }

        public String getStudentName()

        {

            return this.studentName;

        }

        public String getStudentAddress()

        {

            return this.studentAddress;

        }

        public String getCollegeName()

        {

            return this.collegeName;

        }

    

    

    }

studentmain.java

     import java.util.Scanner;

     

     public class StudentMain {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code here

      }

     

    }


3)Volume calculator - over loading

testmain.java

     import java.util.Scanner;

     

     public class TestMain {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code here

      double rad, h1;

      int len, br,h2;

    System.out.println("Enter the choice\n1.Find Volume for Cylinder\n2.Find Volume for Cuboid");

    int choice = sc.nextInt();

    sc.nextLine();

    switch(choice){

        case 1:

            VolumeCalculator cylinder = new VolumeCalculator();

            System.out.println("Enter the radius");

            rad = sc.nextDouble();

            sc.nextLine();

            System.out.println("Enter the height");

            h1 = sc.nextDouble();

            System.out.println("Volume of the Cylinder is "+ cylinder.calculateVolume(rad,h1));

            break;

        case 2:

            VolumeCalculator cuboid = new VolumeCalculator();

            System.out.println("Enter the length");

            len = sc.nextInt();

            sc.nextLine();

            System.out.println("Enter the breadth");

            br = sc.nextInt();

            sc.nextLine();

            System.out.println("Enter the height");

            h2 = sc.nextInt();

            System.out.println("Volume of the Cuboid is "+cuboid.calculateVolume(len,br,h2));

            break;

    }

    }

    

    }

volumecalculator.java

    public class VolumeCalculator {

     

         // Fill the code here

         public double calculateVolume(double radius, double height){

             return (3.14*radius*radius*height);

         }

         public double calculateVolume(int length, int breadth, int height){

             return (length*breadth*height);

        }

    

    

    }


4)Ticket price calculation - static

main.java

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code here

      Ticket obj=new Ticket();

     

    System.out.println("Enter no of bookings:");

    int no_bookings=sc.nextInt();

   

    System.out.println("Enter the available tickets:");

    obj.setAvailableTickets(sc.nextInt());

   

    while(no_bookings>0)

    {

        System.out.println("Enter the ticketid:");

        obj.setTicketid(sc.nextInt());

        

        System.out.println("Enter the price:");

        obj.setPrice(sc.nextInt());

        

        System.out.println("Enter the no of tickets:");

        int no_tickets=sc.nextInt();

        if(obj.calculateTicketCost(no_tickets)==-1)

        {

            continue;

        }

        System.out.println("Available tickets: "+obj.getAvailableTickets());

        System.out.println("Total amount:"+obj.calculateTicketCost(no_tickets));

        System.out.println("Available ticket after booking:"+obj.getAvailableTickets());

        no_bookings--;

    }

    }

    

    }

ticket.java

   public class Ticket {

     

         // Fill the code here

         private int ticketid;

         private int price;

         private static int availableTickets;

         

         public void setTicketid(int ticketid)

        {

            this.ticketid=ticketid;

        }

        public int getTicketid()

        {

            return this.ticketid;

        }

        public void setPrice(int price)

        {

            this.price=price;

        }

        public int getPrice()

        {

            return this.price;

        }

        public static void setAvailableTickets(int availableTickets1)

        {

            if(availableTickets1>=0)

            {

                availableTickets=availableTickets1;

            }

        }    

            public static int getAvailableTickets()

            {

                return availableTickets;

            }

        

    public int calculateTicketCost(int nooftickets)

    {

            if(availableTickets>=nooftickets)

            {

                availableTickets-=nooftickets;

                return nooftickets*this.price;

            }

            else

            {

                return -1;

            }

            

    }

    }


5)Employee salary calculation

main.java

     import java.util.Scanner;

     

     public class Main {

     

      public static Employee getEmployeeDetails(){

      Scanner sc=new Scanner(System.in);

      // Fill the code here

      Employee a = new Employee();

      System.out.println("Enter Id:");

    a.setEmployeeId(sc.nextInt());

    sc.nextLine();

    System.out.println("Enter Name:");

    a.setEmployeeName(sc.nextLine());

    System.out.println("Enter salary:");

    a.setSalary(sc.nextDouble());

    return a;

    }

    public static int getPFPercentage(){

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter PF percentage:");

        int v=sc.nextInt();

        return v;

    }

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        Employee a=getEmployeeDetails();

        int temp=getPFPercentage();

        a.calculateNetSalary(temp);

        System.out.println("Id:"+a.getEmployeeId());

        System.out.println("Name: "+a.getEmployeeName());

        System.out.println("Salary: "+a.getSalary());

        System.out.println("Net Salary: "+a.getNetSalary());

    }

    

    }

employee.java

   public class Employee {

     

         // Fill the code here

         private int employeeId;

         private String employeeName;

         private double salary,netSalary;

         

     public void setEmployeeId(int id){

        this.employeeId=id;

    } 

    public void setEmployeeName(String name){

        this.employeeName=name;

    }

    public void setSalary(double sal){

        this.salary=sal;

    }

    public void setNetSalary(double nsal){

        this.netSalary=nsal;

    }

    

    public int getEmployeeId(){

        return this.employeeId;}

        public String getEmployeeName(){

            return this.employeeName;

        }

        public double getSalary(){

            return this.salary;

        }

        public double getNetSalary(){

            return this.netSalary;

        }

        public void calculateNetSalary(int pfpercentage){

            double temp = (pfpercentage/100.00);

            setNetSalary(salary-(salary*temp));

        }

    }


6)Travel details

testmain.java

    import java.util.Scanner;

     

     public class TestMain {

         public static BusTicket getTicketDetails()

      {

      Scanner sc=new Scanner(System.in);

      BusTicket b=new BusTicket();

      System.out.println("Enter the ticket no:");

      b.setTicketNo(sc.nextInt());

    sc.nextLine();

    System.out.println("Enter the ticket price:");

    b.setTicketPrice(sc.nextFloat());

    return b;

    }

   

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        Person person = new Person();

        System.out.println("Enter the passenger name:");

        person.setName(sc.nextLine());

        System.out.println("Enter the gender(M or F / m or f)");

        person.setGender(sc.next().charAt(0));

        System.out.println("Enter the age:");

        person.setAge(sc.nextInt());

        sc.nextLine();

        BusTicket b=getTicketDetails();

        b.calculateTotal();

        System.out.println("Ticket no:"+b.getTicketNo());

        System.out.println("Passenger Name:"+person.getName());

        System.out.println("Price of a ticket : "+b.getTicketPrice());

        System.out.println("Total Amount : "+b.getTotalAmount());

    }

    

    }

BusTicket.java

      public class BusTicket {

         

          // Fill the code here

          private int ticketNo;

          private float ticketPrice;

          private float totalAmount;

          private Person person = new Person();

          public void setTicketNo(int ticketno)

          {

            this.ticketNo=ticketno;

        }

        public void setTicketPrice(float totalprice)

        {

            this.ticketPrice=totalprice;

        }

        public void setTotalAmount(float totalamount)

        {

            this.totalAmount=totalamount;

        }

        public void setPerson(Person p)

        {

            this.person = p;

        }

        public int getTicketNo()

        {

            return this.ticketNo;

        }

        public float getTicketPrice()

        {

            return this.ticketPrice;

        }

        public float getTotalAmount()

        {

            return this.totalAmount;

        }

        public Person getPerson()

        {

            return this.person;

        }

        public void calculateTotal()

        {

            if(person.getAge()<16)

            {

                setTotalAmount(ticketPrice/2);

            }

            else if(person.getAge()>=60)

            {

                setTotalAmount((ticketPrice*75)/100);

            }

            else if(person.getGender()=='F' || person.getGender()=='f')

            {

                setTotalAmount((ticketPrice*90)/100);

            }

            else

            {

                setTotalAmount(ticketPrice);

            }

        }

    }

Person.java

     public class Person {

         

          // Fill the code here

          private String name;

          private char gender;

          private int age;

          public void setName(String name)

          {

              this.name=name;

        }

        public void setGender(char g)

        {

            this.gender=g;;

        }

        public void setAge(int n)

        {

            this.age=n;

        }

        public String getName()

        {

            return this.name;

        }

        public char getGender()

        {

            return this.gender;

        }

        public int getAge()

        {

            return this.age;

        }

    }

7)Bank account details

    import java.util.Scanner;

     public class AccountDetails{

         

         // Fill the code here

         public static Account getAccountDetails()

         {

             Account a=new Account();

             Scanner sc=new Scanner(System.in);

             System.out.println("Enter account id: ");

            a.setAccountId(sc.nextInt());

            sc.nextLine();

            System.out.println("Enter account type: ");

            a.setAccountType(sc.nextLine());

            int b;

            do{

                System.out.println("Enter Balance");

                a.setBalance(sc.nextInt());

                b=a.getBalance();

                if(b<=0)

                System.out.println("Balance should be positive");

            }

            while(b<=0);

            return a;

        }

        public static int getWithdrawAmount()

        {

            Scanner sc=new Scanner(System.in);

            int w;

            do{

                System.out.println("Enter amount  to be withdrawn:");

                w=sc.nextInt();

                if(w<=0)

                System.out.println("Amount should be positive");

            }

            while(w<=0);

            return w;

            

        }

        public static void main(String[] args)

        {

            Account aObj=new Account();

            aObj=getAccountDetails();

            int c=getWithdrawAmount();

            aObj.withdraw(c);

        }

        

    }

Account.java

     public class Account{

         

         // Fill the code here

         private int accountId;

         private String accountType;

         private int balance;

         public int getAccountId()

         {

             return accountId;

        }

        public String getAccountType()

        {

            return accountType;

        }

        public int getBalance()

        {

            return balance;

        }

        public void setAccountId(int id)

        {

            accountId=id;

        }

        public void setAccountType(String s)

        {

            accountType=s;

        }

        public void setBalance(int b)

        {

            balance=b;

        }

       

        public boolean withdraw(int w)

        {

            if(getBalance()<w)

            {

                System.out.println("Sorry!!! No enough balance");

                return false;

            }

            else

            {

                System.out.println("Balance amount after withdraw: "+(getBalance()-w));

                return true;

            }

        }

        

    }

8)Movie ticket calulation

    import java.util.*;

    public class Main

    {

    public static Movie getMovieDetails()

    {

        Movie mov=new Movie();

        Scanner s=new Scanner(System.in);

        System.out.println("Enter the movie name:");

        mov.setMovieName(s.nextLine());

        System.out.println("Enter the movie category:");

        mov.setMovieCategory(s.nextLine());

        return mov;

    }

    public static String getCircle()

    {

        Scanner s=new Scanner(System.in);

        System.out.println("Enter the circle:");

        return(s.nextLine());

    }

    public static void main(String[] args)

    {

        Scanner s=new Scanner(System.in);

        Movie m=getMovieDetails();

        String circle=getCircle();

        int type=m.calculateTicketCost(circle);

        System.out.println("Movie name = "+m.getMovieName());

        System.out.println("Movie category = "+m.getMovieCategory());

        if(type==0)

        System.out.println("The ticket cost is = "+m.getTicketCost());

        if(type==-1)

        System.out.println("Sorry there is no "+m.getMovieCategory()+" type of category in theater.");

        if(type==-2)

        System.out.println("Sorry!!! Circle is Invalid.");

        if(type==-3)

        System.out.println("Sorry!!! Both circle and category are Invalid.");

    }

        

        // Fill the code here

        

    }

Movie.java 

     public class Movie{

         private String movieName,movieCategory;

         private int ticketCost;

         public void setMovieName(String s){

             movieName=s;

         }

         public void setMovieCategory(String s)

         {

            movieCategory=s;

        }

        public void setTicketCost(int i)

        {

            ticketCost=i;

        }

        public String getMovieName()

        {

            return movieName;

        }

        public String getMovieCategory()

        {

            return movieCategory;

        }

        public int getTicketCost()

        {

            return ticketCost;

        }

        public int calculateTicketCost(String circle)

        {

            String cat=getMovieCategory();

            if(circle.equalsIgnoreCase("GOLD")&& cat.equalsIgnoreCase("2d")){

                setTicketCost(300);

                return 0;

            }

            else if(circle.equalsIgnoreCase("GOLD")&& cat.equalsIgnoreCase("3d"))

            {

                setTicketCost(500);

                return 0;

            }

            else if(circle.equalsIgnoreCase("SILVER")&&cat.equalsIgnoreCase("2d"))

            {

                setTicketCost(250);

                return 0;

            }

            else if(circle.equalsIgnoreCase("SILVER")&&cat.equalsIgnoreCase("3d"))

            {

                setTicketCost(450);

                return 0;

            }

            else if(circle.equalsIgnoreCase("GOLD")||circle.equalsIgnoreCase("SILVER"))

            {

                return -1;

            }

            else if(cat.equalsIgnoreCase("2d")||cat.equalsIgnoreCase("3d"))

            {

                return -2;

            }

            return -3;

        }

   }

9)Average and grade calculation

    public class Student{

         private int id;

         private String name;

         private int marks[];

         private float average;

         private char grade;

         public Student(int a,String b,int[] c)

         {

            id=a;

            name=b;

            marks=c;

        }

        public void setId(int n)

        {

            id=n;

        }

        public int getId()

        {

            return id;

        }

        public void setMarks(int[] marks)

        {

            this.marks=marks;

        }

        public int[] getMarks()

        {

            return marks;

        }

        public void setName(String n)

        {

            name=n;

        }

        public String getName()

        {

            return name;

        }

        public void setAverage(float n)

        {

            average=n;

        }

        public float getAverage()

        {

            return average;

        }

        public void setGrade(char n)

        {

            grade=n;

        }

        public char getGrade()

        {

            return grade;

        }

        public void calculateAvg()

        {

            float a1=0.0F;

            for(int a=0;a<this.getMarks().length;a++)

            {

                a1=a1+this.marks[a];

            }

            this.setAverage(a1/getMarks().length);

        }

        public void findGrade()

        {

            int min=this.marks[0];

            for(int b=0;b<this.getMarks().length;b++)

            {

                if(this.marks[b]<min)

                {

                    min=this.marks[b];

                }

                if(min<50)

                {

                    this.setGrade('F');

                }

                else if(this.getAverage()>=80 && this.getAverage()<=100)

                {

                    this.setGrade('O');

                }

                else{

                    this.setGrade('A');

                }

            }

        }

       

        // Fill the code here

        

    }

StudentMain.java

     import java.util.*;

     public class StudentMain{

         

         // Fill the code here

         public static void main(String[] args)

         {

             Student s=getStudentDetails();

             s.calculateAvg();

             System.out.println("Id:" +s.getId());

            System.out.println("Name:" +s.getName());

            System.out.println("Average:"+String.format("%.2f",s.getAverage()));

            System.out.println("Grade:" +s.getGrade());

        }

        public static Student getStudentDetails()

        {

            Scanner sc=new Scanner(System.in);

            System.out.println("Enter the id:");

            int id=Integer.parseInt(sc.nextLine());

            String name=sc.nextLine();

            System.out.println("Enter the no of subjects");

            int n=sc.nextInt();

            if(n<=0)

            {

                while(n<=0)

                {

                    System.out.println("Invalid number of subjects");

                    System.out.println("Enter the no of subjects");

                    n=sc.nextInt();

                }

            }

            int arr[]=new int[n];

            for(int a=0;a<n;a++)

            {

                System.out.println("Enter mark for subject "+(a+1)+":");

                int b=sc.nextInt();

                if(b<0||b>100)

                {

                    System.out.println("Invalid Mark");

                    System.out.println("Enter mark for subject "+(a+1)+":");

                    b=sc.nextInt();

                }

                arr[a]=b;

            }

            Student obj=new Student(id,name,arr);

            obj.setId(id);

            obj.setName(name);

            return obj;

            

        }

        

    }

10)Student and department detail

   import java.util.*;

     public class TestMain {

     

         // Fill the code here

         public static Department d1;

         public static Student s1;

         public static Student createStudent()

         {

             Scanner sc=new Scanner(System.in);

            s1 = new Student();

            d1 = new Department();

            System.out.println("Enter the Department id:");

            int did = sc.nextInt();

            sc.nextLine();

            System.out.println("Enter the Department name:");

            String dname=sc.nextLine();

            System.out.println("Enter the Student id:");

            int sid = sc.nextInt();

            sc.nextLine();

            System.out.println("Enter the Student name:");

            String sname=sc.nextLine();

            d1.setDid(did);

            d1.setDname(dname);

            s1.setSid(sid);

            s1.setSname(sname);

            s1.setDepartment(d1);

            return s1;

        }

        public static void main(String args[])

        {

            s1=TestMain.createStudent();

            System.out.println("Department id:"+d1.getDid());

            System.out.println("Department name:"+d1.getDname());

            System.out.println("Student id:"+s1.getSid());

            System.out.println("Student name:"+s1.getSname());

        }

    

    

    } 

Student.java

     public class Student {

     private int sid;

     private String sname;

     private Department department;

     public void setSid(int sid)

     {

         this.sid=sid;

     }

    public int getSid()

    {

        return sid;

    }

    public void setSname(String sname)

    {

        this.sname=sname;

    }

    public String getSname()

    {

        return sname;

    }

    public void setDepartment(Department department)

    {

        this.department=department;

    }

    public Department getDepartment()

    {

        return department;

    }

    

        // Fill the code here

    

    

    } 

Department.java

    public class Department {

        private String dname;

        private int did;

        public void setDid(int did)

        {

            this.did=did;

        }

        public int getDid()

        {

            return did;

        }

        public void setDname(String dname)

        {

            this.dname=dname;

        }

        public String getDname()

        {

            return dname;

        }

    

        // Fill the code here

    }


ARRAYS

1)Display array values

     import java.util.Scanner;

     

     public class Main

     {

         public static void main(String args[])

         {

             int i;

             Scanner sc=new Scanner(System.in);

            System.out.println("Enter the array size");

            int a=sc.nextInt();

            int arr[]=new int[a];

            System.out.println("Enter the values");

            for(i=0;i<a;i++)

            {

                

                arr[i]=sc.nextInt();

                

            }

            for(i=0;i<a;i++)

            {

                System.out.println(arr[i]);

            }

            

            //Fill the code here

        }

    }


2)Display array values in reverse order

     import java.util.Scanner;

     

     public class Main

     {

         public static void main(String args[])

         {

             

             Scanner sc=new Scanner(System.in);

            int i;

            System.out.println("Enter the array size");

            int size=sc.nextInt();

            int arr[]=new int[size];

            System.out.println("Enter the values");

            for(i=0;i<size;i++)

            {

                arr[i]=sc.nextInt();

            }

            for(i=size-1;i>=0;i--)

            {

                System.out.println(arr[i]);

            }

            

            //Fill the code here

        }

    }


3)FInd & display the position of a number

     import java.util.Scanner;

     

     public class Main

     {

         public static void main(String args[])

         {

             

             Scanner sc=new Scanner(System.in);

            int i,j;

            System.out.println("Enter the array size");

            int size=sc.nextInt();

            int arr[]=new int[size];

            System.out.println("Enter the values");

            for(i=0;i<size;i++)

            {

                arr[i]=sc.nextInt();

            }

            System.out.println("Enter the number to find");

            int n=sc.nextInt();

            int k=0;

            for(j=0;j<size;j++)

            {

                

                if(arr[j]==n)

                {

                    k=1;

                    break;

                }

                else 

                k=0;

            }

            if(k==1)

            {

                System.out.println(j+1);

            }

            else

            System.out.println("0");

            

            //Fill the code here

        }

    }


4)Sort the values

     import java.util.Arrays;

     import java.util.Scanner;

     

     public class Main

     {

         public static void main(String args[])

         {

             

             Scanner sc=new Scanner(System.in);

            int i,j;

            System.out.println("Enter the array size");

            int size=sc.nextInt();

            int arr[]=new int[size];

            System.out.println("Enter the values");

            for(i=0;i<size;i++)

            {

                arr[i]=sc.nextInt();

            }

        Arrays.sort(arr);

        for(j=0;j<size;j++)

        {

            System.out.println(arr[j]);

        }

            

            //Fill the code here

        }

    }


5)Sort the values in descinding order

     import java.util.Arrays;

     import java.util.Collections;

     import java.util.Scanner;

     

     public class Main

     {

         public static void main(String args[])

         {

             

            Scanner sc=new Scanner(System.in);

            System.out.println("Enter the array size");

            int size=sc.nextInt();

            int arr[]=new int[size];

            System.out.println("Enter the values");

            for(int i=0;i<size;i++)

            {

                arr[i]=sc.nextInt();

            }

            Arrays.sort(arr);

            for(int j=size-1;j>=0;j--)

            {

                System.out.println(arr[j]);

            }

            

            //Fill the code here

       }

    }


6)Find the player details

        //Don't change the code


        public class Player {

     

      private int playerId;

      private String playerName;

      private int age;

      private long phoneNumber;

    public int getPlayerId() {

    return playerId;

    }

    public void setPlayerId(int playerId) {

    this.playerId = playerId;

    }

    public String getPlayerName() {

    return playerName;

    }

    public void setPlayerName(String playerName) {

    this.playerName = playerName;

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    this.age = age;

    }

    public long getPhoneNumber() {

    return phoneNumber;

    }

    public void setPhoneNumber(long phoneNumber) {

    this.phoneNumber = phoneNumber;

    }

    public Player(int playerId, String playerName, int age, long phoneNumber) {

    super();

    this.playerId = playerId;

    this.playerName = playerName;

    this.age = age;

    this.phoneNumber = phoneNumber;

    }

   

   

   

   

    }

    

PlayerUtility.java

     

     public class PlayerUtility {

     

      public static Player findPlayerDetailsById(Player[] arr, int playerIdToSearch){

     

      //Fill the code here

      int flag=0;

      for(int i=0;i<arr.length;i++)

      {

        if(arr[i].getPlayerId()==playerIdToSearch)

        {

            flag=1;

            return arr[i];

        }

    }

    return null;

   

    }

   

   

    }

    

Main.java

     import java.util.*;

     public class Main {

      public static void main(String[] args) {

     

      Player[] pObj=new Player[4];

      pObj[0]=new Player(146,"Athul",16,9876543210l);

      pObj[1]=new Player(135,"Rakshana",16,9956231045l);

      pObj[2]=new Player(168,"Christy",16,9856471230l);

      pObj[3]=new Player(194,"Athulya",16,9768541230l);

   

    // Invoke the method findPlayerDetailsById in the PlayerUtility class and display results

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter the id to be searched");

    int pid= sc.nextInt();

    sc.nextLine();

    PlayerUtility.findPlayerDetailsById(pObj,pid);

    Player p1=PlayerUtility.findPlayerDetailsById(pObj,pid);

    if(PlayerUtility.findPlayerDetailsById(pObj,pid)==null)

    {

        System.out.println("No player found");

    }

    else

    {

    System.out.println("Name:"+p1.getPlayerName());

    System.out.println("Phone number:"+p1.getPhoneNumber());

    }

    }

   

    }


7)Pass and fail count

     import java.util.Scanner;

     

     public class Count {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int i,pass=0,fail=0;

      System.out.println("Enter the no of subjects:");

    int size=sc.nextInt();

    if(size>0 && size<=20)

    {

        int arr[]=new int[size];

        for(i=0;i<size;i++)

        {

            arr[i]=sc.nextInt();

        }

        for(i=0;i<size;i++)

        {

            if(arr[i]<50)

            {

                fail++;

            }

            else

            pass++;

        }

        if(fail==0)

        {

            System.out.println("Ram passed in all subjects");

        }

        else if(pass==0)

        {

            System.out.println("Ram failed in all subjects");

        }

        else

        {

            System.out.printf("Ram passed in %d subjects and failed in %d subjects",pass,fail);

        }

    }

    else

    System.out.println("Invalid input range");

    }

    

    }


8)Search a course

     import java.util.Scanner;

     import java.util.*;

     

     public class Course {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int i;

    String s;

    System.out.println("Enter no of course");

    int size=sc.nextInt();

    String arr[]=new String[size];

    if(size>0 && size<=20)

    {

        System.out.println("Enter course names");

        for(i=0;i<size;i++)

        {

            arr[i]=sc.next();

        }

        System.out.println("Enter the course to be searched");

        s=sc.next();

        int k=0;

        for(i=0;i<size;i++)

        {

            if(s.equals(arr[i]))

            {

                k++;

            }

        

            

            

        }

        if(k!=0)

        {

            System.out.println(s+" course is available");

        }

        else

        {

            System.out.println(s+" course is not available");

        }

    }

    else

    System.out.println("Invalid Range");

    }

    

    }


9)Sum of the maximum and the minimum element

     import java.util.Scanner;

     import java.util.Arrays;

     public class Sum {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int i,c;

      System.out.println("Enter the size of an array");

    int size=sc.nextInt();

    if(size>0)

    {

        int arr[]=new int[size];

        System.out.println("Enter the elements");

        for(i=0;i<size;i++)

        {

            arr[i]=sc.nextInt();

        }

        Arrays.sort(arr);

        c=arr[0]+arr[size-1];

        System.out.println(c);

        

        

        

    }

    else

    {

        System.out.println("Invalid Array Size");

    }

    }

    

    }


10)Cumulative sum in an array

     import java.util.Scanner;

     

     public class CumulativeSum {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int i,c=0;

    System.out.println("Enter the number of elements");

    int size=sc.nextInt();

    if(size>0)

    {

        int arr[]=new int[size];

        System.out.println("Enter the elements");

        for(i=0;i<size;i++)

        {

            arr[i]=sc.nextInt();

            

            

        }

        for(i=0;i<size;i++)

        {

            c=  c+arr[i];

            System.out.print(c+" ");

        }

        

    }

    else

    System.out.println("Invalid Range");

    }

    

    }


11)Array compatiblitty

     import java.util.Scanner;

     

     public class CompatibleArrays {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      int i,j,k=0;

      System.out.println("Enter the size for First array:");

    int size1=sc.nextInt();

    if(size1>0)

    {

        int arr1[]=new int[size1];

        System.out.println("Enter the elements for First array:");

        for(i=0;i<size1;i++)

        {

            arr1[i]=sc.nextInt();

        }

        

        System.out.println("Enter the size for Second array:");

        int size2=sc.nextInt();

        if(size2>0)

        {

            int arr2[]=new int[size2];

            System.out.println("Enter the elements for Second array:");

            for(j=0;j<size2;j++)

            {

                arr2[j]=sc.nextInt();

            }

            if(size1==size2)

            {

                for(i=0;i<size1;i++)

                {

                    if(arr1[i]>=arr2[i])

                    {

                        k++;

                    }

                }

                if(k==size1)

                {

                    System.out.println("Arrays are Compatible");

                }

                else

                {

                    System.out.println("Arrays are Not Compatible");

                }

            }

            else

            {

                System.out.println("Arrays are Not  Compatible ");

            }

        }

        else

        {

            System.out.println("Invalid array size");

        }

    }

    else{

        System.out.println("Invalid array size");

    }

    }

    }


12)Sort the first and second half of an array

     import java.util.Scanner;

     import java.util.Arrays;

     public class ArraySort {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      System.out.println("Enter the size of an array:");

      int n=sc.nextInt();

    if(n>0)

    {

        int[] arr=new int[n];

        System.out.println("Enter the elements:");

        for(int i=0;i<n;i++)

        {

            arr[i]=sc.nextInt();

        }

        int[] arr1=Arrays.copyOfRange(arr,0,(n+1)/2);

        int[] arr2=Arrays.copyOfRange(arr,(n+1)/2,n);

        Arrays.sort(arr1);

        for(int i=0;i<(n/2);i++)

        {

            for(int j=0;j<(n/2)-1;j++)

            {

                if(arr2[j]<arr2[j+1])

                {

                    int temp=arr2[j];

                    arr2[j]=arr2[j+1];

                    arr2[j+1]=temp;

                }

            }

        }

        int len2=arr1.length+arr2.length;

        int[] res=new int[len2];

        System.arraycopy(arr1, 0, res, 0, (n+1)/2);

        System.arraycopy(arr2, 0, res, (n+1)/2, n/2);

        for(int i:res)

        {

            System.out.println(i);

        }

    }

    else{

        System.out.println("Array size should be greater than 0");

    }

    }

    }


13)Highest mark in each semester

     import java.util.Scanner;

     

     public class HighestMarkPerSem {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      System.out.println("Enter no of semester:");

    int sems=sc.nextInt();

    boolean incorrect = false;

    int arr[]=new int[sems];

    for(int i=0;i<sems;i++)

    {

        System.out.println("Enter no of subjects in "+(i+1)+" semester:");

        arr[i]=sc.nextInt();

    }

    int maxMarks[] = new int[sems];

    for(int i=0;i<sems;i++)

    {

        System.out.println("Marks obtained in semester "+(i+1)+":");

        int max = sc.nextInt();

        if(max<0||max>100)

         {

            System.out.println("You have entered invalid mark.");

            System.exit(0);

        }

        for(int j=1;j<arr[i];j++)

        {

            int marks=sc.nextInt();

            if(marks<0||marks>100)

            {

                System.out.println("You have entered invalid mark.");

                System.exit(0);

            }

            if(max<marks)

            max=marks;

        }

        maxMarks[i]=max;

    }

    for(int i=0;i<sems;i++)

    {

        System.out.println("Maximum mark in "+(i+1)+" semester:"+maxMarks[i]);

    }

    }

    

    }


14)Sum of factorial of positive and single digit numbers in an array

     import java.util.Scanner;

     

     public class Factorial {

         static int sum =0;

         static int factorial(int n)

         {

             int f=1;

             for(int i=1;i<=n;i++)

             {

                f*=i;

            }

        return f;

    }

    static int sumFactorial(int[] arr,int n)

    {

        for(int i=0;i<n;i++)

        {

            if(arr[i]>=0 && arr[i]<10)

            {

                sum+=factorial(arr[i]);

            }

        }

        return sum;

    }

    

    

    public static void main(String[] args){

    Scanner sc=new Scanner(System.in);

    // Fill the code

    System.out.println("Enter the size of an array:");

    int s = sc.nextInt();

    int[] arr = new int[100];

    System.out.println("Enter the elements:");

    for(int i=0;i<s;i++)

    {

        arr[i]=sc.nextInt();

    }

    sum=sumFactorial(arr,s);

    if(sum==0)

    {

        System.out.println("No positive and single digit numbers found in an array");

    }

    else

    {

        System.out.println(sum);

    }

    }

    

    }


STRINGS

1)String lenggth

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      String str=sc.nextLine();

      int n=str.length();

    if(n%2==0)

    System.out.println(n+" Even");

    else

    System.out.println(n+" Odd");

    }

    

    }


2)Count of alphabets

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      String s=sc.nextLine();

      int count=0;

    for(int i=0;i<s.length();i++)

    {

        if(Character.isLetter(s.charAt(i)))

        {

            count++;

        }

    }

    System.out.println(count);

    }

    

    }


3)Substring

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      String s=sc.nextLine();

      int i=sc.nextInt();

    int j=sc.nextInt();

    String c=s.substring(i,j);

    System.out.println(c);

    }

    

    }


4)String contains

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      String s=sc.nextLine();

      String c=sc.nextLine();

    if(s.contains(c))

    {

        System.out.println(c+" is contained in a sentence");

    }

    else

    System.out.println(c+" is not contained in a sentence");

    }

    

    }


5)Compare two strings

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      String a=sc.nextLine();

      String b=sc.nextLine();

    if(a.compareTo(b)==0)

    {

        System.out.println("Case sensitive");

    }

    else if(a.compareToIgnoreCase(b)==0)

    {

        System.out.println("Case insensitive");

    }

    else

    System.out.println("Not equal");

    }

    

    }


6)String split-up

     import java.util.Scanner;

     

     public class Main {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      String a=sc.next();

      String b=sc.next();

    String[] c=a.split(b);

    for(int i=0;i<c.length;i++)

    {

        System.out.println(c[i]);

    }

    }

    

    }


7)String find and replace the character 

     import java.util.Scanner;

     

     public class FirstOccurence {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      System.out.println("Enter the string:");

      char[] str=sc.nextLine().toCharArray();

    System.out.println("Enter the character to be searched:");

    char srh=sc.next().charAt(0);

    System.out.println("Enter the character to replace:");

    char rep=sc.next().charAt(0);

   

    int flag=0;

    int len=str.length;

    for(int i=0;i<len;i++)

    {

        char r=str[i];

        if(r==srh){

            str[i]=rep;

            flag++;

            break;

        }

    }

    if(flag==0)

    {

        System.out.println("character not found");

    }

    else

    {

        for(char i:str){

            System.out.print(i);

        }

    }

    }

    

    }


8)Count the number of charcters

     import java.util.Scanner;

     

     public class Count {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the codep

      System.out.println("Enter your name:");

      String s=sc.nextLine();

    int l=s.length();

    int k=0;

    char ch='a';

    int c=0,ch1;

    for(int i=0;i<l;i++)

    {

        ch=s.charAt(i);

        ch1=(int)ch;

        if(Character.isWhitespace(ch)==true)

        {

            c++;

        }

        else if((ch>='A')&&(ch<='Z'))

        {

            c++;

        }

        else if((ch>='a')&&(ch<='z'))

        {

            c++;

        }

        else

        {

           k=1;

           break;

        }

    }

    if(k==0)

    {

        System.out.println(s+" has "+c+" characters");

    }

    else{

        System.out.println("Invalid input");

    }

    }

    

    }


9)Count occurance of a character

    import java.util.Scanner;

    

    public class OccurrenceOfChar {

        public static boolean isAlpha(String name){

            char[] chars=name.toCharArray();

            for(char c:chars){

                if(!Character.isLetter(c)){

                    return false;

                }

            }

            return true;

        }

    

    public static void main(String[] args){

    Scanner sc=new Scanner(System.in);

    // Fill the code

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

    String word= sc.nextLine();

    if(OccurrenceOfChar.isAlpha(word)){

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

        char c= sc.next().charAt(0);

        String s=Character.toString(c);

        if(OccurrenceOfChar.isAlpha(s)){

            int count=0;

            for(int i=0;i<word.length();i++){

                if(word.charAt(i)==c){

                    count++;

                }

            }

            if(count>0){

                System.out.println("No of '"+c+"' present in the given word is "+count+".");

            }

            else{

                System.out.println("The given character '"+c+"' not present in the given word.");

            }

        }

        else{

            System.out.println("Given character is not an alphabet");

        }

    }

    else{

        System.out.println("Not a valid string");

    }

    }

    

    }


10)String concatenation

     import java.util.Scanner;

     import java.util.regex.Pattern;

     import java.util.regex.Matcher;

     

     public class Authority {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

    System.out.println("Inmate's name:");

    String in_name=sc.nextLine();

    System.out.println("Inmate's father's name:");

    String fa_name=sc.nextLine();

    Pattern p=Pattern.compile("[^a-z ]", Pattern.CASE_INSENSITIVE);

    Matcher m=p.matcher(in_name);

    Matcher m1=p.matcher(fa_name);

    if(m.find()==false && m1.find()==false)

    {

        System.out.println(((in_name.concat(" ")).concat(fa_name)).toUpperCase());

    }

    else

    {

        System.out.println("Invalid name");

    }

    }

    

    }


11)Length of string

     import java.util.Scanner;

     

     public class FindLength {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      String s=sc.nextLine();

      int l=s.length();char ch;int count=0;int flag=0;

    for(int i=0;i<l;i++)

    {

        ch=s.charAt(i);

        if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z')))

        {

            count=count+1;

        }

        else if(Character.isWhitespace(ch))

        {

            count=count+1;

        }

        else

        {

           flag=1;

           System.out.println("Invalid String");

           break;

        }

    }

    if(flag==0)

    {

        System.out.println("No of characters is:"+count);

    }

    }

    

    }


12)PAN card validation

     import java.util.Scanner;

     

     public class PanCard {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      System.out.println("Enter the PAN no:");

    String s=sc.nextLine();

    int l=s.length();char ch;int flag=0;

    for(int i=0;i<l;i++)

    {

        ch=s.charAt(i);

        if(i<5)

        {

            if((ch>='A')&&(ch<='Z'))

            {

                continue;

            }

            else{

                flag=1;

                System.out.println("Invalid PAN no");

                break;

            }

        }

        else if(i==9)

        {

            if((ch>='A')&&(ch<='Z'))

            {

                continue;

            }

            

            else

            {

                flag=1;

                System.out.println("Invalid PAN no");

                break;

            }

        }

        else if(i>4&&i<9)

        {

            if((ch>='0')&&(ch<='9'))

            {

                continue;

            }

            else

            {

                flag=1;

                System.out.println("Invalid PAN no");

                break;

            }

        }

        else{

            flag=1;

            System.out.println("Invalid PAN no");

            break;

        }

    }

        

        if(flag==0)

        System.out.println("Valid PAN no");

   

    }

    

    }


13)Print unique characters

     import java.util.Scanner;

     import java.util.*;

     public class UniqueChar {

         public static boolean printUnique(String sentence){

             char[] chars = sentence.toCharArray();

             Map<Character, Integer> map = new LinkedHashMap<>();

             for(char ch : chars){

                 if(Character.isDigit(ch)){

                    return false;

                }else if(!Character.isWhitespace(ch)) {

                    map.put(ch, map.getOrDefault(ch, 0) + 1);

                }

            }

            List<Character> uniqueCharacters = new ArrayList<>();

            for(char key : map.keySet()) {

                if(map.get(key)==1)  {

                    uniqueCharacters.add(key);

                }

            }

            if(uniqueCharacters.isEmpty()) {

                System.out.println("No unique characters");

            }else{

                System.out.println("Unique characters:");

                for(char ch : uniqueCharacters) {

                    System.out.println(ch);

                }

            }

            return true;

        }

    

    public static void main(String[] args){

    Scanner sc=new Scanner(System.in);

    // Fill the code

    String sentence;

    System.out.println("Enter the sentence:");

    sentence = sc.nextLine();

    if(!printUnique(sentence)) {

        System.out.println("Invalid Sentence");

    }

    }

    

    }


14)InitCap

     import java.util.Scanner;

     

     public class InitCap {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      System.out.println("Enter the String:");

      char[]str=sc.nextLine().toCharArray();

    int upps=0,word=0;

    int len=str.length;

    for(int i=0;i<len;i++)

    {

        if(i==0||Character.isWhitespace(str[i-1]))

        {

            word=word+1;

            if(Character.isLowerCase(str[i]))

            {

                str[i]=Character.toUpperCase(str[i]);

            }

            else

            {

                upps=upps+1;

            }

        }

    }

    if(word==upps)

    {

        System.out.println("First Character of each word is already in uppercase");

    }

    else

    {

        for(char i:str)

        {

            System.out.print(i);

        }

    }

    }

    

    }

15)Palindrome

     import java.util.Scanner;

     import java.util.regex.Matcher;

     import java.util.regex.Pattern;

     

     public class Palindrome {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      System.out.println("Enter the word:");

    String str=sc.nextLine();

    int len=str.length();

    Pattern p=Pattern.compile("[^a-z0-9]",Pattern.CASE_INSENSITIVE);

    Matcher m=p.matcher(str);

    boolean b=m.find();

    if(b)

    {

        System.out.println("Invalid Input");

    }

    else

    {

        String rvs=new String();

        for(int i=(len-1);i>=0;i--)

        {

            rvs=rvs+str.charAt(i);

        }

        if(str.equalsIgnoreCase(rvs))

        {

            System.out.println(str+" is a Palindrome");

        }

        else{

            System.out.println(str+" is not a Palindrome");

        }

    }

    // Fill the code

    }

    

    }


16)Find and replace

     ssimport java.util.Scanner;

     

     public class ReplaceWord {

     

      public static void main(String[] args){

      Scanner sc=new Scanner(System.in);

      // Fill the code

      System.out.println("Enter the String:");

      String a=sc.nextLine();

    Character dot=a.charAt(a.length()-1);

    System.out.println("Enter the word to be searched:");

    String b=sc.nextLine();

    System.out.println("Enter the word to be replaced:");

    String n=sc.nextLine();

    String res="";

    int count=0;

    String[] words=a.split("\\W");

    for(String word:words)

    {

        if(word.equals(b))

        {

            res=res+n+" ";

            count=count+1;

        }

        else

        {

            res=res+word+" ";

        }

    }

    if(count>=1)

    {

        System.out.print(res.trim());

        if(dot=='.')

        System.out.print(".");

    }

    else{

        System.out.print("The word "+b+" not found");

    }

    }

    

    }













Comments

Popular posts from this blog

Cognizant Html css js CC

Length of subsequence