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);
}
}
Showing posts with label inputs. Show all posts
Showing posts with label inputs. Show all posts
Tuesday, February 12, 2013
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
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);
}
}
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);
}
}
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);
}
}
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 );
}
}
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 );
}
}
Subscribe to:
Posts (Atom)