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();
}

Tuesday, January 29, 2008

C prog to find remainder of a number

A C prog to find remainder of a number without using division (/) or modulus (%)operator.

Friday, January 25, 2008

C prog without a single Semicolon

Yes why not, we can absolutely write a C prog without using a semicolon and having at least one
executable statement.
main()
{

if(printf("hello suriya"))

{
}
}
Another way. (here notice the use of comma operator described in previous post)
main()
{
while(printf("hello suriya"),0)
{
}
}

Monday, January 21, 2008

C prog without semicolon??

Is it possible to write a C prog without using a single semicolon?
(prog must contain at least one executable statement, say just displaying a hello message)

Wednesday, January 9, 2008

The Comma Operator

Can U predict output of this program:

void main()
{
int p=3,q=5,x,y;

x = p,q;
y = (p,q);
printf ("%d %d",x,y);
}

a. 3 5
b. 5 5
c. 3 3
d. 5 3
e. None (may be error)



The ans is a. 3 5

The comma separates the elements of a function argument list. The comma is also used as an operator in comma expressions. Mixing the two uses of comma is legal, but you must use parentheses to distinguish them. the left operand E1 is evaluated as a void expression, then E2 is evaluated to give the result and type of the comma expression. By recursion, the expression

E1, E2, ..., En

results in the left-to-right evaluation of each Ei, with the value and type of En giving the result of the whole expression.

Tuesday, January 8, 2008

Careful with arithmetic operation of dissimilar data types

Check the following prog:

main()
{
float result;
result =50/100;
printf("%f",result);
}


what is the output?

The output is not like this: 0.50000

The output is 0.000000

because
int/int = int
so change the program to
main()
{
float result;
result=50/100.0;
printf("%f",result);
}

the above programm is (int/float)
so the output is now 0.500000

conclusion:

int/int = int
int/float = float
float/int = float
float/float = float

The above table can be summarized as: integer division takes place only when both elements are integers.

Monday, January 7, 2008

Welcome

Hi
Welcome to the world of programming. "C is Sea" and it is 100% true. After teaching for almost 6 years to many students , I still feel that I am just at the surface of this sea.

In this blog, I will share my experience and will try to give some tips.

Rest on any other day
Suriya