Question

In: Computer Science

please can you write the calc_plant method in another way, I don't understand way they assign...

please can you write the calc_plant method in another way, I don't understand way they assign these specific numbers to min, unhappy1...4. the question says 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.

mport java.io.*;
import java.util.*;
public class State {
  
private int citi1x,citi1y;
private int pop1;
private int citi2x,citi2y;
private int pop2;
private int citi3x,citi3y;
private int pop3;
private int citi4x,citi4y;
private int pop4;
private int plantx,planty;
public int getCity1X(){
return citi1x;
}
public int getCity1Y(){
return citi1y;
}
public int getCity2X(){
return citi2x;
}
public int getCity2Y(){
return citi2y;
}
public int getCity3X(){
return citi3x;
}
public int getCity3Y(){
return citi3y;
}
public int getCity4X(){
return citi4x;
}
public int getCity4Y(){
return citi4y;
}
public void setCity1(int a, int b){
citi1x = a;
citi1y = b;
}
public void setCity2(int a, int b){
citi2x = a;
citi2y = b;
}
public void setCity3(int a, int b){
citi3x = a;
citi3y = b;
}
public void setCity4(int a, int b){
citi4x = a;
citi4y = b;
}
public void setPop1(int a){
pop1 = a;
}
public void setPop2(int a){
pop2 = a;
}
public void setPop3(int a){
pop3 = a;
}
public void setPop4(int a){
pop4 = a;
}
public int getPop1(int a){
return pop1;
}
public int getPop2(int a){
return pop2;
}
public int getPop3(int a){
return pop3;
}
public int getPop4(int a){
return pop4;
}
public int getPlantX(){
return plantx;
}
public int getPlantY(){
return planty;
}
public void setPlantX(int a){
plantx = a;
}
public void setPlantY(int a){
planty = a;
}
public void read_input(){
int x,y;
int pop;
Scanner sc = new Scanner(System.in);
do {
System.out.print("Enter X and Y for city 1: ");
x = sc.nextInt();
y = sc.nextInt();
} while ((x < 1 || x > 25) && (y < 1 && y > 25));
setCity1(x,y);
System.out.print("Enter population for city 1: ");
pop = sc.nextInt();
setPop1(pop*1000);
do {
System.out.print("Enter X and Y for city 2: ");
x = sc.nextInt();
y = sc.nextInt();
} while ((x < 1 || x > 25) && (y < 1 && y > 25));
setCity2(x,y);
System.out.print("Enter population for city 2: ");
pop = sc.nextInt();
setPop2(pop*1000);

do {
System.out.print("Enter X and Y for city 3: ");
x = sc.nextInt();
y = sc.nextInt();
} while ((x < 1 || x > 25) && (y < 1 && y > 25));
setCity3(x,y);
System.out.print("Enter population for city 3: ");
pop = sc.nextInt();
setPop3(pop*1000);
do {
System.out.print("Enter X and Y for city 4: ");
x = sc.nextInt();
y = sc.nextInt();
} while ((x < 1 || x > 25) && (y < 1 && y > 25));
setCity4(x,y);
System.out.print("Enter population for city 4: ");
pop = sc.nextInt();
setPop4(pop*1000);
  
}

public void calc_plant(){

double min = 100000;
for (int i = 1; i<=25; i++){
  
for (int j = 1; j<=25; j++){
  
double unhappy1 = 0;
double unhappy2 = 0;
double unhappy3 = 0;
double unhappy4 = 0;
  
if ((i == citi1x && j == citi1y) || (i == citi2x && j == citi2y) || (i == citi3x && j == citi3y) || (i == citi4x && j == citi4y))
continue;
double dist = Math.sqrt((i-citi1x)*(i-citi1x) + (j-citi1y)*(j-citi1y));
if (dist <= 2){
unhappy1 = 10000000;
}
else {
unhappy1 = pop1/dist;
}
dist = Math.sqrt((i-citi2x)*(i-citi2x) + (j-citi2y)*(j-citi2y));
if (dist <= 2){
unhappy2 = 1000000;
}
else {
unhappy2 = pop2/dist;
}
dist = Math.sqrt((i-citi3x)*(i-citi3x) + (j-citi3y)*(j-citi3y));
if (dist <= 2){
unhappy3 = 1000000;
}
else {
unhappy3 = pop3/dist;
}
dist = Math.sqrt((i-citi4x)*(i-citi4x) + (j-citi4y)*(j-citi4y));
if (dist <= 2){
unhappy4 = 1000000;
}
else {
unhappy4 = pop4/dist;
}
double unhappy = (unhappy1 + unhappy2 + unhappy3 + unhappy4)/(pop1+pop2+pop3+pop4);
  
if (unhappy < min){
min = unhappy;
plantx = i;
planty = j;

}
}
}
  
}
  
public void display_map(){
for (int i = 1; i <=25; i++){
for (int j = 1; j <=25; j++){
if (i == citi1x && j == citi1y)
System.out.print("C1");
else if (i == citi2x && j == citi2y)
System.out.print("C2");
else if (i == citi3x && j == citi3y)
System.out.print("C3");
else if (i == citi4x && j == citi4y)
System.out.print("C4");
else if (i == plantx && j == planty)
System.out.print("PP");
else
System.out.print("<>");
}
System.out.println();
}
System.out.println();
}


}

import java.util.Scanner;

public class StatePlant {
   public static void main(String[] args){
  
   Scanner sc = new Scanner(System.in);
   State s = new State();
   s.read_input();
   s.calc_plant();
   System.out.println("Locate the plan at : " + s.getPlantX() + " " +s.getPlantY() );
   System.out.println("Enter 1 to view a Map of the scenario, or 0 to exit:");
   String inp = sc.nextLine();
   if (inp.charAt(0) == '1'){
   s.display_map();
   }
  
   }
}

I appreciate any help

Solutions

Expert Solution

import java.util.Scanner;

class State {

        private int cityX[], cityY[];
        private int pop[];

        private int plantx, planty;

        public State() {
                cityX = new int[4];
                cityY = new int[4];
                pop = new int[4];
        }

        public int getCityX(int index) {
                return cityX[index];
        }

        public int getCityY(int index) {
                return cityY[index];
        }

        public void setCity(int a, int b, int index) {
                cityX[index] = a;
                cityY[index] = b;
        }

        public void setPop(int a, int index) {
                pop[index] = a;
        }

        public int getPop(int index) {
                return pop[index];
        }

        public int getPlantX() {
                return plantx;
        }

        public int getPlantY() {
                return planty;
        }

        public void setPlantX(int a) {
                plantx = a;
        }

        public void setPlantY(int a) {
                planty = a;
        }

        public void read_input() {
                int x, y;
                int pop;
                Scanner sc = new Scanner(System.in);

                for (int i = 0; i < 4; i++) {

                        do {
                                System.out.print("Enter X and Y for city " + (i + 1) +": ");
                                x = sc.nextInt();
                                y = sc.nextInt();
                        } while ((x < 1 || x > 25 || y < 1 || y > 25));

                        setCity(x, y, i);

                        System.out.print("Enter population for city " + (i + 1) + ": ");
                        pop = sc.nextInt();
                        setPop(pop * 1000, i);
                }

        }

        public void calc_plant() {

                double min = 100000;
                for (int i = 1; i <= 25; i++) {

                        for (int j = 1; j <= 25; j++) {

                                if ((i == cityX[0] && j == cityY[0]) || 
                                                (i == cityX[1] && j == cityY[1]) || 
                                                (i == cityX[2] && j == cityY[2]) || 
                                                (i == cityX[3] && j == cityY[3]))
                                        continue;

                                double totalPopulation = 0;
                                double totalUnhappy = 0;
                                
                                for(int x=0; x<4; x++) {
                                        double dist = Math.hypot(i - cityX[x], j - cityY[x]);
                                        if (dist <= 2) {
                                                totalUnhappy += 10000000;
                                        } else {
                                                totalUnhappy += pop[x] / dist;
                                        }
                                        
                                        totalPopulation += pop[x];
                                }
                                
                                double unhappyRatio = totalUnhappy / totalPopulation;

                                if (unhappyRatio < min) {
                                        min = unhappyRatio;
                                        plantx = i;
                                        planty = j;
                                }
                        }
                }

        }

        public void display_map() {
                for (int i = 1; i <= 25; i++) {
                        for (int j = 1; j <= 25; j++) {
                                if (i == cityX[0] && j == cityY[0])
                                        System.out.print("C1");
                                else if (i == cityX[1] && j == cityY[1])
                                        System.out.print("C2");
                                else if (i == cityX[2] && j == cityY[2])
                                        System.out.print("C3");
                                else if (i == cityX[3] && j == cityY[3])
                                        System.out.print("C4");
                                else if (i == plantx && j == planty)
                                        System.out.print("PP");
                                else
                                        System.out.print("<>");
                        }
                        System.out.println();
                }
                System.out.println();
        }

}

public class StatePlant {
        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);
                State s = new State();
                s.read_input();
                s.calc_plant();
                System.out.println("Locate the plan at : " + s.getPlantX() + " " + s.getPlantY());
                System.out.println("Enter 1 to view a Map of the scenario, or 0 to exit:");
                String inp = sc.nextLine();
                if (inp.charAt(0) == '1') {
                        s.display_map();
                }
                sc.close();
        }
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Can someone please explain these problems, I don't understand, please and thank you!! The patients in...
Can someone please explain these problems, I don't understand, please and thank you!! The patients in the Digoxin trial dataset can be considered a population of people suffering from heart failure. These patients were examined before the drug trial began, and their heart rate and blood pressure were recorded. The mean and standard deviation of the variables are listed below. Each variable follows a normal distribution. Heart rate (beats/min)                          μ = 78.8            σ = 12.66 Systolic blood pressure (mmHg)             μ...
can you please explain to me in simplest way that i can understand the cyert and...
can you please explain to me in simplest way that i can understand the cyert and march behaviour theory. kindly give an example for it. thank you so much.
Can you please write this in python and comment as well so I can understand what...
Can you please write this in python and comment as well so I can understand what yo are doing. Thank you. 1)Loop through list A, at each iteration, show the square root result of that element. Add proper text to your print function. A = [-4, 1, -16, 36, -49, 64, -128] 2)Create a counter variable, name it i and initialize it to 0. Using a for loop, count how many numbers are divisible by 3 in range of 1...
Please define moment of arm (regarding physics) in a way I can understand. it is referring...
Please define moment of arm (regarding physics) in a way I can understand. it is referring to the arm on a mousetrap car that was built. I need to discuss its effectiveness
Please solve and explain all parts. I don't understand You decide to play a trick on...
Please solve and explain all parts. I don't understand You decide to play a trick on your friend by dropping a raw egg onto their head The egg is dropped (no initial velocity) from a height of 15 meters. Your goal is to have the y position of the egg exactly match the height of your friend at the same moment he steps onto the red x you painted on the ground (not suspicious at all). Your friend has a...
HI, I hope you are doing well. I really don't understand this question and don't know...
HI, I hope you are doing well. I really don't understand this question and don't know how to solve it at all because I am completely new to this c++ programming. can you please explain each line of code with long and clear comments? please think of me as someone who doesn't know to code at all. and I want this code to be written in c++ thank you very much and I will make sure to leave thumbs up....
Hi, I have the answers, but I don't understand how to get the answers. Please explain...
Hi, I have the answers, but I don't understand how to get the answers. Please explain thoroughly. Bob earns ($25,000) in passive losses from BHI partnership. He has an outside basis of $40,000 of which $30,000 comes from non-recourse debt, and he has passive income of $50,000. What are the tax consequences to Bob? $10,000 deductible loss What basis does Bob take in his partnership interest? $15,000 How much is Bob at-risk after the allocation? 0 How much, if any...
please i need unique answer , don't copy and paste ,, don't use handwriting.. can you...
please i need unique answer , don't copy and paste ,, don't use handwriting.. can you complete my answer , i need you answer b only Question: 3- Al Yamamah Steel Industries Co. uses the step method for allocating the costs of its service departments to operating departments. The company has two support departments (Human Resource and Information Technology) and two operating departments (Hot Rolled Hollow Steel and Cold Rolled Hollow Steel). Al Yamamah Steel Industries Co. decided to allocate...
Use induction to solve the problem. Can you show me the steps too? I don't understand...
Use induction to solve the problem. Can you show me the steps too? I don't understand how to solve this. 3+4+5+...+(n+2)=1/2n(n+5) 1+5+52+...+5(n-1)=1/4(5n-1)
please I don't really know how to start answering this question I really need to understand...
please I don't really know how to start answering this question I really need to understand it please show the work with a clear handwriting A collision in one dimension A mass m1 = 2 kg moving at v1i = 3 ms−1 collides with another mass m2 = 4 kg moving at v2i = −2 ms−1. After the collision the mass m1 moves at v1f = −3.66 ms−1. (a) Calculate the final velocity of the mass m2. (b) After the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT