Convert input second into Month - java

Months are represented by numbers ranging from 0 to 11. For example, 0 is January, 1 is February, etc.
The code would be
double mon = (sec/2592000);
But how do I control the range from 0-11?

You can use mod operation to control the range:
double mon = ((sec/2592000 - 1) % 12) // from 0 to 11

Related

Loop through the digits of a number, and do sums and subtracts in Java

How can I loop through the digits in a number, and do sum & subtract to every possible combination using Java?
for example let's take the number 114, I want to output it like this:
1+1+4 = 6
1+1-4 = -2
1-1+4 = 4
1-1-4 = -4
11+4 = 15
11-4 = 7
1+14 = 15
1-14 = 13
114 = 114
You can use % operator to get each digit. Last digit from a number is the_number % 10. And to get the next digit you should do the_number = the_number / 10. After you get a digit you can put it into an array, count the digits in another variable (counter++) and after you have all digits you can sum them. For 114 your array will be [4,1,1] because you always get the last digit and if you want to do 11 + 4 you can take the last 1 (last element from array), multiply it by 10 and add 1 and 4. Same for 1-14, take the last 1 (last element from array) - ( 1 multiplied by 10 + 4).

Generating integers of form 2^n - 1 with for loop

I have an assignment that asks me to write a for loop inside a method that will output this sequence:
1 3 7 15 31 ... 255
I know that the pattern is to multiply the number by two then add one (or just to add the exponents of 2 to each number so 1 + 2 = 3 + 4 = 7 + 8 = 15 etc.) but I don't know how exactly to make a loop that'll output that sequence all the way up to 255.
I would just like an example or explanation to guide me a little bit, I don't want anyone to actually give me the exact code I need. Here's what I've done so far:
public static void methodOne() {
for (int j = 1; j <= 255; j *= 2) {
}
}
I tried to use another for loop within the for loop above but it didn't work well, and I'm not sure if that's the right thing to do. I basically want to take j and have it multiplied by two and then add 1 to get the next number in the sequence.
As you noted, the sequence is to double the previous number and add one. Just have your for loop progress like that, and print the number in each iteration:
for (int j = 1; j <= 255; j = (j * 2) + 1) {
System.out.println(j);
}
As is howework, will leave you something to think:
for x in 2:8 range
result = 2^x -1
this is the most succinct form I could think of that uses only additions ( + ) over 1 single tracking variable:
jot 100 |
mawk '$++NF = _+=_++' CONVFMT='%.f' # mawk-1
gawk '$++NF = _+=_++' # gawk, mawk-2, nawk, etc
1 1
2 3
3 7
4 15
5 31
6 63
7 127
8 255
9 511
10 1023
11 2047
12 4095
13 8191
14 16383
15 32767
16 65535
17 131071
18 262143
. . .
30 1073741823
31 2147483647
32 4294967295
33 8589934591
34 17179869183
35 34359738367
36 68719476735
if you wanna go all the way to 2^1023 - 1 without using a big-int library (tested and confirmed working for mawk-1, mawk-2, gawk 5.2.0, and nawk):
jot 1023 |
mawk '$++NF = (((___+=___++)%((_+=_^=_<_)+_*_*_)^_^_)%_ ||
index(___,"e") < (length(___) < _^_^_--)) \
? ___ : sprintf("%.*s%d", -_+(__ = length(_=\
sprintf("%.f",___))),_,substr(_,__)-!!__)' \
CONVFMT='%.16g'
31 2147483647
279 9713344461128645354597309534117594533212034195260697606259062048
69452142602604249087
527 4393470502483590217588416511412091659052438592091715462012456613
8787476373744998733584381700233309151854696392905477491437580723
1981865204004737810631363657727
775 1987223158144907436990693745232003270728814101909371662257986608
6733452194385624145035243633006674917766242952923277737038996224
5646696242104868771205271185818170236930668787910433956560844600
937633663896795708000114284397288455405567
1023 8988465674311579538646525953945123668089884894711532863671504057
8866337902750481566354238661203768010560056939935696678829394884
4072083112464237153197370621888839467124327426381511098006230470
5972654147604250288441907534117123144073695655527041361858167525
5342293149119973622969239858152417678164812112068607

Modulus division when first number is smaller than second number

I apologize if this is a simple question but I'm having trouble grasping the concept of modulus division when the first number is smaller than the second number. For example when 1 % 4 my book says the remainder is 1. I don't understand how 1 is the remainder of 1 % 4. 1 / 4 is 0.25. Am I thinking about modulus division incorrectly?
First, in Java, % is the remainder (not modulo) operator, which has slightly different semantics.
That said, you need to think in terms of integer-only division, as if there were no fractional values. Think of it as storing items that cannot be divided: you can store zero items of size 4 in a storage of overall capacity one. Your remaining capacity after storing the maximum number of items is one. Similarly, 13%5 is 3, as you can fit 2 complete items of size 5 in a storage of size 13, and the remaining capacity is 13 - 2*5 = 3.
If you divide 1 by 4, you get 0 with a remainder of 1. That's all the modulus is, the remainder after division.
I am going to add a more practical example to what "Jean-Bernard Pellerin" already said.
It is correct that if you divide 1 by 4 you get 0 but, Why when you do 1 % 4 you have 1 as result?
Basically it is because this:
n = a / b (integer), and
m = a % b = a - ( b * n )
So,
a b n = a/b b * n m = a%b
1 4 0 0 1
2 4 0 0 2
3 4 0 0 3
4 4 1 0 0
5 4 1 4 1
Conclusion: While a < b, the result of a % b will be "a"
Another way to think of it as a representation of your number in multiples of another number. I.e, a = n*b + r, where b>r>=0. In this sense your case gives 1 = 0*4 + 1. (edit: talking about positive numbers only)
I think you are confused between %(Remainder) and /(Division) operators.
When you say %, you need to keep dividing the dividend until you get the remainder 0 or possible end. And what you get in the end is called Remainder.
When you say /, you divide the dividend until the divisor becomes 1. And the end product you get is called Quotient
Another nice method to clear things up,
In modulus, if the first number is > the second number, subtract the second number from the first until the first number is less than the second.
17 % 5 = ?
17 - 5 = 12
12 % 5 = ?
12 - 5 = 7
7 % 5 = ?
7 - 5 = 2
2 % 5 = 2
Therefore 17 % 5, 12 % 5, 7 % 5 all give the answer of 2.
This is because 2 / 5 = 0 (when working with integers) with 2 as a remainder.

Permutation and combination required

I am trying to read the "day" value from native calendar of blackberry,the value is returned as a integer ,which is mapped to a value for each day of the week.The values are like :
Monday:32768
tue: 16384
wed :8192
thur :4096
fri :2048
sat :1024
sun :65536
I am able to see if the value is mon/tue/wed/thu/fri/sat/sun if the event is occuring for a single day using
if (rule.MONDAY == rule.getInt(rule.DAY_IN_WEEK)) {
System.out.println("occurs monday");
}
rule.getInt(rule.DAY_IN_WEEK)
value is also same as monday value.
Now the issue is, if the events is occuring on two/three or more number of days then
rule.getInt(rule.DAY_IN_WEEK)
returns me a sum of all days selected.
EXAMPLE: if the days are :wed,sat then i get the result as 9216 ,sum of wed+sat ,from this i am not getting to know which are the days the event occurs.
How can i do a permutation/combination of these numbers and get the exact result for 'n' number of days selected.
I assume the days are just bit flags in a number so you might change your check:
if ( (rule.getInt(rule.DAY_IN_WEEK) & rule.MONDAY) != 0 ) {
System.out.println("occurs monday");
}
Use binary and operator like this:
int day = rule.getInt(rule.DAY_IN_WEEK)
if(day & rule.MONDAY != 0) {
System.out.println("occurs monday");
}
if(day & rule.WEDNESDAY != 0) {
System.out.println("occurs wednesday");
} /* and so on */
Notice that:
0000 0100 0000 0000 = 1024
0000 1000 0000 0000 = 2048
.
.
.
check also bit mask
To know which days of the week the event occurs, you need to do something like this:
boolean occursOnMonday = (rule.getInt(rule.DAY_IN_WEEK) & rule.MONDAY) != 0;
where & is the bitwise AND operator. Why is this so?
Wednesday is 8192, which in binary it is 10000000000000 (2 times 13)
Saturday is 1024, which in binary it is 00010000000000 (2 times 9)
So an event that happens on Wed and Sat is 9216, which is 10010000000000.
Then using bit operations you are able to know what bits are in 1 and what bits are in 0, and with that you know what days the event happens.

Simple division in Java - is this a bug or a feature?

I'm trying this simple calculation in a Java application:
System.out.println("b=" + (1 - 7 / 10));
Obviously I expect the output to be b=0.3, but I actually get b=1 instead.
What?! Why does this happen?
If I write:
System.out.println("b=" + (1 - 0.7));
I get the right result, which is b=0.3.
What's going wrong here?
You're using integer division.
Try 7.0/10 instead.
You've used integers in the expression 7/10, and integer 7 divided by integer 10 is zero.
What you're expecting is floating point division. Any of the following would evaluate the way you expected:
7.0 / 10
7 / 10.0
7.0 / 10.0
7 / (double) 10
Please do not take this as an answer to the question. It is not, but an advice related to exploiting the difference of int and float. I would have put this under a comment except that the answer box allows me to format this comment.
This feature has been used in every respectable programming language since the days of fortran (or earlier) - I must confess I was once a Fortran and Cobol punch card programmer.
As an example, integer division of 10/3 yields integer value 3 since an integer has no facility to hold fractional residual .3333.. .
One of the ways we (old time ancient programmers) had been using this feature is loop control.
Let's say we wish to print an array of 1000 strings, but we wish to insert a line break after every 15th string, to insert some prettyfying chars at the end of the line and at the beginning of the next line. We exploit this, given that integer k is the position of a string in that array.
int(k/15)*15 == k
is true only when k is divisible by 15, an occurrence at a frequency of every 15th cell. Which is akin to what my friend said about his grandfather's dead watch being accurate twice a day.
int(1/15) = 0 -> int(1/15)*15 = 0
int(2/15) = 0 -> int(2/15)*15 = 0
...
int(14/15) = 0 -> int(14/15)*15 = 0
int(15/15) = 1 -> int(15/15)*15 = 15
int(16/15) = 1 -> int(16/15)*15 = 15
int(17/15) = 1 -> int(17/15)*15 = 15
...
int(29/15) = 1 -> int(29/15)*15 = 15
int(30/15) = 2 -> int(30/15)*15 = 30
Therefore, the loop,
leftPrettyfy();
for(int k=0; k<sa.length; k++){
print(sa[k]);
int z = k + 1;
if ((z/15)*15 == z){
rightPrettyfy();
leftPrettyfy();
}
}
By varying k in a fanciful way in the loop, we could print a triangular printout
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
That is to demonstrate that, if you consider this a bug, this "bug" is a useful feature that we would not want to be removed from any of the various languages that we have used thus far.
I find letter identifiers to be more readable and more indicative of parsed type:
1 - 7f / 10
1 - 7 / 10f
or:
1 - 7d / 10
1 - 7 / 10d
In my case I was doing this:
double a = (double) (MAX_BANDWIDTH_SHARED_MB/(qCount+1));
Instead of the "correct" :
double a = (double)MAX_BANDWIDTH_SHARED_MB/(qCount+1);
Take attention with the parentheses !

Categories