Question

I am stuck on this question. Write the class, GeoLocation.java. You can refer to Name.java used...

I am stuck on this question.

Write the class, GeoLocation.java. You can refer to Name.java used in the lecture to complete the below exercises. Do the following: 1. Create two instance variables, lat and lng, both of which should be doubles. 2. Write the default constructor. 3. Write the non-default constructor. 4. Write 2 accessor methods, one for each instance variable. 5. Write 2 mutator methods, one for each instance variable. 6. Write a method that will return the location in the format "(lat, lng)" (the toString method). 7. Write a method that will return true if the latitude is between -90 and +90. 8. Write a method that will return true if the longitude is between -180 and +180. 9. Optional: Write a method that will take a String in the format "(lat, lng)" and return a GeoLocation. 10. Now write an application class that instantiates two instances of GeoLocation. One instance should use the default constructor and the other should use the non-default constructor. Display the values of the instance variables by calling the accessor methods.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Create a file named GeoLocation.java and paste the below code in it

CODE:


public class GeoLocation {
   private double lat;
   private double lng;
  
   //Default constructor. It means it takes no arguments.
   //if we didn't create this constructor explicitly.then by default java creates default constructor.
   GeoLocation() {
       super();
   }
  
   //non-default constructor which takes lat and lng as arguments.
   public GeoLocation(double lat, double lng) {
       super();
       this.lat = lat;
       this.lng = lng;
   }

   //accessor method for lat
   public double getLat() {
       return lat;
   }
   //mutator method for lat
   public void setLat(double lat) {
       this.lat = lat;
   }
  
   //accessor method for lng
   public double getLng() {
       return lng;
   }
  
   //mutator method for lng
   public void setLng(double lng) {
       this.lng = lng;
   }
  
   //toString() method . It is used to print the object in desirec format
   @Override
   public String toString() {
       return "("+lat + "," + lng + ")";
   }
  
   //checks lat whether it is between -90 and 90.8 and return true it it is.
   public boolean checkLat()
   {
       if(this.lat>=-90 && this.lat<=90.8)
           return true;
       return false;
   }
  
   //checks lng whether it is between -180 and 180.9 and return true it it is.
   public boolean checkLong()
   {
       if(this.lng>=-180 && this.lng<=180.9)
           return true;
       return false;
   }
   //optional method
   public GeoLocation getGeoLocation(String coordinates)
   {
       String[] latlng=coordinates.split(","); //splitting string to get lat and lng
       String strlat=latlng[0].substring(1); //getting lat
       double lat=Double.parseDouble(strlat); //converting lat from string to double
       String strlng=latlng[1].substring(1); //getting lng
       double lng=Double.parseDouble(strlng); //converting lat from string to double
       GeoLocation geoLocation=new GeoLocation(lat,lng); //creating GeoLocation object
       return geoLocation; //returning object
      
   }
  

}

Create a file name application.java and paste the below code in it.

CODE:

public class application
{
  
   public static void main(String[] args)
   {
       //Creating object using default constructor
       GeoLocation location1=new GeoLocation();
       location1.setLat(108.9); //setting values using mutator method
       location1.setLng(90.7); //setting value using mutator method
      
       //Hardcoding values of lat and lng to create new GeoLocation object
       double lat=78.5;
       double lng=56.7;
      
       //Creating object using non-default constructor
       GeoLocation location2=new GeoLocation(lat,lng);
      
       System.out.println("Location 1:");
      
       System.out.println("Lat:"+location1.getLat()); //printing values using accessor method
       System.out.println("Lng:"+location1.getLng()); //printing values using accessor method
      
       System.out.println("\nLocation 2:");
      
       System.out.println("Lat:"+location2.getLat()); //printing values using accessor method
       System.out.println("Lng:"+location2.getLng()); //printing values using accessor method
      
   }
}

OUTPUT:

Please rate my answer if you liked it.

Add a comment
Know the answer?
Add Answer to:
I am stuck on this question. Write the class, GeoLocation.java. You can refer to Name.java used...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Program Please help me with this. It should be pretty basic and easy but I...

    Java Program Please help me with this. It should be pretty basic and easy but I am struggling with it. Thank you Create a superclass called VacationInstance Variables destination - String budget - double Constructors - default and parameterized to set all instance variables Access and mutator methods budgetBalance method - returns the amount the vacation is under or over budget. Under budget is a positive number and over budget is a negative number. This method will be overwritten in...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one...

    Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one for the hour, one for the minute and one for the second. Your class should have the following methods: A default constructor that takes no parameters (make sure this constructor assigns values to the instance variables) A constructor that takes 3 parameters, one for each instance variable A mutator method called setHour which takes a single integer parameter. This method sets the value of...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type...

    Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named CashPayment that is...

  • I need this in c++ please Write a Map class that is composed of a Location...

    I need this in c++ please Write a Map class that is composed of a Location class. Location class has three private variable string nameOfCity, double variables, latitude and longitude. It has a constructor to initialize these and also the usual getters. It has three functions: string toString() which prints :double getFlyingTime(Location l) which returns the flying time between this and location l. It uses the formula that time = distance/speed. speed = 673 miles/hour. No need to convert to...

  • In C++, Step 1: Implement the Student Class and write a simple main() driver program to...

    In C++, Step 1: Implement the Student Class and write a simple main() driver program to instantiate several objects of this class, populate each object, and display the information for each object. The defintion of the student class should be written in an individual header (i.e. student.h) file and the implementation of the methods of the student class should be written in a corresponding source (i.e. student.cpp) file. Student class - The name of the class should be Student. -...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write...

    Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return...

  • [CODE] Write a class encapsulating the concept of a house, assuming a house has the following...

    [CODE] Write a class encapsulating the concept of a house, assuming a house has the following attributes: value (in dollars), a city location, and number of bedrooms. Your class name should be “House” and your code should be in a file called “House.java”. You will need to create this file. In this class, include: Private instance variables for the previously-mentioned attributes A default constructor An overloaded constructor Accessor and mutator methods (i.e., get and set methods) The toString method The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT