import java.util.*;
 
 public class Try{
 
 	private int [] arr;
	private final int MAX=10;
		Scanner scan=new Scanner(System.in);

	public Try(){
 	arr =new int[MAX];
	}
	public void accept(){
		
		for(int i=0;i<arr.length;i++) 
		arr[i]=scan.nextInt();
	} 
	public int smallest(){
		int smallest=0;
		 
		for (int i=0;i<arr.length;i++) 
		smallest=Math.min(smallest,arr[i]);
 
		return smallest;
	
	}
	public int largest(){
			int largest=0;
		for(int i=0;i<arr.length;i++)
			largest=Math.max(largest,arr[i]);
			return largest;	
		}
	public int sum(){
		int sum=0;
	for(int i=0;i<arr.length;i++)
	sum+=arr[i];
	return sum;
	
	}
 	
	public static void main(String [] args){
	
	int arr[ ]= {1,5,6,4,78,9,56,2,3,50};
 
  
	}
/*public int fibonacci(){}
  can u teach me how to do the fibonacci
*/
		  }

Can someone help me how put a main in this program...

Recommended Answers

All 3 Replies

First of all, the methods for smallest, largest are wrong. You initialize at both cases with zero.
For example you set smallest=0, but what if the user enters only positive numbers, at the accept method?:
1,2,3,4,5,....
Then the smallest 0 will always be smaller than all the elements of the array and the method will return 0 instead of the right value 1

The same goes with the other method. You need to initialize the smallest/largest variable with the first element of the array.

As far as Fibonacci, try to find the formula for fibonacci numbers and implement it. I think that each fibonacci number is the sum of the previous 2 so you will need to have variable with the first 2 values and then use them to calculate the third and then recursively the fourth from the previous 2 and so on.
Have a method with argument n that returns the nth fibonacci number. If n=1 or n=2 just return the fixed values, else calculate based on the formula

Collections.sort(arr); //acs
Collections.sort(arr, Collections.reverseOrder()); //decs

Collections.sort(arr); //acs
Collections.sort(arr, Collections.reverseOrder()); //decs

The idea is for someone to learn as an exercise how to sort an array or how to find the max/min number. Not to just call a method and let it do the work for you. The idea is to learn how to think and implement solutions for your problem. Not all problems can be solved by methods provided by java

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.