Tuesday, February 12, 2013

Tutorial 13 : Java Formatting Double Value


Java Tutorial For Beginner, Tutorial 13 : Formatting Double Value

To format double value to 2 decimal format, use java.text.DecimalFormat class to format the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator.

In this example, to format to decimat format i use the following statements;

Step 1; Use the java.text.* to use java.text.DecimalFormat class
import javax.swing.JOptionPane;

Step 2: Create DecimalFormat object called dFormat, use to format 2 decimal point by using the pattern "#.##"

DecimalFormat dformat= new DecimalFormat("#.##");

Step 3 : Finally, use format() method to format the double value. It accepts double value as argument and returns a string.

dformat.format(celsius)

Code below :


// Celsius to Fahrenheit Converter Example
// How to format double value;
// Watch output without formatting value;

import javax.swing.JOptionPane;

// add this
import java.text.*;

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

String number = JOptionPane.showInputDialog(null, "Enter fahrenheit value : " , "Fahrenheit to Celsius Converter", JOptionPane.QUESTION_MESSAGE);

//convert string to number
double fahrenheit = Double.parseDouble(number);

double celsius = (5.0/9)*(fahrenheit- 32);

// Add this to format the double value
DecimalFormat dformat= new DecimalFormat("#.##");


// To produce, 2 decimal point output
JOptionPane.showMessageDialog(null, "Fahrenheit value is " + fahrenheit + "\n" + "Celcius value is " + dformat.format(celsius) ,
"Fahrenheit to Celsius", JOptionPane.INFORMATION_MESSAGE);

}

}

Tutorial 12 : Fahrenheit To Celsius Example JOption Input

Fahrenheit To Celsius Example.

Step 1 : I use JOptionPane.showInputDialog to get the fahrenheit reading

Step 2 : calculate celsius using the following calculate

double celsius = (5.0/9)*(fahrenheit- 32);

Step 3 : Show the result using JOptionPane.showMessageDialog

Code below :


// Celsius to Fahrenheit Converter Example


import javax.swing.JOptionPane;


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

String number = JOptionPane.showInputDialog(null, "Enter fahrenheit value : " , "Fahrenheit to Celsius Converter", JOptionPane.QUESTION_MESSAGE);

//convert string to number
double fahrenheit = Double.parseDouble(number);

double celsius = (5.0/9)*(fahrenheit- 32);


JOptionPane.showMessageDialog(null, "Fahrenheit value is " + fahrenheit + "\n" + "Celcius value is " + celsius ,
"Fahrenheit to Celsius", JOptionPane.INFORMATION_MESSAGE);

}

}

Tutorial 11 : Entering input JOptionPane, Addition Example

Java Tutorials For Beginners, Tutorial 11 : Entering input JOptionPane, Addition Example.

Another way to get input is to use JOptionPane class. Previously I was using Scanner class.

To use it, add the import javax.swingJOptionPane ; statement .

To get input we use, the following code ,

JOptionPane.showInputDialog(null, "Enter 1st number", "Input Example", JOptionPane.QUESTION_MESSAGE);

The 1st argument as we use null, second to prompt user to enter the 1st number, the third is the title of input box and 4th causes icon ? question mark to appear.

To show the output, we use the following statement:

JOptionPane.showMessageDialog(null, "Total sum is : " + (x+y), "Input Example", JOptionPane.INFORMATION_MESSAGE );

The first normally we put null, second is the output, the third is the title of the dialog box and the 4th show the i icon in the message box.



Code


// Entering input using JOptionPane

import javax.swing.JOptionPane;

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

String number1 = JOptionPane.showInputDialog(null, "Enter 1st number", "Input Example", JOptionPane.QUESTION_MESSAGE);

//CONVERT STRING TO NUMBER

int x = Integer.parseInt(number1);

String number2 = JOptionPane.showInputDialog(null, "Enter 2nd number", "Input Example", JOptionPane.QUESTION_MESSAGE);

//CONVERT STRING TO NUMBER

int y = Integer.parseInt(number2);

//Show the output, summation

JOptionPane.showMessageDialog(null, "Total sum is : " + (x+y), "Input Example", JOptionPane.INFORMATION_MESSAGE );

}
}

Monday, February 11, 2013

Tutorial 10 : If and Else, How to determine the number is even or odd

Java Tutorial For Beginners, Tutorial 9 : If and Else, How to determine the number is even or odd?

Program uses if and else condition. If the number when divided by 2, the remainder is 0 then is an even number. Else the number is an odd number.

Step 1 : Read the number. Use Scanner class to read the input.

Step 2 : Use if and else condition. If number % 2 == 0, then the number is an even number. Else it is an odd number.

Step 3 : Print out the statement.

Code below :


//Program to determine whether a number is even or odd
// Use if else condition
// It is even if the number remainder is 0 when divided by 2
// If the remainder is 1, then the number is odd

//use scanner class for read input

import java.util.Scanner;

public class TestIfTwo{
public static void main(String[] args){
int number = 0;

Scanner myscan = new Scanner(System.in);

//Read input
System.out.print("Enter a number : ");
number = myscan.nextInt();

//if and else condition
if (number % 2 == 0) {System.out.println(number + " is even");}
else {System.out.println(number + " is odd");}

}

}

Java Tutorial For Beginners, Tutorial 9 : If and Else

Java tutorial for beginners, tutorial 9 : If and Else. This program calculates the retail profit, if the value of retail price is more than 250 than the profit is 30 %, else the profit is 40 %.

Step 1 : Read the retail price

Step 2 : If else condition, calculate the profit rate. If it is more than 250, then profit is 30 % of the retail price. Else the profit is 40 % of the retail price.

Step 3 : Print out the profit .

Code below :


// Tutorial on if condition
// For example, if your retail price is more than 250, the profit is 30 %
// Else, the profit rate is fix at 40 %

//use scanner class to input retail price
import java.util.Scanner ; // forgot to add this line

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

//declare and initialize the variables;
int retailPrice = 0;
int profit = 0;

Scanner myscan = new Scanner(System.in);

System.out.print("Enter your retail price : ");
retailPrice = myscan.nextInt();


//Your condition at here
if (retailPrice > 250) {
profit = retailPrice * 30 / 100 ; // more than 250 the profit rate is only 30 %
}
else {
profit = retailPrice * 40 / 100 ; // more than 250 the profit rate is only 30 %
}

//Print out the output

System.out.println("Your profit is : " + profit);

}
}

Sunday, February 3, 2013

Java tutorial 7, Nested For Loops, To Generate Patterns, Part 2


Java tutorial 7, Nested For Loops, To Generate Patterns, Part 2.

The output as stated below, only need to change the condition for 2nd for loop .


//Java Nested Loops

/* Program to generate the following output
Use the same program as previous tutorial

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

*/

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

// I use two nested for loop to generate the pattern
// to generate 5 rows
for(int i = 1; i <= 5 ; i++){

//change here
//
for(int j = 1 ; j <= 5 - i + 1 ; j++){
System.out.print(j + " ");
}

System.out.println(" ");
}

}
}


Java, Nested For Loops, To Generate Patterns

Java, Nested For Loops, To generate.

I use to for loops to generate the following output :


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Step 1 ; Use for to generate the five rows,

Step 2 ; For each row, generate the numbers.

The code as stated below :


//Java Nested Loops

/* Program to generate the following output

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

*/

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

// I use two nested for loop to generate the pattern
// to generate 5 rows
for(int i = 1; i <= 5 ; i++){

//to generate the numbers
for(int j = 1 ; j <= i ; j++){
System.out.print(j + " ");
}

System.out.println(" ");
}

}
}




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 );


}
}