Comparing two locations for accuracy in Android

If you are developing a android application which uses android Location Service and you need to find best estimated location by comparing two locations, where one location is the currently obtained location and other is from the location fix then this post contains answer to your question. The below mentioned code contains a method isBetterLocation which will compare two locations and will return true if current location is more accurate than location fix. You might expect that the most recent location fix is the most accurate. However, because the accuracy of a location fix varies, the most recent fix is not always the best.

isBetterLocation method follows following criteria for choosing location fixes, you vary this criteria depending on the use-cases of the application and field testing.

  • Check if the location retrieved is significantly newer than the previous estimate.
  • Check if the accuracy claimed by the location is better or worse than the previous estimate.
  • Check which provider the new location is from and determine if you trust it more.

Java Programming Question

Q.  Using loops and control statements to draw lines can lead to many interesting designs. Create the
design in the left screen capture of Figure. This design draws lines from the top-left corner, fanning out
the lines until they cover the upper-left half of the panel. One approach is to divide the width and height
into an equal number of steps (we found 15 steps worked well). The first endpoint of a line will always be
in the top-left corner (0, 0). The second endpoint can be found by starting at the bottom-left corner and
moving up one vertical step and right one horizontal step. Draw a line between the two endpoints.
Continue moving up and to the right one step to find each successive end-point. The figure should scale
accordingly as you resize the window.
Java Code:
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Graphics;
public class q2ans extends JPanel
{
public static void main(String args[])
{
q2ans panel = new q2ans(); //creates a panel
JFrame frameapp = new JFrame(); // create a new frame
frameapp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameapp.add(panel);
frameapp.setSize(500, 500); //size of frame
frameapp.setVisible(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int x, y;
int total_lines = 15;
int width = getWidth();
int height = getHeight();
int change_ratio_x = width / total_lines;
int change_ratio_y = height / total_lines;
x = width;
y = 0;
for(int i=1; i<=15; i++)
{
g.drawLine(0,0,x,y);
x = width – i*change_ratio_x;
y = i*change_ratio_y;
}
}
}

Creating multithreaded web server in Java

      Web server is a application which runs on server machine and listens request from various clients on specific port and gives some specific response for each request. Web servers and client machines uses sockets for transferring data between each other. In general if we construct simple web server using single execution instance then it will be able to serve only one client at time, so if multiple clients wants connect to the server then other clients may need to wait. To resolve this problem we can construct multithreaded server, which will create and execute a single thread  for each individual request (may be from same client machine or different client machines). Coding this in Java is very easy as compared to other languages.

 

Java Code of a multithreaded web server available here is able to accept requests from other computers (clients) by using their browser to point towards the server host and, in return displays a web pages or images. Following are steps to create multithreaded web server:

1. Create a Socket to listen webrequest on Port 80.
int port =80;
ServerSocket WebSocket = new ServerSocket(port);
2. Accept a TCP request
Socket connectionSocket = WebSocket.accept();
3. Construct an object to process HTTP request.
HttpRequest request = new HttpRequest(connectionSocket);
    
4. Create a thread to process requests.
Thread thread = new Thread(request);
thread.start(); //start thread

Note:   If you are testing this code with Eclipse or Netbeans IDEs then make one .html file, say index.html in root folder of your project and browse http://127.0.0.1/index.html (similary for other .htlm files).

 

 

Java Programming Question.

Q. String Matrix
Write an Application while will accept a algebraic expression of variables (without limit in
variable numbers) and equivalent number of matrices of any order n of square matrix. The output
should be the corresponding operation on matrix element as specified in equation.


Java Code:
import java.io.*;
import java.util.Stack;
import java.util.Vector;

public class que3ans {

/**
* @param args
*/
static int[][] calculate(int a[][],int b[][],int mat_size,String op)
{
int[][] c=new int[mat_size][mat_size];

switch(op)
{
case “+”:
return(c = add(a,b,mat_size) );

case “-“:
return(c = sub(a,b,mat_size) );
case “*”:
return(c = mul(a,b,mat_size) );
case “/”:
return(c = div(a,b,mat_size) );
default:return c;
}


}
static int[][] mul(int a[][],int b[][],int mat_size)
{
int[][] c=new int[mat_size][mat_size];
int sum=0;
for(int x=0; x<mat_size; x++)
{
for(int i=0; i<mat_size; i++)
{
for(int j=0;j<mat_size; j++)
{
sum = sum + (a[x][j]*b[j][i]);
}
      c[x][i]=sum;
sum =0;
}

}

return c;
}

static int[][] add(int[][] a, int[][] b,int mat_size)
{
int[][] c=new int[mat_size][mat_size];


for(int i=0; i<mat_size; i++)
{
for(int j=0; j<mat_size; j++)
{

c[i][j]=a[i][j]+b[i][j];
}

}

return c;
}

static int[][] sub(int[][] a, int[][] b,int mat_size)
{
int[][] c=new int[mat_size][mat_size];
for(int i=0; i<mat_size; i++)
{
for(int j=0; j<mat_size; j++)
{

c[i][j]=a[i][j]-b[i][j];
}

}

return c;
}

static int[][] div(int[][] a, int[][] b,int mat_size)
{
int[][] c=new int[mat_size][mat_size];

for(int i=0; i<mat_size; i++)
{
for(int j=0; j<mat_size; j++)
{

c[i][j]=a[i][j]/b[i][j];
}

}

return c;


}




public static void main(String[] args) {
// TODO Auto-generated method stub

int mat_size=0;
String single=new String();
DataInputStream ds= new DataInputStream(System.in);
Vector v = new Vector();
int total_operators=0;
int total_operands=0;
//String temp =new String();

Stack<String> operator=new Stack<String>();
Stack<String> operand=new Stack<String>();
Stack<int[][]> Data = new Stack<int[][]>();

String stemp =new String();
//char ops[]={‘(‘,’)’,’*’,’/’,’+’,’-‘};
System.out.println(“Enter Expression”);
try
{
v.addElement(ds.readLine());

}catch(Exception e){}
String[] args1=new String[v.size()];
v.copyInto(args1);
stemp=args1[0];
char[] tempc = new char[stemp.length()];

tempc=stemp.toCharArray();

//Reading MAtrix
System.out.println(“Enter Size Of The Matrix..”);

try{mat_size=Integer.parseInt(ds.readLine());}catch(Exception e){System.out.println(“Exception1″+e);}
int[][] temp3=new int[mat_size][mat_size];
int[][] a1=new int[mat_size][mat_size];
int[][] b1=new int[mat_size][mat_size];


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

if(tempc[i] == ‘+’||tempc[i]==’-‘||tempc[i]==’*’||tempc[i]==’/’||tempc[i]==’)’||tempc[i]=='(‘)
{

operator.push(String.valueOf(tempc[i]));
total_operators++;
}
else
{
operand.push(String.valueOf(tempc[i]));
total_operands++;
}



}
//total_operands=tempc.length-total_operators;


for(int i=0;i<total_operands;i++)
{
System.out.println(“Enter the Values For “+operand.elementAt(i)+” Matrix “);
for(int x=0;x<mat_size;x++)
for(int j=0; j<mat_size; j++)
try{temp3[x][j]=Integer.parseInt(ds.readLine());}catch(Exception e){}
Data.push(temp3);
}



//int x=total_operators;
//int p=(tempc.length-1);
for(int l=0;l<tempc.length; l++)
{ if(tempc[l]==’)’||tempc[l]=='(‘)
{operator.removeElement(“)”);
operator.removeElement(“(“);
}
}

//System.out.print(operator.elementAt(0)+”  “+operator.elementAt(1));

while(!operator.empty()){

a1=Data.pop();
b1=Data.pop();
single = operator.pop();
operator.removeElement(single);

Data.push(que3ans.calculate(a1, b1, mat_size, single));

}

System.out.println(“Answer is =>>”);
a1=Data.pop();
for(int i=0;i<mat_size;i++)
{
for(int j=0;j<mat_size;j++)
System.out.print(”   “+a1[i][j]);
System.out.println(“”);
}




}




}

Java Programming Question.

Q. A small airline has just purchased a computer for its new automated reservations system. You
have been asked to develop the new system. You are to write an application to assign seats on each
flight of the airline’s only plane (capacity: 10 seats).
Your application should display the following alternatives:
1. Please type 1 for First Class and
2. Please type 2 for Economy.
If the user types 1, your application should assign a seat in the first-class section (seats 1–5). If the user
types 2, your application should assign a seat in the economy section (seats 6–10).
Your application should then display a boarding pass indicating the person’s seat number and whether it
is in the first-class or economy section of the plane.
Represent the seating chart of the plane. Initialize all the elements of the array to false to indicate that
all the seats are empty. As each seat is assigned, set the corresponding elements of the array to true to
indicate that the seat is no longer available.
Your application should never assign a seat that has already been assigned. When the economy section is
full, your application should ask the person if it is acceptable to be placed in the first-class section (and
vice versa). If yes, make the appropriate seat assignment. If no, display the message
“Next flight leaves in 3 hours.”

Java Code:

import javax.swing.JOptionPane;
public class que1ans {

static boolean seats[]={false,false,false,false,false,false,false,false,false,false};
String s= new String();
String temp = new String();

int a=0;
int b=0;

void firstClass()
{
for(int i=0; i<5; i++)
if(seats[i]==false)
{
seats[i]=true;
temp = “Seat Is Available n Your Boarding Pass :”+”nYour Seat Number => “+(i+1)+”nYour Class is => First”;
a++;
JOptionPane.showMessageDialog(null, temp);
break;
}
else if(a>=5 && b>=5)
{
temp=”Sorry No Seats Are Availablen Next flight leaves in 3 hours”;
JOptionPane.showMessageDialog(null, temp);
break;
}
else if(a>=5)
{

temp = “Sorry Seats are Not Available In This ClassnSeat May Be Available in Economy ClassnDo you Like To Go For Economy Class”;

s= JOptionPane.showInputDialog(temp+”nPress Y for Yes , N for No”);
if(s.equals(“n”) || s.equals(“N”))
{
temp=”Thanks For Using This System”;
JOptionPane.showMessageDialog(null, temp);
break;

}
else if(s.equals(“Y”) || s.equals(“y”))
{
economyClass();
break;
}
else
{ temp=”Please Enter Valid Choice “;
JOptionPane.showMessageDialog(null, temp);
break;
}



}





}

void economyClass()
{

for(int i=5; i<10; i++)
if(seats[i]==false)
{
seats[i]=true;
temp=”Seat Is AvailablennYour Boarding Pass is:nnYour Seat Number => “+(i+1)+”nYour Class is => Economy”;
JOptionPane.showMessageDialog(null, temp);
b++;
break;
}
else if(a>=5 && b>=5)
{
temp=”Sorry Seats Are Not Availablen Next flight leaves in 3 hours”;
JOptionPane.showMessageDialog(null, temp);
break;
}
else if(b>=5)
{
temp=” Sorry Seats are Not Available In This Class n Seat May Be Available in First Class n Do you Like To Go For First Class”;
s= JOptionPane.showInputDialog(temp+”Press Y for Yes , N for No”);
if(s.equals(“n”) || s.equals(“N”))
{
temp=”Thanks For Using This System”;
JOptionPane.showMessageDialog(null,
temp);
break;
}
else if(s.equals(“Y”) || s.equals(“y”))
{
firstClass();
break;
}
else
{
temp=”Please Enter Valid Choice “;
JOptionPane.showMessageDialog(null, temp);
break;
}
}

}




/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
int c;
que1ans app = new que1ans();
int x=0;
String temp2= new String();

String title=new String(“n Wel-Come To Automated Reservations System”);
try{
do
{
temp2=”Please type 1 for First ClassnPlease type 2 for EconomynPlease type 3 for seating chart of the planenPlease type 4 to exit”;
JOptionPane.showMessageDialog(null, temp2, title, 0);
c = Integer.parseInt(JOptionPane.showInputDialog(“Enter Your Choice..”));

switch(c)
{

case 1:
app.firstClass();
break;

case 2:
app.economyClass();
break;
case 3:
temp2=” First Class => 1-5 n Economy Class => 6-10 n False =>Seat Available n True => Seat Not Available”;
for(int y=0; y<10; y++)
temp2= temp2 + “n Seat No [“+(y+1)+”] =>” + que1ans.seats[y];
JOptionPane.showMessageDialog(null, temp2, title, 0);
break;
case 4:
x++;
break;

default:
temp2=”n Please Enter Valid Option”;
JOptionPane.showMessageDialog(null, temp2, title, 6);
}


}while(x==0);

}
catch(Exception e)
{
System.out.println(“Please Enter Valid Input”);
JOptionPane.showMessageDialog(null, new String(“Please Enter Valid Input And Restart The App”));
}


}

}