Question

In: Computer Science

change the do while by using while or any other way public void read_input(){ int x,y;...

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);
}

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

#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y);...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y); int main(){ char s1[] = "zoey"; char s2[] = "zoey"; string s3 = s1; string s4 = s2; cout << "1. sizeof(s1) = " << sizeof(s1) << endl; if(s1 == s2) cout << "2. s1 == s2 is true" << endl; else cout << "2. s1 == s2 is false" << endl; if(s3 == s4) cout << "3. s3 == s4 is true" <<...
public void printNumbers(int low, int high) { // using a for loop, print all numbers from...
public void printNumbers(int low, int high) { // using a for loop, print all numbers from low to high } public int sumOfNumbers(int n) { // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 } public void printMultiplicationTable(int num) { // using a for loop, print the multiplication table of num (up to first 10!) // i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25,...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m = new int[x][y]; } public Matrix(int x, int y, int z) { m = new int[x][y]; for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { m[i][j] = z; } } } public int rowsum(int i) throws IndexOutOfBoundsException { if (i < 0 || i > m.length-1) { throw new IndexOutOfBoundsException("Invalid Row"); } int sum =...
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers...
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers from low to high for(int i = low; i <= high; i++){ System.out.println(i); } } public int sumOfNumbers(int n){ // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 int sum = 0; for(int i = 1; i <= n; i++){ sum += i; } return sum; } public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int x, int y, int z); void add (int arr [10], int first, int last); void print (int arr [10]); int main () {     int my_arr [10];         cout << "The original array is:\n";     print (my_arr);         start (my_arr);     cout << "\n\nThe array after start is:\n";     print (my_arr);         move (my_arr, 2, 4, 6);     cout << "\n\nThe...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT