PROBLEM DESCRIPTION: ^^^^^^^^^^^^^^^^^^^^ Nobody wants a nuclear power plant in
ID: 3889979 • Letter: P
Question
PROBLEM DESCRIPTION: ^^^^^^^^^^^^^^^^^^^^ Nobody wants a nuclear power plant in their neighborhood. Unfortunately, the governor must locate a nuclear power plant somewhere inside a 25 square mile area containing four cities. The governor, being primarily concerned with reelection, desires to locate the plant in the location which will cause the least amount of unhappiness among the constituents. In this lab, you will write a program to assist the governor in this difficult decision. METHOD: ^^^^^^^ Assume each city is located in an integer-pair (x, y) valued location within a 25 x 25 square mile area. For example, a city can be located at (4, 19), but not at (4.5, 19.8). In other words, there are no real valued coordinate for a location of a city. Your program will consider each integer-pair valued location within the area as a possible site for the nuclear plant, starting at (1, 1) and continuing across each row until the final site (25, 25) is reached. At each possible location for the nuclear plant, your program will compute the average unhappiness if the plant were located there. The following two rules will be used to compute the unhappiness for a city. 1) If the plant is within two miles or less of a city, the unhappiness is infinite (that is, assign a very large number to the unhappiness for that city). 2) Otherwise, the unhappiness is equal to the population of the city divided by the distance of the plant from the city. The average unhappiness equals: Avg. Unhappiness = Sum of the unhappiness of 4 cities / The total state's population. Your program should select the site at which the average unhappiness is smallest. INPUT: ^^^^^^ The user should be prompted to enter the x and y coordinates and the population for each of the four cities from the keyboard. Each coordinate should be checked to ensure it is between 1 and 25. If not, the user should be prompted to enter a value which is in the correct range. The population should be entered in thousands of people. For example, if the population is 10,000, the user should input 10. OUTPUT: ^^^^^^^ The program should print a message indication the coordinates where the plant should be located. Following that message, the user should be prompted to enter a 1 to view a map of the scenario or a 0 to exit the program. An Example Scenario: Enter the x and y for City 1 : 1 1 Enter the Population for City 1: 10 Enter the x and y for City 2 : 1 25 Enter the Population for City 2: 10 Enter the x and y for City 3 : 25 1 Enter the Population for City 3: 10 Enter the x and y for City 4 : 25 25 Enter the Population for City 4: 10 ********** Locate the Plant At: 13 13 Enter 1 to view a Map of the scenario, or 0 to exit: 1 MAP OF SCENARIO --------------- C2<><><><><><><><><><><><><><><><><><><><><><><>C4 <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><>PP<><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> <><><><><><><><><><><><><><><><><><><><><><><><><> C1<><><><><><><><><><><><><><><><><><><><><><><>C3 Note: The previous scenario is just an example, you need to create your own test cases to make sure that your program produces the correct results. Required Implementation: ^^^^^^^^^^^^^^^^^^^^^^^ Implement a State.java file that contains the State class. The state class contains all the getters and setters for the data. It also contains the methods that reads in the input calculates the location of the plant, and displays the output. Another file is required is StatePlant.java which contains your main program and all it does is instantiate the state, read the input for the state, calculate the location, print the location, and based on the user's input, print the map.
Explanation / Answer
Please see below code:
package StatePlant;
import java.util.Scanner;
import plant.City;
import plant.Location;
import plant.State;
public class StatePlant
{
public static void main(String args[])
{
State st=new State();
City city;
Location loc;
int x,y;
Scanner sc=new Scanner(System.in);
for(int i=1;i<=4;i++)
{
System.out.println("Enter x and y for city "+i+": ");
x=sc.nextInt();
y=sc.nextInt();
loc=new Location();
loc.setX(x);
loc.setY(y);
city=new City();
city.setLoc(loc);
System.out.println("Enter population for City "+i+": ");
int population=sc.nextInt();
city.setPopulation(population);
st.addCity(city);
}
Location goodLoc=st.checkBestLocationForPlant();
System.out.println("Location for plant is : "+goodLoc.getX()+" "+goodLoc.getY());
st.printMap(goodLoc);
}
}
package plant;
import java.util.ArrayList;
import java.util.List;
public class State
{
private List<City> cityList;
private List<Location> locations;
public State()
{
cityList=new ArrayList<City>();
locations=new ArrayList<Location>();
}
public List<City> getCityList() {
return cityList;
}
public void setCityList(List<City> cityList) {
this.cityList = cityList;
}
public void addCity(City city)
{
cityList.add(city);
}
public Location checkBestLocationForPlant()
{
prepareUnhappinessValues();
Double minAvg=0.0;
Location minloc = null;
minAvg=locations.get(0).getAvgHappiness();
for(Location loc : locations)
{
if(minAvg>loc.getAvgHappiness())
{
minloc=loc;
}
}
return minloc;
}
private void prepareUnhappinessValues() {
Double distance=0.0;
double unhappiness=0;
Location loc;
for(int i=1;i<=25;i++)
{
for(int j=1;j<=25;j++)
{
loc=new Location();
loc.setX(i);
loc.setY(j);
unhappiness=0;
for(City city: cityList)
{
distance=getDistanceForCity(loc,city);
unhappiness=getUnhappinessValue(distance, loc,city);
loc.setUnhappiness(unhappiness);
}
locations.add(loc);
}
}
}
private double getUnhappinessValue(Double distance, Location loc, City city)
{
double unhappiness=0;
if(distance<=2)
{
return 999999;
}
else
{
unhappiness=city.getPopulation()/distance;
return unhappiness;
}
}
private Double getDistanceForCity(Location loc, City city)
{
int distance=0;
Location loc1=city.getLoc();
int x1=loc.getX();
int y1=loc.getY();
int x2=loc1.getX();
int y2=loc1.getY();
distance=((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2));
return Math.sqrt(distance);
}
public void printMap(Location loc)
{
for(int i=0;i<25;i++)
{
for(int j=0;j<25;j++)
{
if(cityExistsAt(i,j))
{
System.out.print("C");
}
else if(plantExists(i,j,loc))
{
System.out.print("P");
}
else
{
System.out.print("<>");
}
}
System.out.println();
}
}
private boolean plantExists(int i, int j, Location loc)
{
if((loc.getX()==i+1) &&(loc.getY()==j+1))
{
return true;
}
return false;
}
private boolean cityExistsAt(int i, int j)
{
for(City city : cityList)
{
if(city.getLoc().getX()==(i+1) && city.getLoc().getY()==(j+1))
{
return true;
}
}
return false;
}
}
package plant;
import java.util.ArrayList;
import java.util.List;
public class Location
{
private int x;
private int y;
private List<Double> unhappiness;
public Location()
{
unhappiness=new ArrayList<Double>();
}
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;
}
public List<Double> getUnhappiness() {
return unhappiness;
}
public void setUnhappiness(Double unhappiness)
{
this.unhappiness.add(unhappiness);
}
public Double getAvgHappiness()
{
Double avg=0.0;
for(Double unhappiness : unhappiness)
{
avg=avg+unhappiness;
}
return avg/unhappiness.size();
}
}
package plant;
public class City
{
private Location loc;
private int population;
public Location getLoc() {
return loc;
}
public void setLoc(Location loc) {
this.loc = loc;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
}
run the code and see the out put.
i have got the output as below:
Location for plant is : 25 22
C<><><><><><><><><><><><><><><><><><><><><><><>C
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
C<><><><><><><><><><><><><><><><><><><><>P<><>C
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.