Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7911437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:14:55+00:00 2026-06-03T13:14:55+00:00

It might not be related, but this is under Android 2.3.3 and I work

  • 0

It might not be related, but this is under Android 2.3.3 and I work with openjdk7.

I’m trying to compute the velocity and displacement of an object using its acceleration values, given coordinates in 3 spaces plus time. For that, I created a class named Coord4D, and Velocity, Displacement and Acceleration are child of this class. Like this:

Coord4D:

class Coord4D {

    // Statics //
    private static final long ONE_TO_NANO_FACTOR = 1000000000;

    // Members //
    private long key;
    private float xValue;
    private float yValue;
    private float zValue;

  // ... more ...
}

and an example of inheriting class Velocity:

public class Velocity extends Coord4D{

  // ... members and methods and stuff ...

}

And now the problem. In Coord4D, I generalize the method to obtain an antiderivative from derivatives. For instance, I want to retrieve a Velocity instance given two Acceleration instances (past and current) and the Velocity instance from a previous frame.

Everything was working fine when the methods to do this were in Displacement and Velocity, but I found it was redundant since both were computing the exact same things, and I hate writing two times the same lines of code. So I decided to move these methods into Coord4D, and to use generics like this:

protected static <Derivative extends Coord4D, 
AntiDerivative extends Coord4D> AntiDerivative getIntegrationStep(
        Derivative previous, 
        Derivative current, 
        AntiDerivative previousStep){

    // Time is in nanoseconds, must change it in seconds
    float dT = ( current.getTime() - previous.getTime() ) 
            / ONE_TO_NANO_FACTOR;

    // Parameters have seconds for unit : ..., ?*s, ?, ?/s, ?/s², ... 
    float dX = current.getX() - previous.getX();
    float dY = current.getY() - previous.getY();
    float dZ = current.getZ() - previous.getZ();


    // do the integration step and add the previous step value
    float x = dX * dT + previousStep.getX();
    float y = dY * dT + previousStep.getY();
    float z = dZ * dT + previousStep.getZ();

    return (AntiDerivative) new Coord4D(current.getTime(), x, y, z);
}

and the Velocity and Displacement objects have this method (example from Velocity).

public static Velocity getVelocity(Acceleration previousAccel, 
        Acceleration currentAccel, 
        Velocity initialVelo) {

    return getIntegrationStep(previousAccel, 
            currentAccel, 
            initialVelo); 

}

Now, for some reason, this line

return getIntegrationStep(previousAccel, currentAccel, initialVelo);

result in a ClassCastException. I don’t understand why, as I think my logic with the generics is fine. Can someone help me finding where is the flaw?

The stack trace (from LogCat):

W/dalvikvm(8891): threadid=1: thread exiting with uncaught exception (group=0x4001d648)
E/AndroidRuntime(8891): FATAL EXCEPTION: main
E/AndroidRuntime(8891): java.lang.ClassCastException: me.aybabt.android.prototypes.physics.Coord4D
E/AndroidRuntime(8891):     at me.aybabt.android.prototypes.physics.Velocity.getVelocity(Velocity.java:40)
E/AndroidRuntime(8891):     at me.aybabt.android.prototypes.AcceleratorActivity.onSensorChanged(AcceleratorActivity.java:199)
E/AndroidRuntime(8891):     at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:539)
E/AndroidRuntime(8891):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(8891):     at android.os.Looper.loop(Looper.java:150)
E/AndroidRuntime(8891):     at android.app.ActivityThread.main(ActivityThread.java:4293)
E/AndroidRuntime(8891):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(8891):     at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(8891):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
E/AndroidRuntime(8891):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
E/AndroidRuntime(8891):     at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:14:57+00:00Added an answer on June 3, 2026 at 1:14 pm

    This line

    return (AntiDerivative) new Coord4D(current.getTime(), x, y, z);
    

    can’t work.

    AntiDerivate is a subclass of Coord4D. You can’t cast from a class to its subclass. (Only the other direction works.)

    What you can do: Instead of using new Coord4D you get the class of previousStep, which is an instance of Antiderivate, and with Class.newInstance() you create the new instance which you want to return.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might not have a major usecase in projects, but I was just trying
This question might not seem programming related at first, but let me explain. I'm
This might not be exactly a programming question but it is related to programmers
At this point I'm not sure if these leaks might be CoreData related or
Reading the documentation it seems this might not be possible, but it seems that
This might be bit non programming related but thought to add it here. I
I realize this question might not be that programming related, and that it by
I've done all of this in active record, but it's not going to work
Not strictly programming-related, but I do not know where else to post this question:
NOTE: I do believe that this is not an openCV related problem but since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.