Using Two Classes, one main class only for calling methods in the second class
{Hint: suppose you two classes Like Class A and Class B.
Class A has all methods only.
Class B only Calling methods}

Q1- write a java program to do the following
i- method for input array elements number.
ii- method for show array elements.
iii- method for search array elements.
iv- method for to return the maximun number.
v- method for sorting array .
Q2- write a java program to do the following
C = A + B

Recommended Answers

All 2 Replies

import java.util.Scanner;
 
 class methods
 {
 	
 	public  int array[]= new int[5];
 	Scanner scan = new Scanner(System.in);
 	
 	public  methods(int array[])
 	{
 		this.array=array;
 	}
 	
 	public  void arrayInput()
 	{
 		for(int i=0;i<array.length;i++)
 		{
 			System.out.println ("Enter Values :");
 			array[i]=scan.nextInt();
 		}
 	
 	}
 	
 	public  void printArray()
 	{
 		for(int i=0; i<array.length;i++)
 		{
 			System.out.println ("array elements : "+array[i]);
 		}
 	}
 	
 	public  void SearchArray(int x)
 	{
 		
 		System.out.println ("Enter Search Value : ");
 		x=scan.nextInt();
 		for(int i=0; i<array.length; i++)
 		{
 			if(array[i]==x)
 				System.out.println ("found >>"+array[i]);

 				else
 					System.out.println ("NOt FOUND");
 		}
 		
 	}
 
    public void maximum(int max)
    {
        max= array[0];
        for(int i=0;i<array.length;i++)
        {
        	if(array[i]>max)
        		max=array[i];
        		
        		
        }
        System.out.println ("Max ="+max);
    }	
    	public void Sorting(int temp)
    	{
    	    
    		for(int i=0;i<5;i++)
    		{
    			for(int j=0;j<4;j++)
    			{
    				if(array[i]>array[j])
    					temp=array[i];
    					array[i]=array[j];
    					array[j]=temp;
    			}
    		}
    	}
    	
 }
 
 class Test
 {
 	public static void main (String[] args) 
 	{
 	    int x[]= new int[5];
 		int max=0;
 		int y=0;
 		int temp=0;
 	 	Scanner scan = new Scanner(System.in);
 	 	methods meth= new methods (x);
 	 	meth.arrayInput();
 	 	meth.printArray();
 	 	meth.maximum(max);
 	 	meth.SearchArray(y);
 	 	meth.Sorting(temp);
 	 	meth.printArray();
 	}
 }
commented: don't do that -3

What is your question or problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.