Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help modifying my java code to create a method that retieves the weight from the adjacency list and returns it and prints it

I need help modifying my java code to create a method that retieves the weight from the adjacency list and returns it and prints it out

I created a code that represents this graph of a train station and I want to print out the time it would take to get from station 2 to station 3

and use a print statement in the main method to print out 73 Mins

Please do not just add them up. I need a method to be created and used that would accomplish this.

image text in transcribed

Using the Code provided

import java.util.*;

class Graph

{

// Edge object that is stored for each connection to another node

class Edge

{

int v,w;

public Edge(int v,int w)

{

this.v=v; this.w=w;

}

@Override

public String toString()

{

return "("+v+","+w+")";

}

}

// An array of lists of Edge objects

List G[];

// Parameterized constructor

public Graph(int n)

{

G = new LinkedList[n];

// For each node in the graph, initialize an empty adjacency list

for(int i=0; i

G[i]=new LinkedList();

}

// Check if node U is connected to node V

boolean isConnected(int u,int v)

{

// Check each edge for this node to see if it connects to node V

for(Edge i: G[u])

if(i.v==v) return true;

return false;

}

// For node U, add a new connection to node V, with weight W

void addEdge(int u,int v,int w)

{

G[u].add(new Edge(v,w)); G[v].add(new Edge(u,w));

}

// Override the java default toString() method so we can print

// our Graph in the format we want

@Override

public String toString()

{

String result="";

for(int i=1;i

result+=i+"=>"+G[i]+" ";

return result;

}

}

public class GraphExample {

public static void main(String[] args)

{

Graph g=new Graph(7);

g.addEdge(1, 2, 38); g.addEdge(1, 3, 35); g.addEdge(1, 4, 11); g.addEdge(1, 5, 19); g.addEdge(3, 4, 27); g.addEdge(3,5,28); g.addEdge(3,6,13); g.addEdge(5, 6, 26); System.out.println(g);

}

}

Assignment 9: Graph Metro Station Map with Travel Time in Minutes. 2 6 26 5 13 28 19 38 3 35 1 27 4 Assignment 9: Graph Metro Station Map with Travel Time in Minutes. 2 6 26 5 13 28 19 38 3 35 1 27 4

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

Explain the meaning and importance of nonverbal messages.

Answered: 1 week ago