Computer Science, asked by sunil7517, 1 year ago

Write a program that reads the coordinates of his house and 3 houses in order they are visited. the program must print the total distance travelled by the milkman when he reaches home after the deliveries. also, compute and print the maximum and minimum x coordinate value visited by the milkman. all roads in the city are parallel to one of the two axes and intersect each other exactly at 90o .

Answers

Answered by IamSatyam
0
//writing in java using distance formula and array
//these concepts need to be clear 

import java.io.*;class A{public static void main()throws IOException
{
BufferedReader var=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Cooridate of milkman");//format: a,b
String house=var.readLine();
System.out.println("Cooridate of customer 1");
String cus_house1=var.readLine();
System.out.println("Cooridate of customer 2");
String cus_house2=var.readLine();
System.out.println("Cooridate of customer 3");
String cus_house3=var.readLine();
double x=0,y=0;
double a[]=new double[3];
x=house.substring(0,house.lastIndexOf(",")+1);
y=house.substring(house.lastIndexOf(","));
double x1=0,y1=0;
x1=cus_house1.substring(0,cus_house1.lastIndexOf(",")+1);
y1=cus_house1.substring(cus_house1.lastIndexOf(","));
//total distance travelled
double total_dist=0;
//distance between milkman and customer1
total_dist=total_dist+Math.sqrt((Math.pow(x-x1,2)+(Math.pow(y-y1,2)));
a.[0]=total_dist;
//distance betwwen customer 1 to customer 2
x=cus_house1.substring(0,cus_house1.lastIndexOf(",")+1);
y=cus_house1.substring(cus_house1.lastIndexOf(","));
x1=cus_house2.substring(0,cus_house2.lastIndexOf(",")+1);
y1=cus_house2.substring(cus_house2.lastIndexOf(","));
total_dist=total_dist+Math.sqrt((Math.pow(x-x1,2)+(Math.pow(y-y1,2)));
a.[1]=total_dist;
//distance betwwen customer 2 to customer 3
x=cus_house2.substring(0,cus_house2.lastIndexOf(",")+1);
y=cus_house2.substring(cus_house2.lastIndexOf(","));
x1=cus_house3.substring(0,cus_house3.lastIndexOf(",")+1);
y1=cus_house3.substring(cus_house3.lastIndexOf(","));
total_dist=total_dist+Math.sqrt((Math.pow(x-x1,2)+(Math.pow(y-y1,2)));
a.[2]=total_dist;
//printing total distance
System.out.println("TOTAL DISTANCE="+total_dist);
System.out.println("MAXIMUM DISTANCE="+getMax(a));
System.out.println("MINIMUM DISTANCE+"+getMin(a));
}
public static double getMax(double[] inputArray)
{    
double maxValue = inputArray[0];
 for(int i=1;i < inputArray.length;i++){      
if(inputArray[i] > maxValue){        
  maxValue = inputArray[i];    
  }  
  }  
  return maxValue;  
 }

public static double getMin(double[] inputArray){ 
    double minValue = inputArray[0];  
  for(int i=1;i<inputArray.length;i++){  
     if(inputArray[i] < minValue){      
  minValue = inputArray[i];    
  }    
 }    
 return minValue;  
 } 
}
Similar questions