Question

JAVA Bike shares are becoming increasingly common in cities in the United States. Commuters have been...

JAVA
Bike shares are becoming increasingly common in cities in the United States. Commuters have been using bike sharing as a method of transportation for the flexibility, cost savings, exercise, and a myriad of other benefits. While being quite successful, many bike shares are still relatively new and have room for optimization.  To help with the optimization process, we want to analyze bike share data from the city of Los Angeles. The first step of the process is to collect data from each bikeshare station.

Now you are hired to visit the bikeshare stations (2<=N<=100,000) and collect data. All stations should be visited in sequence, where station 1 is the starting point and station N is the finish. You are supposed to visit all of these bikeshare stations one by one, but it is OK if you decide to skip one station in order make your whole travel distance shorter. You cannot skip bikestation 1 and N.

Please design a program named BikeshareDistance.java and implement the following methods to find the minimum distance that you have to travel if you can skip up to one bikeshare station.

   public static long distance(String fileName) throws IOException
   This method returns the minimum distance that you have to travel stations (specified in the file) if can skip upto one bike station.

   public static long[] distance(String[] fileNames) throws IOException
   This method returns minimum travel distance corresponding to each file.
  
  For the data stored in the fileName(s), please refer to the Input File Formt

Since the routes in a downtown area are set with a grid of streets, the distance between two bikeshare station at locations (x1, y1) and (x2, y2) is given by "Manhattan" distance, which is measured by |x1-x2| + |y1-y2|- the absolute difference in x plus the absolute difference in y.

The Manhattan distance is based on the grid-like street geography of the New York borough of Manhattan. As shown in the following figure - Red: Manhattan distance. Green: diagonal, straight-line distance. Red, blue, yellow: equivalent Manhattan distances.



Input file format:

The first line contains a number N-total bikeshare stations, followed by N lines containing coordinates (x, y) of the N stations, both x and y are within the range of [-1000, 1000]. The bikeshare stations are given in the order that they should be checked. Some stations may be checked more than once at difference times in the predetermined sequence. If you skip such bikeshare station, you skip only one instance of the station--you DO NOT skip every visit at the same station.

SAMPLE INPUT:

4
0 0
8 3
11 -1
10 0

The minimum distance traveled is 14.

Homework Answers

Answer #1

CODE:

import java.util.*;
import java.lang.*;
import java.io.*;
class BikeshareDistance
{
public static long[] distance(String[] fileNames) throws IOException
{
        long[] ans=new long[fileNames.length];//array to store the answers.
       for(int m=0;m<fileNames.length;m++)
       {
           //creates a file scanner object.
           Scanner scan=new Scanner(new File(fileNames[m]));
           int N;//number of bike stations.
           int i,j;//loop variables.
           long distsum=0;
           long min;//to store te minimum distance.
           N=scan.nextInt();
           long[] x=new long[N]; //array to store the x cordinate.
           long[] y=new long[N];//array to store y cordinate.
           //loop to take inputs.
           for(i=0;i<N;i++)
           {          
               x[i]=scan.nextLong();//taking x cordinate from file.
               y[i]=scan.nextLong();//taking y cordinate from file.
           }
           //calculating the total distance first.
           for(j=0;j<N-1;j++)
           {
               distsum=distsum+Math.abs(x[j]-x[j+1])+Math.abs(y[j]-y[j+1]);//manhattan distance.
           } //calcualates the total distance.
           min=distsum; //starts with maximum
           for(i=1;i<N-1;i++)
           {   
               //calculate all possible skips except 1 and n
               long tempdist=0,particular,temptotal;
               for(j=i-1;j<i+1;j++)
               {
                   //manhattan distance.
                   tempdist=tempdist+Math.abs(x[j]-x[j+1])+Math.abs(y[j]-y[j+1]);
               }
               particular = Math.abs(x[i-1]-x[i+1])+Math.abs(y[i-1]-y[i+1]);//distance at particular position.
               temptotal=distsum-tempdist+particular; //this calculates the total value if i th position is skipped
               if(min>temptotal)
               {
                   //minimum value is stored here.
                   min=temptotal;
               }
           }
           //minimum value is stored in the answer.
           ans[m]=min;
       }
       //returning answer
       return ans;
   }
   public static void main(String []args) throws IOException
   {
       String[] l={"input.txt"};//take the strings of names of the inputs here.
       long[] k=distance(l);//calling the distance functioin and storing them in k array.
       for(int i=0;i<l.length;i++)
       {
           //printing the output according to file.
           System.out.println("The minimum distance of inputs in file "+l[i]+" is : "+k[i]);
       }
   }
}
CODE ATTACHMENTS:

input.txt:

OUTPUT:

I have done for the filenames.There you need to change the array names in the main method.

Then the distance method takes those strings and returns the answers.

Please do comment for any queries.
PLease like it.
Thank You.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT