Make a java program that will accept 10 int as an array[10] index=10
and find the following using these methods

find the mode,median,mean method...
    
/*
here's my code..

*/
import java.util.*;

    public class List2 {
    //instance variables
   
      private int items[];
      private int count;
   
   
      static Scanner cons = new Scanner(System.in);
   
   
       public List2(int size){
         items  = new int[size];
         count = 0;
      }
   
       public List2() {
         this(5);
      }
   
       public void add(int x){
      
         if(!isFull())
         {
            items[count] = x;
            count++;
         }
      }
   
       public boolean isEmpty(){
         if(count == 0)
         {
            return true;
         }
         else
            return false;
      }
   
       public boolean isFull(){
         if(count == items.length)
         {
            return true;
         }
         else
            return false;
      }
   
       public int[] mode()   //return the mode value in an array
      {
         int[] maximum = new int[count];
         int maxcount  = 0;
         int tempcount=0;
         int temp=0;
      	
         for(int i=0;i<items.length;i++)
         {
            for(int j=i+1;j<items.length-1;j++)
            {
               if(items[i]==items[j])
               {
                  tempcount++;
               }
            }
            if(tempcount>temp)
            {
               maximum[maxcount++]=items[i];
            
            }
         		
            temp=tempcount;
            tempcount=0;
         }
      	
         tempcount = 0;
      	
         for(int i=0; i<count; i++)
         {
            if(maximum[i]!=0)
            {
               tempcount++;
            
            }
         }
      	
         for(int i=0; i<count; i++)
         {
            System.out.println(maximum[i]);																		
         }
      	
         int[] mod = new int[tempcount];
      	
      
         for(int i=0; i<mod.length; i++)
         {
            mod[i] = maximum[i];
         }
      	
         return mod;
      }
   
   
       public int mean()
      {
         int sum = 0;
      
         for(int i=0; i<items.length; i++)
         {
            sum+=items[i];
         }
      
         return sum/items.length;
      }
   	
       public int median()
      {
         int median = 0;
      
         if(items.length%2 == 0)
         {
            median = items[(items.length-1)/2] + items[((items.length-1)/2)+1];
         }
         else
         {
            median = items[items.length/2];
         }
         return median;
      }
   	 
       public String getModString()
      {
         int[] mod = mode();
      	
         String modString = "";
      				
         for(int i=0; i<mod.length; i++)
         {
            modString+=mod[i];
            modString+=", ";
         }
         return modString;
      }
   
       public String toString(){
         StringBuffer sb = new StringBuffer();
         for(int i = 0; i < this.count; i++)
            sb.append(items[i] +
               ", ");
         return sb.toString();
      }
   
   
   																				
       public static void main(String [] args){			//main
         List2 list = null;
      
         Scanner s = new Scanner(System.in);
      
         System.out.println("Enter the size of the list: ");
         list = new List2(s.nextInt());
      
         System.out.println("Enter number : ");
      
         while(!list.isFull())
         {
            list.add(s.nextInt());
         }
      
      
         System.out.println("Number in the list are : " + list);
         System.out.println("The list.count: " + list.count);
         System.out.println("The mode in the list is :  " + list.getModString());
         System.out.println("The median in the list is :  " + list.median());
         System.out.println("The mean in the list is :  " + list.mean());		 
      
         boolean isEmpty = list.isEmpty();
      
         if(isEmpty==true)
         {
            System.out.print("my list is empty :");
         }
         else
         {
            System.out.print("my list is not empty :");
         }
      
       	
      	
      }//end of main
   }//end of class

Recommended Answers

All 2 Replies

What is your mode function for?
Please explain the purpose of mode function.

And where is your problem?

How are we suppose to know what is wrong? Do you get any errors?

Also at the toString method you do correctly this:

public String toString(){
  StringBuffer sb = new StringBuffer();
  for(int i = 0; [B]i < this.count[/B]; i++)
    sb.append(items[i] + ", ");
  
  return sb.toString();
}

You use the count as the top limit at the for loop. You need to do the same for the other methods.

Also since the count is private, you need a get method (not a set method because you don't want others to change its value). The value of count needs to change only inside the class, so correctly you had it to be private.
But I would do this:
In the class:

private int count = 0;

public int getCount() {
  return count;
}
System.out.println("The list.count: " + list.getCount());

If you try to use the List class in another java program then you wouldn't be able to call the: list.count

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.