JAVA: Emergency 911 look up database Create an array with First name Last name a
ID: 3711070 • Letter: J
Question
JAVA: Emergency 911 look up database Create an array with First name Last name address and phone number: Joe Camel 123 Main Street 5552341212 Mary Jane 345 West Avenue 5556781234 Ask the user for their phone number: Look up the phone number and return the address and name. add a land line: cell phone field, also two fields for the coordinates. Look up the address via the coordinates. Joe Camel 123 Main Street 5552341212 L 0.0 0.0 Mary Jane 345 West Avenue 5556781234 L 0.0 0.0 Tonya Smith Found via Google LookUp 5553452345 C 48.38 123.1 Amy Hernandez Found via Google LookUp 555777 5656 C 47.23 124.5
Explanation / Answer
import java.util.*;
public class Group{
//declaring variables
String firstName;
String lastName;
String address;
long number;
//setter methods
void setfirstName(String name){
firstName = name; //this.name represents the current instance non-static variable
}
void setlastName(String name){
lastName = name;
}
void setaddress(String address){
this.address = address;
}
void setnumber(long number){
this.number = number;
}
//main method
public static void main(String []args){
// creating the array of type Group
Group grp[]=new Group[4];// creating the Group array
//creating Group class objects
Group g1=new Group();
Group g2=new Group();
Group g3=new Group();
Group g4=new Group();
//setting values for the objects
g1.setfirstName("joe");
g1.setlastName("Camel");
g1.setaddress("123 Main Street");
g1.setnumber(5552341212l);
//adding objects to array
grp[0]=g1;
g2.setfirstName("Mary");
g2.setlastName("Jane");
g2.setaddress("345 West Avenue");
g2.setnumber(5556781234l);
grp[1]=g2;
g3.setfirstName("Tonya");
g3.setlastName("Smith");
g3.setaddress("C 48.38 123.1");
g3.setnumber(5553452345l);
grp[2]=g3;
g4.setfirstName("Amy");
g4.setlastName("Hernandez");
g4.setaddress("C 47.23 124.5");
g4.setnumber(5557775656l);
grp[3]=g4;
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter phone number"); // asking user to enter phone number
try{
long num=reader.nextLong();
// logic to compare entered phone number with the available phone numbers
for(int i=0;i<4;i++){
if(grp[i].number==num){
System.out.println("address and name for the given phone number is "+grp[i].address+"--address"+" "+grp[i].firstName+" "+grp[i].lastName+"--name");
}
}
}
catch (NumberFormatException e){
System.out.println("not a number");
}
}
output:
Enter phone number 5556781234
address and name for the given phone number is 345 West Avenue--address Mary Jane--name
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.