Please open the menu to show more

Friday, July 28, 2017

Solutions for the assignments given on 27/July/2017 for the 10 to 12 batch

These codes are implemented by Iti Dixit from Banasthali University



## save this class inside Secret.java file ##

## this code is about to show or hide a particular number in a given range

import java.util.Scanner;
public class Secret 
{
    int start,end,number,check;
    
    Secret(int start,int end,int number,int check)
    {
        this.start=start;
        this.end=end;
        this.number=number;
        this.check=check;
    }
    void printNumbers()
    {
        if(check==0)
        {   
         System.out.println("Numbers which do not contain "+number+" are as follows:");
            for(int i=start;i<=end;i++)
            {
                int p=i,flag=0;
                while(p!=0)
                {
                    int r=p%10;
                    if(r==number)
                    {
                        flag=1;
                        break;
                    }
                    p/=10;
                }
                if(flag==0)
                System.out.println(i);
            }
        }
       else if (check==1)
        {
           System.out.println("Numbers which contain "+number+" are as follows:");
          
           for(int i=start;i<=end;i++)
            {
                int p=i,flag=0;
                while(p!=0)
                {
                    int r=p%10;
                    if(r==number)
                    {
                        flag=1;
                        break;
                    }
                    p/=10;
                }
                if(flag==1) {
                System.out.println(i);
              }
            } 
                
         }
        else
            System.out.println("INAVLID");
        }
   public static void main(String args[])
   {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter the values of start,end,number and check :");
       int st=sc.nextInt();
       int en=sc.nextInt();
       int num=sc.nextInt();
       int ch=sc.nextInt();
       Secret s=new Secret(st,en,num,ch);
       s.printNumbers();
   }    
}

======================================================

## save this class inside Square.java file ##

## this code is about learning constructors as well as getting area of square

public class Square
{
    
    float side;
    
    // define a no argument constructor
    Square()
    {
        side=1.07f;
    }

    // define a parameterized constructor
    Square(float side)
    {
        this.side=side;
    }

    void printSide()
    {
        System.out.println("SIDE = "+side);
    }

    void showArea()
    {
        System.out.println("AREA = "+(side*side));
    }

    public static void main(String args[])
    {
       // create object by using no arg constructor
        Square s1=new Square();
        // call the methods
        s1.printSide();
        s1.showArea();
        
       // create object by using parameterized constructor
        Square s2=new Square(2.98f);
        s2.printSide();
        s2.showArea();
        
       // create object by using parameterized constructor
        Square s3=new Square(3.25f);
        s3.printSide();
        s3.showArea();

    }
}


======================================================

## save this class inside CheckTemperature.java file ##

## this code is about learning constructors as well as converting the temprature from Fahrenheit to centigrade and vice versa

public class CheckTemperature 
{
    float F,C;
    
// define a parameterized constructor
    CheckTemperature(float F,float C)
    {
        this.F=F;
        this.C=C;
    }

    void F2C()
    {
        float c=(5*(F-32))/9;
        System.out.println("TEMEPERATURE IN CELSIUS = "+c);
    }

    void C2F()
    {
        float a=((9*C)/5)+32;
        System.out.println("TEMPERATURE IN FAHRENHEIT = "+a);
    }

    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        
        System.out.println("Enter temperature in fahrenheit : ");
        float a=sc.nextFloat();
        
        System.out.println("Enter temperature in celsius : ");
        float c=sc.nextFloat();
        
        // call parameterized constructor and pass values inside it
        CheckTemperature ct=new  CheckTemperature(a,c);
        
        System.out.println("ENTER YOUR CHOICE : \n 1.C to F \n 2. F to C");
        int n=sc.nextInt();
        
        switch(n)
        {
            case 1:
            ct.C2F();
            break;
         
            case 2:
            ct.F2C();
            break;
         
    default:
            System.out.println("INVALID CHOICE");
         }
}
}

1 comment: