CLASS PROJECT ON ALL ARRAY OPERATIONS

 

import java.util.Scanner;
/**
*
* @ahmed farag
*
*project idea
*
* it take degree of student from user
* then insert it into array
* and sort element
* and print it
* and print max and min degree
* enable user to search through two
* methods one for binary search other for linear search
*/
public class StudentsDegrees {

/**
* idea of sort
* select largest element and swap others
*/

public static void mSelectionSort(double array[]){

for (int i=array.length-1;i>=1;i--){
//index of element
int index=0;

//cuurent num i compare two number set one of them as max
double currentMaxElement=array[0];

// loop to determine largest element
for (int j=1;j<=i;j++){
if (currentMaxElement<array[j]){
// max element equal max num
currentMaxElement=array[j];
index=j;
}//end of if
}//end of second for

//check to swap element
if (index!=i){
array[index]=array[i];
array[i] = currentMaxElement;
}//end of if
}//end of  for

}//end of selection method

/**
* arrayBinarySearch
* job of this method
* define while loop
* check on three possible answers
* first if  key less than midlle number of array
* perform high equal mid-1
*
* else if key equal mid number then it retrun mid number
*
* third condition if key larger than middle number
* then  make low = mid+1
* else out of condition
*
*/

private static double mBinarySearch(double list[],int key){

int low=0;
int high=list.length-1;

while(high>=low){

int middleNumberInArray=(low+high)/2;

if(key<list[middleNumberInArray])
high=middleNumberInArray-1;
else if (key==list[middleNumberInArray])
return middleNumberInArray;

else
low=middleNumberInArray+1;
}//end of while
return -low-1;

}//end of method

/**
*method take to parameter one for array other for key (element want to search on it)
*/
private static void mSearchAdvanced(double arrayOfMethod[],int key){

//loop on array elements
for(int indexOfArray=0;indexOfArray<arrayOfMethod.length;indexOfArray++){

//condition to find specific element
if(key==arrayOfMethod[indexOfArray]){
System.out.println("index is :"+indexOfArray);
}//end of if

}//end of for
}//end of method

/**
*define method to loop and print element of array
*pass array to method
*/
public static void mPrintElement(double arr[]){
for (int i=0;i<arr.length;i++){
System.out.println(i+":"+"'"+arr[i]+"'");
}//end of for

}//end of print method

//method print max dgree
private static void mMaxPoint(double array[]){
double MaxDegree=0;;
int indexofMaxDegree=0;

for(int i=0;i<array.length;i++){
if(array[i]>MaxDegree)

{
MaxDegree=array[i];
indexofMaxDegree=i;

}//enf of if

}//end of for
System.out.println("max elamen is"+MaxDegree+"and has index"+indexofMaxDegree);

}//enf of method

/**
*method print smallest value
*/
private static void mSmallestDegree(double min[]){

double smallestNum=min[0];
int indexOfmin=0;

for (int i=0;i<min.length;i++){

if(smallestNum>min[i]){
smallestNum=min[i];
indexOfmin=i;

}//end of if
}//end of for
System.out.println("min elamen is"+smallestNum+"and has index"+indexOfmin);
}//end of method
//main method

public static void main(String []args){

//define an object to call all methods
StudentsDegrees ob=new StudentsDegrees();

/*
* take an object from Scanner class
* allow to me to take an integer numbers
*/
Scanner scan=new Scanner(System.in);

//define value to store number of student on it
int NumberOfStudent=0;

System.out.println("enter number of students");

//store number at this value
NumberOfStudent=scan.nextInt();

double arrayOfDegree[]=new double[NumberOfStudent] ;
//define for loop to take number in it
for(int i=0;i<NumberOfStudent;i++){

System.out.println("student number his degree is:"+i );
//store degrees of user at array
arrayOfDegree[i]=scan.nextDouble();

}//end of for

ob.mSelectionSort(arrayOfDegree);
//after take dgree method sort degree
ob.mPrintElement(arrayOfDegree);

//    print Max dgree and print smallest degree
ob.mMaxPoint(arrayOfDegree);

ob.mSmallestDegree(arrayOfDegree);

//two method of search

ob.mBinarySearch(arrayOfDegree, 50);
ob.mSearchAdvanced(arrayOfDegree, 50);

}//end of main

}//end of class

Advertisements

One thought on “CLASS PROJECT ON ALL ARRAY OPERATIONS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s