0

I have the following thing to do:Task

I've made the Node and Edge class (I hope I got them right) and I'm stuck on the GraphModel class. Here is my code so far:

Node.java (ignore the getNr)

import java.awt.Rectangle;

public class Node {
    private String name;
    private Rectangle rectangle;


    public Node(int x, int y, int width, int height, String name) {
        this.rectangle =new Rectangle(x, y, width, height);
        this.name = name;
    }

    public Node() {
        this.rectangle = new Rectangle (0,0,0,0); //gonna change the parameters later
        this.name = "default name";
    }

    public int getNr() {
        return nr;
    }

    public String getName() {
        return name;
    }
}

Edge.java:

public class Edge {
    private Node nodeA;
    private Node nodeB;

    public Edge(Node nodeA, Node nodeB) {
        this.nodeA = nodeA;
        this.nodeB = nodeB;
    }

    public Node getNodeA() {
        return nodeA;
    }

    public Node getNodeB() {
        return nodeB;
    }
}

Now, I hope they are correct but if you notice there is something not right please let me know. Now about the GraphModel, I used a code that makes a graph with a given number of nodes (which is not good because I want to increase/decrease the N (number of nodes, you will see in the code) when I add/remove a node) . Also, the nodes are represented by integers, I wonder how can I make them be represented by the actual node class that I've created (because I will later have to make a GUI for that). I've tried to use ArrayList<ArrayList<Node>> but I have no idea what should I do next. Here is the code GraphModel.java:

import java.util.*;

public class GraphModel {
    private int numNode;
    private Node node;

    /* Add edge for undirected graph (I don't understand why these give opposite errors like java do you want an int or a node????
    static void addEdge(ArrayList<ArrayList<Node>> gr, Edge edge) {   <---- Initially made list of Node type but I'm noob and it doesn't work
        gr.get(edge.getNodeA().getNr()).add(edge.getNodeB().getNr());
        gr.get(edge.getNodeB()).add(edge.getNodeA());

    }
    */
    static void addEdge(ArrayList<ArrayList<Integer>> gr, int s, int d) {
        gr.get(s).add(d);
        gr.get(d).add(s);

    }

    /*next todo
    static void delEdge(ArrayList<ArrayList<Integer>> gr,
                        int s, int d)
     */


        public static void makeGraph(){
        int NR=4; //I've tried to make it dynamic but I've no idea how (like, adjust it's size when adding edges (will do)
        ArrayList<ArrayList<Integer>> gr = new ArrayList<ArrayList<Integer>>(NR);
        for (int i = 0; i < NR; i++)
            gr.add(new ArrayList<Integer>());


        GraphModel.addEdge(gr,0,1);
        GraphModel.addEdge(gr,0,2);
        GraphModel.addEdge(gr,0,3);
        GraphModel.addEdge(gr,1,2);
        GraphModel.addEdge(gr,1,3);
        printGraph(gr);
    }

    static void printGraph(ArrayList<ArrayList<Integer>> gr) {
        for (int i = 0; i < gr.size(); i++) {
            System.out.println("\nNode " + i + ":");
            for (int j = 0; j < gr.get(i).size(); j++) {
                System.out.print(" -> " + gr.get(i).get(j));
            }
            System.out.println();
        }
    }
}

Hope that makes sense, if not feel free to ask me anything. Also keep in mind that I am a beginner in Object oriented programming, started it like 1 week ago. Thank you very much for your time!

1 Answer 1

1

You can do this

For GraphModel class

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class GraphModel {
    Set<Node> nodes;
    Set<Edge> edges;

    public GraphModel() {
        this.nodes = new HashSet<>();
        this.edges = new HashSet<>();
    }

    // add node
    public void addNode(Node node) {
        nodes.add(node);
    }

// add edge
public void addEdge(Edge edge) {
    edges.add(edge);
}

// remove node
public void removeNode(Node node) {
    nodes.remove(node);
    // remove all edges who are connected with this node
    List<Edge> edgesToRemove = new ArrayList<>();
    // get all edges who are connected to this node
    for(Edge edge: edges){
        if(edge.getNodeA().equals(node) || edge.getNodeB().equals(node)){
            edgesToRemove.add(edge);
        }
    }
    // remove edges
    edges.removeAll(edgesToRemove);
}

// remove an edge
public void removeEdge(Edge edge) {
    edges.remove(edge);
}

public static void main(String[] args) {
    GraphModel graph = new GraphModel();
    Node A = new Node(0, 0, 1, 1, "A");
    Node B = new Node(1, 1, 1, 1, "B");
    graph.addNode(A);
    graph.addNode(B);
    Edge edge = new Edge(A, B);
    graph.addEdge(edge);
    System.out.println(graph);
    graph.removeNode(A);
    System.out.println(graph);
}

@Override
public String toString() {
    return "GraphModel{" +
            "nodes=" + nodes +
            ", edges=" + edges +
            '}';
}
}

For Node class

import java.awt.*;

public class Node {
private String name;
private Rectangle rectangle;


public Node(int x, int y, int width, int height, String name) {
    this.rectangle = new Rectangle(x, y, width, height);
    this.name = name;
}

public Node() {
    this.rectangle = new Rectangle(0, 0, 0, 0); //gonna change the parameters later
    this.name = "default name";
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Rectangle getRectangle() {
    return rectangle;
}

public void setRectangle(Rectangle rectangle) {
    this.rectangle = rectangle;
}

@Override
public boolean equals(Object o) {
    Node node = (Node) o;
    return this.name.equals(node.getName());
}

@Override
public String toString() {
    return "Node{" +
            "name='" + name + '\'' +
            ", x=" + rectangle.x +
            ", y=" + rectangle.y +
            ", width=" + rectangle.width +
            ", height=" + rectangle.height +
            '}';
}
}

and for Edge class

public class Edge {
private Node nodeA;
private Node nodeB;

public Edge(Node nodeA, Node nodeB) {
    this.nodeA = nodeA;
    this.nodeB = nodeB;
}

public Node getNodeA() {
    return nodeA;
}

public Node getNodeB() {
    return nodeB;
}

@Override
public String toString() {
    return "Edge{" +
            "nodeA=" + nodeA +
            ", nodeB=" + nodeB +
            '}';
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.