Java Tutorials For Beginners
Java Tutorials For Beginners
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);
}
}
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 );
}
}
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");}
}
}
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);
}
}
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(" ");
}
}
}
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(" ");
}
}
}
Subscribe to:
Posts (Atom)