icse-promo

Question

 You are given a sequence of N integers,which are called as pseudo arithmetic sequences(sequences that
are in arithmetic progression).
Sequence of N integers:2,5,6, 9,12
we observe that 2+12=5+ 9=6+8=14.
The sum of the above sequence can be calculated as 14 x 3 = 42.
For sequence containing an odd number of elements the rule is to double the middle element,for example
2,5,7,9,1
=2+12=5+9=7+7=14
14 x 3=42 [middle element= 7]
A class Pseudoarithmetic determines whether a given sequence is a pseudo-arithmetic sequence . The details of the class are given below:
Classname:Pseudoarithmetic
Data Members / Instance variables:
n:to store the size of the sequence
a[ ]:integer array to store the sequence of numbers
ans,flag:store the status
sum:store the sum of sequence of numbers
r:store the sum of the two numbers
Member functions:
Pseudarithmetic():default constructor
void accept(int nn): to assign nn to n and to create an integer array.
Fill in the elements of the array.

boolean check():return true if the sequence is a pseudo-arithmetic sequence otherwise returns false

 

Specify the class Pseudoarithmetlc , giving the details of the constructor() , void accept(int) and boolean check(). Also define main() function to create an object and call the member functions accordingly to enable the task.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;

public class Pseudoarithmetic
{
    int n,sum,r;
    boolean  ans,flag;
    int a[];
    public Pseudoarithmetic()
    {  
        ans=true;
        flag=true;
    }
    public void accept(int nn)
    {
        int i=0;
        Scanner sc=new Scanner(System.in);
        n=nn;
        a=new int[n];
        System.out.println("Enter" +n + "elements");
        for(i=0;i< n;i++)
        {
            a[i]=sc.nextInt();
        }
    }
    public boolean check()
    {
        int i=0,j=0;
        r=a[0]+a[n-1];
        if(n%2==0)
        {	
            for(i=0;i< n/2;i++)
            {
                if((a[i]+a[n-1-i])!=r)
                {
                    flag=false;
                }
            }
        }
        else
        {
            for(j=0;j<= n/2;j++)
            {		
                if((a[j]+  a[n-1-j])!=r)
                {
                    ans=false;
                }
            }
        }
        if(flag==true)
        {
            sum=n/2*r;
        }
        if(ans=true)
        {
            sum=(n/2+1)*r;
        }
        if(ans==false || flag==false)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("enter size of sequence");
        int nn=sc.nextInt();
        Pseudoarithmetic ob1=new Pseudoarithmetic();
        ob1.accept(nn);
        System.out.println(ob1.check());
    }
}


				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *