我正在编写代码来查找和交叉2行.当线的斜率相等时,它们不相交.但另一方面,具有相等值斜率的输入是完全有效的.
public static Point calculateIntersection(Line line1, Line line2) {
if (line1 == null || line2 == null) {
throw new NullPointerException(" some message ");
}
if (line1.getConstant() == line2.getConstant()) {
return new Point(0, line1.getConstant());
}
if (line1.getSlope() == line2.getSlope()) {
throw new IllegalArgumentException("slopes are same, the lines do not intersect.");
}
int x = (line2.getConstant() - line1.getConstant()) / (line1.getSlope() - line2.getSlope());
int y = line1.getSlope() * x + line1.getConstant();
return new Point(x, y);
}
Run Code Online (Sandbox Code Playgroud)
问题是抛出非法争论异常是正确的事情吗?由于输入有效,它并不能完全说服我.
自定义异常是正确的做法吗?听起来是一个不错的选择,但额外的意见会有所帮助.
谢谢
假设我想设计一个客户端需要以特定顺序调用函数的类,例如,
hasNext();
next();
Run Code Online (Sandbox Code Playgroud)
或者,作为一个非常通用的例子,一个CookFood带有方法的类:
class CookFood {
getListOfItems();
mixAllItems();
heat();
}
Run Code Online (Sandbox Code Playgroud)
在第二个例子中,我想强制说只有在获得物品后才能进行混合,并且只有在混合后才能进行加热.是否有任何已知的模式或良好实践来强制执行函数调用序列?
我与有效的java有一点冲突.一方面,它强烈鼓励使用最终修饰符.它还鼓励使用foreach循环.
但是我没有在任何地方看到任何代码,代码如下:
for (final element e : list) {
// do whatever.
}
Run Code Online (Sandbox Code Playgroud)
如果预计元素不会改变,那么使用final似乎是好的.为什么不那么常见?
我的笔记本电脑已经格式化并安装了新的操作系统,从那时起我就收到了这个错误:ImportError:没有名为git的模块
这是指一个简单地导入git的python代码.
我的笔记本电脑格式化之前git的位置:/ usr/local/bin/git格式化笔记本电脑后git的位置:/ usr/bin/git
如何/我在我的python代码中更改以引用正确的路径?
以下哪个选项被认为是循环使用的好方法String?
我们应该使用charAt或转换为char数组吗?我正在寻找所有方面的答案,包括性能和使用的空间
public static void doChose (String str) {
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
}
// vs
char[] chars = str.toCharArray();
for (char c : chars) {
System.out.println(c);
}
}
Run Code Online (Sandbox Code Playgroud) Java文档指定
Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
我试图理解为什么?11的优点是什么?
我有一种情况,我使用构建器模式来构造一个对象.最好的例子是披萨代码
public class Pizza {
private int size;
private boolean cheese;
private boolean pepperoni;
private boolean bacon;
public static class Builder {
//required
private final int size;
//optional
private boolean cheese = false;
private boolean pepperoni = false;
private boolean bacon = false;
public Builder(int size) {
this.size = size;
}
public Builder cheese(boolean value) {
cheese = value;
return this;
}
public Builder pepperoni(boolean value) {
pepperoni = value;
return this;
}
public Builder bacon(boolean value) {
bacon = …Run Code Online (Sandbox Code Playgroud) 这是我的情景
public int foo(int a) {
return new Bar().bar(a, new Date());
}
My test:
Bar barObj = mock(Bar.class)
when(barObj.bar(10, ??)).thenReturn(10)
Run Code Online (Sandbox Code Playgroud)
我尝试插入任何(),anyObject()等.任何想法插入什么?
但是我一直得到例外:
.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
3 matchers expected, 1 recorded:
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
Run Code Online (Sandbox Code Playgroud)
我们不使用powermocks.
我有一个代码,它将一个二分图作为输入,并返回一个键为"1"的映射,其值为"set1"中的节点和键"2"的列表,其值为"set2中的节点"列表.现在,地图是可变的.从理论上讲,我应该使用防御性副本来返回地图.但是,在这种情况下它真的需要吗?这看起来有点矫枉过正.
例如:
class BiPartite {
Graph graph;
Map bipartite
Bipartite(graph) {
this.graph = graph;
}
void calcBipartite() {
// calculate map
}
Map getMap() {
// should i make defensive copy ? Appears overkill.
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个用例要求,我想设计一个hashtag排名系统.应该选择10个最流行的#标签.我的想法是这样的:
[hashtag,rateofhitsperminute,rateofhisper5minutes]
然后我将查询,找出10个最受欢迎的#hashtags,其每分钟的速率最高.
我的问题是,我可以使用哪种数据库来提供像'rateofhitsperminute'这样的统计数据?
计算这样一个细节和存储在db中的好方法是什么?有些DB提供这些功能吗?