gpt4 book ai didi

java - 如何在程序中为具有多个参数的方法指定简单的逻辑,而无需使用一堆代码?

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:41 25 4
gpt4 key购买 nike

我正在编写一个多项式类并实现一些简单的方法,但我想实现更高阶的多项式,但又不想大幅增加编写的代码量。到目前为止,里面的方法都很简单(比如添加);但如果我向构造函数添加高阶多项式,则方法将必须非常长才能容纳构造函数将采用的众多参数。我想知道是否有一种更具分析性的方法来实现这些函数,例如直接指定它们的逻辑而不是仅仅将它们写出来。这是到目前为止我的代码:

class Polynomial {

final static int MAX_DEGREE = 4;

private int x0;
private int x1;
private int x2;
private int x3;
private int x4;

Polynomial() {
this(0);
}

Polynomial(int x0) {
this(x0, 0);
}

Polynomial(int x0, int x1) {
this(x0, x1, 0);
}

Polynomial(int x0, int x1, int x2) {
this(x0, x1, x2, 0);
}

Polynomial(int x0, int x1, int x2, int x3) {
this(x0, x1, x2, x3, 0);
}

Polynomial(int x0, int x1, int x2, int x3, int x4) {
this.x0 = x0;
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.x4 = x4;
}

public static Polynomial add(Polynomial p1, Polynomial p2) {
return new Polynomial((p1.x0 + p2.x0), (p1.x1 + p2.x1), (p1.x2 + p2.x2), (p1.x3 + p2.x3), (p1.x4 + p2.x4));
}

public static int getGetx(int i, Polynomial p) {
if (i == 0) return p.x0;
else if (i == 1) return p.x1;
else if (i == 2) return p.x2;
else if (i == 3) return p.x3;
else if (i == 4) return p.x4;
else throw new IllegalArgumentException("Degree called is too big");
}

public static Polynomial negate(Polynomial p) {
return new Polynomial(-1*p.x0, -1*p.x1, -1*p.x2, -1*p.x3, -1*p.x4);
}

public static Polynomial subtract(Polynomial p1, Polynomial p2) {
return new Polynomial((p1.x0 - p2.x0), (p1.x1 - p2.x1), (p1.x2 - p2.x2), (p1.x3 - p2.x3), (p1.x4 - p2.x4));
}

public static Polynomial simplePolynomialDerivative(Polynomial p) {
return new Polynomial(p.x1, 2 * p.x2, 3 * p.x3, 4 * p.x4);
}

public static Polynomial stringToPolynomial(String s) {
if (s.contains("a") | s.contains("b") | s.contains("c") | s.contains("d") | s.contains("e") | s.contains("f") | s.contains("g") | s.contains("h") | s.contains("i") | s.contains("j") | s.contains("k") | s.contains("l") | s.contains("m") | s.contains("n") | s.contains("o") | s.contains("p") | s.contains("q") | s.contains("r") | s.contains("s") | s.contains("t") | s.contains("u") | s.contains("v") | s.contains("w") | s.contains("y") | s.contains("z") | s.contains("!") | s.contains("@") | s.contains("#") | s.contains("$") | s.contains("%") | s.contains("&"))
throw new IllegalArgumentException("Polynomials may only include independent variable x and numeric types"); //contains bad stuff
else {
//do stuff here
}
return new Polynomial();
}

public static void printPolynomial(Polynomial p) {
for (int i = 0; i <= MAX_DEGREE; i++) {
if (!(getGetx(i, p) == 0)) {
if (!(i == 0)) System.out.print(" + ");
if (i == 0) System.out.print(p.x0);
else if (i == 1) System.out.print(p.x1 + "x");
else System.out.print(p.getGetx(i, p) + "x^" + i);
}
}
}

public static String polynomialToString(Polynomial p) {
StringBuilder s = new StringBuilder("");
for (int i = 0; i <= MAX_DEGREE; i++) {
if (!(getGetx(i, p) == 0)) {
if (!(i == 0)) s.append(" + ");
if (i == 0) s.append(p.x0);
else if (i == 1) s.append(p.x1).append("x");
else s.append(p.getGetx(i, p)).append("x^").append(i);
}
}
return s.toString();
}

public static double polynomialSubstitute(double i, Polynomial p) {
return ((p.x0 + i*p.x1 + (Math.pow(i,2))*p.x2 + (Math.pow(i,3))*p.x3 + (Math.pow(i,4))*p.x4));
}

public static long numericalDerivative(Function f) {

return 0L;
}

public static void main(String[] args) {
Polynomial p = new Polynomial(1,5,3,2);
Polynomial pD = simplePolynomialDerivative(p);
double k = 0.0;
for(int i = 1; i<100; i++) {
k -= polynomialSubstitute(k,p)/polynomialSubstitute(k,pD);

}
System.out.println(k);
}
}

猜测让我相信解决方案位于新的 Java 8 lambda 中,但由于我没有使用它们的经验,因此我们将不胜感激。另外,我还没有编码 numericDerivative() 和 stringToPolynomial() ,所以你可以忽略它们。您可以看到,我已经不得不通过使用 getGetx() 来解决这个问题。

最佳答案

你也可以用这种方法。请查找内嵌评论。希望这会有所帮助。

    import java.util.Scanner;

public class Polynomial {
int[] polyArr;

Polynomial(int[] arr) {
//taking an array as argument in the constructor
polyArr = new int[arr.length]; //create your polyArr of the same
//length as array in argument
for(int i = 0;i < arr.length;i++) {
polyArr[i] = arr[i];
}
}

public static Polynomial add(Polynomial p1, Polynomial p2) {
int[] arr = new int[p1.polyArr.length];
for(int i = 0;i < arr.length;i++) {
arr[i] = p1.polyArr[i] + p2.polyArr[i]; // merge the two polynomials data in
//one array
}
return new Polynomial(arr); //pass that array while creating new Polynomial
//object.
}

public static void main(String[] args) {
//this way you can give you input. Index 0,1,2,3 representing x0, x1, x2, x3 respectively
int arr[] = {1,2,3,4};
Polynomial poly = new Polynomial(arr);
}
}

关于java - 如何在程序中为具有多个参数的方法指定简单的逻辑,而无需使用一堆代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41657911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com