- Java Coding and random CS things (womp's learning thread)
- 12 Sep 2017 06:26:39 pm
- Last edited by mr womp womp on 14 Sep 2017 05:11:49 pm; edited 3 times in total
Alright, so I'm in the process of learning Java. This is a thread dedicated to things that I learn and questions that inevitably arise from the process. I am currently taking 2 courses which relate to Java, the first one is intro to OOP, where we just learn as much Java as possible, and the 2nd is computing mathematics, which although the theory could apply to any language, we apply it to Java.
As a first post, here is some fun little code snippet that messes with the IEEE-754 floating point representation of the 'float' variable type in Java.
Code:
Feel free to post about, random code, information, Java related things or whatever you'd like that could be of some educational value
EDIT: I got bored, here is my implementation of fizzbuzz (up to 7)
Code:
I would have liked to do it with a method that takes an int to test against and a string as arguments, but that got confusing for my little beginner brain so I did it all in the main.
Now that I look at it, it appears as though I'm thinking in ti-basic and translating to java in my head, which is probably not a good habit
For reference, here is what the code would look like just in ti-basic, formatted in the same way...
Code:
As a first post, here is some fun little code snippet that messes with the IEEE-754 floating point representation of the 'float' variable type in Java.
Code:
package project1;
public class Project1 {
public static void main(String[] args) {
final float a=2000000000f;
final float b=20f;
System.out.println(a==a+b);
}
}
Feel free to post about, random code, information, Java related things or whatever you'd like that could be of some educational value
EDIT: I got bored, here is my implementation of fizzbuzz (up to 7)
Code:
package fizzbuzz;
public class FizzBuzz {
public static void main(String[] args) {
for (int i=1;i<=100;i++){
String fb=" ";
if (i%3==0){fb+="Fizz";}
if (i%5==0){fb+="Buzz";}
if (i%7==0){fb+="Fuzz";}
if (fb.length()!=1){System.out.println(fb.substring(1,fb.length()));}
else {System.out.println(i);}
}
}
}
I would have liked to do it with a method that takes an int to test against and a string as arguments, but that got confusing for my little beginner brain so I did it all in the main.
Now that I look at it, it appears as though I'm thinking in ti-basic and translating to java in my head, which is probably not a good habit
For reference, here is what the code would look like just in ti-basic, formatted in the same way...
Code:
For(I, 1,100)
" "→Str1
If not(fPart(I/3)):Str1+"Fizz"→Str1
If not(fPart(I/5)):Str1+"Buzz"→Str1
If not(fPart(I/7)):Str1+"Fuzz"→Str1
If length(Str1)≠1:Then:Disp sub(Str1,2,length(Str1)-1)
Else:Disp I
End