Java4teachus

Tuesday, February 26, 2019

Control Structures in Java

Control Structure 


In any programming languages  to perform any logical operations of a program like whether the condition statement is true or false. So let us have to know about the control statements  or structures in java .

The purpose of control statements is to perform the operations at only once whether condition is true or false  (or) To perform the operations repeatedly until   the condition is become false.

Most of the programming languages the control structures are classified into 3 types. They are :
  1. Conditional Statements
  2. looping / interactive Statements
  3. Miscellaneous  Statements

Control Statements

The control statement is mainly used  for performing either the condition is true or false. The operation is  once depends upon on the condition evaluation. We have 3 type of conditional statements. They are :

1)  if  
2) if else
3) switch

Flowchart For Conditional Statement




Syntax For if Statement:

if (Test Condition)
{
Executable block of statement's ;
}
statement-x ;

Syntax For if else Statement:

if (Test condition)
{
Executable block of statement's; → 1
}
else
{
Executable block of statement's ; →2
}
statement-x ;

Looping statements

This statements is used for performing some operations repeatedly until condition becomes false.We have 3 types of looping statements.They are :

 1) While
 2) Do while
 3) For

All the time of dealing with loop statements the programmer must ensure 3 parts. They are :

A) Initialization part    ( where to start )
B) Condition part         ( how many types to be repeated )
C) Updation part          ( Increment / Decrement ratio)


Flowchart For Looping Statements



Syntax for while:

While (Test Condition)                   
{
Execute Block of Statement's ;
}
Statement-x


Syntax do while:

do
{
Execute
Block of statements)
} While (Test cond )
 Statement-x

Syntax for looping:

 for ( Initialization part; conditional part; operation part )
 {
 Execute Block of statements ( s );
 }
 Statements-x

Sunday, February 17, 2019

How To accept the Values With Dynamically in Java

Q. Write a java program to accept the  two integer values and Multiply them dynamically . 

Program:-

class Mul
{
int a,b,c;
void accept(int p,int q)
{
a=p;
b=q;
}
void multiply()
{
c=a*b;
}
void display()
{
System.out.println("value of a ="+a);
System.out.println("value of b ="+b);
System.out.println("Multiply ="+c);
}
}
class MulDemo
{
public static void main(String k[])
{
if(k.length!=2)
{
Sysetm.out.println("Please Enter the 2 values");
}
else 
{
int x=Integer.parseInt(k[0]);
int x1=Integer.parseInt(k[1]);
Mul m= new Mul();
m.accept(x,x1);
m.multiply();
m.display();
}
}
}

Q. Write a java program to accept the  integer values and square them  dynamically .  

Program:-

class Square
{
int n,res;
void accept(int x)
{
n=x;
}
void calSquare()
{
res=n*n;
}
void display()
{
System.out.println("value of n="+n);
System.out.println("Square="+res);
}
}
class SquareDemo
{
public static void main(String k[])
{
if(k.length!=1)
{
System.out.println("Please Enter the one Value");
}
else
{
int x=Integer.parseInt(k[0]);
Square s=new Square();
s.accept(x);
s.calSquare();
s.display();
}
}
}

Q. Write a java program to accept the  two values and find the Area Of Triangle dynamically.

  
Program:-

class Triangle
{
float b,,h,res;
void accept(float x,float y)
{
b=x;
h=y;
}
void area()
{
res=0.5*b*h;
}
void display()
{
System.out.println("value of b="+b);
System.out.println("value of h="+h);
System.out.println("AreaOfTRaingle="+res);
}
}
class TraingleDemo
{
public static void main(String k[])
{
if(k.length!=2)
{
System.out.println("Please Enter the two Value");
}
else
{
float x1=Float.parseFloat(k[0]);
float x2=Float.parseFloat(k[1]);
Triangle t=new Triangle();
s.accept(x1,x2);
s.area();
s.display();
}
}
}

Q. Write a java program to accept the  two integer values and swapping them dynamically .  

program:- 

class Swap
{
int a,b,t,;
void accept(int p,int q)
{
a=p;
b=q;
}
void swapValues()
{
t=a;
a=b;
b=a;
}
void display()
{
System.out.println("values of a="+a);
System.out.println("values of b="+b);]
}
}
class SwapDemo
{
public static void main(String k[])
{
if (k.length!=2)
{
System.out.println("Please Enter Two values");
}
else
{
int x1=Integer.parseInt(k[0]);
int x2=Integer.parseInt(k[1]);
Swap s=new Swap();  //create an Object of swap class
s.accept(x1,x2);   //swaping of two values
s.swapValues();
s.display();
}
}
}



Output:-

cd/
cd blog data
javac SwapDemo.java
java SwapDemo 10 20

Swapping-of-two-values.png



Q. Write a java program to accept two  values and find Big value with the  dynamically .  

Program:-

class Big

{
int a,b; 
void set(int x,int y) 
{
a=x;
b=y;
}
void findBig()
{
if(a==b)
{
System.out.println("Both values are same");
}
else
{
if (a>b)
{
System.out.println(a+" is greater than"+ b);
}
else
{
System.out.println(b+"is greater than"+a);
}
}
}
}
class BigDemo
{
public static void main(String k[])
{
if (k.length!=2)
{
System.out.println("Please Enter the 2 values");
}
else
{
int x1=Integer.parseInt(k[0]);
int x2=Integer.parseInt(k[1]);
Big b=new Big();
b.set(x1,x2);
b.findBig();
}
}
}

Q. Write a java program to accept two  values and find the Biggest value with the  dynamically .  



Program:-

class Biggest
{
int a,b,c;
void set(int x,int y,int z)
{
a=x;
b=y;
c=z;
}
void findBiggestValue()
{
if (a>0 && a>b && a>c)
{
System.out.println(a+" is bigger ");
}
if (b>0 && b>c && b>a)
{
System.out.println(b+" is bigger ");
}
if (c>0 && c>b && c>a)
{
System.out.println(c+" is bigger");
}
else
{
System.out.println("Both values are same");
}
}
}
class BiggestDemo
{
public static void main(String k[])
{
if (k.length!=3)
{
System.out.println("Please Enter the 3 values");
}
else
{
int x1=Integer.parseInt(k[0]);
int x2=Integer.parseInt(k[1]);
int x3=Integer.parseInt(k[2]);
Biggest b=new Biggest();
b.set(x1,x2,x3);
b.findBiggestValue();
}
}
}


Q. Write a java program which will accept the numerical integer values fin that whether it is Positive Number are not?.



Program:-



class PositiveNumber
{
int n;
void set(int x)
{
n=x;
}
void decide()
{
if (n>0)
{
System.out.println(n+"is positive");
}
else 
{
System.out.println(n+"is negative");
}
}
}
class  PositiveNumberDemo
{
public static void main(String[] k) 
{
if (k.length!=1)
{
System.out.println("Please Enter the one value");
}
else
{
int x1=Integer.parseInt(k[0]);
PositiveNumber po=new PositiveNumber();
po.set(x1);
po.decide();
}
}
}

Q. Write a java Program which will accept the numerical values and find that whether it is Even Or Odd numbers?.


Program:-


class EvenOdd

{
int a;
void set(int x)
{
a=x;
}
void decide()
{
if (a<=0)
{
System.out.println("Enter The Positive Number");
}
else
{
if (a%2==0)
{
System.out.println(a+"Even Number");
}
else
{
System.out.println(a+"Odd Number");
}
}
}
}
class EvenOddDemo 
{
public static void main(String[] k) 
{
if (k.length!=1)
{
System.out.println("Please Enter the one value");
}
else
{
int x1=Integer.parseInt(k[0]);
EvenOdd e=new EvenOdd();
e.set(x1);
e.decide();
}
}
}

Thursday, February 07, 2019

Wrapper Classes in Java

Wrapper Classes In Java


We know that when we pass any "Command Line Argument" through the command prompt and which is directly sending to the main method ( main ( ) ), It is available in the form "Array of object of String class" (String args[]). The string type of data we cannot perform any numerical operations. 

 So hence it is designed to convert string type data into numerical / fundamental type data by using wrapper classes.

Define Wrapper class & Explain its purpose

For each & every fundamental data types their exist one pre-defined class then it is also known as "Wrapper classes".  The purpose of Wrapper class is that to convert String data type into numerical / fundamental data type values. 

The following table gives fundamental data type with  corresponding wrapper class name & conversion method which will convert string type of  data into fundamental type data.


In generally each & every Wrapper class contain the following method whose purpose into convert string data type into numerical / fundamental data type.

                      
                                              Wrapper class name
                                                                 ðŸ”º
Public  static  xxx  parse xxx ( String )

Here note that xxxx represents any fundamental data type( Expect char data type ).


Points to Remembers to use Methods In a Class.

1. Beside the  what type of method (instance or static method) We are using and presenting which class.
2. Beside what type of parameters a method is taking and pass it values return type.
3. Beside what type of method is returning and hold the result in a variable of return type.

EX:-   Math ↴
             int  sum( int,int ) 
The below example will shown how to useful in the wrapper classes in java.

Math m = new Math(  );
int x  = m. sum ( 10,20 );
System.out.println(x) = //30.

EX 1:String s = "100";
            S = s + 2
            System.out.println ( S );     //1002 (invalid)


Here the value of s is declared as  "100" , In java if any values put  into a  "100"  then it called as String. So String type of values as  convert into fundamental type by using Wrapper class.

  int a = Integer.parseInt(s) // wrapper class name
  a = a+2;    // valid
 System.out.println(a);    // 102

EX : 2 String S1 = " 10.5f";
              S1  =  S1+2; 
              System.out.println( s1 );    // 10.5f2 ( invalid ) 

float x = Float. parseFloat ( s1 );  // wrapper class name
x = x+2;   // valid
System.out.println(x); //12.5f

EX : 3

String s [ ] = { "10", "20"};   10/20
System.out.println( s[0] + s[1] );  //1020 (invalid)

int x = Integer.parse Int ( s[0] );  // wrapper class name
int y = Integer.parse Int ( s[1] );  // wrapper class name
int z = x+y;   

⧭ Here we will discuss more examples on wrapper class.

System.out.println ( "sum="+z);        //sum=30 (valid)
System.out.println("sum="+x+y);    //sum1020
System.out.println ("sum="+x+y);   //sum=30
System.out.println(="mul"=+x+y);  //mul=200
System.out.println ("sub="+x-y);     //error
System.out.println("sub="+(y-x));   //sub=10
System.out.println("div="+x/y);      //Div=D
System.out.println ("MOD="+y/x);  //mod=0