A community has 7 swimming pools with coordinates A(4,8), B(1,3), C(4,2), D(13,1
ID: 3854381 • Letter: A
Question
A community has 7 swimming pools with coordinates A(4,8), B(1,3), C(4,2), D(13,1), E(12,9), F(10,5), G(6,6)
1. Create three classes: Pool, Temperature, and Location.
2. A swimming pool shall have an object of “Temperature” and an object of “Location” as data
members. (“Compositionally” speaking: A pool has-a temperature, and a pool has-a location)
3. A temperature has “degree” and “scale”.
4. A location is defined by (x, y); hence a location has an ‘x’ value and a ‘y’ value.
5. Provide ToString method for all three classes.
6. Provide other member methods for each of the classes properly . For example, a FindDistance method to find the distance between pools.
7. All methods shall be public.
8. The Pool class shall have a static data member ‘Count’ The driver program shall print out the static member count before any pool object is created, and also after each pool is instantiated.
9. A maintenance crew can set the temperature for each pool within the range of 98 oF and
104 oF.
Note: generate a random number between [98, 104].
10. A maintenance person will go through all seven pools and set the temperatures. The person starts from location (0,0), and will go to the nearest pool after she/he finishes the job. The process continues until all the pools are visited. Each pool can only be visited once.
Your driver program determines and displays the route she/he takes to visit all the pools
Explanation / Answer
// Pool.java
public class Pool {
private static int count;
public Pool() {}
public Pool(Temperature temp, Location location) {
count++;
this.temp = temp;
this.location = location;
}
private Temperature temp;
private Location location;
public int getCount()
{
return count;
}
public Temperature getTemp() {
return temp;
}
public void setTemp(Temperature temp) {
this.temp = temp;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
@Override
public String toString() {
return "Pool [temp=" + temp + ", location=" + location + "]";
}
public double findDistance(Pool pool)
{
return location.findDistance(pool.getLocation());
}
}
// Temperature.java
public class Temperature {
public Temperature() { this(0);}
public Temperature(double degree) {
this.degree = degree;
this.scale = "F";
}
private double degree;
private String scale;
public double getDegree() {
return degree;
}
public void setDegree(double degree) {
this.degree = degree;
}
public String getScle() {
return scale;
}
public void setScle(String scale) {
this.scale = scale;
}
@Override
public String toString() {
return "Temperature [" + degree + "o" + scale + "]";
}
}
// Location.java
public class Location {
public Location(int x, int y) {
this.x = x;
this.y = y;
}
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return "Location [x=" + x + ", y=" + y + "]";
}
public double findDistance(Location l)
{
return Math.sqrt(Math.pow((x - l.getX()), 2) + Math.pow((y-l.getY()), 2));
}
}
// PoolTest.java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class PoolTest {
public static void main(String[] args){
Random rn = new Random();
int temp;
temp = rn.nextInt(7) + 98;
Temperature t = new Temperature();
List<Pool> poolList = new ArrayList<Pool>();
Location l1 = new Location(4, 8);
Pool p1 = new Pool(t, l1);
poolList.add(p1);
Location l3 = new Location(1, 3);
Pool p3 = new Pool(t, l3);
poolList.add(p3);
Location l4 = new Location(4,2);
Pool p4 = new Pool(t, l4);
poolList.add(p4);
Location l5 = new Location(13, 1);
Pool p5 = new Pool(t, l5);
poolList.add(p5);
Location l6 = new Location(12, 9);
Pool p6 = new Pool(t, l6);
poolList.add(p6);
Location l7 = new Location(10, 5);
Pool p7 = new Pool(t, l7);
poolList.add(p7);
Location l2 = new Location(6, 6);
Pool p2 = new Pool(t, l2);
poolList.add(p2);
Location loc = new Location(0, 0);
for(int i = 0; i < 7; i++)
{
Pool p = findNearest(loc, poolList);
temp = rn.nextInt(7) + 98;
p.getTemp().setDegree(temp);
System.out.println("Setting temerature of " + p);
loc = p.getLocation();
poolList.remove(p);
}
}
public static Pool findNearest(Location l, List<Pool> poolList)
{
double distance = 100000000;
Pool pool = new Pool();
for (Pool p : poolList)
{
double calcDistance = p.getLocation().findDistance(l);
if (calcDistance < distance)
{
distance = calcDistance;
pool = p;
}
}
return pool;
}
}
// Sample run
Setting temerature of Pool [temp=Temperature [98.0oF], location=Location [x=1, y=3]]
Setting temerature of Pool [temp=Temperature [102.0oF], location=Location [x=4, y=2]]
Setting temerature of Pool [temp=Temperature [103.0oF], location=Location [x=6, y=6]]
Setting temerature of Pool [temp=Temperature [101.0oF], location=Location [x=4, y=8]]
Setting temerature of Pool [temp=Temperature [101.0oF], location=Location [x=10, y=5]]
Setting temerature of Pool [temp=Temperature [103.0oF], location=Location [x=12, y=9]]
Setting temerature of Pool [temp=Temperature [98.0oF], location=Location [x=13, y=1]]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.