IF statement - is a conditional statement.
syntax:
if(conndition)
{
statement 1;
}
IF ELSE - also conditional statement that the program will choose the true statement or false statement depending on thier condition.
syntax:
if(condition)
{
statement 1;
stmt 2;
}
else
{
statement 1;
stmt 2;
}
Wednesday, July 7, 2010
Thursday, July 1, 2010
Sample Program Using Class and Objects
I will create a program using classes and objects. I Used two class name which are MainProgram and Message.
In class name MainProgram, the user will input thier name, and this input data will pass to class Message to print.
Program Code:
import java.io.*;
import java.util.Scanner;
public class MainProgram
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
String name="";
System.out.print("Enter your name:");
input=name.nextLine();
//calling the class
//create an object to call the method in class Message
Message x = new Message();
x.printing(name);
}
}
Class Program:
class Message
{
public void printing(String nm)
{
System.out.print("Your name is:" + nm);
}
}
If you run this program you go to the MainProgram . The mainProgram will process all class program.
Output:
Your name is: Jaypee
In class name MainProgram, the user will input thier name, and this input data will pass to class Message to print.
Program Code:
import java.io.*;
import java.util.Scanner;
public class MainProgram
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
String name="";
System.out.print("Enter your name:");
input=name.nextLine();
//calling the class
//create an object to call the method in class Message
Message x = new Message();
x.printing(name);
}
}
Class Program:
class Message
{
public void printing(String nm)
{
System.out.print("Your name is:" + nm);
}
}
If you run this program you go to the MainProgram . The mainProgram will process all class program.
Output:
Your name is: Jaypee
Compute two numbers using java
this program will compute two numbers and get the sum, difference, product and quotient. x=5, y=6.
program code:
import java.io.*;
public class Computation
{
public static void main(String args[])
{
//Declaration
int x=5;
int y=6;
int sum=0;
int diff=0;
int prod=0;
int quo=0;
//computation
sum= x + y;
diff= x - y;
prod= x * y;
quo = x / y;
//Display result
System.out.println("The sum is:" + sum);
System.out.println("The diff is: + diff);
System.out.println("The prod is: + prod);
System.out.print("The Quo is: + quo);
}
}
program code:
import java.io.*;
public class Computation
{
public static void main(String args[])
{
//Declaration
int x=5;
int y=6;
int sum=0;
int diff=0;
int prod=0;
int quo=0;
//computation
sum= x + y;
diff= x - y;
prod= x * y;
quo = x / y;
//Display result
System.out.println("The sum is:" + sum);
System.out.println("The diff is: + diff);
System.out.println("The prod is: + prod);
System.out.print("The Quo is: + quo);
}
}
Tuesday, June 29, 2010
Add two numbers using java
This program will compute two numbers and get the sum.
a=90, c=80
program code:
import java.io.*;
public class compute
{
public static void main(String args[])
{
//Declaration of variables
int a=90;
int b=80;
int sum=0;
//compute the sum
sum=a+b;
System.out.print("The sum is" + sum);
}
}
a=90, c=80
program code:
import java.io.*;
public class compute
{
public static void main(String args[])
{
//Declaration of variables
int a=90;
int b=80;
int sum=0;
//compute the sum
sum=a+b;
System.out.print("The sum is" + sum);
}
}
Friday, June 25, 2010
Encapsulation in Java
Encapsulation - refers to the hiding of data and methods within an objects. and also protects data from corruption.
- easier to modify programs since one object type is modified at a time.
- easier to modify programs since one object type is modified at a time.
Labels:
encapsulation,
Encapsulation in java
Abstraction in java
Abstraction refers to the ability to make a class abstract in OOP.
An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.
An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.
Labels:
abstract in java,
Abstraction in java,
abstration
Thursday, June 24, 2010
Creating an object in java program
Creating an object in java program
class Puppy{
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Your Name is :" + name );
}public static void main(String []args){
// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "Jaypee" );
}
}sample output:
Your Name is : Jaypee
Subscribe to:
Posts (Atom)