Sunday, November 20, 2016

BlueJ Program to check whether the values in a list are in order


In this program, user will enter number of values in an array and the program will check whether the values are in order
(ascending).

For example if the values in the array are 3 5 23 32 34 90, the the output will be " The values are in order." and if the numbers are 3 5 23 32 34 90 21, the output would be "The values are not in order.".


import java.io.*;

class Order
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int arr[];
int i;

Order()
{
 arr=new int[10];
}

public void takeValues() throws Exception
{
for(i=0;i< 10;i++)
{
System.out.print("\nEnter Value :");
arr[i]=Integer.parseInt(br.readLine());
}
}

public void show()
{
System.out.println("\nValues are :");
for(i=0;i< 10;i++)
{
System.out.print(" "+arr[i]);
}
}

boolean isOrder()
{
for(i=0;i< 9;i++)
{
 if(arr[i]>arr[i+1])
break;
}
if (i>8)
return true;
else
return false;
}

public static void main(String args[])throws Exception
{
 Order ob=new Order();
ob.takeValues();
ob.show();
if(ob.isOrder())
System.out.print("\nValues are in order.");
else
System.out.print("\nValues are not in order.");
}
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner