Showing posts with label Java Basic. Show all posts
Showing posts with label Java Basic. 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. }