0
public class Intersection {
	
	private static String intersect[]= new String[3];

	public  static void main(String args[])
	{
	 
	 
	 	// initialize  Intersection
		intersect[0]="A";
		intersect[1]="B";
		intersect[2]="C";
		
		for (int i=0; i<intersect.length ;i++)
		{System.out.println(intersect[i]);}
		
		Intersection inter= new Intersection();
		System.out.print(inter.getIndex("C"));
	 
		
					
			
	}
	//get index of intersection
	public  int getIndex(String str)
	{	
		for(int i=0;i<intersect.length;i++)
		{
			if(intersect[i].equalsIgnoreCase(str))
			{	
				 
				return i;				
			}			
		}
		return -1;
		
	}	
    
}
public interface Graph {
   
   //implement intersections and distance between them
  public void add(Intersection start, Intersection end, int distance);
  
  
  
  //get the shortest distance between two intersections
  public int getDistance(Intersection start, Intersection end);
  
  
  //store the list of intersection that lead to the given intersection.
  public List <Intersection> getPredecessor(Intersection v);

That's how I implement the graph interface so far.

import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.ArrayList;
import java.util.List;

public class Map implements Graph{

  private final int[][]distances;
  public Map(int intersect) 
  {
    	distances= new int[intersect][intersect];
  }
  //link two intersection by given distance
  public void add(Intersection start, Intersection end, int distance)
  {
  		distances[start.getIndex("A")][start.getIndex("B")]=distance;
	
  }
  
  public boolean isDestination(Intersection u){
  	
  	
  }
  
  
  public int getDistance(Intersection start, Intersection end)
  
  {
  	return distances[start.getIndex("A")][end.getIndex("B")];
  }
  public List <Intersection> getPredecessor(Intersection v){
  	List<Intersection> list = new ArrayList<Intersection>();
		
		for (int i = 0; i < distances.length; i++)
		{
			if (distances[i][v.getIndex("A")] > 0)
			{
				list.add( v );
			}
		}
		
		return list;
  }
    
}

There are two problems now. First one is I do not know how to continue to develop priority queue and second one is how to continue to implement Dijkstra's algorithm. I feel lost.
Your kind help is really appreciated

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by VernonDozier
0

Class Intersect - You have no constructor written. You have no non-static class variables. You have a non-static method called getIndex that refers to only static variables. You have this line:

Intersection inter= new Intersection();

which creates a new instance of a class that has no non-static variables and only public methods. What's the point of having an instance for such a class?

Here you have MULTIPLE instances of a class that contains no non-static variables. Again, what's the point?

//get the shortest distance between two intersections
  public int getDistance(Intersection start, Intersection end);
  
  
  //store the list of intersection that lead to the given intersection.
  public List <Intersection> getPredecessor(Intersection v);

Why did you make intersection[] static? If it was simply to correct an error, that's not a good reason. Having multiple instances of a class where all the variables are static seems pointless to me.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.