Java exercises and solutions: stem and leaf


Java arrays: stem and leaf data presentation

Exercise: Write a Java program to display an integer data set in the form of stem and leaf. The data points are input by the user from keyboard. This program will display the output similar to the one shown below:

Java program to present data in stem and leaf form  

If you are not sure about how to present data set in the form of stem and leaf, you will need to read this page:

Stem and Leaf display  

Solution:

import java.util.Scanner;

public class StemLeaf
{
public static void main(String[] args)
{
showStemLeaf();
}

static void showStemLeaf(){
//

int n;

Scanner sc=new Scanner(System.in);
System.out.print("Enter number of data points:");
n=sc.nextInt();
if (n < 3)
{
System.out.println("The number of data points should be greater than 2.");

}
else
{

//declare an array of n size to store integral data points
int[] dataset = new int[n];
//allow user inputs
int i = 0;
for (i = 0; i < n; i++)
{
System.out.print("["+i+"]:");
dataset[i] = sc.nextInt();
}

//sort the data set
bubblesort(dataset, n);
stemleaf(dataset,n);

}

//

}

//method to sort data set
static void bubblesort(int[] dataset, int n)
{
int i, j;
for (i = 0; i < n; i++)
for (j = n - 1; j > i; j--)
if (dataset[j] < dataset[j - 1])
{
int temp = dataset[j];
dataset[j] = dataset[j - 1];
dataset[j - 1] = temp;
}

}

//show stem and leaf

static void stemleaf(int[] data_points,int n)
{


//Display sorted array
System.out.println("The sorted array is:");
for(int i=0;i<n;i++){
System.out.print(data_points[i]+"\t");
if(i%5==0 && i!=0) System.out.println();
}

//store stem and leaf in a 2D array
int[][] stem_leaf=new int[n][2];
for(int i=0;i<n;i++){
stem_leaf[i][0]=data_points[i]/10;
stem_leaf[i][1]=data_points[i]%10;
}


//initialize 2D array storing numbers of occurences, and values
int[][] mode=new int[n][2];
for(int i=0;i<n;i++)
for(int j=0;j<2;j++)mode[i][j]=0;


//find mode
mode[0][0]=1;
int count=1;
for(int i=count-1;i<n;i++){
for(int j=count-1;j<n-1;j++){
if(stem_leaf[i][0]==stem_leaf[j+1][0]) {count++;mode[i][0]++;mode[i][1]=stem_leaf[i][0];}
else if(i==0) mode[i][1]=stem_leaf[i][0];}
}
System.out.println();
System.out.println("Stem and Leaf Display:");
System.out.println("Frequency\tStem\tLeaf");
System.out.println("================================");
int c=0,leaf=0;
for(int i=0;i<n;i++){
if(mode[i][1]!=0){
leaf+=mode[i][0];

System.out.format("%-18d",mode[i][0]);
System.out.format("%-6d",mode[i][1]);
for(int j=c;j<leaf;j++){
System.out.format("%s",stem_leaf[j][1]);}

c=leaf;
System.out.println();
     }

  }

 }


}




Comments

StMit comment

 StMit

for(int i=count-1;i<n;i++){
for(int j=count-1;j<n-1;j++){
if(stem_leaf[i][0]==stem_leaf[j+1][0]) {count++;mode[i][0]++;mode[i][1]=stem_leaf[i][0];}
else if(i==0) mode[i][1]=stem_leaf[i][0];}
}

How the above makes a 2D Array with such a result
4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0,


2016-01-25
mon comment

 mon

hi ,
if the " int[][] mode=new int[n][2]; "
how can you print System.out.format("%-18d",mode[i][0]);? the format method needs to be string and int


2014-07-12



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.