Search This Blog

Featured Post

Machine Learning, Big Data, AI, Deep Learning

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, April 17, 2019

Java Number & Math

Math in Java


Input

public class test{
public static void main(String[] args){

System.out.println("sin:" + Math.sin(Math.PI/2));
System.out.println("cos:" + Math.cos(0));
System.out.println("tan:" + Math.tan(Math.PI/3));
System.out.println("atan:" + Math.atan(1));
System.out.println("toDegrees:" + Math.toDegrees(Math.PI/2));
System.out.println(Math.PI);
}

}

Output
D:\javafiles>java test.java
sin:1.0
cos:1.0
tan:1.7320508075688767
atan:0.7853981633974483
toDegrees:90.0
3.141592653589793


----------


Java xxxValue()



Input


class test{


public static void main(String[] args){

Integer x = 5;
System.out.println(x.byteValue());
System.out.println(x.doubleValue());
System.out.println(x.floatValue());
System.out.println(x.intValue());
System.out.println(x.longValue());
System.out.println(x.shortValue());

}
}

Output

D:\javafiles>java test.java
5
5.0
5.0
5
5
5

Reference: http://www.runoob.com/java/number-xxxvalue.html----------


Java compareTo()

Input


public class test{
public static void main(String args[]){

Integer x = 5;
System.out.println(x.compareTo(3));
System.out.println(x.compareTo(5));
System.out.println(x.compareTo(8));
}
}


Output

D:\javafiles>java test.java
false
true
false

Reference: http://www.runoob.com/java/number-compareto.html
----------

Java equals()


Input


public class Test{
public static void main(String args[]){
Integer x = 5;
Integer y = 10;
Integer z =5;
Short a = 5;

System.out.println(x.equals(y));
System.out.println(x.equals(z));
System.out.println(x.equals(a));
}
}

Output

D:\javafiles>java test.java
false
true
false

Reference: http://www.runoob.com/java/number-equals.html
----------

Java valueOf()

Input

public class test{
public static void main(String[] args){

Integer x =Integer.valueOf(9);
Double c = Double.valueOf(5);
Float a = Float.valueOf("80");
Integer b = Integer.valueOf("444",16); //16 means use Hexadecimal
System.out.println(x);
System.out.println(c);
System.out.println(a);
System.out.println(b);
}

}

Output

D:\javafiles>java test.java
9
5.0
80.0

1092

Reference: http://www.runoob.com/java/number-valueof.html
----------

Java toString()


Input

public class test{
public static void main(String[] args){

Integer x = 5;

System.out.println(x.toString());
System.out.println(Integer.toString(12));
}

}

Output

D:\javafiles>java test.java
5
12

Reference: http://www.runoob.com/java/number-tostring.html
----------

Java parseInt()


Input
public class test{
public static void main(String[] args){
int x =Integer.parseInt("9");
double c = Double.parseDouble("5");
int b = Integer.parseInt("444",16);

System.out.println(x);
System.out.println(c);
System.out.println(b);
}
}

Output
D:\javafiles>java test.java
9
5.0
1092

Reference: http://www.runoob.com/java/number-parseInt.html
---------

Java abs()

Input 
public class test{
public static void main(String[] args){
Integer a = -8;
double d = -100;
float f = -90;
System.out.println(Math.abs(a));
System.out.println(Math.abs(d));
System.out.println(Math.abs(f));
}

}

Output

D:\javafiles>java test.java
8
100.0
90.0

Reference: http://www.runoob.com/java/number-abs.html
---------

Java ceil() & floor()


Input 

public class test{
public static void main(String[] args){
double d = 100.675;
float f = -90;
System.out.println(Math.ceil(d));
System.out.println(Math.ceil(f));
System.out.println(Math.floor(d));
System.out.println(Math.floor(f));
}

}
Output

D:\javafiles>java test.java
101.0
-90.0
100.0
-90.0

Reference: http://www.runoob.com/java/number-ceil.html
---------

Java rint()

Input 
public class test{
public static void main(String[] args){
double d = 100.675;
double e = 100.500;
double f = 100.200;
System.out.println(Math.rint(d));
System.out.println(Math.rint(e));
System.out.println(Math.rint(f));
}

}


Output
D:\javafiles>java test.java
101.0
100.0

100.0

Reference: http://www.runoob.com/java/number-rint.html
---------

Java round()

Input 
public class test{
public static void main(String[] args){
double d = 100.675;
double e = 100.500;
float f = 100;
float g = 90f;
System.out.println(Math.round(d));
System.out.println(Math.round(e));
System.out.println(Math.round(f));
System.out.println(Math.round(g));
}

}

Output
D:\javafiles>java test.java
101
101
100
90


Reference: http://www.runoob.com/java/number-round.html
---------

Java min()


Input 
public class test{
public static void main(String[] args){
System.out.println(Math.min(12.123, 12.456));
System.out.println(Math.min(23.12, 23.0));
}

}

Output

D:\javafiles>java test.java
12.123
23.0

Reference: http://www.runoob.com/java/number-min.html
---------

Java max()

Input 

public class test{
public static void main(String[] args){
System.out.println(Math.max(12.123, 18.456));
System.out.println(Math.max(23.12, 23.0));
}

}
Output

D:\javafiles>java test.java
18.456
23.12

Reference: http://www.runoob.com/java/number-max.html
---------

Java exp()


Input 
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;

System.out.printf("Value of e : %.4f%n", Math.E);
System.out.printf("Value of exp(%.3f) : %.3f%n", x, Math.exp(x));
}

}

Output
D:\javafiles>java test.java
Value of e : 2.7183
Value of exp(11.635) : 112983.831

Reference: http://www.runoob.com/java/number-exp.html
---------

Java log()


Input 
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;

System.out.printf("Value of e : %.4f%n", Math.E);
System.out.printf("Value of log(%.3f) : %.3f%n", x, Math.log(x));
}

}

Output
D:\javafiles>java test.java
Value of e : 2.7183
Value of log(11.635) : 2.454

Reference: http://www.runoob.com/java/number-log.html

---------

Java pow()

Input 
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;

System.out.printf("Value of e : %.4f%n", Math.E);
System.out.printf("Value of pow(%.3f, %.3f): %.3f%n", x, y, Math.pow(x, y));
}

}

Output

D:\javafiles>java test.java
Value of e : 2.7183
Value of pow(11.635, 2.760): 874.008

Reference:http://www.runoob.com/java/number-pow.html
---------

Java sqrt()

Input 
public class test{
public static void main(String[] args){
double x = 11.635;
double y = 2.76;

System.out.printf("Value of e: %.4f%n", Math.E);
System.out.printf("Value of sqrt(%.3f): %.3f%n", x, Math.sqrt(x));
}

}

Output
D:\javafiles>java test.java
Value of e: 2.7183
Value of sqrt(11.635): 3.411

Reference: http://www.runoob.com/java/number-sqrt.html
---------

Java random()

Input 
public class test{
public static void main(String[] args){
System.out.println( Math.random() );
System.out.println( Math.random() );
}

}

Output
D:\javafiles>java test.java
0.7514961840542885
0.36280573195436616

Reference: http://www.runoob.com/java/number-random.html
---------

Sunday, April 14, 2019

2019 Easy guide for Java - Installation and setup

Setup and Install Flow

Since I need to install Java at my company laptop for writing some easy program to help my works. (Also for learning and fun) However, I faced so many problems during setup and install the Java. Here is a record for the flow and problem faced for future reference. Hopefully it helps those visitors from all over the world who face similar problems. 

As my laptop is using Windows 7, so all the procedures below are the setup process for Windows 7. I do the same with another laptop that using Windows 10. Probably the setup procedure are somehow the same.

1.) Download Java JDK and Java JRE

If you want to drink coffee, of course you need to buy or make a cup of coffee. If you need play with Java, you need not buy but could download for free from the Internet. At the moment, Java is owned by Oracle so we could download it officially from Oracle official page.

https://www.oracle.com/technetwork/java/javase/downloads/index.html

For JDK (Java Development Kit) , there are a few options: JavaSE, JavaEE, JavaME...etc. What's the difference amount these versions?

JavaSE is Standard Edition.
JavaEE is Enterprise Edition
JavaME is Micro Edition

Go https://stackoverflow.com/questions/2857376/difference-between-java-se-ee-me to see more.

As a normal user, I download the SE edition. The latest JavaSE version is 12. For easy handling, I just download the jdk-12_windows-x64_bin.exe. Just Download and double click the downloaded file, follow the steps and everything got done.

Beside of JDK, we also need the JRE (Java Runtime Environment). Why we need the runtime environment? If you ask this question, you probably have no idea how java works, so am I. You can go https://www.geeksforgeeks.org/differences-jdk-jre-jvm/ to learn more.

So where to download the JRE? You can go here:
https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html

At the moment, there are two latest JRE version: 8u201 and 8u202. Some people says that the bigger number is the best options, however, it may not be the case. Actually it is about CPU(Critical Patch Update) and PSU(Patch Set Update).  Go https://www.oracle.com/technetwork/java/javase/cpu-psu-explained-2331472.htmlto know more. Although the link is for an old version Java, it also clarify the difference for two versions.
For me, I downloaded 8u201 version.

2.) Install

Once download the two JDK and JRE installation files, just open the files and follow the guild to install JavaSE (JDK) and Java JRE.

Here are problems i faced during installation

Problem 1 - changed install location not workable

The preset install location for JDK is
C:\Program Files\Java\ jdk-12 (folder name depends on version) \

and JRE is
C:\Program Files\Java\jre1.8.0_201(folder name depends on version) \

However, I had changed the path to D: during my first installation. It makes me lots of trouble. I tried every solution e.g. set the correct variable environment and every possible but it still not work. Therefore, for beginner it's not recommended change the suggested installation path.

Problem 2 - Path setting

As a Windows user, we need to set the environment variable. Go https://www.java.com/en/download/help/path.xml to learn how to set environment variable.
Still can't run java after setting the environment, there are many reasons for not working.
Go https://stackoverflow.com/questions/4419983/a-jre-or-jdk-must-be-available-in-order-to-run-eclipse-no-jvm-was-found-after-s  and  https://stackoverflow.com/questions/2619584/how-to-set-java-home-on-windows-7to know more.

Also, in environment variable, always put JDK/bin  in priority and next JRE/bin. I have no idea the reasons but it works for me once i reset JDK/bin on top.