Java Math Equation Alarm Snooze - java

I'm currently coding a clock app in java for android, and I want to implement a snooze function where when the alarm goes off, a random math problem would appear on the screen. The user would have to successfully answer the math problem before the alarm would snooze/turn off. I'm completely lost on how I would implement this feature. Any guidance on how i'd implement this would be greatly appreciated. Thanks.

First, I'd like to say that what ever I'm about to tell you is the way I would do it, since you did not specify any way specifically you would like to do it. I am no Java expert, but you could probably generate two random integers from a certain range (maybe something like 2 to 11) using Math.random. Try looking at this question for info on how to do that: How do I generate random integers within a specific range in Java? or Math.random() explained Then maybe generate a random number from 1 to 4 where 1 is addition, 2 is subtraction, 3 is multiplication, and 4 is addition. Then you could have an if statement for subtraction and division that checks if the first number is greater than the second number. Also, you could have another if statement for division checking if the division statement ends up with a whole number. Then you could have a final if statement checking if the user typed in the right answer. If you want you could make little alerts for if they got it wrong or right. As for the snooze function, you could just make a property that adds a certain amount of minutes to your method for the alarm. Also, I'd like to say that I like your idea for the math problems!

Related

Get current time (milliseconds) without a call to a System method

I am trying to challenge myself; my goal is to create and print a random number without a Math or System method.
I already have a method of printing to the console without using System.out.println() but I have hit a roadblock in finding a way to get the time (System.currentTimeMillis()) without a System call.
I understand that I need the current time to use as a seed for my random number generation.
I'd like to not use any math or time-related imports
Edit: Probably shouldn't have asked this... it was just an idea I had haha
Using java.time.Instant:
long now = Instant.now().toEpochMillis();
This of course only works if by "System call" you really mean "no call to a method of the class System" as opposed to "no method of the Java system".
Well, you’ve got to get your seed from somewhere. Would a hard-coded random constant be OK? No, I thought not. Otherwise you would have been set.
So you need to read that seed from somewhere outside the program. You can get it from a server, from the file system, from user input, there are many options, it doesn’t have to be the system clock. I once wrote a program that generated random numbers by shuffling some bits from recent database transactions around and hashing the result. I didn’t even use a pseudo-random number generator. But you need to make some call to outside your program.

How can a float value be trimmed to the first three digits?

its my first question here and maybe i'm just blind or extremely stupid, but i haven't found a matching answer to my problem in the past three days. Currently i'm coding an Idlegame (just to learn more about OOP and such stuff) and obviously there will be some numbers that are too big for int or long (sth like "1500000000000" (1.5E12)). So i decided to go along with floats for two reasons:
Every other type that can store way more than int or long (like BigInteger or BigDecimal) wouldn't solve my problem of shortening my shown numbers in the ui.
I like the numberformatting of float with the exponent shown at the end.
Because of some issues with the length of the outcoming number (1.50000003E12 (i think the ...03 at the end of the number comes from the Float.toString() which i use to set it onto a javafx label)) i thought about formatting them in an uniform way (in my opinion it should look like 1.23E45) so i googled a lot, read a lot, tried a lot, but still got no working solution. on SO i read multiple ways to format floats with "String.format()" and "DecimalFormat", but the outcome wasn't as expected. the float value is written completely what in fact is the opposite of that, what i wanna achieve.
So my wish would be a working solution, or a hint that would help me further to achieve my goal to only show the first three digits of a float and add the exponent.
If anything is missing that would help to find the best solution for my problem feel free to ask for it.

Creating simple wandering AI in Java?

I'm trying to make a simple AI for a little character that just wanders around for my 2D game. I am currently using my own way where there is a thread for each character that chooses a random number between 0~2 every 0.5 seconds.
If the number is 0 : character does not move.
If the number is 1 : character moves right.
If the number is 2 : character moves left.
But I figured out that if I use this, there will be too many threads if there are many characters, therefore making the game slower. Is there a more efficient way to make a simple AI for this type of character?
Is there a particular reason why each character has to be represented by a thread? If you're dealing with massive amounts of updates, then you may possibly consider creating one thread that deals with generating random moves of all characters in parallel with your game, however I'm guessing that isn't going to be your case.
The simple and optimal way to do this is to simply generate a random move for each character. If each character is its own class, then add a method called "moveRandomDirection()" that generates a random number and applies the move to its position.
Perhaps you're overthinking it.

Java Program for Number Guessing Game [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am beginner in java. Please provide a sample program in Java to play a number-guessing game. The game works as follows:
The user chooses a number in his mind and types “ready” to indicate to the computer that he is ready to
begin playing.
The computer asks a series of questions to arrive at the number the user has in mind. The user can only
respond with “higher”, “lower” or “yes”.
The game ends when the user responds with “yes” or “end”.
Example:
User chooses number 40 in his mind.
Computer: Is the number 30?
User: higher
Computer: Is the number 50?
User: lower
Computer: Is the number 35?
User: higher
Computer: Is the number 40?
User: yes
Firstly : You shouldn't ask for sample code without any of your own code - that's likely why this is being downvoted.
Moving on, you should decide how you want your program to guess. For example, a bisection algorithm might be useful for you here. You'd need to set some initial code which differed, in order to bound the person's number.
Eg: (pseudo-code)
int guessLower=0;
int guessUpper=1000000;
int myGuess=10;
// Ask user to pick number
// Ask user if number is == guess
// If yes, finish.
// If the answer is higher than the guess, change guessLower to the value of guess, and change guess to halfway between guess and guessUpper.
// If the answer is lower, do the opposite (guessUpper = myGuess; myGuess = (guessLower + myGuess)/2;
// Repeat until finished
This (~)code won't work if the value the person makes up isn't within the limits you set - so consider asking first if it is higher than guessLower, and lower than guessUpper. If not, try moving the markers by some amount. More complicated algorithms for searching can be easily imagined, and you should be careful about the differences between integers and doubles etc.
On a side note, for comparing strings, you should look up using equals(...), eg input.equals("higher");. Don't use the C++ == for comparing the values of strings, I anticipate this might be a problem you'll hit...
I won't write or provide a program for you, this is pretty basic.
A few bits to get you started:
You need to use a Scanner object to read in from the keyboard. This question should cover that part pretty well: Getting Keyboard Input
You also need to understand booleans. Click here for that.
And finally you should probably learn about "relational operators," like > is greater than, < is less than, etc. Check here for that.

Programmatically finding periodicity of a given function

I am working on a project in Android for my Signal Processing course. My aim is to find signal properties, such as periodicity, even/odd, causality etc, given a user-input function. Right now, I am stuck at trying to figure out how to programmatically calculate the periodicity of a given function. I know the basics behind periodicity: f(t+T) = f(t)
The only idea I have right now is to extensively calculate values for the function, and then check for repetition of values. I know the method is quite stupid, given the fact I don't know how many such values I need to calculate to determine if it is periodic or not.
I know this can be done easily in Matlab, but again very difficult to port Matlab to Java. Is there something I am missing? I have been searching a lot, but haven't found anything useful.
Thanks for any help, in advance!
If the function f is given as a symbolic expression, then the equation you stated can in some cases be solved symbolically. The amount of work required for this will depend on how your functions are described, what kinds of functions you allow, what libraries you use and so on.
If your only interaction with the function is evaluating it, i.e. if you treat the function description as a black box or obtain its values from some sensor, then your best bet would be a Fourier transformation of the data, to convert it from the time domain into frequency domain. In particularly, you probably want to choose your number of of samples to analyze as a power of two, and then use FFT to quickly obtain intensities for various frequencies.

Categories