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
jjhames -1 Newbie Poster
new_programmer 0 Junior Poster in Training
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
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.