Java Tutorial

by Dinesh Thakur Category: Control Structures

The do loop enables us to repeatedly execute a block of code until a boolean expression evaluates to false. It is almost identical to the while loop with the difference that here the logical expression is evaluated at the bottom of the loop rather than the top. This means that the contents of the loop will execute at least once.

 
by Dinesh Thakur Category: Control Structures

Algorithm for Armstrong Number:

step 1:Set s=0

step 2: Read num

step 3: Set b=num

step 4: Repeat through step-7 while num greater than 0

step 5: r=num%10

step 6: s=s+(r*r*r)

 
by Dinesh Thakur Category: Control Structures

In this example we use BufferedReader class for Reading text from a character-input stream. After Enter any 5 Number we check if number ==0 then is zero if number greater than 0 then it is positive other wise negative.

 
by Dinesh Thakur Category: Control Structures

The Random class provides a template for the creation of random number generators. The Math.random() method provides a static function for the generation of random double values.

Two constructors are provided for creating Random objects. The default constructor initializes the seed of the random number generator using the current system time. The other constructor allows the seed to be set to an initial long value.

 
by Dinesh Thakur Category: Control Structures

Algorithm for Digits Are Present in the Number:

step 1: Set c=0

step 2: Read num

step 3: Repeat through step-5 while num greater than 0

step 4: c=c+1

 
by Dinesh Thakur Category: Control Structures

Algorithm for Swap Two Variables Using XOR Operator:

step 1: Read a, b

step 2: a=a^b

step 3: b=b^a

step 4: a=a^b

step 5: Print a,b

step 6: Exit

 
by Dinesh Thakur Category: Control Structures

The currentTimeMillis() method returns the current time in milliseconds since 00:00:00,January I, 1970.This method is used to know the time interval a process or job takes to perform. We record the time before and after the operation and the difference of two timings tells us the total time the operation took.

 
by Dinesh Thakur Category: Control Structures

In this example, the continue statement is placed in the body of inner for loop. While executing inner for loop, if the condition (m==n) evaluates to true then the continue statement is executed and the remaining statement for displaying values of m and n is skipped and control is transferred to the increment expression (n++) of inner for loop. This increments the value of n by 1 and test condition (n<=2) is evaluated again for this incremented value of n. This process continues. We have used continue statement in this program so that the same value of m and n should not be displayed.

 
by Dinesh Thakur Category: Control Structures

The while loop is a construct that repeatedly executes a block of code as long as a boolean condition remains true. The logical expression in while loop is evaluated first. If the logical expression evaluates to false, the body of while loop will not execute even once. If the logical expression in while evaluates to true, the statements in the body of while loop are executed. After executing the body, control jumps again at the top to recheck whether the boolean expression is still true or not. The body of the loop will continue to execute until the expression evaluates to false. When the logical expression becomes false, control continues the execution with the statements following while loop.

 
by Dinesh Thakur Category: Control Structures

Algorithm for Triangle of any size:

step 1: set i=0,j=0,row=0,osc=0,var=0

step 2: read row

step 3: if row greater than 0 then execute following codes otherwise goto

          step-33

step 4: osc=row-l

step 5: set isc= 1

 
by Dinesh Thakur Category: Control Structures

Algorithm for Number of Times a Word has Been Mentioned :

step 1: read String w
step 2: read String w1
step 3: set len=w1.length()
step 4: set result=0

 
by Dinesh Thakur Category: Control Structures

The for loop enables code to execute repeatedly until a boolean expression evaluates to false.

 
by Dinesh Thakur Category: Control Structures

We can change the format of the date in two steps: First, we create a formatter with the getDateInstance method. Then, we invoke the format method, which returns a String containing the formatted date.

 
by Dinesh Thakur Category: Control Structures

The Calendar class is an abstract class used to convert dates. We can use this class to convert a Date object to fields, such as YEAR, MONTH, HOUR, and so on. We can also use these fields to update a Date object.

 
by Dinesh Thakur Category: Control Structures

Java.io.Console class is a new class in Java 6 that may be used for reading text from character based console device. The data can be read from the console device provided it is associated with the Java Virtual Machine (JVM).

 
by Dinesh Thakur Category: Control Structures

 Algorithm for Swap Variables in a Different Way:

step 1: Read a, b

step 2: a=(a+b)-(b=a)

step 3: Print a,b

step 4: Exit

 
by Dinesh Thakur Category: Control Structures

Algorithm for Java Sum of Digits :

 

step 1: Set sum=0

step 2: Read num

step 3: Repeat through step-5 while num greater than 0

step 4: temp=num % 10

step 5: sum=sum+temp

 

Page 9 of 40



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.