Question: Problem 2 ( 4 points ): Create a new class called FriendGraph that extends the AdjacencyMatrixBasedNonDirectedGraphData class and add a new method called addContact() Create

Problem 2 (4 points): Create a new class called FriendGraph that extends the AdjacencyMatrixBasedNonDirectedGraphData class and add a new method called addContact()

Create a new class called FriendGraph that extends the AdjacencyMatrixBasedNonDirectedGraphData class. Like that class, the FriendGraph class should also be defined using a generic data type (E).

Create a method in the FriendGraph class called addContact(). This method should take in two Vertex objects: a starting vertex and an ending vertex. In the body of this method, you need to add one to the current value of the existing edge weights between these two vertices. Since this is a non-directed graph, you will need to update the edge weights in both direction (i.e., from start to end and from end to start). HINT: Look at the addEdge() method to give you some ideas how to do this.

AdjacencyMatrixBasedNonDirectedGraphData class:

package model;

import java.util.*;

public class AdjacencyMatrixBasedNonDirectedGraphData implements NonDirectedGraphDataInterface

{

List> vertices = null;

int[][] edgeWeights = null;

public List> getVertices(){

if(vertices ==null){

vertices = new ArrayList>();

}

return vertices;

}

private int[][] getEdgeWeights(){

if(edgeWeights == null){

edgeWeights = new int[0][0];

}

return edgeWeights;

}

private void setEdgeWeights(int[][] edgeWeights){

this.edgeWeights = edgeWeights;

}

@Override

public void addVertex(Vertex v) {

getVertices().add(v);

int [][] newEdgeWeights = new int[getVertices().size()][getVertices().size()];

for(int row=0; row< newEdgeWeights.length; row++){

for(int col =0; col

if(row

newEdgeWeights[row][col] = getEdgeWeights()[row][col];

}else{

newEdgeWeights[row][col]=0;

}

}

}

setEdgeWeights(newEdgeWeights);

}

@Override

public void removeVertex(Vertex v) {

int index = getVertices().indexOf(v);

if(index!= -1){

int [][] newEdgeWeights = removeRowColFromArray(getEdgeWeights(), index, index);

setEdgeWeights(newEdgeWeights);

getVertices().remove(index);

}

}

@Override

public void addEdge(Vertex startingVertex, Vertex endingVertex, int weight) {

if(getVertices().contains(startingVertex)&& getVertices().contains(endingVertex)){

int row = getVertices().indexOf(startingVertex);

int col = getVertices().indexOf(endingVertex);

getEdgeWeights()[row][col] = weight;

getEdgeWeights()[col][row] = weight;

}

}

@Override

public void removeEdge(Vertex startingVertex, Vertex endingVertex) {

if(getVertices().contains(startingVertex) && getVertices().contains(endingVertex)){

int row = getVertices().indexOf(startingVertex);

int col = getVertices().indexOf(endingVertex);

getEdgeWeights()[row][col] = 0;

getEdgeWeights()[col][row] = 0;

}

}

@Override

public boolean hasEdge(Vertex startingVertex, Vertex endingVertex) {

boolean hasEdge = false;

if(getVertices().contains(startingVertex) && getVertices().contains(endingVertex)){

int row = getVertices().indexOf(startingVertex);

int col = getVertices().indexOf(endingVertex);

if(getEdgeWeights()[row][col] != 0){

hasEdge = true;

}

}

return hasEdge;

}

private int[][] removeRowColFromArray(int[][] startingArray, int skipRow, int skipCol){

int newSize = startingArray.length-1;

int[][] newArray = new int[newSize][newSize];

int newRow = 0;

int newCol = 0;

for(int row=0; row

for(int col=0; col

if(row>= skipRow){

newRow = row+1;

}

else{

newRow = row;

}

if(col>= skipCol){

newCol = col+1;

}

else{

newCol = col;

}

newArray[row][col] = startingArray[newRow][newCol];

}

}

return newArray;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!