Showing posts with label Core Java Tutorial. Show all posts
Showing posts with label Core Java Tutorial. Show all posts

Tuesday, July 12, 2011

1st Java example of boolean

 Boolean data type only used for true and false, or in other word only use to say that statement is true or false.
In this  program we take (boolean booleanType=false;) boblean data type for (booleanType) and equal this data type to false in next step i apply (if) statement,that if
(booleanType==false) is truse then output is (boolean example data type value :false) which is line 8th in below program.
To understand logical (if and else) operates see java logical operators section in this site.

source code 
  1. class theboolean
  2. {
  3.        public static void main(String[] args)
  4.    {
  5.            boolean booleanType=false;
  6.         if(booleanType==false)
  7.         {
  8.             System.out.println("boolean example data type value :"+booleanType);
  9.         }
  10.     }
  11. }

How to take user input in java


This program tell you how to take user input using java programing.
you only have to do add two line in program one is[InputStreamReader ir=new InputStreamReader(System.in);] which tell the java compiler that user is providing input from keybord, and 2nd line is [BufferedReader br=new BufferedReader(ir);]
hear java program take the input of user as buffer or in other word take in buffer memory.
In both line 6 and 7 we make object of InputStreamReader and BufferedReader method which are pre defined in java compiler.
We have to make object of each method to instantiate in a program, or in other word
if you want to use method in java user define or pre define you must have to make object of that methat.
      
source code
  1. import java.io.*;
  2. class user_input_two_no_for_sum
  3. {
  4.     public static void main(String[] args)throws IOException
  5.     {
  6.         InputStreamReader ir=new InputStreamReader(System.in);
  7.         BufferedReader br=new BufferedReader(ir);
  8.         int no1,no2,sum;
  9.         System.out.println("Enter no1=");
  10.         no1=Integer.parseInt(br.readLine());
  11.         System.out.println("Enter no2=");
  12.         no2=Integer.parseInt(br.readLine());
  13.         sum=no1+no2;
  14.             System.out.println("Total="+sum);
  15.             }
  16. }

3 part sum of three number divide by fourth number

This java program is able to calculate result in decimal value, you only have to change 8 line of previous program from(double A,B,C,D,total;) to ( Float A,B,C,D,total;).
In this program we take four input number from user -A, B, C, D.
hear A, B, C are added and divided by D,this is the hole detail of program. 

source code
  1. import java.io.*;
  2. class sum_of_three_no_divide_by_fourth_no
  3. {
  4.     public static void main(String[] args)throws IOException
  5.     {
  6.         InputStreamReader ir=new InputStreamReader(System.in);
  7.         BufferedReader br=new BufferedReader(ir);
  8.         Float A,B,C,D,total;
  9.         System.out.println("Eter no1");
  10.         A=Float.parseFloat(br.readLine());
  11.         System.out.println("Eter no2");
  12.         B=Float.parseFloat(br.readLine());
  13.         System.out.println("Eter no3");
  14.         C=Float.parseFloat(br.readLine());
  15.         System.out.println("Eter no4");
  16.         D=Float.parseFloat(br.readLine());
  17.         total=(A+B+C)/D;
  18.         System.out.println("Total="+total);
  19.        
  20.                 }
  21. }

2 part sum of three number divide by fourth number

This java program is able to calculate result in decimal value, you only have to change 8 line of previous program from(int A,B,C,D,total;) to (double A,B,C,D,total;).
In this program we take four input number from user -A, B, C, D.
hear A, B, C are added and divided by D,this is the hole detail of program. 


source code
  1. import java.io.*;
  2. class sum_of_three_no_divide_by_fourth_no
  3. {
  4. public static void main(String[] args)throws IOException
  5. {
  6. InputStreamReader ir=new InputStreamReader(System.in);
  7. BufferedReader br=new BufferedReader(ir);
  8. double A,B,C,D,total;
  9. System.out.println("Eter no1");
  10. A=Double.parseDouble(br.readLine());
  11. System.out.println("Eter no2");
  12. B=Double.parseDouble(br.readLine());
  13. System.out.println("Eter no3");
  14. C=Double.parseDouble(br.readLine());
  15. System.out.println("Eter no4");
  16. D=Double.parseDouble(br.readLine());
  17. total=(A+B+C)/D;
  18. System.out.println("Total="+total);
  19. }
  20. }

1 part sum of three number divide by fourth number

This java program is not able to calculate result in decimal value, and if you provide that type of input it give wrong answer. 
For Ex- n1=3, n2=2, n3=3 and n4=3 then answer (Total=2) which is wrong right answer is (2.6666).
In 2nd part program we provide how to get rid from this error.


source code
  1. import java.io.*;
  2. class sum_of_three_no_divide_by_fourth_no
  3. {
  4.     public static void main(String[] args)throws IOException
  5.     {
  6.         InputStreamReader ir=new InputStreamReader(System.in);
  7.         BufferedReader br=new BufferedReader(ir);
  8.         int A,B,C,D,total;
  9.         System.out.println("Eter no1");
  10.         A=Integer.parseInt(br.readLine());
  11.         System.out.println("Eter no2");
  12.         B=Integer.parseInt(br.readLine());
  13.         System.out.println("Eter no3");
  14.         C=Integer.parseInt(br.readLine());
  15.         System.out.println("Eter no4");
  16.         D=Integer.parseInt(br.readLine());
  17.         total=(A+B+C)/D;
  18.         System.out.println("Total="+total);
  19.        
  20.                 }
  21. }

Saturday, April 23, 2011

Data Type In Java

  The Java programming language is statically-typed, which means that all variables must first be declared before they can be used.

     
1-byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

    2-short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

    3-int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.

    4-long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.

    5-float: The float data type is a single-precision 32-bit IEEE 754 floating point.  As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the Java.math.Big Decimal class instead. Numbers and Strings covers Big Decimal and other useful classes provided by the Java platform.

    6-double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

    7-boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

    8-char: The char data type is a single 16-bit Unicode character. It has a minimum value of  ( 0) and a maximum value of  ( 65,535 ).

Thursday, April 21, 2011