EXAMPLE-1
/* A program on instance variables.
Call this file
Demo1.java
*/
class B {
double a;
double b;
double c;
}
// This class declares an object of type B
class Demo1 {
public static void
main(String args[]) {
B bb = new B ();
double v;
// assign values
to b instance variables
bb.a= 10;
bb.b = 20;
bb.c = 15;
// compute
v= bb.a *
bb.b * bb.c;
System.out.println("v value" + v);
}
}
OUTPUT
C:\SATYA>javac Demo1.java
C:\SATYA>java Demo1
C:\satya>
V value is 3000.0
C:\satya>
EXAMPLE-2
// This program declares two B objects.
class B {
double a;
double b;
double c;
}
class Demo2 {
public static void
main(String args[]) {
B bb1 = new B
();
B bb2 = new B
();
double v;
// assign values
to bb1's instance variables
bb1.a= 10;
bb1.b = 20;
bb1.c = 15;
/* assign
different values to bb2 instance
variables */
bb2.a= 1;
bb2.b = 2;
bb2.c = 1;
// compute v
firstbb1
v= bb1.a *
bb1.b * bb1.c;
;
System.out.println("V is " + v);
// compute volume
of second bb2
v= bb2.a *
bb2.b * bb2.c;
System.out.println("V is " + v);
}
}
/* Here, B uses a
constructor to initialize */
class B {
double a;
double b;
double c;
// This is the
constructor for B.
B() {
System.out.println("Constructing
B");
a = 10;
b = 10;
c = 10;
}
// compute and
return v
double v() {
return a *b *c;
}
}
class Demo6 {
public static void
main(String args[]) {
// declare,
allocate, and initialize Box objects
B bb1 = new B ();
B bb2 = new B
();
double v;
// get v of first
B
v = bb1.v();
System.out.println("V is " + v);
// get v of second
B
v = bb2.v();
System.out.println("V is " + v);
}
}
OUTPUT
C:\SATYA>javac Demo2.java
C:\SATYA>java Demo2
C:\satya>
V value is 3000.0
V is 3.0
EXAMPLE-3
// This program includes a method inside the B class.
class B {
double a;
double b;
double c;
// display v of a B
void v () {
System.out.print("V is ");
System.out.println(a*
b * c);
}
}
class Demo3 {
public static void
main(String args[]) {
B bb1 = new B
();
B bb2 = new B
();
// assign values
to bb1s instance variables
bb1.a= 10;
bb1.b = 20;
bb1.c = 10;
/* assign
different values to bb2
instance
variables */
bb2.a= 1;
bb2.b = 2;
bb2.c = 1;
// display vof
first B
bb1.v();
// display v of
second B
bb2.v();
}
}
OUTPUT
C:\SATYA>javac Demo3.java
C:\SATYA>java Demo3
C:\satya>
V is 2000.0
V is 2.0
EXAMPLE-4
// Now, v () returns the v value of a B.
class B {
double a;
double b;
double c;
// compute and
return v
double v () {
return a * b * c;
}
}
class Demo4 {
public static void
main(String args[]) {
B bb1 = new B
();
B bb2 = new B
();
double v;
// assign values
to bb1's instance variables
bb1.a= 10;
bb1.b = 20;
bb1.c = 10;
/* assign
different values to bb2
instance
variables */
bb2.a= 1;
bb2.b = 2;
bb2.c = 3;
// get v of first
B
v = bb1.v();
System.out.println("V is " + v);
// get v of second
B
v = bb2.v();
System.out.println("V is " + v);
}
}
OUTPUT
C:\SATYA>javac Demo4.java
C:\SATYA>java Demo4
C:\satya>
V is 2000.o
V is 6.0
EXAMPLE-5
// This program uses a parameterized method.
class B {
double a;
double b;
double c;
// compute and
return v
double v () {
return a * b * c;
}
// sets dimensions
of B
Void D(double w,
double h, double d) {
a = w;
b = h;
c = d;
}
}
class Demo5 {
public static void
main(String args[]) {
B bb1 = new B ();
B bb2 = new B
();
double v;
// initialize each
B
bb1.D(10, 20, 15);
bb2.D(3, 6,
9);
// get v of first
B
v = bb1.v();
System.out.println("V is " + v);
// get v of second
B
v = bb2.v();
System.out.println("V is " + v);
}
}
OUTPUT
C:\SATYA>javac Demo5.java
C:\SATYA>java Demo5
C:\satya>
V is 30000.0
V is 162.0
EXAMPLE-6
/* Here, B uses a
constructor to initialize */
class B {
double a;
double b;
double c;
// This is the
constructor for B.
B() {
System.out.println("Constructing B");
a = 10;
b = 10;
c = 10;
}
// compute and
return v
double v() {
return a *b *c;
}
}
class Demo6 {
public static void
main(String args[]) {
// declare,
allocate, and initialize Box objects
B bb1 = new B ();
B bb2 = new B
();
double v;
// get v of first
B
v = bb1.v();
System.out.println("V
is " + v);
// get v of second
B
v = bb2.v();
System.out.println("V is " + v);
}
}
OUTPUT
C:\SATYA>javac Demo6.java
C:\SATYA>java Demo6
C:\satya>
Constructing B
Constructing B
V is 1000.0
V is 1000.0
EXAMPLE-7
/* Here, B uses a parameterized constructor to initialize */
class B {
double a;
double b;
double c;
// This is the
constructor for B.
B(double w, double
h, double d) {
a = w;
b = h;
c = d;
}
// compute and
return v
double v() {
return a * b * c;
}
}
class Demo7 {
public static void
main(String args[]) {
// declare,
allocate, and initialize B objects
B bb1 = new B(10,
20, 15);
B bb2 = new B(3,
6, 9);
double v;
// get v of first
B
v = bb1.v();
System.out.println("Vis " + v);
// get v of second
B
v = bb2.v();
System.out.println("V is " + v);
}
}
OUTPUT
C:\SATYA>javac Demo7.java
C:\SATYA>java Demo7
C:\satya>
V is 3000.0
V is 162.0
EXAMPLE-8
// This class defines an integer stack that can hold 10
values.
class Stack {
int stck[] = new
int[10];
int tos;
// Initialize
top-of-stack
Stack() {
tos = -1;
}
// Push an item onto
the stack
void push(int item)
{
if(tos==9)
System.out.println("Stack is full.");
else
stck[++tos] =
item;
}
// Pop an item from
the stack
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return
stck[tos--];
}
}
class TestStackDemo {
public static void
main(String args[]) {
Stack st1 = new
Stack();
Stack st2 = new
Stack();
// push some
numbers onto the stack
for(int i=0;
i<10; i++) st1.push(i);
for(int i=10;
i<20; i++) st2.push(i);
// pop those
numbers off the stack
System.out.println("Stack
in mystack1:");
for(int i=0;
i<10; i++)
System.out.println(st1.pop());
System.out.println("Stack in mystack2:");
for(int i=0;
i<10; i++)
System.out.println(st2.pop());
}
}
OUTPUT
C:\SATYA>javac TestStackDemo.java
C:\SATYA>java TestStackDemo
C:\satya>
Stack in my stack1:
9
8
7
6
5
4
3
2
1
0
Stack in my stack2:
19
18
17
16
15
14
13
12
11
10
EXAMPLE-9
/* This program demonstrates the difference between public and private */
class Access {
int a; // default
access
public int b; //
public access
private int c; //
private access
// methods to access
c
void setc(int i) {
// set c's value
c = i;
}
int getc() { // get
c's value
return c;
}
}
class AccessTestdemo {
public static void
main(String args[]) {
Access ob = new
Access();
// These are OK, a
and b may be accessed directly
ob.a = 10;
ob.b = 20;
// This is not OK
and will cause an error
// ob.c = 100; //
Error!
// You must access
c through its methods
ob.setc(100); //
OK
System.out.println("a, b, and c: " + ob.a + " " +
ob.b + " " + ob.getc());
}
}
OUTPUT
C:\SATYA>javac AccessTestDemo.java
C:\SATYA>java AccessTestDemo
C:\satya>
a,b and c:10 20 100
EXAMPLE-10
// Objects are passed by reference.
class TestArg {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class CallByRefDemo {
public static void
main(String args[]) {
TestArg ob = new TestArg(10, 20);
System.out.println("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}}
OUTPUT
C:\SATYA>javac CallByRefDemo.java
C:\SATYA>java CallByRefDemo
C:\satya>
ob.a and ob.b
beforecall: 10 20
Ob.a and ob.b
aftercall: 20 10
EXAMPLE-11
// Simple Types are passed by value.
class CallByValue{
void meth(int i, int
j) {
i *= 2;
j /= 2;
}
}
class CallByValueDemo {
public static void
main(String args[]) {
CallByValue ob =
new CallByValue ();
int a = 150, b =
200;
System.out.println("a and b before call: " +
a + " " + b);
ob.meth(a, b);
System.out.println("a and b after call: " +
a + " " + b);
}
}
OUTPUT
C:\SATYA>javac InnerClassDemo2.java
C:\SATYA>java InnerclassDemo2
C:\satya>
a and b before call:150
200
A and b after
call:200 150
EXAMPLE-12
// Display all command line arguments.
class CommandLineDemo {
public static void
main(String args[]) {
for(int i=0;
i<args.length; i++)
System.out.println("args[" + i + "]: " +
args[i]);
}
}
OUTPUT
C:\SATYA>javac CallByValueDemo.java
C:\SATYA>java CallByValueDemo
C:\satya>
EXAMPLE-13
// A simple example of recursion.
class Fact {
// this is a
recusive function
int fact(int n) {
int result;
if(n==1) return 1;
result = fact(n-1)
* n;
return result;
}
}
class RecursionDemo {
public static void
main(String args[]) {
Fact f = new
Fact();
System.out.println("Factorial of 3 is " + f.fact(3));
System.out.println("Factorial of 4 is " + f.fact(4));
System.out.println("Factorial
of 5 is " + f.fact(5));
}
}
// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for
one integer parameter.
void test(int a1) {
System.out.println("a1: " + a1);
}
// Overload test for
two integer parameters.
void test(int a, int
b) {
System.out.println("a1 and b1: " + a 1+ " " + b1);
}
// overload test for
a double parameter
double test(double
a1) {
System.out.println("double a: " + a1);
return a1*a1;
}
}
class Overload {
public static void
main(String args[]) {
OverloadDemo ob =
new OverloadDemo();
double result;
// call all
versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result =
ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);
}
}
OUTPUT
C:\SATYA>javac RecursionDemo.java
C:\SATYA>java RecursionDemo
C:\satya>
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
EXAMPLE-14
// Demonstrate an inner class.
class Out {
int outer_x = 10;
void test() {
Inn inner = new Inn ();
inner.display();
}
// this is an innner
class
class Inn {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
}
class InnerClassDemo {
public static void
main(String args[]) {
Out outer = new
Out();
outer.test();
}
}
OUTPUT
C:\SATYA>javac InnerClassDemo.java
C:\SATYA>java InnerClassDemo
C:\satya>
Display:outer_x=10
EXAMPLE-15
// This program will not compile.
class Out {
int outer_x = 1000;
void test() {
Inner inner = new
Inner();
inner.display();
}
// this is an innner
class
class Inn {
int y = 100; // y
is local to Inner
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
void showy() {
System.out.println(y); // error, y not known here!
}
}
class InnerClassDemo1 {
public static void
main(String args[]) {
Out outer = new
Out();
outer.test();
}
}
OUTPUT
C:\SATYA>javac InnerClassDemo1.java
C:\SATYA>java InnerclassDemo1
C:\satya>
Display:outer_x=1000
Error,y not known here!
EXAMPLE-16
// Define an inner class within a for loop.
class Out {
int outer_x = 10;
void test() {
for(int i=0;
i<10; i++) {
class Inn {
void display()
{
System.out.println("display: outer_x = " + outer_x);
}
}
Inn inner = new Inn ();
inner.display();
}
}
}
class InnerClassDemo2 {
public static void
main(String args[]) {
Out outer = new
Out();
outer.test();
}
}
OUTPUT
C:\SATYA>javac InnerClassDemo2.java
C:\SATYA>java InnerclassDemo2
C:\satya>
Display:over_x=10
Display:over_x=10
Display:over_x=10
Display:over_x=10
Display:over_x=10
Display:over_x=10
Display:over_x=10
Display:over_x=10
Display:over_x=10
Display:over_x=10
EXAMPLE-17
// This program demonstrates the length array member.
class LengthDemo {
public static void
main(String args[]) {
int a1[] = new
int[10];
int a2[] = {3, 5,
7, 1, 8, 99, 44, -10};
int a3[] = {4, 3,
2, 1};
System.out.println("length of a1 is " + a1.length);
System.out.println("length of a2 is " + a2.length);
System.out.println("length of a3 is " + a3.length);
}
}
OUTPUT
C:\SATYA>javac LengthDemo.java
C:\SATYA>java LengthDemo
C:\satya>
EXAMPLE-18
// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for
one integer parameter.
void test(int a1) {
System.out.println("a1: " + a1);
}
// Overload test for
two integer parameters.
void test(int a, int
b) {
System.out.println("a1 and b1: " + a 1+ " " + b1);
}
// overload test for
a double parameter
double test(double
a1) {
System.out.println("double a: " + a1);
return a1*a1;
}
}
class Overload {
public static void
main(String args[]) {
OverloadDemo ob =
new OverloadDemo();
double result;
// call all
versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);
}
}
OUTPUT
C:\SATYA>javac InnerClassDemo2.java
C:\SATYA>java InnerclassDemo2
C:\satya>
EXAMPLE-19
// Automatic type conversions apply to overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for
two integer parameters.
void test(int a1,
int b1) {
System.out.println("a1 and b1: " + a1 + " " + b1);
}
// overload test for
a double parameter and return type
void test(double a1)
{
System.out.println("Inside test(double) a1: " + a1);
}
}
class Overload1 {
public static void
main(String args[]) {
OverloadDemo ob =
new OverloadDemo();
int i = 88;
ob.test();
ob.test(10, 20);
ob.test(i); //
this will invoke test(double)
ob.test(123.2); //
this will invoke test(double)
}
}
OUTPUT
C:\SATYA>javac InnerClassDemo2.java
C:\SATYA>java InnerclassDemo2
C:\satya>
EXAMPLE-20
/* Here, B defines three constructors to initialize
class B {
double a;
double b;
double c;
// constructor used
when all dimensions specified
Box(double w, double
h, double d) {
a = w;
b = h;
c = d;
}
// constructor used
when no dimensions specified
B() {
a = -1; // use -1 to indicate
b = -1; // an
uninitialized
c = -1; // box
}
// constructor used
when cube is created
B(double l) {
a = b = c = l;
}
// compute and
return volume
double v() {
return a * b * c;
}
}
class OverloadConsDemo {
public static void
main(String args[]) {
// create B using
the various constructors
B aa = new B(10,
20, 15);
B bb = new B();
B cc = new B(7);
double v;
// get v of first
B
v = aa.v();
System.out.println("V of aa
is " + v);
// get v of second B
v = bb.v();
System.out.println("V of bb is " + v);
// get volume of
cube
v = cc.v();
System.out.println("V of cc is " + v);
}
}
No comments:
Post a Comment