Tuesday, November 10, 2015

Object in Java

OBJECT:
                        In order to store the data for the data members of the class, we must create an object.

1. Instance (instance is a mechanism of allocating sufficient amount of memory space for data
members of a class) of a class is known as an object.

2. Class variable is known as an object.

3. Grouped item (grouped item is a variable which allows us to store more than one value) is
known as an object.

4. Value form of a class is known as an object.

5. Blue print of a class is known as an object.

6. Logical runtime entity is known as an object.

7. Real world entities are called as objects.

Class In Java

QUE- What is Class in Java Programming

CLASS: “A class is a way of binding the data and associated methods in a single unit
Any JAVA program if we want to develop then that should be developed with respective
class only i.e., without class
In object oriented programming’s
member methods and non-member methods
• A member method is one which is comes under the scope of the
only member methods
• Non-member methods
does not allow non-member methods
Class diagram for defining a class
Syntax for defining a CLASS:
Class <clsname>
{
Variable declaration;
Methods definition;
};
Here, class is a keyword
Clsname represents a JAVA valid variable name and it is treated as name of the
are used for creating objects.
Class contains two parts namely
declaration represents what type of
definition represents the type of
operation.
By making use of the variables
must be defined with in the class
Example: Define a class called a student..?
Answer:
Class student
{
Int stno;
String stname;
J2SE (Core JAVA) Notes
there is no JAVA program.
programming’s, generally we write two types of
methods.
class
methods.
are those which are not comes under the scope of the
at all.
class:
eclaration;
efinition;
which is used for developing or creating user defined datatypes
variable declaration and method definitions
data members which we use as a part of the
methods which we used as the path of the
variables, which are declared inside the class? Every operation in
only i.e., outside definition is not possible

unit”.
, methods. They are
class. In JAVA we use

class. JAVA



Syntax for defining a CLASS:

Class <clsname>
{
Variable declaration;
Methods definition;
};
Here, class is a keyword
Clsname represents a JAVA valid variable name and it is treated as name of the
are used for creating objects.
BASIC CONCEPTS OF JAVA

Java is OBJECT ORIENTED PROGRAMMING

In an IT we have two types of programming models (pa
procedure oriented programming language
If we represent the data using
security for the data which we represent
language using structures concept, the student data can be accessed by all the functions which we
write as a part of C program. If one of the functions manipulates or damages the data then we are
loosing correction-less (integrity) of the data.
languages are FORTRON, COBOL, PASCAL, BASIC, C, etc.
When we represent the data in
Examples of object oriented programming languages
COBOL, OBJECT PASCAL, Cpp, JAVA

programming language it has to satisfy 8 principles of
OOPs Principles:

1. Class.

2. Object.

3. Data Abstraction.

4. Data Encapsulation.

5. Inheritance.

6. Polymorphism.

7. Dynamic Binding.

8. Message Passing.

J2SE (Core JAVA) Notes
and these kinds of applications are preferred by
language:
paradigms) are available. They are
and object oriented programming language
procedural oriented programming languages
represent. For example when we represent the data of a student in C
Examples of procedure oriented programming
object oriented programming language
are LISP, ADA, ALGOL, SMALLTALK, OBJECT
JAVA, DOT NET, etc. In order to say any language is an
OOPs.

Java Codes

Que- How to create a sells details using java codes

import java.awt.*;smpcprogram.blogspot.in
import java.awt.event.*;
public class Sales extends Frame implements ItemListener
{
Frame f;
Label l1,l2,l3,l4,l5,l6,l7,l8;
TextField t1,t2,t3,t4,t5,t6,t7;
Checkbox cb1,cb2,cb3;
CheckboxGroup cbg;
public Sales()
{
l1=new Label("Item code");
l2=new Label("Item name");
l3=new Label("Price");
l4=new Label("Quantity");
l5=new Label("Amount");
l6=new Label("Discount");
l7=new Label("Discount amount");
l8=new Label("Payable amount");

t1=new TextField(5);
t2=new TextField(15);
t3=new TextField(5);
t4=new TextField(5);
t5=new TextField(5);
t6=new TextField(5);
t7=new TextField(10);

f=new Frame("Sales report");
cbg=new CheckboxGroup();
cb1=new Checkbox("5",cbg,false);
cb2=new Checkbox("10",cbg,false);
cb3=new Checkbox("15",cbg,false);
f.setSize(600,600);
f.setVisible(true);
f.setBackground(Color.pink);

f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(l3);
f.add(t3);
f.add(l4);
f.add(t4);
f.add(l5);
f.add(t5);
f.add(l6);
f.add(cb1);
f.add(cb2);
f.add(cb3);
f.add(l7);
f.add(t6);
f.add(l8);
f.add(t7);
f.setLayout(null);
cb1.addItemListener(this);
cb2.addItemListener(this);
cb3.addItemListener(this);
l1.setBounds(200,100,70,25);
t1.setBounds(275,100,70,25);
l2.setBounds(200,150,70,25);
t2.setBounds(275,150,70,25);
l3.setBounds(200,200,70,25);
t3.setBounds(275,200,70,25);
l4.setBounds(200,250,70,25);
t4.setBounds(275,250,70,25);
l5.setBounds(200,300,70,25);
t5.setBounds(275,300,70,25);
l6.setBounds(200,350,50,25);
cb1.setBounds(310,350,40,15);
cb2.setBounds(360,350,40,15);
cb3.setBounds(410,350,40,15);
l7.setBounds(200,380,70,25);
t6.setBounds(275,380,70,25);
l8.setBounds(200,425,50,25);
t7.setBounds(275,425,50,25);
}
public void itemStateChanged(ItemEvent ie)
{
String s1=t3.getText();
String s2=t4.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=a*b;
String s3=String.valueOf(c);
t5.setText(s3);

if(ie.getItemSelectable()==cb1)
{
int d=c/20;
String s4=String.valueOf(d);
t6.setText(s4);
int e=c-d;
String s5=String.valueOf(e);
t7.setText(s5);
}

if(ie.getItemSelectable()==cb2)
{
int d=c/10;
String s4=String.valueOf(d);
t6.setText(s4);
int e=c-d;
String s5=String.valueOf(e);
t7.setText(s5);
}

if(ie.getItemSelectable()==cb3)
{
int d=(3*c)/20;
String s4=String.valueOf(d);
t6.setText(s4);
int e=c-d;
String s5=String.valueOf(e);
t7.setText(s5);
}
}
public static void main(String args[])
{
Sales ob=new Sales();
}
}






































Coding for calculator using Java codes

import java.awt.*;
import java.awt.event.*;
public class Calc extends Frame implements ActionListener
{
Frame f;
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1,b2,b3,b4,b5;
public Calc()
{
 l1=new Label("Enter 1st no.");
l2=new Label("Enter 2nd no.");
l3=new Label("result");
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
b1=new Button("Add");
b2=new Button("sub");
b3=new Button("mul");
b4=new Button("div");
b5=new Button("mod");
f=new Frame();
f.setLayout(new FlowLayout());
f.setSize(600,600);
f.setBackground(Color.pink);
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(l3);
f.add(t3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s1=t1.getText();
String s2=t2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(ae.getSource()==b1)
{
c=a+b;
}
if(ae.getSource()==b2)
{
c=a-b;
}

if(ae.getSource()==b3)
{
c=a*b;
}
if(ae.getSource()==b4)
{
c=a/b;
}

if(ae.getSource()==b5)
{
c=a%b;
}
String s3=String.valueOf(c);
t3.setText(s3);
}
public static void main(String args[])
{
Calc obj=new Calc();
}
}


Applet Program (Java)

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
//<applet code="App" width=1000 height=800></applet>
public class App extends Applet implements ItemListener
{
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1,b2;
Choice ch1, ch2;
Checkbox cb,cb1,cb2;
CheckboxGroup cbg1;
Checkbox male,female;
TextArea ta;
List li;

public void init()
{
setBackground(Color.pink);
setForeground(Color.green);
setFont(new Font("Aerial",Font.BOLD,13));
}
public void start()
{
l1=new Label("First Name",Label.CENTER);

l2=new Label("Second Name",Label.CENTER);
l3=new Label("Result",Label.CENTER);
t1=new TextField(30);
t2=new TextField(30);
t3=new TextField(30);
//t1.setEchoChar('*');
//l1.setText("First Name");

b1=new Button("save");

b2=new Button("Delete");

ch1=new Choice();
ch2=new Choice();
ch1.add("India");
ch1.add("Pak");
ch1.add("Nep");


ch2.add("Bihar");
ch2.add("karanchi");
ch2.add("kathmandu");

cb=new Checkbox("Patel");
cbg1 =new CheckboxGroup ();
cb1=new Checkbox("java");
cbg1 =new CheckboxGroup ();
cb2=new Checkbox("C++");
cbg1 =new CheckboxGroup ();


male=new Checkbox("Male");
male.setCheckboxGroup (cbg1);

female=new Checkbox("Female");
female.setCheckboxGroup (cbg1);

ta=new TextArea(5,20);

li=new List();


li.addItem("Sunday");
li.addItem("Monday");
li.addItem("Tuesday");

add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
add(b2);
add(ch1);
add(ch2);
add(cb);
add(cb1);
add(cb2);
add(male);
add(female);
add(ta);

add(li);


cb.addItemListener(this);
cb1.addItemListener(this);
cb2.addItemListener(this);



}


public void itemStateChanged (ItemEvent ie)
{

if(ie.getItemSelectable ()==cb)
{
if(cb.getState())
{
t1.setText("Patel");
}
}
else
{
t1.setText(" ");
}
if(ie.getItemSelectable ()==cb1)
{
if(cb1.getState())
{
t1.setText("Java");
}
}
else
{
t1.setText(" ");
}

if(ie.getItemSelectable ()==cb2)
{
if(cb2.getState())
{
t1.setText("C++");
}
}
else
{
t1.setText(" ");
}
if(cb2.getState())
{
t1.setText("C++");
}
else
{
t1.setText(" ");
}
}
}
public static void main(String args[])
{
App obj=new App();
}



Saturday, September 19, 2015

C++(Age )

If the ages of Ram, Sulabh and Ajay are input by the user, write a program to determine the youngest of the three. 


 int main()
{
      int ram_age,sulabh_age,ajay_age;
      cout<<"Enter Ram age:";
      cin>>ram_age;
      cout<<"Enter Sulabh age:";
      cin>>sulabh_age;
      cout<<"Enter Ajay age:";
      cin>>ajay_age;
 
      if (ram_age<sulabh_age && ram_age<ajay_age)
            cout<<"Ram is youngest";
      else if(sulabh_age<ram_age && sulabh_age<ajay_age)
            cout<<"Sulabh is youngest";
      else
            cout<<"Ajay is youngest";
 
      getch();
      return 0;

}