In: Computer Science
change the do while by using while or any other way
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);
}
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.