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 1075697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:18:37+00:00 2026-05-16T21:18:37+00:00

I’m new to Java programming, I am programming Java 1.6 with Android. I have

  • 0

I’m new to Java programming, I am programming Java 1.6 with Android.

I have a simple function that makes a number go up and down between 0 and 200. I would like to put this into a Sine function but keep getting errors with what I’ve been trying.

I want my program to update an int (Number1) via a sine wave y axis.

Any ideas change the following logic into a Sine function? (disregard the 2nd number)

code:

private int Number1 = 150;
private int Number2 = 0;
private int counter = 0;     

  public void updateNumbers() {   
            if (counter == 0) {
                if (Number1 < 200) {
                    Number1 = Number1 + 50;
                    Number2 = Number2 - 50;
                    if (Number1 >= 200) {
                        counter = 1;
                    }
                }               
            } else if (counter == 1) {
                if (Number2 < 200) {                                       
                    Number1 = Number1 - 50;
                    Number2 = Number2 + 50;               
                    if (Number2 >= 200) {
                        counter = 0;
                    }
                }
            }
        }           
  • 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-05-16T21:18:38+00:00Added an answer on May 16, 2026 at 9:18 pm

    Okay, so what you want to do is build a sine wave that goes between 0 and 200, but with what period? Did you want it to loop about every 8 calls?

    How about this, leveraging the built-in Java Math.sin function:

    private static final double PERIOD = 8; // loop every 8 calls to updateNumber
    private static final double SCALE = 200; // go between 0 and this
    
    private int _pos = 0;
    private int Number1 = 0;
    
    public void updateNumber() {
        _pos++;
        Number1 = (int)(Math.sin(_pos*2*Math.PI/PERIOD)*(SCALE/2) + (SCALE/2));
    }
    

    Basically, we keep a variable that counts how many updates we’ve done, and scale that to match the period of a sine wave, 2*PI. That acts as the input to the ‘real’ sin function, giving us something that goes between -1 and 1 but has the right frequency. Then, to actually set the number, we just scale that to be between -100 and 100 and then add 100 to move it to be in the 0-200 range you wanted from the beginning.

    (You don’t have to cast the number to an int if a double works for you, I was just keeping with the spirit of what you wrote above.)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't

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.