我听说写代码的同时也要发博客,于是就随便发一篇
这些代码并没有体现出java的优越性,大家看着乐呵就好(指不定还会高血压)
import java.lang.Math;
public class run{
public static void init(Box box){
box.setStickLength(10);
box.setDx(0.001);
}
public static void main(String[] args) {
Thyta thyta = new Thyta();
Box box = new Box();
thyta.init(box);
init(box);
calculator Calculator = new calculator();
while (true){
Calculator.Task01(box);
System.out.println(Math.toDegrees(thyta.getThyta()));
}
}
}
这段代码是执行用的,run类,会一直输出单摆的角度,StickLength是单摆摆长,Dx是微分变量
public class Thyta extends Box{
private static boolean init = false;
public static void init(Box Box){
init(45, false, Box);
}
public static void init(double Orin, boolean Radian, Box Box){
Box.setThyta(Orin, Radian);
init = true;
}
public static double getThyta(){
if (init) {
return Box.Thyta();
}else{
return 114514;
}
}
public static void setThyta(double newThyta){
if (init){
Box.setThyta(newThyta, true);
}else{
System.out.println(1145141919);
}
}
public static void setThyta(double newThyta, boolean Radian){
Box.setThyta(newThyta, Radian);
}
}
这是Thyta类,专门用来init的
import java.lang.Math;
public class Box{
private static double Thyta;
private static double doubleForce; // 这个是加速度的加速度
private static double aForce; // 正如其名,这个是单摆的加速度
private static double arcLength; // 弧长的意思
private static double stickLength; // 单摆的长度
private static double dx$;
private static final double g = 9.8065; // 重力加速度
private static boolean lengthAlrSet = false; // 单摆的长度有没有被设置
private static boolean dxAlrSet = false;
public static double Thyta(){
return Thyta;
}
public static double doubleForce(){
return doubleForce;
}
public static double aForce(){
return aForce;
}
public static double arcLength(){
return arcLength;
}
public static double stickLength(){
return stickLength;
}
public static double g(){
return g;
}
public static double dx(){
return dx$;
}
public static void setThyta(double newThyta, boolean Radian){
if (Radian){
Thyta = newThyta;
}else{
Thyta = Math.toRadians(newThyta);
}
}
public static void setDoubleForce(double newForce){
doubleForce = newForce;
}
public static void setaForce(double newForce){
aForce = newForce;
}
public static void setArcLength(double newLength){
arcLength = newLength;
}
public static void setStickLength(double newLength){
if (!lengthAlrSet) {
stickLength = newLength;
lengthAlrSet = true;
}
}
public static void setDx(double dx){
if (!dxAlrSet){
dx$ = dx;
dxAlrSet = true;
}else{
System.out.println(114514);
}
}
}
这是一个集成了所有的变量的Box类,具体怎么做的随便看看就好
import java.lang.Math;
public class calculator {
public static void Task01(Box Box){
Box.setArcLength(Box.stickLength() * Box.Thyta());
Box.setDoubleForce(-1 * Box.g() * Math.cos(Box.Thyta()) / Box.stickLength());
Box.setaForce(Box.aForce() + Box.doubleForce() * Box.dx());
Box.setThyta(Math.toDegrees(Box.Thyta()) + Box.aForce() * 1, false);
}
}
这是单摆算法本体