Sunday, July 20, 2014

Comparator Interface example in Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;


  class Mobile {
 
 private int cameraMegaPixel;
 private int batteryLifeInHours;
 private String brandName;
 
 Mobile(String BrandName, int cameraMP, int batteryHours) {
   
  this.brandName = BrandName;
  this.cameraMegaPixel = cameraMP;
  this.batteryLifeInHours = batteryHours;
 }
 
 public int getbatteryHours() {
  return batteryLifeInHours;
 }
 
 
 
 public String getBrandName() {
  return brandName;
 }
 
 public int getCameraMP() {
 return cameraMegaPixel;
}
 
 public String toString()
 {
String str =  brandName +  " \t " + batteryLifeInHours + "\t  " + cameraMegaPixel;

return str;
 }
 
}


class SortMobileByMP implements Comparator {  

public int compare(Mobile mob1, Mobile mob2) {

int result =0;
if (mob1.getCameraMP() < mob2.getCameraMP())
result =-1;
else if (mob1.getCameraMP() > mob2.getCameraMP())
result = 1;
else
result =0;

return result;
}
}

  class SortMobileByBatteryLife implements Comparator {  


public int compare(Mobile mob1, Mobile mob2) {

int result =0;
if (mob1.getbatteryHours() < mob2.getbatteryHours())
result =-1;
else if (mob1.getbatteryHours() > mob2.getbatteryHours())
result = 1;
else
result =0;

return result;
}
 

}

public class ComparatorExample_mobile {

/**
* @param args
*/
public static void main(String[] args) {
compareExample();


}

public static void compareExample()
{


 ArrayList mobileList = new ArrayList(
  Arrays.asList(
                                                                       new Mobile("Samsung S5",5,12),
  new Mobile("IPhone 5c",8,14),
  new Mobile("HTC touch",6,10),
  new Mobile("Nokia Lumia",12,18)   )
  );

 System.out.println("Mobile Phone list befor sorting :");

 System.out.println("Name   \t Battery Life (hours) Camera (MP)");

 for (int i = 0; i < mobileList.size(); i++) {
  System.out.println(mobileList.get(i));
 }

 Collections.sort(mobileList, new SortMobileByMP());

 System.out.println("Mobile Phone list after sorting :(by camera MP)");
 System.out.println("Name   \t Battery Life (hours) Camera (MP)");
 for (int i = 0; i < mobileList.size(); i++) {
  System.out.println(mobileList.get(i));
 
 }
 
  System.out.println("Mobile Phone list befor sorting :");

System.out.println("Name   \t Battery Life (hours) Camera (MP)");

 for (int  i = 0; i < mobileList.size(); i++) {
 System.out.println(mobileList.get(i));
 }

 Collections.sort(mobileList, new SortMobileByBatteryLife());

       System.out.println("Mobile Phone list after sorting :(by Battery Life)");

               System.out.println("Name   \t Battery Life (hours) Camera (MP)");
 for (int  i = 0; i < mobileList.size(); i++) {
   System.out.println(mobileList.get(i));
 }

}
}

Wednesday, April 30, 2014

Java program using Eclipse IDE.

Beginning java programming using Eclipse


In this tutorial I will show you how to use the Eclipse IDE for the first time.
Start eclipse.exe from start menu.

Because this is your first time running Eclipse, it will take a while to load as it sets up the environment.




Once Eclipse is fully loaded you will see the Welcome Screen. You can close this straight away..



OK now lets start our first application.

Click File > New > Java Project



The 'Create a Java Project' box will popup.

Give your Project a name. In this case I have named it 'JavaPractice


Click FINISH.

You have now setup your first project which will appear in the Project Explorer window.


Before we can begin to write any code, we must first add a Package which will contain all our project files.

Select you project, right click and menu will appear. Select New option from menu and another menu will appear. Select package and new package dialog box will open.




Give your package a name relevant to your project.




Click FINISH

Now your Package has been created we need to add a Class file. Select you pacakage, right click and menu will appear. Select New option from menu and another menu will appear




The Create a New Java Class box will popup.

Give your Class a name and tick the public static void main(String[] args)box. Note that package name is shown in Package text box.



Your Class will now appear in the Workspace and you are ready to start writing code! Note the name of file and class are same.



Here we will write simple statement which will print the words into the console.




To Run your Java project. Right click the 'FirstExample.java' file in the Package Explorer window, then click Run As >Java Application





The output will now be displayed in the console.


In this way your first java program is complete using Eclipse IDE.

Friday, November 20, 2009

Will this program compile? If yes what will be output and why? If not error plz.
void main( )
{

int k=6;
printf ( "k%d" , printf( "%d" , k) ) ;
}

What will be out put of this program:
main( )

{

int i=3, j=0;

if( i==3 && j++ )

printf("%d %d",i , j ) ;

else

printf("%d %d ", i , j ) ;

}

Also Give explaination.

Thursday, November 19, 2009

What will be output: (explain why?)

void main()

{
int a=10,b=300;
char x=1, y=0;
if(a,b,x,y)
printf("Delhi");

else

printf("Mumbai");

}

Thursday, February 14, 2008

Indexing of array elements

Can we use char/float variable for indexing of array elements? For ex,
int arr[10];

arr[ j ] =3;
whether j can be of type char or float?

Thursday, January 31, 2008

C prog to find remainder of a number

This prog finds remainder of a number without using divisor (/), modulus (%) or multiplication (*) operator.

#include
#include
main()
{
int a,b;

printf("enter the value of Dividend=");
scanf("%d",&a);
printf("enter the value of divisor=");
scanf("%d",&b);
if(a>=b)
{
while(a-b>=b)
{
a=a-b;
}
printf("the reminder is =%d",a-b);
}
else
printf(" Dividend must be greater than divisor");
getch();
}