patternjavaMinor
Simulating Pendulum Motion in Android
Viewed 0 times
androidpendulumsimulatingmotion
Problem
Quite sometime back I had written an Android application which simulates the path of a pendulum. You can also have look at the complete discussion in my technical blog here.
Please review it and provide me with your valuable comments.
```
package com.somitsolutions.android.pendulamsimulation;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class PendulamSimulationActivity extends Activity {
//private Paint mPaint;
private boolean leftToRightMovement = true;
private boolean rightToLeftMovement = false;
private boolean atTheMiddlePositionWhileLeftToRight = false;
private boolean atTheMiddlePositionWhileRightToLeft = false;
private boolean firstHalf = true;
private boolean secondHalf = false;
private volatile double ballX = 0;
private volatile double ballY = 0;
//private boolean theCenterBeingDrawn = false;
double angleAccel = 0.0;
double angleVelocity = 0;
double dt = 0.15;
private boolean flag = false;
boolean flagCondition = false;
private int anchorX;
private int anchorY;
public static double initialAngle = Math.PI/4;
public static double angle = Math.PI/4;
double angleInThePreviousStep = Math.PI/4;
private static final int length = 150;
private Paint mPaint;
private Paint mRefreshPaint;
//SurfaceHolder surfaceHolder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Panel p = new Panel(this);
mPaint = new Paint();
mPaint.setColor(Color.YELLOW);
mRefreshPaint = new Paint();
mRefreshPaint.setColor(Color.BLACK);
setContentView(p);
}
class Panel extends SurfaceView implements Sur
Please review it and provide me with your valuable comments.
```
package com.somitsolutions.android.pendulamsimulation;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class PendulamSimulationActivity extends Activity {
//private Paint mPaint;
private boolean leftToRightMovement = true;
private boolean rightToLeftMovement = false;
private boolean atTheMiddlePositionWhileLeftToRight = false;
private boolean atTheMiddlePositionWhileRightToLeft = false;
private boolean firstHalf = true;
private boolean secondHalf = false;
private volatile double ballX = 0;
private volatile double ballY = 0;
//private boolean theCenterBeingDrawn = false;
double angleAccel = 0.0;
double angleVelocity = 0;
double dt = 0.15;
private boolean flag = false;
boolean flagCondition = false;
private int anchorX;
private int anchorY;
public static double initialAngle = Math.PI/4;
public static double angle = Math.PI/4;
double angleInThePreviousStep = Math.PI/4;
private static final int length = 150;
private Paint mPaint;
private Paint mRefreshPaint;
//SurfaceHolder surfaceHolder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Panel p = new Panel(this);
mPaint = new Paint();
mPaint.setColor(Color.YELLOW);
mRefreshPaint = new Paint();
mRefreshPaint.setColor(Color.BLACK);
setContentView(p);
}
class Panel extends SurfaceView implements Sur
Solution
Boolean usage:
You have waaay too many explicit boolean flags, which are only a implicit variant of some others.
This clutters your code extremely, especially in your
All other booleans can be achieved by combining these three:
Comparing and concatenating booleans
You check your booleans against boolean values. That doesn't make sense. Compare these statments who are equal in result:
Commenting:
You have lots of Todo comments left in your code, if you don't need to do anything anymore they are pure clutter, utterly useless and should be removed.
This comment (and all the other ones, that are so similar) is not helpful. If you reduce the number of bools you use, you have exactly that, written in your code. This makes your comment noise. It should be removed. Then your if-statement looks like this:
You can do the same with almost all the if-statements you have in your
You have waaay too many explicit boolean flags, which are only a implicit variant of some others.
private boolean leftToRightMovement = true;
private boolean rightToLeftMovement = false;
private boolean atTheMiddlePositionWhileLeftToRight = false;
private boolean atTheMiddlePositionWhileRightToLeft = false;
private boolean firstHalf = true;
private boolean secondHalf = false;This clutters your code extremely, especially in your
onDraw() method. Keep it simple and just do the following:private boolean leftToRightMovement = true;
private boolean isAtMiddlePosition = false;
private boolean isInFirstHalf = true;All other booleans can be achieved by combining these three:
rightToLeftMovement = !leftToRightMovement;
atTheMiddlePositionWhileLeftToRight = leftToRightMovement && isAtMiddlePosition;
atTheMiddlePositionWhileRightToLeft = !leftToRightMovement && isAtMiddlePosition;
secondHalf = !isInFirstHalf;Comparing and concatenating booleans
You check your booleans against boolean values. That doesn't make sense. Compare these statments who are equal in result:
boolValue == true --> boolValue
boolValue == false --> !boolValueCommenting:
You have lots of Todo comments left in your code, if you don't need to do anything anymore they are pure clutter, utterly useless and should be removed.
//First Half ... Left to Right
if(leftToRightMovement == true && rightToLeftMovement ==false && atTheMiddlePositionWhileLeftToRight == false && atTheMiddlePositionWhileRightToLeft == false && firstHalf == true && secondHalf == false){This comment (and all the other ones, that are so similar) is not helpful. If you reduce the number of bools you use, you have exactly that, written in your code. This makes your comment noise. It should be removed. Then your if-statement looks like this:
if(leftToRightMovement && firstHalf && !atMiddlePosition){You can do the same with almost all the if-statements you have in your
onDraw()Code Snippets
private boolean leftToRightMovement = true;
private boolean rightToLeftMovement = false;
private boolean atTheMiddlePositionWhileLeftToRight = false;
private boolean atTheMiddlePositionWhileRightToLeft = false;
private boolean firstHalf = true;
private boolean secondHalf = false;private boolean leftToRightMovement = true;
private boolean isAtMiddlePosition = false;
private boolean isInFirstHalf = true;rightToLeftMovement = !leftToRightMovement;
atTheMiddlePositionWhileLeftToRight = leftToRightMovement && isAtMiddlePosition;
atTheMiddlePositionWhileRightToLeft = !leftToRightMovement && isAtMiddlePosition;
secondHalf = !isInFirstHalf;boolValue == true --> boolValue
boolValue == false --> !boolValue//First Half ... Left to Right
if(leftToRightMovement == true && rightToLeftMovement ==false && atTheMiddlePositionWhileLeftToRight == false && atTheMiddlePositionWhileRightToLeft == false && firstHalf == true && secondHalf == false){Context
StackExchange Code Review Q#49290, answer score: 5
Revisions (0)
No revisions yet.