Saturday, February 2, 2013


Java Programming, Do While, If, Miles To Kilometer , Part 2

Use do while loop to repeat the program, to exit user enters - 1.0 And also if clause

Code below :


// Miles to kilometer example
// In part two we use do while to repeat the program
// and also if and else

// Use scanner class

import java.util.Scanner;

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

double miles ;

// use constant
final double MILES_TO_KILOMETER = 1.609 ;

//Initialize scanner class
Scanner newscan = new Scanner(System.in);


// To repeat use do while loop

do {

System.out.println("Miles to kilometer program \n Enter -1.0 to terminate ");
System.out.print("Enter miles value : ");

// Read miles value
miles = newscan.nextDouble();

// Print out the value

// if miles not equal to -1 print the kilometer value
if( miles != -1.0 )
{
System.out.println("Kilometer value is : " + (miles * MILES_TO_KILOMETER ) + "\n");
}
// say good bye
else {  System.out.println("Thank you, bye");}

} while(miles != -1.0); // loop will terminate if miles value is -1.0


}
}



Java CONSTANT, How to convert miles to kilometer

Java Programming, Constant

To declare CONSTANT the syntax as stated below,

final datatype CONSTANTNAME = value;

In this example , we declare constant MILES_TO_KILOMETER as stated below

final double MILES_TO_KILOMETER = 1.609 ;

I use scanner class to read the miles value, the full code as stated below.

// Miles to kilometer example

// Use scanner class

import java.util.Scanner;

public class MilesKilometer {
public static void main(String[] args){
double miles ;
// use constant 
final double MILES_TO_KILOMETER = 1.609 ;
//Initialize scanner class
Scanner newscan = new Scanner(System.in);
System.out.print("Enter miles value : ");
// Read miles value
miles = newscan.nextDouble();
// Print out the value
System.out.println(" Kilometer value is : " + ( miles *  MILES_TO_KILOMETER ) + "\n");

}
}

Java Programming :How to add ten numbers using while loop and arrays.

Java Programming :How to add ten numbers using while loop and arrays.

Step 1:
declare and initialize 3 variables. integer i as counter and integer sum.

Step 2:
declare and initialize array integer type, size of the array is ten.

Step 3: while loop to generate the numbers and add the numbers.

Step 4:
Print out the result.

Full code below :


// How to add 10 numbers using while

public class AddTenNumbers{
public static void main(String[] args){
int i = 0 ; // as counter
int sum = 0 ; // sum up the total
int number[] = new int[10]; // use array to generate ten numbers

// use while to generate the numbers and sum up the numbers

while( i < 10){

// Generate the 10 numbers
number[i] = i + 1 ;

// SUm the numbers
sum = sum + number[i];

i++; // forget this

}

// Print the sum

System.out.println("SUmmation of 1 till 10 is : " + sum);

}
}

Java, How to add two integer numbers , Scanner Class

Java, How to add two integer numbers , Scanner Class I use Scanner class, need to import Scanner class using import statement.

Then create a new scanner object called myscan, you could rename myscan.

Read two integer numbers from the console using the statement x= myscan.nextInt(), if you want to read double value u use nextDouble();

 The full code is as stated below;

//use scanner class

import java.util.Scanner;

// class name

public class AddNumber{
public static void main(String[] args){
// declare to variables;
int x, y;

// Initialize scanner class
Scanner myscan = new Scanner(System.in);

System.out.print("Enter 1st integer number");
x = myscan.nextInt();

System.out.print("Enter 2nd integer number");
y = myscan.nextInt();

System.out.println("Summation of x + y is : " + (x+y));

}

}

How to read input using Java Scanner Class

This tutorial is how you can read inputs for command prompt. You will be using Scanner class to read the input from keyboard.

You import Scanner class using > import java.util.Scanner ; statement

You create scanner object using the statement ;
Scanner myscan = new Scanner(System.in);

myscan can be any name.

To read integer number , you use the following statement.

x = myscan.nextInt();

The full code the program is stated below;


// How to read input from console

import java.util.Scanner;

public class ScannerInput2 {
public static void main(String[] args){
int x ;
Scanner myscan = new Scanner(System.in);

// Ask input
System.out.print("Enter an integer number :");

// Read from console
x = myscan.nextInt();

//Print out the number
System.out.println("Your integer number is :" + x );


}
}