英文:
SICP method to find squareRoot
问题
public class Newton {
public static double sqrt(double x) {
return sqrtIter(1, x);
}
public static double sqrtIter(double guess, double x) {
if (x < 0) {
throw new IllegalArgumentException("x must not be negative.");
}
while (true) {
if (goodEnough(guess, x)) {
return guess;
} else {
guess = improve(guess, x);
}
}
}
public static double improve(double guess, double x) {
return average(guess, x / guess);
}
public static double average(double x, double y) {
return (x + y) / 2;
}
public static boolean goodEnough(double guess, double x) {
return Math.abs(guess * guess - x) < 0.0001;
}
}
请注意,我已经将代码中的错误进行了修正。如果你有任何其他问题,都可以随时问我。
英文:
Suppose such a program to find square-root:
def sqrt(x):
return sqrt_iter(1, x)
def sqrt_iter(guess, x):
if x < 0: raise ValueError("n must not be negative.")
while True:
if good_enough_p(guess, x):
return guess
else:
guess = improve(guess, x) def improve(guess, x):
return average(guess, x/guess)
def good_enough_p(guess, x):
return abs(guess**2 - x) < 0.00001
def average(x, y):
return (x + y) / 2
I tried to re-write it in java as:
public class Newton {
public static double sqrt(double x) {
return sqrtIter(1, x);
}
public double sqrtIter(double guess, double x) {
if (x < 0) {
throw new IllegalArgumentException("x must not be negative.");
}
while (true) {
if (goodEnough(double guess, double x)) {
return guess;
} else {
guess = improve(guess, x);
}
}
public double improve(double guess, double x) {
return average(guess, x/guess);
}
public double average(double x, double y) {
return (x + y) / 2;
}
public boolean goodEnough(double guess, double x) {
return Math.abs(guess*guess -x) < 0.0001;
}
}
}
However, it report 6 errors:
Newton.java:10: error: '.class' expected
if (goodEnough(double guess, double x)) {
^
Newton.java:10: error: ';' expected
if (goodEnough(double guess, double x)) {
^
Newton.java:10: error: ';' expected
if (goodEnough(double guess, double x)) {
^
Newton.java:12: error: 'else' without 'if'
} else {
^
Newton.java:16: error: illegal start of expression
public double improve(double guess, double x) {
^
Newton.java:26: error: class, interface, or enum expected
}
^
6 errors
I' afraid that have to finish reading a book such as Core-Java-Volumn before could correct them.
Could you please give some hints to correct the program?
答案1
得分: 3
总体而言,你的 Java 翻译几乎是正确的。
问题在于在调用方法时需要一个实例,并且需要去除类型。
public class Newton {
public static void main(String args[]) {
Newton instance = new Newton();
System.out.println(instance.sqrt(4.33));
}
public double sqrt(double x) {
return sqrtIter(1, x);
}
public double sqrtIter(double guess, double x) {
if (x < 0) {
throw new IllegalArgumentException("x must not be negative.");
}
while (true) {
if (goodEnough(guess, x)) {
return guess;
} else {
guess = improve(guess, x);
}
}
}
public double improve(double guess, double x) {
return average(guess, x / guess);
}
public double average(double x, double y) {
return (x + y) / 2;
}
public boolean goodEnough(double guess, double x) {
return Math.abs(guess * guess - x) < 0.0001;
}
}
输出结果:
2.080865423875971
英文:
Overall your translation with java was almost fine.
The issues were that you need an instance and remove types when call a method.
public class Newton {
public static void main(String args[]) {
Newton instance = new Newton();
System.out.println(instance.sqrt(4.33));
}
public double sqrt(double x) {
return sqrtIter(1, x);
}
public double sqrtIter(double guess, double x) {
if (x < 0) {
throw new IllegalArgumentException("x must not be negative.");
}
while (true) {
if (goodEnough(guess, x)) {
return guess;
} else {
guess = improve(guess, x);
}
}
}
public double improve(double guess, double x) {
return average(guess, x / guess);
}
public double average(double x, double y) {
return (x + y) / 2;
}
public boolean goodEnough(double guess, double x) {
return Math.abs(guess * guess - x) < 0.0001;
}
}
Output
2.080865423875971
答案2
得分: 1
在Java中,静态上下文只能从静态上下文中调用。
尝试
public class Newton {
public static double sqrt(double x) {
return sqrtIter(1, x);
}
public static double sqrtIter(double guess, double x) {
if (x < 0) {
throw new IllegalArgumentException("x must not be negative.");
}
while (true) {
if (goodEnough(guess, x)) {
return guess;
} else {
guess = improve(guess, x);
}
}
}
static public double improve(double guess, double x) {
return average(guess, x / guess);
}
static public double average(double x, double y) {
return (x + y) / 2;
}
static public boolean goodEnough(double guess, double x) {
return Math.abs(guess * guess - x) < 0.0001;
}
}
英文:
in java static context can be called from static context only .
try
public class Newton {
public static double sqrt(double x) {
return sqrtIter(1, x);
}
public static double sqrtIter(double guess, double x) {
if (x < 0) {
throw new IllegalArgumentException("x must not be negative.");
}
while (true) {
if (goodEnough( guess, x)) {
return guess;
} else {
guess = improve(guess, x);
}
}
}
static public double improve(double guess, double x) {
return average(guess, x/guess);
}
static public double average(double x, double y) {
return (x + y) / 2;
}
static public boolean goodEnough(double guess, double x) {
return Math.abs(guess*guess -x) < 0.0001;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论