public class Location { public static final Location JMU = new Location(38.43542
ID: 3700200 • Letter: P
Question
public class Location
{
public static final Location JMU = new Location(38.435427, -
78.872942);
public static final Location ISAT = new Location(38.434663, -
78.863732);
private double latitude;
private double longitude;
/**
* Explicit value constructor for the this Location.
*
* @param latitude The latitude in degrees
* @param longitude The longitude in degrees
*/
public Location(double latitude, double longitude)
{
this.latitude = latitude;
this.longitude = longitude;
}
/**
* Are the two values within .000001 of each other?
*
* @return true if the two values are the same in
* the two objects and false otherwise
*/
public boolean equals(Location other)
{
return (this.latitude - other.latitude <= .000001
&& this.longitude - other.longitude <= .000001);
}
/**
* Returns the latitude and longitude for this Location.
*
* @return string representation of this Location
*/
public String toString()
{
return String.format("%.6f/%.6f", latitude, longitude);
}
}
Location.java ==== 1. Is the Location class mutable or immutable? Why? 2. Why is [mutable/immutable] the appropriate design for this class? 3. Why do we have only one constructor? Would there be any use for 4. What is the purpose of the two Location constants JMU and ISAT? 5. Is there any danger in making the constants public? Why or why not? another?Explanation / Answer
/'main' method must be in a class 'Rextester'.
//Compiler version 1.8.0_111
import java.util.*;
import java.lang.*;
public class Location
{
public static final Location JMU = new Location(38.435427, -
78.872942);
public static final Location ISAT = new Location(38.434663, -
78.863732);
private double latitude;
private double longitude;
/**
* Explicit value constructor for the this Location.
*
* @param latitude The latitude in degrees
* @param longitude The longitude in degrees
*/
public Location(double latitude, double longitude)
{
this.latitude = latitude;
this.longitude = longitude;
}
public Location()
{
this.latitude = 0.0;
this.longitude = 0.0;
}
/**
* Are the two values within .000001 of each other?
*
* @return true if the two values are the same in
* the two objects and false otherwise
*/
public boolean equals(Location other)
{
return (this.latitude - other.latitude <= .000001
&& this.longitude - other.longitude <= .000001);
}
/**
* Returns the latitude and longitude for this Location.
*
* @return string representation of this Location
*/
public String toString()
{
return String.format("%.6f/%.6f", latitude, longitude);
}
public static void main(String[]args){
Location l=new Location(122.33,1223.22);
Location l1=new Location();
System.out.println(l1);
// this is why it is mutable
l1.longitude =1.2;
System.out.println(l1);
}
}
Answer 1)
Class in Mutable , as after creating object you can change the value of latitude and longitude.
2)
With mutable design in this class you can change the value of latitude and longitude any time. lets say example of map if object is mnoving then it's latitude and longitude is changing constantly in this type of application you can use this.
3)
you can have default constructor , using default constructor you can define specific value for latitude and longitude , as let say equator's latitude and longitude is fix .
4)
It defines two location with their latitude and longitude .
5)
yes using public it can be accessed anywhere but due to final you can not change it. it's consequence is based on where you are using this class.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.