In: Computer Science
please I don't understand this code. Can you put comments to explain the statements. Also, if there any way to rewrite this code to make it easier, that gonna help me a lot.
import 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();
}
}
}
Java code with comments
============================================================================================
//StatePlant.java
import java.util.Scanner;
public class StatePlant {
public static void main(String[] args){
//scanner class object for taking input from
keyboard
Scanner sc = new Scanner(System.in);
//state class object
State s = new State();
//input from user
s.read_input();
//plant calculate
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(); //input from user
if (inp.charAt(0) == '1'){
s.display_map(); //display map() call
}
}
}
============================================================================================
//State.java
import java.io.*;
import java.util.*;
public class State {
//instance variable declare
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;
//getter for each varible
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 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;
}
// setter for each variable
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 void setPlantX(int a){
plantx = a;
}
public void setPlantY(int a){
planty = a;
}
//read_input() function defination
public void read_input(){
//local variable declare
int x,y;
int pop;
//scanner class object for taking input from keyboard
Scanner sc = new Scanner(System.in);
//input valid point from user by using loop for city 1
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));
//set point for city 1
setCity1(x,y);
//input population for city 1
System.out.print("Enter population for city 1: ");
pop = sc.nextInt();
setPop1(pop*1000);
//input valid point from user by using loop for city 2
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));
//set point for city 2
setCity2(x,y);
//input population for city 2
System.out.print("Enter population for city 2: ");
pop = sc.nextInt();
setPop2(pop*1000);
//input valid point from user by using loop for city 3
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));
//set point for city 3
setCity3(x,y);
//input population for city 3
System.out.print("Enter population for city 3: ");
pop = sc.nextInt();
setPop3(pop*1000);
//input valid point from user by using loop for city 4
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));
//set point for city 4
setCity4(x,y);
//input population for city 4
System.out.print("Enter population for city 4: ");
pop = sc.nextInt();
setPop4(pop*1000);
}
//plant calculate
public void calc_plant(){
double min = 100000;
//nested for loop
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 condtion
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));
//nested if else
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;
}
}
}
}
//display map() function
public void display_map(){
//nested for loop
for (int i = 1; i <=25; i++){
for (int j = 1; j <=25; j++){
//nested if else block
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();
}
}
============================================================================================
output