返回信息流有偿求解
(a)What is an exception in Java? Give an example (not from the lecture notes) to show how exceptions can be used in Java, making sure you cover the definition of an exception and code-user and code-owner code associated with the use of an exception.
(b) Describe how exceptions can be used to design classes in a safe manner. Describe three other features of the safe classes style, explaining the benefits of each feature you have chosen.
(c) A network consists of a number of nodes connected together with links. The actual network structure is dynamic in that nodes can go off line and be added, and also connections between two nodes can be made and broken. Show how to model this network as a collection of Node objects and Link objects, together with appropriate methods, in a safe style. To do this you should define invariants and explain how adding and removing nodes and links take place in a safe way using the methods you have chosen. A requirement of the system is that given a Node object, it is always possible to find all the links connected to that node, and also that each Link object can return the Nodes that it connects.
这是一条镜像帖。来源:北邮人论坛 / java / #66761同步于 2023/4/27
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
有没有大神会这种题
zhrbocool
2023/4/27镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
你这样回答:
I promise I will do my homework by myself.
【 在 zhrbocool 的大作中提到: 】
: 有偿求解
: (a)What is an exception in Java? Give an example (not from the lecture notes) to show how exceptions can be used in Java, making sure you cover the definition of an exception and code-user and code-owner code associated with the use of an exception.
: (b) Describe how exceptions can be used to design classes in a safe manner. Describe three other features of the safe classes style, explaining the benefits of each feature you have chosen.
: ...................
前两个是概念题,上网查就行。对图的表示方式有链表和矩阵,对于这个可以新增删除的,使用链表我觉得更好。每个节点存储相连的节点,每条边存储连接的节点。当新增节点时,增加一个key,新增边时,增加边对应。以及在对应的节点链表上增加节点。如果这个方法可以,应该是leetcode 中等难度。
## Answer
### (a) What is an exception in Java? Give an example (not from the lecture notes) to show how exceptions can be used in Java, making sure you cover the definition of an exception and code-user and code-owner code associated with the use of an exception.
In Java, an exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Exceptions are used to handle errors and other exceptional events that occur during the execution of a program.
Here is an example of how exceptions can be used in Java:
public class Example {
public static void main(String[] args) {
try {
int[] arr = new int[5];
arr[10] = 50; // This line will throw an ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
In this example, we create an array of size 5 and try to access the 10th element, which does not exist. This will throw an ArrayIndexOutOfBoundsException. We catch this exception using a try-catch block and print an error message.
The code inside the try block is the code-owner code, which is the code that may throw an exception. The code inside the catch block is the code-user code, which is the code that handles the exception.
### (b) Describe how exceptions can be used to design classes in a safe manner. Describe three other features of the safe classes style, explaining the benefits of each feature you have chosen.
Exceptions can be used to design classes in a safe manner by providing a way to handle errors and other exceptional events that may occur during the execution of a program. By using exceptions, we can ensure that our code is robust and can handle unexpected situations.
Here are three other features of the safe classes style:
1. Encapsulation: Encapsulation is the practice of hiding the implementation details of a class from the outside world. By encapsulating our code, we can ensure that the internal state of our objects is not modified in unexpected ways. This helps to prevent bugs and makes our code more reliable.
2. Invariants: Invariants are conditions that must always be true for a class to be considered valid. By defining invariants for our classes, we can ensure that our code is always in a consistent state. This helps to prevent bugs and makes our code more reliable.
3. Defensive programming: Defensive programming is the practice of writing code that is designed to handle unexpected situations. By using defensive programming techniques, we can ensure that our code is robust and can handle unexpected situations. This helps to prevent bugs and makes our code more reliable.
### (c) A network consists of a number of nodes connected together with links. The actual network structure is dynamic in that nodes can go off line and be added, and also connections between two nodes can be made and broken. Show how to model this network as a collection of Node objects and Link objects, together with appropriate methods, in a safe style. To do this you should define invariants and explain how adding and removing nodes and links take place in a safe way using the methods you have chosen. A requirement of the system is that given a Node object, it is always possible to find all the links connected to that node, and also that each Link object can return the Nodes that it connects.
To model the network as a collection of Node objects and Link objects, we can define two classes: Node and Link.
Here is an example implementation:
public class Node {
private List<Link> links;
public Node() {
this.links = new ArrayList<>();
}
public void addLink(Link link) {
this.links.add(link);
}
public void removeLink(Link link) {
this.links.remove(link);
}
public List<Link> getLinks() {
return this.links;
}
}
public class Link {
private Node node1;
private Node node2;
public Link(Node node1, Node node2) {
this.node1 = node1;
this.node2 = node2;
this.node1.addLink(this);
this.node2.addLink(this);
}
public void remove() {
this.node1.removeLink(this);
this.node2.removeLink(this);
}
public Node getNode1() {
return this.node1;
}
public Node getNode2() {
return this.node2;
}
}
In this implementation, each Node object has a list of Link objects that it is connected to. Each Link object has two Node objects that it connects.
To ensure that the network is always in a consistent state, we can define the following invariants:
1. Each Link object must have exactly two Node objects.
2. Each Node object must have at least one Link object.
3. Each Link object must be connected to two different Node objects.
To add a new Node object to the network, we can simply create a new instance of the Node class. To add a new Link object to the network, we can create a new instance of the Link class and pass in the two Node objects that we want to connect.
To remove a Node object from the network, we can first remove all of the Link objects that it is connected to, and then remove the Node object itself. To remove a Link object from the network, we can simply call the remove method on the Link object.
To find all of the Link objects connected to a given Node object, we can simply call the getLinks method on the Node object. To find the two Node objects that a given Link object connects, we can call the getNode1 and getNode2 methods on the Link object.
让它用三种说法回答一下,然后你翻译成中文综合修改一下,最后再翻译回去让它润色就行了。基本查不出来吧
【 在 zhrbocool 的大作中提到: 】
: [ema1]不行会被查