tag:blogger.com,1999:blog-82832545827269519022024-03-28T23:56:44.376-07:00Learn Java from Basic to AdvancedAnonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.comBlogger33125tag:blogger.com,1999:blog-8283254582726951902.post-29821112737164391682016-08-13T01:04:00.002-07:002016-08-13T01:55:59.400-07:00PackageSo far, we use directory <em>D:\javacode</em> only to place our java code. Many times when we get a chance to work on a small project, one thing we intend to do is to put all java files into one single directory. It is quick and easy. However if our small project gets bigger, and the number of files is increasing, putting all these files into the same directory would make complication. In java we can avoid this problem by using package.
<br />
<br />
A package is a namespace that organizes a set of related classes. Conceptually you can think of package as being similar to different folders on your computer. Package is the way we organize files into different directories according to their functionality, usability as well as category they should belong to. Package also helps us to avoid class name collision when we use the same class name as that of others. The benefits of using package reflect the ease of maintenance, organization, and increase collaboration among developers.
<br />
<br />
<br />
<h3>
Create a Package
</h3>
<br />
To create a package, you choose a name for the package and put a statement package with that name at the top of every source file that you want to include in the package. The package statement must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.
<br />
<br />
Suppose we have two files named <span style="font-family: "courier new" , "courier" , monospace;">Circle.java</span> and <span style="font-family: "courier new" , "courier" , monospace;">Triangle.java</span>, and we want to put this file in a package <span style="font-family: "courier new" , "courier" , monospace;">geometry</span>. First thing we have to do is to specify the keyword <span style="font-family: "courier new" , "courier" , monospace;">package</span> with the name of the package we want to use (<span style="font-family: "courier new" , "courier" , monospace;">geometry</span> in our case) on top of our source file, before the code that defines the real classes in the package, as shown in our <span style="font-family: "courier new" , "courier" , monospace;">Circle</span> and <span style="font-family: "courier new" , "courier" , monospace;">Triangle</span> class below:
<br />
<br />
<pre class="brush: java">package geometry;
public class Circle {
private final double PI = 3.1416:
private double r;
public Circle(double r) {
this.r = r;
}
public double area() {
return (PI * r * r)
}
public double circumference() {
return (2 * PI * r);
}
}
</pre>
<br />
<pre class="brush: java">package geometry;
public class Triangle {
private double height;
private double base;
public Triangle(double height, double base) {
this.height = height;
this.base = base;
}
public double area() {
return ((base * height) / 2);
}
}
</pre>
<br />
One thing you must do after creating a package for the class is to create nested subdirectories to represent package hierarchy of the class. In our case, we have the package <span style="font-family: "courier new" , "courier" , monospace;">geometry</span>, which requires only one directory. So, we create a directory <span style="font-family: "courier new" , "courier" , monospace;">geometry</span> and put our <span style="font-family: "courier new" , "courier" , monospace;">Circle.java</span> and <span style="font-family: "courier new" , "courier" , monospace;">Triangle.java</span> into it. Compile those two files, then we will get <span style="font-family: "courier new" , "courier" , monospace;">Circle.class</span> and <span style="font-family: "courier new" , "courier" , monospace;">Triangle.class</span>.
<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqN92qXUrlhhBM0ofSk9R7KsS6a4sLW5l4-mFwaEPaztnKeQG73WtHOUyEYyuUQsKD7JfQnUp-E40vdjIDFCmFypjRYW1Y-fMvGZlmy2ZXoYZXWwXZTI96jReFU3T1CE3Hea30beI11NQy/s1600/geometry2.png" imageanchor="1"><img border="0" height="177" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqN92qXUrlhhBM0ofSk9R7KsS6a4sLW5l4-mFwaEPaztnKeQG73WtHOUyEYyuUQsKD7JfQnUp-E40vdjIDFCmFypjRYW1Y-fMvGZlmy2ZXoYZXWwXZTI96jReFU3T1CE3Hea30beI11NQy/s640/geometry2.png" width="640" /></a>
<br />
<br />
<br />
<h3>
How to Use Package
</h3>
<br />
To use classes stored in package, use keyword <span style="font-family: "courier new" , "courier" , monospace;">import</span>. Now we will create <span style="font-family: "courier new" , "courier" , monospace;">CallPackageDemo</span> class in directory <em>D:\javacode</em> and use <span style="font-family: "courier new" , "courier" , monospace;">Circle</span> and <span style="font-family: "courier new" , "courier" , monospace;">Triangle</span> class in package <span style="font-family: "courier new" , "courier" , monospace;">geometry</span>. Create <span style="font-family: "courier new" , "courier" , monospace;">CallPackageDemo.java</span> in <em>D:\javacode </em>directory with following program:
<br />
<br />
<pre class="brush: java">import geometry.*;
class CallPackageDemo {
public static void main(String[] args) {
Circle circle = new Circle(7);
Triangle triangle = new Triangle(4, 3);
System.out.printin("Circle area : " + circle.area());
System.out.printin("Circle circumference : " + circle.circumference());
System.out.printin("Triangle area : " + triangle.area());
}
}
</pre>
<br />
Compile and run <span style="font-family: "courier new" , "courier" , monospace;">CallPackageDemo</span>, then the result that will be given by the above program as follows:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Circle area : 153.9384</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Circle circumference : 43.9284</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Triangle area : 6.0</span><br />
<br />
Instead of call all package content using asterisk (*) like this following statement:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">import geometry.*;</span>
<br />
<br />
You can also call the class inside the package one by one like this following statement:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">import geometry.Circle;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">import geometry.Triangle;</span>
<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com4tag:blogger.com,1999:blog-8283254582726951902.post-92205276586332605602016-08-10T00:34:00.000-07:002016-08-10T05:09:26.055-07:00Method OverridingWhen we define a method in a derived class where name and the argument exactly same with in parent class, then we already override method in parent class. If we invoke method that has been overridden by the object of the derived class, then the method that will be executed is the method contained in the derived class, not in contained in the parent class. In other words, if we do override the method in the parent class, then the method in the parent class will be hidden in derived class. Consider the following code examples:<br />
<br />
<pre class="brush: java">class A {
private int a;
public void setA(int value) {
a = value;
}
public int getA() {
return a;
}
public void showValue() {
System.out.println("Value a : " + getA());
}
}
class B extends A {
private int b;
public void setB(int value) {
b = value;
}
public int getB() {
return b;
}
//override method showValue() in class A
public void showValue() {
System.out.println("Value b : " + getB());
}
}
class DemoOverride1 {
public static void main(String[] args) {
B obj = new B();
obj.setA(100);
obj.setB(200);
//will call method in class B
obj.showValue();
}
}
</pre>
<br />
The result that will be given by the above program as follows:<br />
<br />
V<span style="font-family: "courier new" , "courier" , monospace;">alue b : 200</span><br />
<br />
As shown in the above result that when we create object of class B, and we call method <span style="font-family: "courier new" , "courier" , monospace;">showValue()</span> through the object, then the method <span style="font-family: "courier new" , "courier" , monospace;">showValue() </span>will be called is method <span style="font-family: "courier new" , "courier" , monospace;">showValue()</span> in class B. Method <span style="font-family: "courier new" , "courier" , monospace;">showValue()</span> contained in class A will be hidden. However, in certain cases, sometimes we also need to call the method contained in the class A. To overcome this, we can use keyword <span style="font-family: "courier new" , "courier" , monospace;">super</span>, as shown in the following code:<br />
<br />
<pre class="brush: java">class A {
private int a;
public void setA(int value) {
a = value;
}
public int getA() {
return a;
}
public void showValue() {
System.out.println("Value a : " + getA());
}
}
class B extends A {
private int b;
public void setB(int value) {
b = value;
}
public int getB() {
return b;
}
//override method showValue() in class A
public void showValue() {
super.showValue(); //call method in class A
System.out.println("Value b : " + getB());
}
}
class DemoOverride2 {
public static void main(String[] args) {
B obj = new B();
obj.setA(100);
obj.setB(200);
//will call method in class B
obj.showValue();
}
}
</pre>
<br />
This time, the program will provide the following result:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Value a: 100</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value b: 200</span><br />
<br />
Why? Because, in method <span style="font-family: "courier new" , "courier" , monospace;">showValue()</span> that contained in class B, explicitly we also invoke method <span style="font-family: "courier new" , "courier" , monospace;">showValue()</span> contained in class A.<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com3tag:blogger.com,1999:blog-8283254582726951902.post-41770235886881976322016-08-09T18:27:00.002-07:002016-08-09T19:13:48.984-07:00Protected Access ModifierIn the previous post, you learned how to <span style="font-family: "courier new" , "courier" , monospace;">private</span> and <span style="font-family: "courier new" , "courier" , monospace;">public</span> access modifier work. Now we will discuss another access modifier in Java, which is <span style="font-family: "courier new" , "courier" , monospace;">protected</span>. As previously mentioned, the derived class still can not access the private part of its parent class. In certain cases, sometimes we need data that can only be accessed by classes that have child relationship, without allowing the outside environment to access the data. In this situation, we can not use <span style="font-family: "courier new" , "courier" , monospace;">private</span> access modifier for data, because <span style="font-family: "courier new" , "courier" , monospace;">private</span> is only recognized by a particular class only. Not too <span style="font-family: "courier new" , "courier" , monospace;">public</span>, because if we use this access level, the external environment is still able to access the data. The right solution for such problem is by using a access modifier <span style="font-family: "courier new" , "courier" , monospace;">protected</span>. In this way, the data can be accessed by all classes that have child relationship, but the external environment were not given the right to access the data.<br />
<br />
Java provides keyword <span style="font-family: "courier new" , "courier" , monospace;">protected</span> to declare protected data. Here is an example of a program that can show how <span style="font-family: "courier new" , "courier" , monospace;">protected</span> access modifier work:<br />
<br />
<pre class="brush: java">class A {
private int a; //only recognized by class A
//only recognized by class A and its derivative class
protected void setA (int value) {
a = value;
}
//only recognized by class A and its derivative class
protected int getA() {
return a;
}
}
class B extends A {
private int b;
//constructor class B
B(int valueA, int valueB) {
//a = valueA; //INCORRECT, because a is not recognized here
setA(valueA); //using method setA()
b = valueB;
}
public void showData() {
//using method getA
System.out.println("Value a : " + getA());
System.out.println("Value b : " + b);
}
}
class C {
private int c;
public void setC(int value) {
//set A (10); //INCORRECT, setA() is not recognized here
c = value;
}
public int getC() {
return c;
}
public void showC() {
//System.out.println("Value a : " + getA()); //INCORRECT
System.out.println("Value c : " + c);
}
}
Class DemoProtected1 {
public static void main(String[] args) {
B obj = new B(40, 50);
obj.showData();
obj.setA(100);
System.out.println("Value a : " + obj.getA()):
}
}
</pre>
<br />
The above program will produce following output:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Value a: 40</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value b: 50</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value a: 100</span><br />
<br />
In the program above, we define method <span style="font-family: "courier new" , "courier" , monospace;">setA()</span> and <span style="font-family: "courier new" , "courier" , monospace;">getA()</span> in class A with <span style="font-family: "courier new" , "courier" , monospace;">protected</span>. Thus, the method can be accessed by derived class (in this case class B). However, when we access the method of class C, there will be error during the compilation. This is because the class C does not have a hierarchical relationship to class A. Here, class C is considered as an external environment which is not allowed to access the protected data from class A.<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com3tag:blogger.com,1999:blog-8283254582726951902.post-19679982847475384852016-08-08T21:08:00.001-07:002016-08-09T23:52:41.945-07:00Superclass and SubclassInheritance is one characteristic of object-oriented programming, which states that a class can be derived again into new classes. Java allows us to define a generic class. Furthermore, the class can be derived again into a new class with more specific properties. In Java terminology, the parent class is called superclass and the new derived class is called subclass. In this derivation process, the derived class will inherit the properties contained in the parent class. Furthermore, the derived class can have specific properties previously not owned by the parent class. For example, class <em>Animal</em>. The class can then be derived again into new more specific classes without leaving the properties of class <em>Animal</em>, to: class <em>Herbivore</em> and <em>Carnivore</em>. Derived classes can also be derived again into new classes. For example, class <em>Herbivore</em> can be derived to class <em>Cow</em> and <em>Sheep</em>, class <em>Carnivore</em> can be derived to class <em>Lion</em> and <em>Tiger</em>, and so forth. The class hierarchy can be illustrated by the following picture:
<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg38sGIpMiKkxAFfCNW0LNG2aOtwV-v6bm4BDVFQc0UYNp7EBWLifbv1O9FezobmScPS6_GUJifwLl7QD1OB3mg5jV7XTKwZPkl2s14XG0ExFhWaqXjG-XTPRsXsbGUQ96h0J6t6BW_epx2/s1600/animal+hierarchy.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg38sGIpMiKkxAFfCNW0LNG2aOtwV-v6bm4BDVFQc0UYNp7EBWLifbv1O9FezobmScPS6_GUJifwLl7QD1OB3mg5jV7XTKwZPkl2s14XG0ExFhWaqXjG-XTPRsXsbGUQ96h0J6t6BW_epx2/s400/animal+hierarchy.png" /></a>
<br />
<br />
In this case, class <em>Animal</em> is a superclass of class <em>Herbivore</em> and <em>Carnivore</em>. <em>Herbivore</em> and <em>Carnivore</em> class itself acts as a subclass. However, as you can see above, a subclass can be a superclass for other derived classes. In this case, subclass <em>Herbivore </em>can be a superclass of class <em>Cow </em>and <em>Sheep</em>. Likewise subclass <em>Carnivore</em>, can be a <em>superclass </em>of class <em>Lion</em> and <em>Tiger</em>.<br />
<br />
<br />
<h3>
Making Subclass</h3>
<br />
Java provides keyword <em>extends</em> that used to perform class derivation. The general form of the use of the keyword as follows:<br />
<br />
<em>class subclass-name extends superclass-name {</em><br />
<em> class body</em><br />
<em>}</em><br />
<br />
Consider the following simple program, which will show how to create a derived class and any benefits from the process.<br />
<br />
<pre class="brush: java">class A {
private int a;
public void setA (int value) {
a = value;
}
public int getA() {
return a;
}
}
//create subclass from class A
class B extends A {
private int b;
public void setB(int value) {
b = value;
}
public int getB() {
return b;
}
}
class InheritanceDemo1 {
public static void main(String[] args) {
B obj = new B();
obj.setA(100);
obj.setB(200);
System.out.println("nilai a : " + obj.getA());
System.out.println("nilai b : " + obj.getB());
}
}
</pre>
<br />
As you can see in the code above, class B is a subclass or an instance of class A. As a result, class B will inherit the properties contained in the class A. As a proof of this statement, observe the return code above. There, we create an object or an instance of class B, but in fact, the object also has method <span style="font-family: "courier new" , "courier" , monospace;">setA()</span> and <span style="font-family: "courier new" , "courier" , monospace;">getA()</span>, but we never define both method in class B. This means, class B has inherited method <span style="font-family: "courier new" , "courier" , monospace;">setA()</span> and <span style="font-family: "courier new" , "courier" , monospace;">getA()</span> of class A. However, private data contained in the superclass (class A) remains inaccessible to the subclass (class B). When executed, the above program will give the following results:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Value a : 100</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value b : 200</span><br />
<br />
Derived classes can be derived again into other new classes. For example in this case, we want to derived class B into a new class, namely the class C. In this way, the class C will inherit the properties contained in the class A and class B. The following code will demonstrate this:<br />
<br />
<pre class="brush: java">class A {
private int a;
public void setA (int value) {
a = value;
}
public int getA() {
return a;
}
}
//create subclass from class A
class B extends A {
private int b;
public void setB(int value) {
b = value;
}
public int getB() {
return b;
}
}
//create subclass from class B
class C extends B {
private int c;
public void setC(int value) {
c = value;
}
public int getC() {
return c;
}
class InheritanceDemo2 {
public static void main(String[] args) {
C obj = new C();
obj.setA(100);
obj.setB(200);
obj.setC(300);
System.out.println("Value a : " + obj.getA());
System.out.println("Value b : " + obj.getB());
System.out.println("Value c : " + obj.getC());
}
}
</pre>
<br />
This time, the result that will be provided by the program is as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Value a : 100</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value b : 200</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value c : 300</span><br />
<br />
From these results, we can see clearly that the object of class C will have the methods defined in the class A and B, namely: method <span style="font-family: "courier new" , "courier" , monospace;">setA()</span>, <span style="font-family: "courier new" , "courier" , monospace;">getA()</span>, <span style="font-family: "courier new" , "courier" , monospace;">setB()</span>, and <span style="font-family: "courier new" , "courier" , monospace;">getB()</span>.<br />
<br />
Unlike C++, Java does not support multiple inheritance. In C++, a derivative class may be formed from two or more parent classes. The derived class will then have the properties contained in all of the parent class.<br /><br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com6tag:blogger.com,1999:blog-8283254582726951902.post-10242125923203728182016-07-13T01:17:00.005-07:002016-07-13T01:44:49.616-07:00Keyword finalIn Java, keyword <span style="font-family: "courier new" , "courier" , monospace;">final</span> has three functions, as follows:<br />
<ul>
<li>When used to declare a variable, then the value of the variable can not be changed.</li>
<li>When used to declare a method, then the method can not be overridden by derived class.</li>
<li>When used to define a class, then the class can no longer be derived to new class.</li>
</ul>
<br />
Here, the point which we will discuss only the first point only. Second and third points will be discussed later in next post, when we talk about inheritance to a derived class.<br />
<br />
If we want to use keyword <span style="font-family: "courier new" , "courier" , monospace;">final</span> to declare a variable as a constant, then we need to fill the value into the variable. The value will be fixed or can not be changed during the execution of the program. Here is the general form of the declaration of a constant in Java:<br />
<br />
<em>access-modifier final data-type constant-name = value;</em><br />
<br />
Just as in C/C ++, in Java, constants are generally written in capital letters. Here are some examples of declaring constants:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">final double PI = 3.1416;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">private final int ERROR = -1;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">private final int ENGLISH = 1;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">private final int ARABIC = 0;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">private final String FILE_MENU_TITLE = "File";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">private final String EXIT_BUTTON_TITLE = "Exit";</span><br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com2tag:blogger.com,1999:blog-8283254582726951902.post-21793130288816393652016-07-13T00:00:00.001-07:002016-07-13T01:01:39.204-07:00Access Modifier: private, protected, and publicAs we've mentioned earlier, in doing encapsulation, in fact we combine the data and code (method) into one. In these circumstances, we can determine access level of data and method, called access modifier, using keyword <span style="font-family: "courier new" , "courier" , monospace;">private</span>, <span style="font-family: "courier new" , "courier" , monospace;">public</span> or <span style="font-family: "courier new" , "courier" , monospace;">protected</span>. In object oriented programming, data is normally designed to be private or only accessible to classes that have it all. Accessing data from the outside can only be done through method. This means, the method acts as the interface. Therefore, methods are generally public. Nevertheless, if we want to define methods that can only be accessed by the class itself, then we must declare it with the keyword <span style="font-family: "courier new" , "courier" , monospace;">private</span>. One more thing, if we want the data and method that we defined in the class can be accessed by derived classes, the data and the method must be declared with the keyword <span style="font-family: "courier new" , "courier" , monospace;">protected</span>.<br />
<br />
In general, data and method encapsulation in a class can be written as follows:<br />
<br />
<em>class ClassName {</em><br />
<em> access-modifier data1;</em><br />
<em> access-modifier data2;</em><br />
<em> ...</em><br />
<em> access-modifier dataN;</em><br />
<em></em><br />
<em> access-modifier method1 (list-of-parameters) {</em><br />
<em> //code for method1</em><br />
<em> }</em><br />
<em></em><br />
<em> access-modifier method2 (list-of-parameters) {</em><br />
<em> //code for method2</em><br />
<em> }</em><br />
<em> ...</em><br />
<em> access-modifier methodN (list-of-parameters) {</em><br />
<em> //code for method</em><br />
<em> }</em><br />
<em>}</em><br />
<br />
In this case, access-modifier is one of keyword <span style="font-family: "courier new" , "courier" , monospace;">private</span>, <span style="font-family: "courier new" , "courier" , monospace;">public</span>, or <span style="font-family: "courier new" , "courier" , monospace;">protected</span>. If we do not include one of these keywords when declaring the data or method, then by default Java will regard it as public. Consider the following example:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">class AccessModifierExample {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> //by default, a is public</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> int a;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> //b is declared as public</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public int b;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> //c is declared as private</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> private char c;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ...</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> //by default, method1() is public</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> int method1 () {...)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> //method2 is declared as protected</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> protected void method2 () {...}</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> //method3 is declared as private </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> method3 private char () {...}</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<br />
Here is an example of a program that will show the effect of <span style="font-family: "courier new" , "courier" , monospace;">public</span> and <span style="font-family: "courier new" , "courier" , monospace;">private</span> access modifier in a class. <span style="font-family: "courier new" , "courier" , monospace;">protected</span> access modifier will be discussed in next post.<br />
<pre class="brush: java">class AccessModifier {
int a;
public int b;
private int c;
public void setC(int value) {
c = value;
}
public int getC() {
return c;
}
}
class PublicAndPrivateDemo {
public static void main(String[] args) {
AccessModifier obj = new AccessModifier();
obj.a = 10; //CORRECT, because by default a is public
obj.b = 20; //CORRECT, because b is public
//obj.c = 30; //WRONG, because c is private
obj.setC(30); //CORRECT, because method setC() is public
System.out.println ("Value obj.a : " + obj.a);
System.out.println ("Value obj.b : " + obj.b);
System.out.println ("Value obj.c : " + obj.getC);
}
}
</pre>
<br />
As you can see above, we declare data <span style="font-family: "courier new" , "courier" , monospace;">a</span> and <span style="font-family: "courier new" , "courier" , monospace;">b</span> contained in <span style="font-family: "courier new" , "courier" , monospace;">AccessModifier</span> class as <span style="font-family: "courier new" , "courier" , monospace;">public</span>, so that data can be accessed from the outside environment (in this case, the call is done in <span style="font-family: "courier new" , "courier" , monospace;">PublicAndPrivateDemo</span> class). However, we declare data <span style="font-family: "courier new" , "courier" , monospace;">c</span> as private so it is not accessible from the outside environment. Therefore, to access data <span style="font-family: "courier new" , "courier" , monospace;">c</span> we need to define a method that serves as the interface, namely: method <span style="font-family: "courier new" , "courier" , monospace;">setC()</span> and <span style="font-family: "courier new" , "courier" , monospace;">getC()</span> which are both <span style="font-family: "courier new" , "courier" , monospace;">public</span>. Method <span style="font-family: "courier new" , "courier" , monospace;">setC()</span> is used to fill in the value of data <span style="font-family: "courier new" , "courier" , monospace;">c</span>, while the method <span style="font-family: "courier new" , "courier" , monospace;">getC()</span> is used to get the value of data <span style="font-family: "courier new" , "courier" , monospace;">c</span>. Thus, although <span style="font-family: "courier new" , "courier" , monospace;">c</span> is private, but indirectly also the outside environment can still access the data. Here is the result for above program:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Value obj.a : 10</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value obj.b : 20</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value obj.c : 30</span><br />
<br />
Because it is public, then we can immediately call <span style="font-family: "courier new" , "courier" , monospace;">a</span> and <span style="font-family: "courier new" , "courier" , monospace;">b</span> value that contained in <span style="font-family: "courier new" , "courier" , monospace;">AccessModifier</span> class in the following manner:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">System.out.println ("Value obj.a : " + obj.a);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">System.out.println ("Value obj.b : " + obj.b);</span><br />
<br />
However, for data <span style="font-family: "courier new" , "courier" , monospace;">c</span>, we can only call value through the method <span style="font-family: "courier new" , "courier" , monospace;">getC()</span> using the following way:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">System.out.println ("Value obj.c : " + obj.getC);</span><br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com3tag:blogger.com,1999:blog-8283254582726951902.post-65911650283432983942016-07-12T21:10:00.003-07:002016-07-12T21:18:49.898-07:00Object as Return ValueObject also can be used as return value on a method. Here is an example of a program that will demonstrate the concept:<br />
<pre class="brush: java">class Box {
double length;
double width;
double height;
Box(double l, double w, double h) {
length = l;
width = w;
height = h;
}
double calculateVolume() {
return (length * width * height);
}
//Method that return object box
Box enlarge(int size) {
Box temp = new Box(length * size, width * size, height * size);
return temp;
}
}
class DemoReturnObject {
public static void main(string[] args) {
Box originalBox, newBox:
originalBox = new Box(4, 3, 2);
newBox = originalBox.enlarge(2);
System.out.println("originalBox object value");
System.out.println("length: " + originalBox.length);
System.out.println("width: " + originalBox.width);
System.out.println("height: " + originalBox.height);
System.out.println("Volume: " + originalBox.calculateVolume());
System.out.println("newBox object value");
System.out.println("length: " + newBox.length);
System.out.println("width: " + newBox.width);
System.out.println("height: " + newBox.height);
System.out.println("Volume: " + newBox.calculateVolume());
}
}
</pre>
<br />
The above program will give the following result:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">originalBox object value</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">length: 4.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">width: 3.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">height: 2.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume: 24.0</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">newBox object value</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">length: 8.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">width: 6.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">height: 4.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume: 192.0</span><br />
<br />
As you can see above that method <span style="font-family: "courier new" , "courier" , monospace;">enlarge()</span> will return an object box with <span style="font-family: "courier new" , "courier" , monospace;">length</span>, <span style="font-family: "courier new" , "courier" , monospace;">width</span>, and <span style="font-family: "courier new" , "courier" , monospace;">height</span> two times larger than its original object.<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com2tag:blogger.com,1999:blog-8283254582726951902.post-14621808879235801172016-07-12T19:57:00.002-07:002016-07-12T20:11:33.010-07:00Object as ParameterIn Java, the object can serve as parameter from a method. So far we are only using variables with simple type as parameters that we pass into a method. In this post we will see how Java treats the object as a parameter. To simplify the discussion, here we will add a method to the class <span style="font-family: "courier new" , "courier" , monospace;">Box</span> containing the parameter of object. The method we will use to compare the box object with each other, whether equal or not. The source code is as follows:<br />
<br />
<pre class="brush: java">class Box {
double length;
double width;
double height;
Box (double l, double w, double h) {
length = l;
width = w;
height = h;
}
double calculateVolume() {
return(length * width * height);
}
//Define method with object Box as parameter
boolean isSame(Box k) {
if ((k.length == this .length) &&
(k.width == this.width) &&
(k height == this.height)) {
return true;
} else {
return false;
}
}
}
class DemoParamObject {
public void main(String [] args) {
Box b1, b2, b3, b4;
b1 = new Box(4, 3, 2);
b2 = new Box(6, 5, 4);
b3 = new Box(4, 3, 2);
b4 = new Box(6, 5, 4);
System.out.println ("b1 == b2 : " + b1.isSame(b2));
System.out.println ("b1 == b3 : " + b1.isSame(b3));
System.out.println ("b2 == b4 : " + b2.isSame(b4));
}
}
</pre>
<br />
The result will be given by the above program as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">b1 == b2: false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">b1 == b3: true</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">b2 == b4: true</span><br />
<br />
Method <span style="font-family: "courier new" , "courier" , monospace;">isSame()</span> that you see above has box object as parameter. Please also be noted how to call:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">b1.isSame(b2)</span><br />
<br />
The above code is used to check whether the object <span style="font-family: "courier new" , "courier" , monospace;">b1</span> and <span style="font-family: "courier new" , "courier" , monospace;">b2</span> is equal to or not. If the value of both object is same, the method will return <span style="font-family: "courier new" , "courier" , monospace;">true</span>. Otherwise, the value to be returned is <span style="font-family: "courier new" , "courier" , monospace;">false</span>.
<br />
<br />
Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com2tag:blogger.com,1999:blog-8283254582726951902.post-30794861152050491112016-07-12T01:13:00.000-07:002016-07-12T19:41:53.561-07:00Constructor OverloadingThe overload process can also be applied to the constructor of a class. This is because constructor also is a method that returns a class type (himself). A class can has more than one constructor. To demonstrate it, consider the following class definition:<br />
<br />
<pre class="brush: java">class Box {
double length;
double width;
double height
//Constructor for class Box
Box (double l, double w, double h) {
length = l;
width = w;
height = h;
}
//Method for calculate box volume
double hitungVolume() {
return(length * width * height);
}
}
</pre>
<br />
You can see clearly that the constructor in the class <span style="font-family: "courier new" , "courier" , monospace;">Box</span> above has three parameters. This means, every initialization of object Box, we also had to pass three arguments into it. Thus, we were not able to make the object box in the following manner:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Box k = new Box();</span><br />
<br />
This is because the default constructor on the class <span style="font-family: "courier new" , "courier" , monospace;">Box</span> is no longer valid, because it is replaced by the constructor we define above. To overcome this, we can do constructor overloading, as shown in the program code below.<br />
<br />
<pre class="brush: java">class Box {
double length;
double width;
double height;
//Define constructor without parameter
Box() {
length = 0;
width = 0;
height = 0;
}
//Define constructor with one parameter
Box(double side) {
length = side;
width = side;
height = side;
}
//Define constructor with three parameters
Box(double l, double w, double h) {
length = l;
width = w;
height = h;
}
double calculateVolume() {
return(length * width * height);
}
}
class DemoOverloadConstructor {
public static void main(String[] args) {
Box b1, b2, b3;
b1 = new Box();
b2 = new Box (10);
b3 = new Box (4, 3, 2);
//Display volume from each Box object
System.out.println("Volume b1 = " b1.calculateVolume());
System.out.println("Volume b2 = " b2.calculateVolume());
System.out.println("Volume b3 = " b3.calculateVolume());
}
}
</pre>
<br />
The result will be given is as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume k1 = 0.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume k2 = 1000.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume k3 = 24.0</span><br />
<br />
It was clear in the above program that we define three constructors for class <span style="font-family: "courier new" , "courier" , monospace;">Box</span>. First, with no parameter. Here, all data contained in the class <span style="font-family: "courier new" , "courier" , monospace;">Box</span> will be initialized with a value of 0. Second, have one parameter, that is used to create cube object (cube is a geometry that has same length, width, and height). Third, has three parameters, are used to create box with different value of length, width, and height. Actually, we can create a cube object by using a third constructor, as long as we fill the same value to the existing three arguments.<br />
<br />
In the above example, we pass the argument with a value of 10 in the second constructor. This causes the volume obtained from the object is 1000.0, which is derived from 10 x 10 x 10. For the third constructor, we pass arguments to the value of 4, 3 and 2 so that the volume obtained was 24.0.<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com2tag:blogger.com,1999:blog-8283254582726951902.post-3340225386361315422016-07-11T20:15:00.001-07:002016-07-11T21:00:46.321-07:00Method OverloadingIn one class, we can define more than one method with the same name, as long as the parameter contained in those methods differ. Parameters in a method said to be different from other methods if:<br />
<ul>
<li>The number is different, although the data type is same</li>
<li>The data type is different, although the number is same</li>
<li>The number and data type are different </li>
</ul>
<br />
The process of defining a method with same name is called method overloading. Consider the following two examples of method:<br />
<br />
<span style="font-family: "Courier New", Courier, monospace;">int divide(int a, int b) {</span><br />
<span style="font-family: "Courier New", Courier, monospace;"> return a/b;</span><br />
<span style="font-family: "Courier New", Courier, monospace;">}</span><br />
<span style="font-family: "Courier New", Courier, monospace;"></span><br />
<span style="font-family: "Courier New", Courier, monospace;">double divide(double a, double b) {</span><br />
<span style="font-family: "Courier New", Courier, monospace;"> return a/b;</span><br />
<span style="font-family: "Courier New", Courier, monospace;">}</span><br />
<br />
The above code is legal to be defined in a class. In this example, we define two methods with the same name, but the parameters are different. In this case, the parameters are distinguished by data type. The first <em>divide</em> method uses two parameters with type <span style="font-family: "courier new";">int</span>, while the second method parameters use two parameters with type <span style="font-family: "courier new" , "courier" , monospace;">double</span>.<br />
<br />
Here is an example of a program that will show the process of method overloading in a class.<br />
<br />
<pre class="brush: java">class Division {
//Define method with two parameters with type int
int divide(int a, int b) {
return a/b;
}
//Define method with two parameters with type double
double divide(double a, double b) {
return a/b;
}
}
class DemoOverload1 {
public static void main(String[] args) {
Division d = new Division();
int x = d.divide(10, 4);
double y = d.divide(10.0, 4.0);
System.out.println("Division result for int type = " + x);
System.out.println("Division result for double type = " + y);
}
}
</pre>
<br />
The results will be given by the above program as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Division result for int type = 2</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Division result for double type = 2.5</span><br />
<br />
It appears from the result above, the program will examine the arguments first. Method that is called is a method that has suit parameter with argument types that are passed.<br />
<br />
One more thing to consider in working with method overloading is the order of parameters. Although the number and type of parameters contained in some of the same method, but if the order is different, then the method will also be considered different. For example, we have two methods. The first method has two parameters of type <span style="font-family: "courier new" , "courier" , monospace;">int</span> and <span style="font-family: "courier new" , "courier" , monospace;">String</span>, while the second method parameters are <span style="font-family: "courier new" , "courier" , monospace;">String</span> and <span style="font-family: "courier new" , "courier" , monospace;">int</span>. If the location or the order of the parameters are different, then the program will consider the method as method overloading.<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com2tag:blogger.com,1999:blog-8283254582726951902.post-72666997511550441312016-06-20T18:11:00.000-07:002016-06-20T18:31:48.379-07:00Keyword thisIn certain cases, the method is sometimes necessary to refer to object that has it. To allow this, Java provides keyword <span style="font-family: "courier new" , "courier" , monospace;">this</span>. Keyword <span style="font-family: "courier new" , "courier" , monospace;">this</span> is actually a reference to the object that is currently active. Consider the following code:<br />
<br />
<pre class="brush: java">class Box {
double length, width, height;
//constructor
Box(double l, double w, double h) {
length = l;
width = w;
height = h;
}
}
</pre>
<br />
Actually, the above code can also be written using reference this, as follows:<br />
<br />
<pre class="brush: java">class Box {
double length, width, height;
//constructor
Box(double l, double w, double h) {
this.length = l;
this.width = w;
this.height = h;
}
}
</pre>
<br />
For the case above, the presence of keyword <span style="font-family: "courier new" , "courier" , monospace;">this</span> is actually going to be a little inconvenient and can be said to be unnecessary. However, in other contexts, the use of keyword <span style="font-family: "courier new" , "courier" , monospace;">this</span> is actually required. As proof of this statement, you try to take a good look at the explanation below.<br />
<br />
As we all know, Java does not allow us to declare variable with the same name within a block. On the other hand, Java allows us to declare name of the same parameter as the name of the data contained in the class. Nevertheless, the name of a local variable (in this case, the variable used as a parameter) will overwrite with the name of the data contained in the class. To avoid overwriting process variable, then we must use keyword <span style="font-family: "courier new" , "courier" , monospace;">this</span>, as shown in the following code:<br />
<br />
<pre class="brush: java">class Box {
double length, width, height;
//constructor
Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
}
</pre>
<br />
Here, <span style="font-family: "courier new" , "courier" , monospace;">this.length</span>, <span style="font-family: "courier new" , "courier" , monospace;">this.width</span>, and <span style="font-family: "courier new" , "courier" , monospace;">this.height</span> refer to data <span style="font-family: "courier new" , "courier" , monospace;">length</span>, <span style="font-family: "courier new" , "courier" , monospace;">width</span>, and <span style="font-family: "courier new" , "courier" , monospace;">height</span> contained in the class <span style="font-family: "courier new" , "courier" , monospace;">Box</span>, not refer to parameters contained in the constructor.<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com9tag:blogger.com,1999:blog-8283254582726951902.post-86416735526147794492016-06-20T17:34:00.000-07:002016-06-20T18:06:10.756-07:00ConstructorConstructor is a special method that is defined in the class and will be called automatically whenever there is an instance of an object. Constructor itself serves to initialize the value of the data contained in the relevant class. If we do not define a constructor in the class that we created, Java will make it for us automatically. This kind of constructor is called default constructor. The default constructor initializes all the data with zero values. However, once we define a new constructor for a class that we created, then the default constructor is not not used anymore. Just as method, constructor can have parameter and can also be overloaded.<br />
<br />
Keep in mind that the constructor does not have a return value, not even void. This is because implicitly the actual type of the return of the constructor is the type of the class itself. Constructor name must exactly match with the class name. Here is a sample program that demonstrates a constructor for class <span style="font-family: "Courier New", Courier, monospace;">Box</span>:<br />
<br />
<pre class="brush: java">class Box {
double length;
double width;
double height;
//Define constructor for class Box
Box() {
length = 4;
width = 3;
height = 2;
}
double calculateVolume() {
return (length * width * height);
}
}
class DemoConstructor1 {
public static void main(String[] args) {
Box b1, b2;
b1 = new Box();
b2 = new Box();
System.out.println("Volume b1 = " + b1.calculateVolume());
System.out.println("Volume b2 = " + b2.calculateVolume());
}
}
</pre>
<br />
In the above program, we set the initial value for the data <span style="font-family: "courier new" , "courier" , monospace;">length</span>, <span style="font-family: "courier new" , "courier" , monospace;">width</span>, and <span style="font-family: "courier new" , "courier" , monospace;">height</span> each with value 4, 3 and 2. This would apply to any establishment of <span style="font-family: "courier new" , "courier" , monospace;">Box</span> object. Here is the result that will be given by the program:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume b1 = 24.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume b2 = 24.0</span><br />
<br />
Initialization process using this way can still be said to be less dynamic. Because, every time the creation of the object box, the value of which is used as the initial value is always 4, 3, and 2. To make the constructor is more dynamic, then we can put parameter, as shown in the following program example:
<br />
<br />
<pre class="brush: java">class Box {
double length;
double width;
double height;
//Define constructor with parameter
Box(double l, double w, double h) {
length = l;
width = w;
height = h;
}
double calculateVolume() {
return (length * width * height);
}
}
class DemoConstructor2 {
public static void main(String[] args) {
Box b1, b2;
b1 = new Box(4, 3, 2);
b2 = new Box(6, 5, 4);
System.out.println("Volume b1 = " + b1.calculateVolume());
System.out.println("Volume b2 = " + b2.calculateVolume());
}
}
</pre>
<br />
This time, we created constructor that using parameter. This causes the initial value can be dynamic depending on the value that are passed during the creation of the object. Consider the following code:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">b1 = new Box (4, 3, 2);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">b2 = new Box (6, 5, 4);</span><br />
<br />
The first line shows that we do initialization <span style="font-family: "courier new" , "courier" , monospace;">length</span> with value 4, <span style="font-family: "courier new" , "courier" , monospace;">width</span> with value 3, and <span style="font-family: "courier new" , "courier" , monospace;">height</span> with value 2. The second line we initialize the data <span style="font-family: "courier new" , "courier" , monospace;">length</span>, <span style="font-family: "courier new" , "courier" , monospace;">width</span>, and <span style="font-family: "courier new" , "courier" , monospace;">height</span> of each with 6, 5 and 4.
<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com31tag:blogger.com,1999:blog-8283254582726951902.post-69332075850629150262016-06-15T20:51:00.002-07:002016-06-17T23:58:33.287-07:00MethodAs explained in previous post, the class contains a collection of data and method. Here is the general form of writing method in the class.<br />
<br />
<em>type methodName(list-of-parameter) {</em><br />
<em> //code goes here</em><br />
<em>}</em><br />
<br />
In the common form above, type is the data type that will be returned by the method. In Java, method is divided into two types: void and non-void. Method void is a method that return a value, and method non-void is a method that return a value. For a void method, returned data type should be written with <span style="font-family: "courier new" , "courier" , monospace;">void </span>keyword.<br />
<br />
If the method that we create is intended to return a value, then <span style="font-family: "courier new" , "courier" , monospace;">return</span> statement must be included in the method, followed by the value to be returned, as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">return value;</span><br />
<br />
value can be either constant or variable.<br />
<br />
<br />
<h3>
Defining Method</h3>
<br />
Here we will define a method into a class that we previously created, the <span style="font-family: "courier new" , "courier" , monospace;">Box</span> class. We'll add a method <span style="font-family: "courier new" , "courier" , monospace;">printVolume()</span> to print the value of the volume of the box object. This method is a method that does not return a value. Therefore, we must change the type of return with the <span style="font-family: "courier new" , "courier" , monospace;">void </span>keyword. Here is the program code:<br />
<br />
<pre class="brush: java">class Box {
double length:
double width;
double heigth;
//Define void method (does not return value)
void printVolume() {
System.out.println("Box volume = " + (length * width * heigth));
}
}
class DemoMethod1 {
public static void main(String[] args) {
Box b1, b2, b3;
b1 = new Box();
b2 = new Box();
b3 = new Box();
//load data to object b1
b1.length = 4;
b1.width = 3;
b1.heigth = 2;
//load data to object b2
b2.length = 6;
b2.width = 5;
b2.heigth = 4;
//load data to object b3
b3.length = 8;
b3.width = 7;
b3.heigth = 6;
//call printVolume() method for each object
b1.printVolume();
b2.printVolume();
b3.printVolume();
}
}
</pre>
<br />
The result given by the above program as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Box volume = 24.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Box volume = 120.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Box volume = 336.0</span><br />
<br />
Method call in a class is done by writing the object name and followed by a dot (.) and the method name to be executed. <br />
<br />
<br />
<h3>
Return a Value in Method</h3>
<br />
In the section above you've seen how to define a method does not return a value. Now, we will continue to defining method that return a value. Here, we will modify the previous program by changing the method <span style="font-family: "courier new" , "courier" , monospace;">printVolume()</span> into method <span style="font-family: "courier new" , "courier" , monospace;">calculateVolume()</span>, that will return a value of <span style="font-family: "courier new" , "courier" , monospace;">double</span> type. Here is the program code:<br />
<br />
<pre class="brush: java">class Box {
double length:
double width;
double height;
//Define method that return double type
double calculateVolume() {
//calculate volume
double vol = length * width * height;
//return value
return vol;
}
}
class DemoMethod2 {
public static void main(String[] args) {
Box b1, b2, k 3;
b1 = new Box();
b2 = new Box();
b3 = new Box();
b1.length = 4;
b1.width = 3;
b1.height = 2;
b2.length = 6;
b2.width = 5;
b2.height = 4;
b3.length = 8;
b3.width = 7;
b3.height = 6;
System.out.println("Volume b1 = " + b1.calculateVolume());
System.out.println("Volume b2 = " + b2.calculateVolume());
System.out.println("Volume b3 = " + b3.calculateVolume());
}
}
</pre>
<br />
In the program above, method <span style="font-family: "courier new" , "courier" , monospace;">calculateVolume()</span> returns a value of <span style="font-family: "courier new" , "courier" , monospace;">double</span> type. The result of above program as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume b1 = 24.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume b2 = 120.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume b3 = 336.0</span><br />
<br />
The data type that returned by the method must be compatible to the type that defined in method. For example, the method that defined return boolean value can not return an integer value. If you want to store the value returned by method into a variable, then the data type of the variable must be compatible to the type that defined in the method. For example, if you want to store the value returned by the method <span style="font-family: "courier new" , "courier" , monospace;">calculateVolume()</span> to <span style="font-family: "courier new" , "courier" , monospace;">vol</span> variable, then the <span style="font-family: "courier new" , "courier" , monospace;">vol</span> variable must be <span style="font-family: "courier new" , "courier" , monospace;">double</span> type (according to the returned type that in <span style="font-family: "courier new" , "courier" , monospace;">calculateVolume()</span> method.
<br />
<br />
<br />
<h3>
Parameterized Method
</h3>
<br />
Method can has one or more parameters. Method can return the value that varies according to the value of parameter. Take a look following without parameter method:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int rectangleArea() {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return 10 * 5;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<br />
syntactically, the above method is correct and will return value 50. However, the above method is not flexible, because the method can only be used for the rectangle which has size 10 x 5 only. What if there is a rectangle whose length is 4 and width is 3? To solve such as this case, Java allows us to put the parameter in a method so that the method can be used for any size of rectangle. Here is the code that should be written:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int rectangleArea(int length, int width) {
</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return length * width:</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<br />
Through this way, we can fill any value into the method. For example, consider the following code:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int area1, area2, area3;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;">area1 = rectangleArea(10, 5); //The result is 50</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">area2 = rectangleArea(4, 3); //The result is 12</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">area3 = rectangleArea(5, 2); //The result is 10</span><br />
<br />
There are two terms you need to know in working with the method: parameter and argument. A parameter is a variable that is defined in the method creation, while the argument is the value used when calling the method. Consider again the following method definition:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int rectangleArea(int length, int width) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return length * width;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<br />
Here, the <span style="font-family: "courier new" , "courier" , monospace;">length</span> and <span style="font-family: "courier new" , "courier" , monospace;">width</span> variables called parameter.<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">area1 = rectangleArea(10, 5);</span><br />
<br />
As in the above statement, the value of 10 and 5 are called argument. Now, we are going to implement the above concepts into <span style="font-family: "courier new" , "courier" , monospace;">Box</span> class. Here, the <span style="font-family: "courier new" , "courier" , monospace;">length</span>, <span style="font-family: "courier new" , "courier" , monospace;">width</span>, and <span style="font-family: "courier new" , "courier" , monospace;">height</span> data will be filled by a method. Here is the program code:<br />
<br />
<pre class="brush: java">class Box {
double length;
double width;
double height;
//Define method that using parameter
void fillData (double l, double w, double h) {
length = l;
width = w;
height = h;
}
double calculateVolume() {
return (length * width * height)
}
}
class DemoMethod3 {
public static void main(String[] args) {
Box b = new Box();
//call fillData() method
b.fillData(4, 3, 2);
System.out.println("Box volume = " + b.calculateVolume());
}
}
</pre>
<br />
The results will be given by program above as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Box volume = 24.0</span><br />
<br />
In the above program we define a method that does not return a value and has three parameters: <span style="font-family: "courier new" , "courier" , monospace;">l</span>, <span style="font-family: "courier new" , "courier" , monospace;">w</span>, and <span style="font-family: "courier new" , "courier" , monospace;">h</span>. These parameters then can be filled with the desired value during the process of the method call. In the above example, we pass value of 4, 3 and 2 as the argument of the method <span style="font-family: "courier new" , "courier" , monospace;">fillData()</span> to fill <span style="font-family: "courier new" , "courier" , monospace;">length</span>, <span style="font-family: "courier new" , "courier" , monospace;">width</span>, and <span style="font-family: "courier new" , "courier" , monospace;">height</span> data of the <span style="font-family: "courier new" , "courier" , monospace;">b</span> object.<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com3tag:blogger.com,1999:blog-8283254582726951902.post-68749024092902092902016-06-15T00:09:00.001-07:002016-06-17T20:27:34.479-07:00Class and ObjectClass is the core of Java programming. Why? Because Java is a programming language that support and implement full object-oriented concept. Each Java program is a class. This means, every concept or case of programming that you want to be implemented in Java, it should be wrapped into a class.<br />
<br />
<h3>
What Is Class?</h3>
<br />
So far, actually we have been using class in every programming we wrote. However, classes we already use is still very minimal, which is only intended to demonstrate the syntax in Java programming. The class is a class that acts as main class (main program), which contains <span style="font-family: "courier new" , "courier" , monospace;">main()</span> method.<br />
<br />
Classes can be defined as a blueprint that defines variables (data) and methods (behavior) of an object. Take a look of example object: car. Car has data such as brand, type, color, and so on. Car also has a specific behaviors that can differentiate between one and the other cars, such as the braking system, gear, and so forth. Now let us take the example of other object: human. Human has data or physical characteristics, such as: name, height, weight, fingerprint, hair color, and so on. In addition, human also has behaviors such as: how to walk, how to speak, and so forth. In the program, such objects can be defined as a class.<br />
<br />
In programming, class actually not much different from the simple data type explained in previous post. The difference, simple data type used to declare normal variable, while the class is used to declare a variable to be an object.<br />
<br />
It is important to note that the class is still abstract. By the time we create a new class, meaning we have defined a new data type. Once defined, the new data type can be used to create an object of that type. In other words, the class is a template for object creation, and the object is a concrete manifestation (instance) of a class. For example, human is class; whereas the object instance or a concrete manifestation of the human class is Larry, Patrick, Lisa, and others.<br />
<br />
<h3>
Defining Class</h3>
<br />
In Java, a class defined using keyword <span style="font-family: "courier new" , "courier" , monospace;">class</span>. Here is the general form used to define a class:<br />
<br />
<em>class ClassName {</em><br />
<em> type data1;</em><br />
<em> </em><em>type data2;</em><br />
<em> </em><em>...</em><br />
<em> </em><em>type dataN;</em><br />
<em><br /></em>
<em> </em><em>type method1(parameter) {</em><br />
<em> </em><em> </em><em>// code for method1</em><br />
<em> </em><em>}</em><br />
<em><br /></em>
<em> </em><em>type method2(parameter) {</em><br />
<em> </em><em> </em><em>// code for method2</em><br />
<em> </em><em>}</em><br />
<em><br /></em>
<em> </em><em>type methodN(parameter) {</em><br />
<em> </em><em> </em><em>// code for methodN</em><br />
<em> </em><em>}</em><br />
<em>}</em><br />
<em><br /></em>Data or variable defined within a class are often referred to the instance variables. The value of these data will be accessible via method.<br />
<br />
<h3>
Simple Class Example</h3>
<br />
Now we will begin creating a simple class. Here we will create a class <span style="font-family: "courier new" , "courier" , monospace;">Box</span>, which has data: <span style="font-family: "courier new" , "courier" , monospace;">length</span>, <span style="font-family: "courier new" , "courier" , monospace;">width</span>, <span style="font-family: "courier new" , "courier" , monospace;">and</span> <span style="font-family: "courier new" , "courier" , monospace;">height</span>. For now, we do not need to add a method to the class. Method will be explained in the next post.<br />
<br />
Consider the following example:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">class Box {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> double length;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> double width;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> double height;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span>
<br />
<br />
From the code above, we have defined a new data type named <span style="font-family: "courier new" , "courier" , monospace;">Box</span>. It is important to remember that the class definition will only make a template, instead of creating object. The actual object of the class must be made through the following code:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Box b = new Box(); //Create the object of Box class with the name of b</span><br />
<br />
Here, <span style="font-family: "courier new" , "courier" , monospace;">Box</span> is a class and<span style="font-family: "courier new" , "courier" , monospace;"> b</span> is the object or instance of the class <span style="font-family: "courier new" , "courier" , monospace;">Box</span>. Through the object <span style="font-family: "courier new" , "courier" , monospace;">b</span>, we can access and manipulate the data contained in <span style="font-family: "courier new" , "courier" , monospace;">Box</span> class, by using the dot operator (.), As shown in the code below:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">b.length = 4;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">b.width = 3;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">b.height = 2;</span><br />
<br />
The code is used to fill value to the <span style="font-family: "courier new" , "courier" , monospace;">length</span>, <span style="font-family: "courier new" , "courier" , monospace;">width</span>, and <span style="font-family: "courier new" , "courier" , monospace;">height</span>, which is owned by the object<span style="font-family: "courier new" , "courier" , monospace;"> b</span> with value 4, 3 and 2. For more details, Take a look at the following full program which will show the use of the class <span style="font-family: "courier new" , "courier" , monospace;">Box</span> above.<br />
<br />
<pre class="brush: java">class Box {
double length;
double width;
double height;
}
class BoxDemo1 {
public static void main(String[] args) {
double volume;
Box b = new Box();
//Load value into the data of b object
b.length = 4;
b.width = 3;
b.height = 2;
//Calculate b volume
volume = b.length * b.width * b.height;
//Display the volume to the screen
System.out.println("box volume = " + volume);
}
}
</pre>
<br />
The above source code should be saved into a file with the name <strong>BoxDemo1.java</strong>, not <strong>Box.java</strong>. The reason is the <span style="font-family: "courier new" , "courier" , monospace;">main()</span> method resides in <span style="font-family: "courier new" , "courier" , monospace;">BoxDemo1</span> class. At compilation time, the above program will produce two .class files: <strong>Box.class</strong> and <strong>BoxDemo1.class</strong>. This happens because in Java, every class will be compiled into its own .class file. If you want to run the program, then you have to execute the file <strong>BoxDemo1.class</strong>, not <strong>Box.class</strong>. Here is the result that will be provided by the program.<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">box volume = 24.0</span><br />
<br />
The program above also can be written in two classes. Every class is written in a single .java file. Thus, in the above case we can make two files .java: <strong>Box.java</strong> and <strong>BoxDemo1.</strong>java. The source code should you write to in the two files are as follows:<br />
<br />
a. The content of <strong>Box.java </strong>file <strong></strong><br />
<pre class="brush: java">class Box {
double length;
double width;
double height;
}
</pre>
<br />
b. The content of <strong>BoxDemo1.java</strong> file <strong></strong><br />
<pre class="brush: java">class BoxDemo1 {
public static void main(String[] args) {
double volume;
Box b = new Box();
//Load value into the data of b object
b.length = 4;
b.width = 3;
b.height = 2;
//Calculate b volume
volume = b.length * b.width * b.height;
//Display the volume to the screen
System.out.println("box volume = " + volume);
}
}
</pre>
<br />
Please be noted that any object or instance of a class will have its own copy of data. This means, an object with another object, the value of the data can be different. Take a look at the code example below:<br />
<br />
<pre class="brush: java">class Box {
double length;
double width;
double height;
}
class BoxDemo2 {
public static void main(String[] args) {
double volume1, volume2;
Box b1 = new Box(); //declaring object b1
Box b2 = new Box(); //declaring object b2
//Load value into b1 object
b1.length = 4;
b1.width = 3;
b1.height = 2;
//Load value into b2 object
b2.length = 6;
b2.width = 5;
b2.height = 4;
//Calculate volume of b1 object
volume1 = b1.length * b1.width * b1.height ;
//Calculate volume of b2 object
volume2 = b2.length * b2.width * b1.height ;
//Display the volume of b1 and b2 to the screen
System.out.println("Volume b1 = " + volume1);
System.out.println("Volume b2 = " + volume2);
}
}
</pre>
<br />
Save the above source code into the file <strong>BoxDemo2.java</strong>. At run time, the above program will give the following result:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume b1 = 24.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Volume b2 = 120.0</span><br />
<br />
As shown in the above code that object <span style="font-family: "courier new" , "courier" , monospace;">b1</span> and <span style="font-family: "courier new" , "courier" , monospace;">b2</span> is an instance or a concrete manifestation of the class <span style="font-family: "courier new" , "courier" , monospace;">Box</span>. The value of the data contained on the two objects can be different so that the value of the produced volumes would be different.<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com3tag:blogger.com,1999:blog-8283254582726951902.post-79905883917099882472016-06-14T20:58:00.000-07:002016-06-14T21:33:55.217-07:00Jump StatementJump statement is used to control program flow. More precisely, to move the program execution to the line of code that we want. In Java, there are three jump statements: <span style="font-family: "courier new" , "courier" , monospace;">break</span>, <span style="font-family: "courier new" , "courier" , monospace;">continue</span>, and <span style="font-family: "courier new" , "courier" , monospace;">return</span>.<br />
<br />
<h3>
break</h3>
<br />
By using <span style="font-family: "courier new" , "courier" , monospace;">break</span> statement, we can stop certain repetition process regardless defined condition or the rest of the statement contained in the loop block. This means that, when we put the <span style="font-family: "courier new" , "courier" , monospace;">break</span> statement at loop block, the repetition will be immediately terminated and program execution will continue to the section of code below the corresponding loop structure, for example, take a look at the following program:.<br />
<br />
<pre class="brush: java">class BreakDemo {
public static void main(String[] args) {
for (int i=0; i<10; i++) {
if (i == 5) {
break; //exit from loop block
}
System.out.println("row-" + i);
}
System.out.println("Statement after loop block");
}
}
</pre>
<br />
<span style="font-family: inherit;">The program above will give the following result:</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;">row-0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">row-1</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">row-2</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">row-3</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">row-4</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Statement after loop block</span><br />
<br />
As shown in the above result, the program will stop the repetition when the variable <span style="font-family: "courier new" , "courier" , monospace;">i</span> is worth 5, regardless to consider the condition <span style="font-family: "courier new" , "courier" , monospace;">(i < 10)</span> as defined in the loop structure. The rest of statement contained in the loop block will not be executed again. After dialing <span style="font-family: "courier new" , "courier" , monospace;">break</span> statement, the program will exit from loop block. The above provision also apply to the other loop structure: <span style="font-family: "courier new" , "courier" , monospace;">while</span> and <span style="font-family: "courier new" , "courier" , monospace;">do-while</span>.<br />
<br />
<h3>
continue</h3>
<br />
Like another programming language, Java provides <span style="font-family: "courier new" , "courier" , monospace;">continue</span> statement that used to force the program to continue of loop process. <span style="font-family: "courier new" , "courier" , monospace;">continue</span> statement can be regarded as the opposite of the <span style="font-family: "courier new" , "courier" , monospace;">break</span> statement.<br />
<br />
<h3>
return</h3>
<br />
In Java, <span style="font-family: "courier new" , "courier" , monospace;">return</span> statement is used to exit from code execution that contained in a method. Method will be explained further in the next post.<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com2tag:blogger.com,1999:blog-8283254582726951902.post-74250813652963981612016-06-13T21:17:00.001-07:002016-06-14T19:41:24.105-07:00LoopingLooping is a process in a program where the execution of statement is repeated until
the condition for stop found. In Java, there are three types of loop structures: <span style="font-family: "courier new" , "courier" , monospace;">for</span>, <span style="font-family: "courier new" , "courier" , monospace;">while</span>,
and <span style="font-family: "courier new" , "courier" , monospace;">do-while</span>.
<br />
<br /><br />
<h3>
<span style="font-family: "courier new" , "courier" , monospace;">for</span> Structure</h3>
<br />
<span style="font-family: "courier new" , "courier" , monospace;">for</span> structure generally used to perform repetition that its number is definitely
or previously known. In this type, we have to define the initialization and the
conditions for exit from loop. In addition, we also need to add
iteration, the variable controller to make increment or
decrement process. Here is a common form of writing the syntax for building for structure:<br />
<br />
<em>for (initialization; condition; iteration) {</em><br />
<em> //Statement that will be repeated</em><br />
<em>}</em><br />
<br />
A loop process will continue as long the condition returns <span style="font-family: "courier new" , "courier" , monospace;">true</span>. The loop process can only be terminated if the condition
is <span style="font-family: "courier new" , "courier" , monospace;">false</span> or has not met again. The complete work of loop structure is as the following: when the program entering loop structure, program will perform initialization process. In general,
this is an expression that will set the initial value of the loop index, which
acts as a counter of the number of repetition to be performed. It
should be noted that in initialization process, this will only be done once.
Furthermore, the program will check the condition defined. The condition must
be an expression that produces a boolean value.
If the condition is <span style="font-family: "courier new" , "courier" , monospace;">true</span>, then
the statement contained in the loop block will be executed. Conversely, if
<span style="font-family: "courier new" , "courier" , monospace;">false</span>, then the loop will be discontinued. Lastly, the program will run
iteration part, which is usually increment or decrement process of loop
index<br />
<br />
Please look at the following simple program:<br />
<br />
<pre class="brush: java">class DemoFor1 {
public static void main(String[] args) {
for (int i=0; i<10, i++) {
System.out.println("Java");
}
}
}
</pre>
<br />
When executed, the program will print text "Java" 10 times, from index-0 to 9, as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<br />
In loop structure above, we use increment for the iteration process. However, we also can use decrement to produce the same output, as follows:<br />
<br />
<pre class="brush: java">class DemoFor2 {
public static void main(String[] args) {
for (int i=10; i>0, i--) {
System.out.println("Java");
}
}
}
</pre>
<br />
This time, the program will print text "Java" 10 times, but the index starts from 10 to 1 (downhill).
<br />
<br /><br />
<h3>
<span style="font-family: "courier new" , "courier" , monospace;">while</span> Structure</h3>
<br />
<span style="font-family: "courier new" , "courier" , monospace;">while</span> structure is a kind of loop that defines the condition in the
beginning of the block. This means that, if the conditions are not met (<span style="font-family: "courier new" , "courier" , monospace;">false</span>)
then loop process will never do. <br />
<br />
Value initialization process on the
<span style="font-family: "courier new" , "courier" , monospace;">while</span> structure is written before we write <span style="font-family: "courier new" , "courier" , monospace;">while</span> structure
itself, the iteration will be written in the block. Here is the general form of
<span style="font-family: "courier new" , "courier" , monospace;">while</span> structure in Java.<br />
<br />
<em>initialization</em><br />
<em>while (condition) {</em><br />
<em> // Statements that will be repeated</em><br />
<em> ...</em><br />
<em> iteration</em><br />
<em>}</em><br />
<br />
For example, to display the text "Java" 10 times to the screen, we
can write code as follows:
<br />
<br />
<pre class="brush: java">class DemoWhile {
public static void main(String[] args) {
int i=0;
while (i<10) {
System.out.println("Java");
i++;
}
}
}
</pre>
<br />
The above program will give the following result:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Java</span><br />
<br /><br />
<h3>
<span style="font-family: "courier new" , "courier" , monospace;">do-while</span> Structure
</h3>
<br />
<span style="font-family: "courier new" , "courier" , monospace;">do-while</span> structure actually similar with <span style="font-family: "courier new" , "courier" , monospace;">while</span> structure. The
difference is only in the condition placement. In the <span style="font-family: "courier new" , "courier" , monospace;">while</span> structure, the
condition placed on the initial loop block, and in the
<span style="font-family: "courier new" , "courier" , monospace;">do-while</span> structure, the condition is at the end of the block. Consequently, for <span style="font-family: "courier new" , "courier" , monospace;">do-while</span>
structure, process repeatability will be performed at least once despite the
fact the condition is not met (<span style="font-family: "courier new" , "courier" , monospace;">false</span>). Here is a common form of writing
do-while structure.
<br />
<br />
<em>initialization</em><br />
<em>do {</em><br />
<em> // Statements that will be repeated</em><br />
<em> ...</em><br />
<em> iteration</em><br />
<em>} while (condition);</em><br />
<br />
Please look at the following program: <br />
<br />
<pre class="brush: java">class DoWhileExample {
public static void main(string[] args) {
int i=6;
do {
System.out.println("I am learning Java”);
i++;
} while (i < 5);
}
}
</pre>
<br />
This time, although the value of i is greater than 5, which means the condition
is <span style="font-family: "courier new" , "courier" , monospace;">false</span>, but the program will continue to execute the statement inside the loop
block 1 times. How it works as follows: At first, the program will
set the variable<span style="font-family: "courier new" , "courier" , monospace;"> i</span> with a value of 6. Next, the program will enter into the
loop block and print text "I am learning Java" to the screen as
much as 1 times as well as raise the value of <span style="font-family: "courier new" , "courier" , monospace;">i</span> becomes 7. After that, the
program will check the conditions defined. Because 7 is greater than 5, then
the condition <span style="font-family: "courier new" , "courier" , monospace;">(i < 5)</span> is false, and this will stop repetition process.
Here is the result that will be given:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">I am learning Java</span>
<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com2tag:blogger.com,1999:blog-8283254582726951902.post-39174402940693161342016-06-12T19:53:00.001-07:002016-06-14T19:38:24.248-07:00Decision MakingJava provides two statements for decision making: <span style="font-family: "courier new" , "courier" , monospace;">if</span> and <span style="font-family: "courier new" , "courier" , monospace;">switch</span>. Both are used to control the statement execution depending on condition determined before.<br />
<br />
<br />
<h2>
<span style="font-family: "courier new" , "courier" , monospace;">if</span> Statement</h2>
<br />
<span style="font-family: "courier new" , "courier" , monospace;">if</span> statement can be used to handle the selection of branching which is based on one, two, or more than two conditions. Here is an explanation of <span style="font-family: "courier new" , "courier" , monospace;">if</span> statement for each condition.<br />
<br />
<br />
<br />
<h3>
One Condition</h3>
<br />
The structure is the simplest because it only involves one condition. A common form of writing if statement with one condition as follows:<br />
<br />
<em>//If only consists of one statement</em><br />
<em>if(condition) statement</em><br />
<br />
<em>//If consists of two or more statements</em><br />
<em>if(condition) {</em><br />
<em> statemen1;</em><br />
<em> statemen2;</em><br />
<em> ...</em><br />
<em>}</em><br />
<br />
Program below will demonstrate the use of <span style="font-family: "courier new" , "courier" , monospace;">if</span> for one condition:<br />
<br />
<pre class="brush: java">class OneConditionIfDemo {
public static void main(String[] args) {
int a=1, b=10:
if (a < 5) {
System.out.println("Value of a is less than 5”);
}
if (b < 5) {
System.out.println("Value of b is less than 5”);
}
}
}
</pre>
<br />
The result will be given by the above program as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Value of a is less than 5</span><br />
<br />
From these results, it appears that only a program executed by the statement contained in the first <span style="font-family: "courier new" , "courier" , monospace;">if</span> block. This is because the expression <span style="font-family: "courier new" , "courier" , monospace;">(a < 5)</span> is <span style="font-family: "courier new" , "courier" , monospace;">true</span> or fulfilled. In contrast, the second <span style="font-family: "courier new" , "courier" , monospace;">if</span> block, expression <span style="font-family: "courier new" , "courier" , monospace;">(b < 5)</span> is <span style="font-family: "courier new" , "courier" , monospace;">false</span> so that the statement contained in the block are never executed by the program. Now, we take a second example, the condition will check the value of a character. Here is a code that can be written:<br />
<br />
<pre class="brush: java">class OneConditionIfDemo2 {
public static void main(String[] args) {
char ch = 'E';
if (ch == 'a' || ch == 'A’ ||
ch == 'i' || ch == 'I' ||
ch == 'u' || ch == 'U' ||
ch == 'e' || ch == 'E' ||
ch == 'o' || ch == 'O') {
System.out.println(ch + " is vowel.");
}
}
}
</pre>
<br />
When executed, the above program will give the following results:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">E is vowel.</span><br />
<span style="font-family: "courier new";"><br /></span><br />
<br />
<h3>
Two Conditions</h3>
<br />
The structure of this type is more complex compared with the first type above. In this form, the program provides an additional condition to handle events that are not met or the first is <span style="font-family: "courier new" , "courier" , monospace;">false</span>. The general form of the electoral structure of this type as follows:<br />
<br />
<em>// If only consists of one statement</em><br />
<em>if (condition)</em><br />
<em> statement if the condition is true</em><br />
<em>else</em><br />
<em> statement if the condition is false</em><br />
<em><br /></em>
<em>// If the statement consists of two or more</em><br />
<em>if (condition) {</em><br />
<em> // Statements to be excuted if the condition is true</em><br />
<em> statemen1;</em><br />
<em> statemen2,</em><br />
<em> ...</em><br />
<em>} else {</em><br />
<em> // Statements to be </em><em>excuted</em> <em>if the condition is false</em><br />
<em> statemen1;</em><br />
<em> statemen2;</em><br />
<em> ...</em><br />
<em>}</em><br />
<br />
Here is a sample program that demonstrates the use of statement using two conditions.<br />
<br />
<pre class="brush: java">class TwoConditionIfDemo1 {
public static void main(String[] args) {
int a=1, b=10
if (a < 5) {
System.out.println(a + " is less than 5");
} else { //(a >= 5)
System.out.println(a + " is greater than 5");
}
if (b < 5) {
System.out.println(b + " is less than 5");
} else { //(b >= 5)
System.out.println(b + " is greater than 5");
}
}
}
</pre>
<br />
When executed, the above program will give the following results:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">1 is less than 5</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">10 is greater than 5</span><br />
<br />
It appears from the above results that at first <span style="font-family: "courier new" , "courier" , monospace;">if</span> block, condition <span style="font-family: "courier new" , "courier" , monospace;">(a < 5)</span> is <span style="font-family: "courier new" , "courier" , monospace;">true</span>, then the statement contained in these conditions will be directly executed. The statement contained in the <span style="font-family: "courier new" , "courier" , monospace;">else</span> part will be executed if the condition is <span style="font-family: "arial" , "helvetica" , sans-serif;">false</span> or not met. The second <span style="font-family: "courier new" , "courier" , monospace;">if</span> condition <span style="font-family: "courier new" , "courier" , monospace;">(b < 5)</span> is not met. the statement that will be executed is the statement contained in the <span style="font-family: "courier new" , "courier" , monospace;">else </span>block. As another example, here we will modify the program in the previous section. In this program, we can determine whether we enter the letter which is vowel or consonant. Here is the code:<br />
<br />
<pre class="brush: java">class TwoConditionIfDemo2 {
public static void main(String[] args) {
char ch = 'B';
if (ch == 'a' || ch == 'A' ||
ch == 'i' || ch == 'I' ||
ch == 'u' || ch == 'U' ||
ch == 'e' || ch == 'E' ||
ch == 'o' || ch == 'O') {
System.out.println(ch + " is vowel.");
} else {
System.out.println(ch + " is consonant.");
}
}
}
</pre>
<br />
When executed, the above program will give the following results:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">B is a consonant.</span><br />
<br />
In the <span style="font-family: "courier new" , "courier" , monospace;">if</span> block above, the condition is false so the program execute the else block.<br />
<br />
<br />
<br />
<h3>
Three or More Conditions</h3>
<br />
The selection structure of this type is an extension of previous types. Here, <span style="font-family: "courier new" , "courier" , monospace;">if</span> statement has more than two conditions. The general form of writing as follows:<br />
<br />
<em>if (condition1)</em><br />
<em> statement if the condition1 is true</em><br />
<em>else if (condition2)</em><br />
<em> </em><em>statement if the condition1 is false</em><br />
<em>else</em><br />
<em> statement if condition1 and condition2 is false</em><br />
<br />
Here is an example of a program that can show the workings of the electoral structure if that had three cases.<br />
<br />
<pre class="brush: java">class ThreeConditionIfDemo1 {
public static void main(String[] args) {
int number = 4;
if (number < 0) {
System.out.println(number + " is NEGATIF number.");
} else if (number == 0) {
System.out.println("Value entered is ZERO”);
} else { //(number > 0)
System.out.println(number + " is POSITIVE number.”);
}
}
}
</pre>
<br />
When executed, the above program will give the following result:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">4 is POSITIVE number.</span><br />
<br />
Initially, the program will check whether a given number (in this case <span style="font-family: "courier new" , "courier" , monospace;">number = 4</span>) is less than zero or not. Because the expression<span style="font-family: "courier new" , "courier" , monospace;"> (4 < 0)</span> is <span style="font-family: "courier new" , "courier" , monospace;">false</span>, then the program will check the next else condition<span style="font-family: "courier new" , "courier" , monospace;"> (4 == 0)</span>, and will not execute the statement, because the result is also <span style="font-family: "arial" , "helvetica" , sans-serif;">false</span>, then the program will execute the statement contained in the last <span style="font-family: "courier new" , "courier" , monospace;">else</span>.<br />
<br />
Now, we will make an example of another program which contains more than three conditions. Here, we will take the case which is the conversion of a number value to the index value (A, B, C, D, or E). For example, here we already have formula as follows:<br />
<br />
value = (60% x last exam result) + (40% x mid exam result)<br />
<br />
value >= 80: A<br />
value < 80 and >= 70: B<br />
value < 70 and >= 50: C<br />
value < 50 and >= 30: D<br />
value < 30: E<br />
<br />
By using these conditions, then we can write the code as follows:<br />
<br />
<pre class="brush: java">class Demo ThreeConditionIfDemo2 {
public static void main(string[] args) {
char indexValue;
double midExamResult, lastExamResult, finalScore;
midExamResult = 75.0;
lastExamResult = 60.0;
//Calculate final score using formula above
finalScore = (0.4 * midExamResult) + (0.6 * lastExamResult);
if (finalScore >= 80) {
indexValue = 'A';
} else if (finalScore >= 70) {
indexValue = 'B';
} else if (finalScore >= 50) {
indexValue = 'C';
} else if (finalScore >= 30) {
indexValue = 'D';
} else { //(finalScore < 30)
indexValue = 'E';
}
System.out.println("Final score: " + finalScore);
System.out.println("Index value: " + indexValue);
}
}
</pre>
<br />
When executed, the program will provide the following result:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Final score: 66.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Index value: C</span><br />
<span style="font-family: "courier new";"><br /></span><br />
<br />
<h2>
<span style="font-family: "courier new" , "courier" , monospace;">switch</span> Statement</h2>
<br />
The <span style="font-family: "courier new" , "courier" , monospace;">switch</span> statement is an alternative to an decision making statement. This statement is usually used to simplify the complexity of the <span style="font-family: "courier new" , "courier" , monospace;">if</span> statement that contain many conditions. Here is a common form of writing the syntax for the <span style="font-family: "courier new" , "courier" , monospace;">switch</span> statement:<br />
<br />
<em>switch (expression) {</em><br />
<em> case value1:</em><br />
<em> // Statement will be executed when expression is same with value1</em><br />
<em> break;</em><br />
<em> case value2:</em><br />
<em> // Statement will be </em><em>executed</em> <em>when </em><em>expression is same</em> <em>with </em><em>value2</em><br />
<em> break;</em><br />
<em> ...</em><br />
<em> case </em><em>valueN :</em><br />
<em> // Statement will be </em><em>executed</em> <em>when expression is same with </em><em>valueN</em><br />
<em> break;</em><br />
<em> default:</em><br />
<em> // Statement will be </em><em>executed</em> <em>when all </em><em>defined </em><em>values are not same with the expression</em><br />
<br />
Here is an example of a program that will show the use of the <span style="font-family: "courier new" , "courier" , monospace;">switch</span> statement in Java:<br />
<br />
<pre class="brush: java">class SwitchDemo1 {
public static void main(String[] args) {
int numberOfDay = 4;
switch (numberOfDay) {
case 1:
System.out println("Day " + numberOfDay + " is Sunday");
break;
case 2:
System.out println("Day " + numberOfDay + " is Monday");
break;
case 3:
System.out println("Day " + numberOfDay + " is Tuesday");
break;
case 4:
System.out println("Day " + numberOfDay + " is Wednesday");
break;
case 5:
System.out println("Day " + numberOfDay + " is Thursday");
break;
case 6:
System.out println("Day " + numberOfDay + " is Friday");
break;
case 7:
System.out println("Day " + numberOfDay + " is Saturday");
break;
default:
System.out.println("There is no day of " + numberOfDay);
}
}
}
</pre>
<br />
The result will be given by the above program as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Day 4 is Wednesday</span><br />
<br />
As shown in the above result that the statement to be executed is a statement that is at a value which is equal to the expression defined. If all defined constant value not in accordance with the expression, then the progam will execute the statement contained in <span style="font-family: "courier new" , "courier" , monospace;">default</span> section.<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com2tag:blogger.com,1999:blog-8283254582726951902.post-14419595662071786112016-06-12T19:46:00.003-07:002016-06-12T19:49:05.217-07:00Control StatementControl statement is used to control program flow in accordance with what we want. These statements are grouped into three types: decision making, looping, and jump statement. Decision making is used to determine which statement will be executed depending on the expression or defined condition. Looping is used to repeat the execution of statement in accordance with the condition specified. And jump statement is used to move the process execution to the code that we want.
<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com2tag:blogger.com,1999:blog-8283254582726951902.post-36687627002892837262016-06-11T18:43:00.003-07:002016-06-11T18:46:33.863-07:00Use of Parenthesis in ExpressionAt the time of writing down the expression, use parentheses in an expression you want to execute first. For example,
consider the following example of expression:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int a = 3, b = 4;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">int result = a + b * 2</span><br />
<br />
This expression will initially multiplying <span style="font-family: "Courier New", Courier, monospace;">b</span> with 2, then add to the
value of <span style="font-family: "Courier New", Courier, monospace;">a</span>. This is because the operator * has a higher priority than the +
operator. Through this kind of writing, the above expression would return the
value 11. However, if you want the sum of <span style="font-family: "Courier New", Courier, monospace;">a</span> and <span style="font-family: "Courier New", Courier, monospace;">b</span> executed first, and then multiplied by 2,
you should have to use parentheses, as shown below.<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int result = (a + b) * 2</span><br />
<br />
Through this kind of writing, the above expression would yield a value of 14.
<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com1tag:blogger.com,1999:blog-8283254582726951902.post-81989129170874067222016-06-11T18:09:00.001-07:002016-06-11T18:34:29.554-07:00Logical OperatorLogical operator is used
to perform the operation on two operands with boolean type . The result of
this operation also will be boolean type. Here is a table showing
logical operator:<br /><br />
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 325px;">
<colgroup><col style="mso-width-alt: 3693; mso-width-source: userset; width: 76pt;" width="101"></col>
<col style="mso-width-alt: 8192; mso-width-source: userset; width: 168pt;" width="224"></col>
<tbody>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: #daeef3; border-image: none; border: 0.5pt solid windowtext; height: 15pt; width: 76pt;" width="101"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Operator</span></strong></td>
<td style="background-color: #daeef3; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 168pt;" width="224"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Description</span></strong></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>&&</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>AND</span></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>||</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>OR</span></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>^</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>XOR (exclusive OR)</span></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>!</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>NOT (negation)</span></td>
</tr>
</tbody></colgroup></table>
<br />
If there are two boolean operands (for example: A and B) and both are used in AND, OR, XOR, and NOT operation, then the resulting values are as follows:<br />
<br />
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 510px;">
<colgroup><col span="6" style="mso-width-alt: 3108; mso-width-source: userset; width: 64pt;" width="85"></col>
<tbody>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: #daeef3; border-image: none; border: 0.5pt solid windowtext; height: 15pt; width: 64pt;" width="85"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>A</span></strong></td>
<td style="background-color: #daeef3; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 64pt;" width="85"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>B</span></strong></td>
<td style="background-color: #daeef3; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 64pt;" width="85"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>A && B</span></strong></td>
<td style="background-color: #daeef3; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 64pt;" width="85"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>A || B</span></strong></td>
<td style="background-color: #daeef3; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 64pt;" width="85"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>A ^ B</span></strong></td>
<td style="background-color: #daeef3; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 64pt;" width="85"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>!A</span></strong></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>false</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>true</span></td>
</tr>
</tbody></colgroup></table>
<br />
As shown in table above, the AND operation will only return true if both operands (A and B) are true; OR operation will only return
false if the second operand is false; XOR operation will only return
true if either of its operands (not both) are true; while the NOT operation
will return the negation or reversal of a previous value.<br />
<br />
Here is an example of code that will show the values resulting from table above<br />
<br />
<pre class="brush: java">class LogicalOperatorDemo {
public static void main(String[] args) {
System.out.println("AND operation");
System.out.println("true && true = " + (true && true));
System.out.println("true && false = " + (true && false));
System.out.println("false && true = " + (false && true));
System.out.println("false && false = " + (false && false));
System.out.println();
System.out.println("OR operation");
System.out.println("true || true = " + (true || true));
System.out.println("true || false = " + (true || false));
System.out.println("false || true = " + (false || true));
System.out.println("false || false = " + (false || false));
System.out.println();
System.out.println("XOR operation");
System.out.println("true ^ true = " + (true ^ true));
System.out.println("true ^ false = " + (true ^ false));
System.out.println("false ^ true = " + (false ^ true));
System.out.println("false ^ false = " + (false ^ false));
System.out.println();
System.out.println("NOT operation");
System.out.println("!true = " + (!true));
System.out.println("!false = " + (!false));
}
}
</pre>
<br />
<br />
The results will be given by the above program as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">AND operation</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">true && true = true</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">true && false = false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">false && true = false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">false && false = false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;">OR operation</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">true || true = true</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">true || false = false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">false || true = false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">false || false = false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;">XOR operation</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">true ^ true = true</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">true ^ false = false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">false ^ true = false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">false ^ false = false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;">NOT operation</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">!true = false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">!false = true</span>
<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com1tag:blogger.com,1999:blog-8283254582726951902.post-6650446809898945782016-06-10T00:38:00.001-07:002016-06-10T03:26:34.289-07:00Relational OperatorRelational operator is an operator that declare the relation between the operand
with the other operand. There are: equal to, not equal to, greater than, less than, greater than or equal to, less than or equal to. Result is given of an operation that
involves relational operator will be boolean (<span style="font-family: "courier new" , "courier" , monospace;">true</span>/<span style="font-family: "courier new" , "courier" , monospace;">false</span>). The
following table shows list of relational operator in Java:<br />
<br />
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 375px;">
<colgroup><col style="mso-width-alt: 3730; mso-width-source: userset; width: 77pt;" width="102"></col>
<col style="mso-width-alt: 9984; mso-width-source: userset; width: 205pt;" width="273"></col>
<tbody>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: #daeef3; border-image: none; border: 0.5pt solid windowtext; height: 15pt; width: 77pt;" width="102"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Operator</span></strong></td>
<td style="background-color: #daeef3; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 205pt;" width="273"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Description</span></strong></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>==</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Equal to</span></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>!=</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Not equal to</span></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>></span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Greater than</span></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span><</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Less than</span></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>>=</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Greater than or equal to</span></td>
</tr>
<tr height="21" style="height: 15.75pt;">
<td height="21" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15.75pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span><=</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Less than or equal to</span></td>
</tr>
</tbody></colgroup></table>
<br />
<br />
Operator <span style="font-family: "courier new" , "courier" , monospace;">==</span> and <span style="font-family: "courier new" , "courier" , monospace;">!=</span> can be applied to any type, either numeric, character, or
boolean. And the four other operators can only be used for numeric and
char type, to determine whether an operand is greater than or less than the other
operand. Consider the following code example:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int a = 5, b = 10;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">boolean c = a > b;</span><br />
<br />
Here, the value of <span style="font-family: "courier new" , "courier" , monospace;">c</span> would be <span style="font-family: "courier new" , "courier" , monospace;">false</span>, because value of <span style="font-family: "courier new" , "courier" , monospace;">b</span> is greater
than the value of <span style="font-family: "courier new" , "courier" , monospace;">a</span>. In general, the relational operators used in the expression (either on the decision making or looping). Here is an example of code that will demonstrate the use of relational
operator in the program.<br />
<br />
<pre class="brush: java">class RelationalOperatorDemo {
public static void main(String[] args) {
int a=5, b=10;
System.out.println("a == b is " + (a == b));
System.out.println("a != b is " + (a != b));
System.out.println("a > b is " + (a > b));
System.out.println("a < b is " + (a < b));
System.out.println("a >= b is " + (a >= b));
System.out.println("a <= b is " + (a <= b));
}
}
</pre>
<br />
The results will be given by the above program as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">a == b is false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">a != b is true</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">a > b is false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">a < is true</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">a >= b is false</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">a <= b is true</span><br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com3tag:blogger.com,1999:blog-8283254582726951902.post-66077270421063079932016-06-08T23:52:00.001-07:002016-06-10T03:14:11.790-07:00Arithmetic OperatorArithmetic operator is operator that used to perform mathematical calculation such as
addition, subtraction, multiplication, and division. The following table shows
a list of operator that belong to the group of arithmetic operator:
<br />
<strong>
</strong><br />
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 397px;">
<colgroup><col style="mso-width-alt: 3584; mso-width-source: userset; width: 74pt;" width="98"></col>
<col style="mso-width-alt: 10934; mso-width-source: userset; width: 224pt;" width="299"></col>
<tbody>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: #daeef3; border-image: none; border: 0.5pt solid windowtext; height: 15pt; width: 74pt;" width="98"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span><strong>Operator</strong></span></td><td style="background-color: #daeef3; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 224pt;" width="299"><span style="font-family: "arial";"><strong><span style="mso-spacerun: yes;"> </span>Remarks</strong></span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>+</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Addition</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>-</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Subtraction</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>*</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Multiplication</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>/</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Division</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>%</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Modulus (remainder of division)</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>++</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Increment (increase the value by 1)</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>--</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px; width: 224pt;" width="299"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Decrement (decrease the value by 1)</span></td>
</tr>
</tbody></colgroup></table>
<br />
Operand of arithmetic operator must be numeric type. You can not use it to
boolean data type. As an exception, for the char type, you can still
perform arithmetic operations.<br />
<br />
Here is an example of an arithmetic operation:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int a = 2, b = 4;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">b = b + a;</span><br />
<br />
<br />
<h3>
Equals (=) Operator </h3>
<br />
Called as assignment operator, which is used to enter a value into a
variable. In the first line of code above, we insert the value 2 to the
variable a, and the value 4 into the variable b.<br />
<br />
Now try to consider the second line of code above. The code means that the
new value of b is the old value of b (4) plus the value of a (2), so the
result is 6. Same as in C/C ++, in Java, this such as statement
can also be shortened to as following:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">b += a; //Same with b = b + a</span><br />
<br />
Here is a table showing a list of operator in Java shorthand for assignment
process or the process of entering a value into a variable:<br />
<br />
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 336px;">
<colgroup><col style="mso-width-alt: 3401; mso-width-source: userset; width: 70pt;" width="93"></col>
<col style="mso-width-alt: 8886; mso-width-source: userset; width: 182pt;" width="243"></col>
<tbody>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: #daeef3; border-image: none; border: 0.5pt solid windowtext; height: 15pt; width: 70pt;" width="93"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Operator</span></strong></td>
<td style="background-color: #daeef3; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 182pt;" width="243"><strong><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Remarks</span></strong></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>+=</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Assignment for addition</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>-=</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Assignment for subtraction</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>*=</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Assignment for multiplication</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>/=</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Assignment for division</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>%=</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>Assignment for modulus</span></td>
</tr>
</tbody></colgroup></table>
<br />
Here is an example of its use:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">b -= a; //Same with b = b - a</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">b *= a; //Same with b = b * a</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">b /= a; //Same with b = b / a</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">b %= a; //Same with b = b % a</span><br />
<br />
<br />
<h3>
Basic Arithmetic Operators</h3>
<br />
Basic arithmetic operators are: addition, subtraction, multiplication, and
division. Basic arithmetic operators are operators that you need in numerical computation processes in
general. Operator minus (-) also can be used to negate a
single operand.<br />
<br />
When we work with the division operator (/), if both operands is integer, then the result would be an integer. However, if one or both of its
operands is floating-point, the result to be obtained would be floating-point.<br />
<br />
Here is an example of a program that will demonstrate the use of basic
arithmetic operators, both for the integer or floating-point:<br />
<br />
<pre class="brush: java">class BasicArithmeticDemo {
public static void main(String[] args) {
System.out.println("Arithmetic operation for integer type");
int a = 2 + 1;
int b = a - 1;
int c = a * b;
int d = c / 3;
int e = -a;
System.out.println("Value of a: “ + a);
System.out.println("Value of b: “ + b);
System.out.println("Value of c: “ + c);
System.out.println("Value of d: “ + d);
System.out.println("Value of e: “ + e);
System.out.println();
System.out.println ("Arithmetic operation for floating-point type");
double fa = 2 + 1;
double fb = fa - 1;
double fc = fa * fb;
double fd = fc / 3;
double fe = -a;
System.out.println("Value of fa: “ + fa);
System.out.println("Value of fb: “ + fb);
System.out.println("Value of fc: “ + fc);
System.out.println("Value of fd: “ + fd);
System.out.println("Value of fe: “ + fe);
}
}
</pre>
<br />
The results will be obtained from the above program as follows:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Arithmetic operation for integer type</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value of a: 3</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value of b: 2</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value of c: 6</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value of d: 2</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value of e: -3</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Arithmetic operation on floating-point type</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value of fa: 3.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value of fb: 2.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value of fc: 6.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value of fd: 2.0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Value of fe: -3.0</span><br />
<br />
<br />
<h3>
Modulus (Remainder of Division) Operator</h3>
<br />
In Java, the modulus operator (%) is used to determine the side of an operating
division of integer or real number. Consider the following
code example:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int a = 11, b = 4;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">int c = a % b;</span><br />
<br />
Here, variable <span style="font-family: "courier new" , "courier" , monospace;">c</span> will be worth 3. Where the value of 3 come from? Initially
11 divided by 4 will yield a value of 2. Furthermore, the value of 2 is
multiplied by 4 to produce value 8. And the value 3 is derived from 11 minus 8.
In other words, 3 is the remainder of a division operation for 11/8.<br />
<br />
Now, consider again the following code:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">double da = 13.75;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">int b = 4</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">double dc = da % b;</span><br />
<br />
In this case, the variable dc will be worth 1.75, that is remainder of the
13.75/4.<br />
<br />
Here is an example of using the modulus operator in the program.<br />
<br />
<pre class="brush: java">class ModulusDemo {
public static void main(String[] args) {
int a = 11, b = 4;
int c = a * b;
double da = 13.75;
double dc = da * b;
System.out.println("Remainder of division of " + a + "/" + b + " is " + c);
System.out.println("Remainder of division of " + da + "/" + b + " is " + dc);
}
}
</pre>
<br />
When executed, the above program will give the following results:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">Remainder of division of 11/4 is 3</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Remainder of division of 13.75/4 is 1.75</span><br />
<br />
<br />
<h3>
Increment and Decrement Operator</h3>
<br />
Java provides increment (++) and decrement
(--) operator. Increment used to raise the value of the operand with a value of 1, while
the decrement is the opposite, to reduce the value of the operand with a
value of 1. Actually, these operators is a form of shorthand writing any code.
Let your attention to the following code:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">a = a + 1;</span><br />
<br />
can be written with the increment operator, as written in the following code:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">a++;</span><br />
<br />
Now, for the decrement:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">a = a - 1;</span><br />
<br />
can be written with a decrement operator, as written in the following code<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">a--;</span><br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com2tag:blogger.com,1999:blog-8283254582726951902.post-6337182529447827722016-06-08T03:35:00.002-07:002016-06-08T03:41:06.820-07:00String IntroductionSo far already explained simple data type, variable, and array. But why the string type not yet explained? The answer is, in Java, string is not a simple type, and not also an array of character (such as the concept of string in C/C++). In Java, a string is defined as an object. For that reason, it will be explained later in next post. Java has defined <span style="font-family: "courier new" , "courier" , monospace;">String</span> object to represent string type. As an introduction, the following is an example of declaring a variable of string type in Java:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">String str;
</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">str = "Sample string in Java";</span>
<br />
<br />
Here, <span style="font-family: "courier new" , "courier" , monospace;">str</span> is an object of <span style="font-family: "courier new" , "courier" , monospace;">String</span> type, instead of variable of simple type. Therefore, <span style="font-family: "courier new" , "courier" , monospace;">str</span> has special method to manipulate its value. Thus, we can call those methods from <span style="font-family: "courier new" , "courier" , monospace;">String</span> type through <span style="font-family: "courier new" , "courier" , monospace;">str</span> object, as shown in the following code sample:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">i<span style="font-family: "courier new" , "courier" , monospace;">nt jPosition = str.charAt('J');
</span></span><br />
<span style="font-family: "courier new" , "courier" , monospace;">System.out.println(str.toUppercase());
</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">...
</span><br />
<br />
The string type will be explained more detail in next post. Nevertheless, we will often use it in our program examples. The only you need to know for now, is just how you declare object of <span style="font-family: "Courier New", Courier, monospace;">String</span> type.
<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com1tag:blogger.com,1999:blog-8283254582726951902.post-17299442101035051642016-05-21T03:38:00.001-07:002016-06-08T03:18:47.087-07:00ArrayArray is a collection of same type variables referred by the same name. Each element of array can be accessed via index. In Java, array index always integer starting from 0. Here is a picture to illustrate an array:
<br />
<br />
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 320px;">
<colgroup><col span="5" style="width: 48pt;" width="64"></col>
<tbody>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: #b7dee8; border-image: none; border: 0.5pt solid windowtext; height: 15pt; width: 48pt;" width="64"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>A[0]</span></td>
<td style="background-color: #b7dee8; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 48pt;" width="64"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>A[1]</span></td>
<td style="background-color: #b7dee8; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 48pt;" width="64"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>A[2]</span></td>
<td style="background-color: #b7dee8; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 48pt;" width="64"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>A[3]</span></td>
<td style="background-color: #b7dee8; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 48pt;" width="64"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>A[4]</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>10</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>20</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>30</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>40</span></td>
<td style="background-color: transparent; border-color: black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: "arial";"><span style="mso-spacerun: yes;"> </span>50</span></td>
</tr>
</tbody></colgroup></table>
<br />
The above picture shows A array which contains five elements of integer type: 10, 20, 30, 40, and 50. Each element of the array can be accessed by A[index].
<br />
<br />
<br />
<h3>
One-Dimensional Array
</h3>
<br />
Arrays are declared with a [ ] (bracket). In Java, a common form of one-dimensional array declaration as follows:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">type arrayName[];</span>
<br />
<br />
or it can be written as follows:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">type[] arrayName;
</span><br />
<br />
In this blog, we will use second form. <span style="font-family: "courier new" , "courier" , monospace;">type </span>above signifies base type of each array element that exists. The base type will determine what type of data to be stored in each array element.
<br />
<br />
For example, if we want to know the number of days of each month, then we can implement it through an array. For example, here we are going to name the array with <span style="font-family: "courier new" , "courier" , monospace;">numberOfDays</span> and data type of each element is <span style="font-family: "courier new" , "courier" , monospace;">int</span>, then we can write it as follows:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int[] numberOfDays;</span>
<br />
<br />
We just declared a variable with array type. The number of elements and values of each element is set to <span style="font-family: "courier new" , "courier" , monospace;">null </span>value. To determine the number of elements of the array, we need to allocate memory space by using the <span style="font-family: "courier new" , "courier" , monospace;">new</span> keyword. The <span style="font-family: "courier new" , "courier" , monospace;">new</span> keyword itself will be explained more detail in next post. We need to use <span style="font-family: "courier new";">new</span> keyword to determine the number of array elements. Here is the general form of use<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">arrayVariable = new type[numberOfElements];</span>
<br />
<br />
In this case, we will determine the number of elements of the array with 12 (which contained a number of months in a year) so we need to write the following code:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">numberOfDays = new int[12];</span>
<br />
<br />
<span style="font-family: "courier new";">numberOfDays <span style="font-family: "times new roman";">variable </span></span>will refer to an array with 12 elements in <span style="font-family: "courier new" , "courier" , monospace;">int </span>type, and the value of each element will be set to a 0. In order to fill value in each array element, we must use index, such as the following :
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">numberOfDays[0] = 31;
</span><br />
<br />
The above code will put value 31 into the first array element (in this case means that January has 31 days). Remember, array index start at 0, not 1.
<br />
<br />
Here is an example of code that demonstrates the declaration and use of arrays in Java.
<br />
<br />
<pre class="brush: java">class DemoArray1D {
public static void main(String[] args) {
//declare a variable with array type in int
int[] numberOfDays;
//define number of array element
numberOfDays = new int [12];
//fill value in each array element
numberOfDays[0] = 31;
numberOfDays[1] = 28;
numberOfDays[2] = 31;
numberOfDays[3] = 30;
numberOfDays[4] = 31;
numberOfDays[5] = 30;
numberOfDays[6] = 31;
numberOfDays[7] = 31;
numberOfDays[8] = 30;
numberOfDays[9] = 31;
numberOfDays[10] = 30;
numberOfDays[11] = 31;
//display an array element
System.out.println("March has “ + numberOfDays[2] + " days.");
}
}
</pre>
<br />
The results will be given by the above program as follows:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">March has 31 days.</span>
<br />
<br />
Usually, Java professionals generally combine array declaration with the determination of the number of elements, as shown in the following code:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int[] numberOfDays = new int[12];</span>
<br />
<br />
or
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int numberOfDays[] = new int[12];</span>
<br />
<br />
Array can also be initialized when declared. The values for each element are in the block that begins with { and ends with }, and each element is limited by a comma (,). After the initialization block, we have to add a semicolon (;). For more details, see the following code sample which is a modification of the previous program. In this example, the value of each element of the array will be initialized in the declaration.
<br />
<br />
<pre class="brush: java">class Array1DInitialization {
public static void main(String[] args) {
//declare a variable with array type in int and fill value in each array element
int[] numberOfDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println("March has “ + numberOfDays[2] + “ days.”);
}
}
</pre>
<br />
The result provided by this program will be the same with the result given by the previous program.
<br />
<br />
<br />
<h3>
Multi-Dimensional Arrays
</h3>
<br />
Multi-dimensional array actually is array of array. There is an array where each element is also an array. The general way in Java to declare two-dimensional array as follows:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">type arrayName[][];</span><br />
<br />
or
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">type[][] arrayName;</span> <br />
<br />
For three-dimensional array, its general form as follows:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">type arrayName[][][];</span><br />
<br />
or <br />
<br />
type<span style="font-family: "courier new" , "courier" , monospace;">[][][] arrayName;</span> <br />
<br />
Here is an example of two-dimensional array declaration with the number of lines 2 and the number of columns 3, which each element of type is <span style="font-family: "courier new" , "courier" , monospace;">int</span>.
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int[][] twoD = new int[2][3];</span>
<br />
<br />
If described the position of each element as follows:
<br />
<br />
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 297px;">
<colgroup><col span="3" style="mso-width-alt: 3620; mso-width-source: userset; width: 74pt;" width="99"></col>
<tbody>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-image: none; border: 0.5pt solid windowtext; height: 15pt; width: 74pt;" width="99"><span style="font-family: Arial;"><span style="mso-spacerun: yes;"> </span>twoD[0][0]</span></td>
<td style="background-color: transparent; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 74pt;" width="99"><span style="font-family: Arial;"><span style="mso-spacerun: yes;"> </span>twoD[0][1]</span></td>
<td style="background-color: transparent; border-color: windowtext windowtext windowtext black; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt 0px; width: 74pt;" width="99"><span style="font-family: Arial;"><span style="mso-spacerun: yes;"> </span>twoD[0][2]</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-color: black windowtext windowtext; border-style: none solid solid; border-width: 0px 0.5pt 0.5pt; height: 15pt;"><span style="font-family: Arial;"><span style="mso-spacerun: yes;"> </span>twoD[1][0]</span></td>
<td style="background-color: transparent; border-color: black windowtext windowtext black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: Arial;"><span style="mso-spacerun: yes;"> </span>twoD[1][1]</span></td>
<td style="background-color: transparent; border-color: black windowtext windowtext black; border-style: none solid solid none; border-width: 0px 0.5pt 0.5pt 0px;"><span style="font-family: Arial;"><span style="mso-spacerun: yes;"> </span>twoD[1][2]</span></td>
</tr>
</tbody></colgroup></table>
<br />
We can also do initialize the value of two-dimensional array through the following ways:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">
int[][] twoD = { {10,20,30}, {40,50,60} };</span>
<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com2tag:blogger.com,1999:blog-8283254582726951902.post-6801302342189329822016-05-21T03:24:00.001-07:002016-05-21T03:28:09.256-07:00TypecastingIn cases of specific programming, sometimes we need to change the data type to the other type. If both types are compatible or suitable, then Java will perform automatic type conversion. For example, we can always enter a value of <span style="font-family: "courier new" , "courier" , monospace;">int</span> type to a variable declared with <span style="font-family: "courier new" , "courier" , monospace;">long</span> type. However, if the types are not compatible, then we need to explicitly typecast to the variable to be converted. Typecasting is the casting process of data type to other data type.
<br />
<br />
<br />
<h3>
Automatic Conversion in Java
</h3>
<br />
By the time we enter a value into a variable that has a specific data type to the value of other types, then Java will internally perform automated conversion, with terms as follows:
<br />
<br />
<ul>
<li>Both data types (origin and destination) are compatible </li>
<li>The destination data type has a greater range of origin data type
</li>
</ul>
<br />
For example, <span style="font-family: "courier new" , "courier" , monospace;">int</span> data type always has greater range than <span style="font-family: "courier new" , "courier" , monospace;">byte</span> type, so we do not need to perform explicit conversion from <span style="font-family: "courier new" , "courier" , monospace;">byte</span> to <span style="font-family: "courier new" , "courier" , monospace;">int</span>. Consider the following example code:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">byte b = 2;
</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">int c = b;</span>
<br />
<br />
we don’t need to write as follow:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">int c = (int) b;</span>
<br />
<br />
<br />
<h3>
Conversion of Not Compatible Data Type
</h3>
<br />
We can also do the conversion from real numbers (floating-point) to integers. In such cases, a decimal value contained behind the comma will be discarded. For example, now we will convert the value of <span style="font-family: "courier new" , "courier" , monospace;">double</span> to <span style="font-family: "courier new" , "courier" , monospace;">int</span>. Consider the following code snippet:
<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">double d = 274.5678;
</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">int x = (int) d; // Typecasting from double to int</span>
<br />
<br />
Here, variable <span style="font-family: "courier new" , "courier" , monospace;">x</span> will be worth 274 and the fractional value (0.5678) is discarded.
<br />
<br />Anonymoushttp://www.blogger.com/profile/08175652861696823611noreply@blogger.com3