48. // Demonstrate lifetime of a variable.
class LifeTime {
public static void main(string args[]) {
int x;
for (x = 0; x<3; x++) {
int y = -1; // y is initialized each time block is entered
system.out.println("y is :" + y); // this always prints -1
y = 100;
system.out.println("y is now : " + y);
}
}
}
: ا ن داد در ذی ﺥ وﺝ ای
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
.د ار ده -١ y ، for ه م ورود ر ،ه آ ه ن
. ده ار ﺝ ی را از د ،ا ی ن ١٠٠ار وﺝ د
از ی يه م ان ،ا در ا كه :ا و ﺥ ی
: ذی ،ل ان .آ د ی و ود هي
// This program will not compile
class scopeErr {
public static void main(string args[]) {
int bar = 1;
{ // creates a new scope
int bar = 2 // compile time error – bar already defined!
}
}
}
casting ﺕ ی و
97. ، ا expression ار ه از ﺙ ه .ا اﺝ ا ﺥ اهcase ر ند
.ا اﺥ ري اdefault ر ،ا د ( اﺝ ا ﺥ اهdefault) ض ر د
م يا دی ا وﺝ د اdefault و هcase از ه
. اه
ر را ی ن ده . ه ی د د اswitch ر داﺥ دbreak رت از
ار switchر از آ د آ ﺥ اول ی ، اﺝ ا اﺝ break ر ی د
.د ﺥ رجswitch رت آ ل از . ی ﺥ اه ،
: د ا د اswitch ر آ از د آ ه د اي را ل در زی
//A simple example of the switch.
class SampleSwitch{
public static void main ( String args[] ) {
for(int i=0; i<6; i++ )
switch(i){
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater then 3.");
}
}
}
: ار زی ﺥ وﺝ ای
i is zero.
i is one.
i is two.
98. i is three.
i is greater than 3.
i is greater than 3.
i د و case ا ﺙ آ ه را ،د ، داﺥ آ ه ری ه
ر i از ای . ا رات ی د . ، اﺝ ا ﺥ اه دا
رت ای د،و اه expression ار ا case دی ی از د،ه ٣ از
.د اﺝ اdefault
ي ادا case ، اﺝ اي فآ راbreak .ا اﺥ ري اdefault ر د
ان . دا درbreak رات ون دcase ی ا . ه ﺥ اه ی
: ی ي را در ،ل
//In a switch/ break statements are optional.
class MissingBreak{
public static void main(String args[] ){
for(int i=0; i<6; i++ )
switch(i}(
case 0:
case 1:
case 2:
case 3:
case 4:
System.out.println("i is less than 5");
break;
case 5:
case 6:
case 7:
case:٨
case 9:
System.out.println("i is less than 10");
break;
default:
System.out.println("i is 10 or more");
}
}
99. :د ار زی ﺥ اه ﺥ وﺝ ای
i is less than 5
i is less than٥
i is less than 5
i is less than 5
i is less than 5
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is 10 or more
i is 10 or more
break ر ی د ن ر ، case ه آ ، اﺝ ا ه ری ه
.د ( switch ي )ی ا
ف ل د،ا ا ﺥ اي ل ﻝ در
ن دادن اي .دارد ه ي وا زی دي در آ ر ده يbreak ر د
ل ل ط ی ع ، دو ر ای آ ر ده ي وا
زي د آ د اswitch از ی . ای روای ﺝ ی ه ن ه را
. ي را ارا ده ﺙ
//An improved version of the season program.
class Switch{
public static void main(String args[]){
int month = 4;
String season;
switch(month){
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
100. break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autumn";
break;
default:
season = "Bogus Month";
}
System.out.println("April is in the" + season + ".");
}
}
ﺕ در ﺕSwitch رات
. ی د ا ﺥ رﺝswitch ر ی د از ان switch از ی ا
ط ك آ ی switch ر د ی . از درswitch ﻝ را ای
switch ی آ در و داﺥswitch درcase ي ﺙ ،ه ﺥ دش
. ا يآ ،ل ان . اه ا ، ﺝ د ار ﺥ رﺝ
switch(count}(
case 1:
switch(target){ // nested switch
case 0:
System.out.println("target is zero”);
break;
case 1:// no conflicts with outer switch
System.out.println("target is one”);
break;
}
113. ن را ی ی ار ا مint-var ، ع Type
collection د ن ای د ا ﺥ ا . دری ا از ا ا ی
.د
: ﺝ آ ل زی
//use a for-each style for loop.
Class ForEach{
Public static void main (String args[]){
Int nums[] = {1,2,3,4,5,6,7,8,9,10};
Int su, = 0;
//use for-each style for to display and sum the values
for ( int s, nums ){
system.out.println("value is: " + x );
sum +=x;
}
system.out.println("summation : " + sum );
}
}
: رت زی ا ﺥ وﺝ
Valu is : 1
Valu is : 2
Valu is : 3
Valu is : 4
Valu is : 5
Valu is : 6
Valu is : 7
Valu is : 8
Valu is : 9
Valu is : 10
Summation : 55
ي ﺕ ار در رای ه ي چ
.د ل ای آ ر د ی
114. //use for-each style for on a two-dimensional array.
Class foreacha {
Public static void main ( String args[]){
Int sum = 0;
Int nums[][] = new int[3][5];
//give nums some values
for ( int i = 0 ; i<3 ; i++ )
for ( int j = 0 ; j<5 ; j++ )
nums[i][j] = (i+1)*(j+1);
//use for-each for to display and sum the values
for ( int x[] : nums ){
for ( int y : x )
system.out.println("value is: " + y );
sum += y;
}
}
system.out.println("Summation: ") + sum);
}
}
: رت زی ا ﺥ وﺝ
Valu is : 1
Valu is : 2
Valu is : 3
Valu is : 4
Valu is : 5
Valu is : 2
Valu is : 4
Valu is : 6
Valu is : 8
Valu is : 10
Valu is : 3
Valu is : 6
Valu is : 9
Valu is : 12
Valu is : 15
Summation : 90
127. د ز ن هي ﺥ ون را ا د، ای وي a ن
ﺝ د ن ن ﺥ ر ی .ا " ﺥ اه " ا ی
ده . ا ت را ، ار ده از ر اري دل راa وری و
ع ازC ن رای ، دد ArrayIndexOutOfBoundsException ای
. ی ب C[42] اري را آ ش ن ،ه ل1ا دارايint
: آ ه اﺝ اي ه دو را را ﻝ ﺥ وﺝ در ای
C:>java MultiCatch
a = 0
Divide by 0 :java.lang.ArithmeticException :/ by zero
After try/catch blocks.
C:>java MultiCatch TestArg
a = 1
Array index oob java.lang.ArrayIndexOutOfBoundsException :42
After try/catch blocks.
ئ يا آ زی آ ا ا ، آ د ا catch رات از د و
ر آ ی د ا ان دﻝ . ای ار هي از ه ی از آ س ی
ع را ﺥ اه ي ن ت ن ع و زی آ آ ،ا د ا آ از ی آ سcatch
ن زی آ س ی ،ه ی از آ س ی زی آ س ای ، ا .
ي را در ،ل ان . ا ﺥ س د و ، در ﺝ وا ، آ . ر
: ی
/* This program contains an error.
A subclass must come befor its superclass in
a series of catch statements .If not/
unreachable code will be created and a
comple-time error will result.
*/
class SuperSubCatch {
public static void main(String args[] ){
try {
int a = 0;
int b = 42 / a;
128. } catch(Eeception e ){
System.out.println("Generic Exception catch.");
}
/* This catch is never reached because
ArithmeticException is a subclass of Exception .*/
catch(ArithmeticException e ){ // ERROR - unreachable
System.out.println("This is never reached.");
}
}
}
ی دو آ آ دری مﺥ ،ی ی آ را آ اه ای ا
زی آ س از یArithneticException ی . از نا ر catch ر د
از ﺝException ي ﺥ هي آcatch ر د ، اوﻝ اException
اه اﺝ ا هcatch ر د دو . ی را ادارArithneticException
. ده راcatch رت دو ف آ دن ای اي ،
try رات ﺕ درﺕ ﺵ د
try ك ی ان داﺥ راtry ر ی د د.ی ان در راtry رات د
رت روي ن ،د واردtry ر رآ ی د ار داد. ه دی
، ﺥص اي ی ا catch ی ادار آ داﺥtry ر ی د د. ا
ار رد ﺝ اي یtry ي ر د ط catch و ادار آ دور زد
آ ز دوی catch رات ازد ی ای ی اوم ﻝ د . ای
ا catch رات از د ه ،ا م try رات در د آ
ﻝ را آ . در زی ئ را ادار اﺝ اي ﺝ وا ﺥ دش ا ، ا
: د ا د اtry رات در آ از د ه
// An example of nested try statements.
class NestTry {
public static void main(String args[] ){
try {
129. int a = args.length;
/* If no command-line args are present/
the following statement will generate
a divide-by-zero exception .*/
int b = 42 / a;
System.out.println("a = " + a);
try { // nested try block
/* If one command-line arg is used/
then a divide-by-zero exception
will be generated by the following code .*/
if(a==1 )a = a/(a-a); // division by zero
/* If two command-line args are used/
then generate an out-of-bounds exception .*/
if(a==2 ){
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e ){
System.out.println("Array index out-of-bounds :" + e)
}
} catch(ArithmeticException e ){
System.out.println("Divide by 0 :" + e);
}
}
}
رت زی . ده يﺝي را داﺥ دیtry ك آ ،ی ه ری ه
" ی ا آ ن اﺝ ا يﺥ را ون ر آ .و آر
ن نﺥ ر ی د . اﺝ اي ای دtry ك ﺥ رﺝ "
ك داﺥ ن . آ ﻝtry ك در " را از داﺥ " ی ا
ادار ﺥ اه د و در ر دادtry ك ﺥ رﺝ دا ئ را ای ا
ود رای " از داﺥ " ی ،ی ا ن اﺝ ا يﺥ را ون ر .ا
ت اي وﺝ د دار آ ه ی از . در زی اﺝ اه ي ﻝ ﺥ اهtry ك داﺥ
: ده ن ق را
130. C:>java NestTry
Divide by 0 :java.lang.ArihmeticException :/ by zero
C:>java NestTry One
a = 1
Divide by 0 :java.lang.ArihmeticException :/ by zero
C:>java NestTry One Two
a = 2
Array index out-of-bounds:
java.lang.ArrayIndexOutOfBoundsException :42
ا try رات دن د در ، روش ه وﺝ د دا اﺥ ا ا
ك ی روش را درون ی اﺥ ا ا ،ل ان . ا ق ن ی رو
درونtry ، ﻝ وﺝ د دارد . در ایtry ر دی م ده . درون ن روش ی د اtry
د . در در آ اﺥ ا آ روش راtry ك ﺥ رﺝ ن داﺥ ی روش ه
nesttry() اﺥ روشtry ك در آ در ن آ ه را زی
. نآ د ا
/* Tyr statements can be implicitly nested via
calls to methods .*/
class MethNestTry {
static void nesttry(int a ){
try { // mested try block
/* If one command-line arg is used/
then a divide-by-zero exception
will be generated by the following code .*/
if(a==1 )a = a/(a-a); // division by zero
/* If two command-line args are used/
then generate an out-of-bounds exception .*/
if(a==2 ){
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e ){
System.out.println("Array index out-of-bounds :" + e)
131. }
}
public static void main(String args[] ){
try {
int a = args.length;
/* If no command-line args are present/
the following statement will generate
a divide-by-zero exception .*/
int b = 42 / a;
System.out.println("a = " + a);
nesttry(a);
} catch(ArithmeticException e ){
System.out.println("Divide by 0 :" + e);
}
}
}
. ا ل ه ن ﺥ وﺝ
Throw
را دی ای . ا ب اﺝ اي ﺝ وا آ ا ل
. ی ب throw ر د از د ا ی ر ئ را ا ی ا
: ار زی throw
ThrowThrowableInStance;
زی آ س از ی یthrowable ع از ی یThrowable Instance در ای
throwableاز ي نآ ، هchar یint ، د . ا اع throwable
وردن ی اي . دو د تا ان ا ا وobject وstring
ئﺝ ی و ی ای د یcatch داﺥ ﺝ را د از ی وﺝ د دارد : اthrowable
ي رات د.د throw ر از د ﺝ ی ن اﺝ اnew .
catchر د ی د ار رد ﺝ try ك ی . دی اه اﺝ ا
178. ی ي ، زی ا ا ن ای د ا ی هي از وراﺙ را ی
زی آ آ س ی ا ، د از وراﺙ ا ورد . را ﺝ د ا
ا ا ی . ای آ س ی را ا م كی ي وی
د ﺥ دش ه ی را آ و ه آ س ارث رث د ی آ
د را آ س د رث آ ﺝ وا ، آ ی . در روش ن ا
را زی آ س ارث ي را ا م داد و ارث د ا آ .آ )(superclass
از ی " و ای ، ی " زی آ س " روای . )(subclass
آ س ي و رو هي . زی آ س ، آ "ا آ س
آ . ا د ﺥ د را را رث د و
وراﺙ
د از واژ آ ي ی آ س را ا ی د آ دن از ی آ س ، ﺥ اي ارث
ن د اي را ل ، ای آ اي ار ده . ي extendsدر آ س دی
آ .د Bای د م م Aو ی زی آ س يی آ س ده .
د. ی زی آ س از Aای د د از واژ آ ي extendsا آ آ
.// A simple example of inheritance
.// Create a superclass
{ class A
;int i, j
{() void showij
;)System.out.println("i and j :" + i + " " + j
}
}
.// Create a subclass by extending class A
{ class B extends A
;int k
{() void showk
;)System.out.println("k :" + k
}
179. void sum )({
System.out.println("j+j+k :" +( i+j+k));
}
}
class SimpleInheritance {
public static void main(String args[] ){
A superOb = new A)(;
B subOb = new B)(;
// The superclass may be used by itself.
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb :");
superOb.showij)(;
System.out.println)(;
/* The subclass has access to all public members of
its superclass .*/
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb :");
subOb.showj)(;
subOb.showk)(;
System.out.println)(;
System.out.println("Sum of i/ j and k in subOb:");
subOb.sum)(;
}
}
: ار زی ، ﺥ وﺝ ای
Contents of superOb:
i and j :10 20
Contents of subOb:
i and j :7 8
k :9
187. ده . ای م و اd وh ،w ه ي را راsuper() اﺥ ا Boxweight() در ای
width دی از ای د ا Box() ز ن اﺥ ا آ ر
دی اوﻝ را ﺥ دش ایBoxweight آ . دی ار ده اوﻝ و راdepth،وheight ،
ی ار ده اوﻝ راweight د ﺥ د ار ا آ . آ ار ده
. زد private دی را ی ای رت ارد در را زادBox . ای
ا ن ز ن د.ا اﺥ ا ن ر super() ، ل در
اﺥ ا آ س ی د از ه را اsuper() ان ، ا
ان . دا ر آ ا د،ه اي آ اﺝ ا ز .د
ق اي ن را ز وﺝ د دارد آBoxweightاز زي آ د ی ل ، در ای
د از ا super() ﻝ ی . در ه اه box نی ﺥ نو
Box و داﺥdepth وwidth , height آ آ د. د اﺥ ا ی ي ر
. ا در رت اﺥ
// A complete implementation of BoxWeight.
class Box {
private double width;
private double heght;
private double deoth;
// construct clone of an object
Box(Box ob ){ // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d ){
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box (){
width =- 1; // use- 1 to indicate
188. height =- 1; // an uninitialized
depth =- 1; // box
}
// constructor used when cube is created
Box(double len ){
width = height = depth = len;
}
// compute and return volume
double volume (){
return width * height * depth;
}
}
// BoxWeight now fully implements all construtors.
class BoxWeight extends Box {
double weight; // weight of box
// construct clone of an object
BoxWeight(BoxWeight ob ){ // pass object to consructor
super(ob);
weight = ob.weight;
}
// constructor used when all parameters are specified
Box(double w, double h, double d, double m ){
super(w, h, d); // call superclass constructor
weight = m;
}
// default constructor
BoxWeight (){
super();
weight =- 1;
}
// constructor used when cube is created
BoxWeight(double len, double m ){
super(len);
weight = m;
}
}
class DemoSuper {
public static void main(String args[] ){
189. BoxWeight mybox1 = new BoxWeight(10 .20 .15 .34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
BoxWeight mybox3 = new BoxWeight(); // default
BoxWeight mycube = new BoxWeight(3, 2);
BoxWeight myclone = new BoxWeight(mybox1);
double vol;
vol = mybox1.vilume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.vilume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
System.out.println();
vol = mybox3.vilume();
System.out.println("Volume of mybox3 is " + vol);
System.out.println("Weight of mybox3 is " + mybox3.weight);
System.out.println();
vol = myclone.vilume();
System.out.println("Volume of myclone is " + vol);
System.out.println("Weight of myclone is " + myclone.weight);
System.out.println();
vol = mycube.vilume();
System.out.println("Volume of mycube is " + vol);
System.out.println("Weight of mycube is " + mycube.weight);
System.out.println();
}
}
: آ ﺥ وﺝ زی را ﻝ ای
Volume of mybox1 is 3000
Weight of mybox1 is 34.3
Volume of mybox2 is 24
Weight of mybox2 is 0.076
Volume of mybox3 is- 1
191. Super .member
super . ای دو روش ی ی ی ا member ، در ای
را در ه نا ئ ی زی آ س ، ا ا آ ر د دارد آ در ن ا ای اي
: ی د آ س را در ا ز . ای آ س
// Using super to overcome name hiding.
class A {
int i;
}
// Create a subclass by extending class A.
class B extends A {
int i; // this i hides the in A
B(int a, int b ){
super.i = a; // i in A
i = b; // i in B
}
void show )({
System.out.println("i in superclass :" + super.i);
System.out.println("i in subclass :" + i);
}
}
class UseSuper {
public static void main(String args[] ){
B subOb = new B(1, 2);
subOb.show)(;
}
}
: ده ی ﺥ وﺝ زی را ای
i in superclass :1
i in subclass :2
i ند اsuper زد ، ا ن ر راA درi رB درi ا
superان از ﺥ اه دی ه ری ورد . ه ﺝ د در آ س ی
. ا ی زی آ س ی آ رو اﺥ ا اي
192. (Multilevel) اﺕ چ ای د ی
ﺝ ا .آ ﻝ ا ی وراﺙ ی زی آ ا ا
آ س لا ان . د آ ا ي ی آ س دی ان آ س آ از ی زی آ س
.و A و ی زی آ س ازBا ی زی آ س از C داC وB ،A
ه يﺥ د آ س ﺝ د در آ ي ﺥ ، ه زی آ س آ ا ا ق ای
.د و را رثA وBه ي ﺝ آC ، ای د . در ای را رث
زی آ س د ا ان ی آ س Boxweight ي ، زی آ س در
ارث راBox وBoxweight ي ﺥ آshipment . ی را ای دshipment ان
. آ اري ﻝ را ی ا آ ه ی آ نا cost م د وی
// Extend BoxWeight to include shipping costs.
// Start with Box.
class Box {
private double width;
private double height;
private double depth;
// construct clone of an object
Box(Box ob ){ // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d ){
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box )({
width =- 1; // use- 1 to indicate
193. height =- 1; // an uninitialized
depth =- 1; // box
}
// constructor used when cube is created
Box(double len ){
width = height = depth = len;
}
// compute and return volume
double volume(){
return width * height * depth;
}
}
// Add weight.
class BoxWeight extends Box {
double weight; // weight of box
// construct clone of an object
BoxWeight(BoxWeight ob ){ // pass object to constructor
super(ob);
weight = ob.weight;
}
// constructor used when all parameters are specified
BoxWeight(double w, double h, double d, double m ){
super(w, h, d); // call superclass constructor
weight = m;
}
// default constructor
BoxWeight (){
super)(;
weight =- 1;
}
// constructor used when cube is created
BoxWeight(double len, double m ){
super(len);
weight = m;
}
}
// Add shipping costs
class Shipment extends BoxWeight {
194. double cost;
// construct clone of an object
Shipment(Shipment ob ){ // pass object to constructor
super(ob);
cost = ob.cost;
}
// constructor used when all parameters are specified
BoxWeight(double w, double h, double d, double m, double c ){
super(w, h, d); // call superclass constructor
cost = c;
}
// default constructor
Shipment (){
super();
cost =- 1;
}
// constructor used when cube is created
BoxWeight(double len, double m, double c ){
super(len, m);
cost = c;
}
}
class DemoShipment {
public static void main(String args[] ){
Shipment shipment1 = new Shipment(10, 20, 15, 10, 3.41);
Shipment shipment2 = new Shipment(2, 3, 4, 0.76, 1.28);
double vol;
vol = shipment1.volume();
System.out.println("Volume of shipment1 is " + vol);
System.out.println("Weight of shipment1 is " + shipment1.weight);
System.out.println("Shipping cost :$" + shipment1.cost);
System.out.println();
vol = shipment2.volume();
System.out.println("Volume of shipment2 is " + vol);
System.out.println("Weight of shipment2 is " + shipment2.weight);
System.out.println("Shipping cost :$" + shipment2.cost);
}
}
196. زی آ س ز آ در ی ري د ی اوﻝsuper() ن و . اﺥ ا
د.ا دی د اsuper() د ، ﺥ ا ر ه د ، ای اﺝ ا
ه ی از زی آ ون را ز ضی ز د د اsuper()
: ن اﺝ ا ز ز ده آ ن ي . اﺝ ا ﺥ اه
// Demonstrate when constructors are called.
// Create a super class.
class A {
A (){
System.out.println("Inside A's constructor.")
}
}
// Create a subclass by extending class A.
class B extends A {
B (){
System.out.println("Inside B's constructor.")
}
}
// Create another subclass by extending B.
class C extends B {
C (){
System.out.println("Inside C's constructor.")
}
}
class CallingCons {
public static void main(String args[] ){
C c = new C();
}
}
: ح زی ﺥ وﺝ ای
Inside A's constructor
Inside B's constructor
Inside C's constructor
197. در ر ن .ا اﺥ ا ن ن ز آ ، ه ری ه
نی آ س . ن اﺝ ا ز ا آ ، آ
ز ن اي اﺝ ا آ ار ده اوﻝ ارد ، ه ه يﺥ د زی آ
زی آ س د م ار ده اوﻝ ا زه ، ﺝ ا از و ا دا
د. م ای ، ی اول ای آ ر ا و
ه Overrideآ دن
ﺝ د ع ی روش ی روش در ی زی آ س ه ن م و آ س،و ا در ی
ﺝ د در ن روش در زی آ س ، روش ی ، ي ﺥ د را دا در آ س
از داﺥ ی ی روش ﻝ ی .و ي وي ن ﺝ د ی از را ﻝ آ س
، ی زی آ س از ن روش آ روای د ، ه ار اﺥ ا زی آ س
رد . ن ﺥ اه د ، ی از ه ن روش آ آ س د و روای ارﺝ ع ﺥ اه
ی : زی را در
.// Method overriding
{ class A
;int i, j
{) A(int a, int b
;i = a
;j = b
}
// display i and j
{)( void show
;)System.out.println("i and j :" + i + " " + j
}
}
{ class B extends A
;int k
{) B(int a, int b, int c
;)super(a, b
;k = c
198. }
// display k -- this overrides show )(in A
void show (){
System.out.println("k :" + k);
}
}
class Override {
public static void main(String args[] ){
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show )(in B
}
}
: ار زی ای ﻝ
K : 3
B آ داﺥshow از د ، روای اﺥ ا Bع ئ از روي یshow() و
ن ، روای اB داﺥshow() آ ، روای د. ی ار د رد ا ی
. آ را ﻝA در
، ای آ ر را دا د ﻝ يی آ س روای ﺥ اه ا
show() ي آ س روایB از ل ، در ای روای ان . م ده اsuper د از ا
اﺝ ز هي آ . ای ا ﺥ اه اﺥ ا زی آ س ط داﺥ روای
. در ی ی ده
class B extends A {
int k;
B(int a, int b, int c ){
super(a, b);
k = c;
}
void show (){
super.show(); // this calls A's show)(
System.out.println("k :" + k);
}
}
199. : آ ه ی ، ﺥ وﺝ زی را ی ﺝی را درA ای روای از ا
i and j :1 2
k:3
روش آ .ﻝ اﺥ ا راshow() ي آ س روایsuper.show()، در ای
دو روش ، .ا ن ع دو روش ی و آ ا ا ا ق ز
ل ح ل ، ای روای ا ان . ( ﺥ اهoverioaded) د ا ﺥ
:ی را در
// Methods with differing type signatures are overloaded -- not
// overridden.
class A {
int I, j;
A(int a, int b ){
i = a;
j = b;
}
// display i and j
void show (){
System.out.println("i and j :" + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
B(int a, int b, int c ){
super(a, b);
k = c;
}
// overload show)(
void show(String msg ){
System.out.println(msg + k);
}
}
class Override {
public static void main(String args[] ){
201. ی روش ﻝ در ی آ س ای ا .د ی اﺝ ا روای از روش ﻝ
ارﺝ ع آ س ی ی ئ از ا آ ا اع ز ، ی زی آ س
. ن روش اﺝ ا ﺥ اه ي روای ار رد ارﺝ ع
: ه ن ی ي روش را زی آ ه ﻝ را در ای
// Dynamic Method Dispatch
class A {
void callme (){
System.out.println("Inside A's callme method");
}
}
class B extends A {
// override callme)(
void callme (){
System.out.println("Inside B's callme method");
}
}
class C extends A {
// override callme)(
void callme (){
System.out.println("Inside C's callme method");
}
}
class Dispatch {
public static void main(String args[] ){
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
204. }
class Rectangle extends Figure {
Rectangle(double a, double b ){
super(a, b);
}
// override area for rectangle
double area (){
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b ){
super(a, b);
}
// override area for right triangle
double area (){
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class FindAreas {
public static void main(String args[] ){
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r;
System.out.println("Area is " + figref.area))(;
figref = t;
System.out.println("Area is " + figref.area))(;
figref = f;
System.out.println("Area is " + figref.area))(;
}
}
: ار زی ا ای
208. }
// area is now an abstract method
abstruct double area();
}
class Rectangle extends Figure {
Rectangle(duoble a, double b ){
super(a, b);
}
// override area for rectangle
double area (){
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b ){
super(a, B);
}
// override area for right triangle
double area (){
System.out.println("Inside Area for Teriangle.");
return dim1 * dim2 / 2;
}
}
class AbstractAreas {
public static void main(String args[] ){
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectanlge(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK/ no object is created
figref = r;
System.out.println("Area is " + figref.area))(;
figref = t;
System.out.println("Area is " + figref.area))(;
}
}
255. از ایindexOf() در .ﺝ ا زﺝ ای startIndex
ز startIndex از ﺝ lastIndexOf() ی .در ادا اﺥ ر ای
.د
//Demonstrate indexOf() and lastIndexOf()
Class indexofdemo{
Public static void main(String args[]){
String s=”now is the time for all good men “+
“to come to the aid of their country.”;
System.out.println(s);
System.out.println(“indexOf(t)=”+s.indexOf(‘t’));
System.out.println(“indexOf(the)=”+s.indexOf(“the”));
System.out.println(“lastIndexOf(the)=”+s.lastIndexOf(“the”));
System.out.println(“indexOf(t,10)=”+s.indexOf(‘t’,10));
System.out.println(“lastIndexOf(t,60)=”+s.lastIndexOf(‘t’,60));
System.out.println(“indexOf(the,10)=”+s.indexOf(“the”,10));
System.out.println(“lastIndexOf(the,60)=”+s.lastIndexOf(“the”,60));
}
}
: ا ن داد در زی از اﺝ ا ﺥ وﺝ
Now is the time for all good men to come to the aid of their country.
indexOf(t)=7
lastIndexOf(t)=65
indexOf(the)=7
last indexOf(the)=55
indexOf(t,10)=11
lastIndexOf(t,60)=55
indexOf(the,10)=44
lastIndexOf(the,60)=55
رﺵ ه ﺕ
ا ی ده راstring اه ی ه ی ه string ه ن
د زی ا از ه وی از ی StringBuilder یStringBuffer
256. Substring
: م دارد. م دو ی .ای اج را ا زی ر ی ا د ازای ا
String substring(int startIndex)
ا م از زی ر ای . را وع زی ر ایstartIndex
. ادا ر زو ا startIndex از ا
.زد اه را زی ر ا اوا دن ای ن ا م دوم
Strng substring(int startIndex,int endIndex)
. ی ا ا endIndex ا ا و ای startIndex
ی زی ر ی ه م دن ی ا ﺝ ی substring زی از در
.د د ا دی زی ر
//substring replacement.
Class stringreplace{
Public static void main(string args[]){
String org = "This is atest.This is,too.";
string search = "is";
string sub = "was";
string result = " ";
int I;
do {
System.out.println(org);
I = org.indexof(search);
if(I != -1)
{
result = org.substring(0,I) ;
result = result + sub;
result = result + org.substring(I + search.length());
Org = result;
}
}
While (I != -1);
}
282. . ب ا راioexeption ا
ر را ده .ای ن ل ه از را ﺥ ا ن راread() د از ذی روش ا
:د ر داد “q” ف ده ر ادا
//use a BufferedReader to read characters from the console.
Import java.io.*;
Class BRRead{
Public static void main(String args[]) throws IOExeption
{
Char c;
BufferedReader br=new BufferReader(new InputstreamReader(System.in));
System.out.println(“Enter charactors,’q’ to quit.”);
//read characters
Do{
C=(char)br.read();
System.out.println(c);
}while(c!=’q’);
}
}
: ا ن داد در ذی ا از ﺥ وﺝ
Enter charactors,’q’ to quit.
123abcq
1
2
3
A
B
C
Q
system.in ا وت دا ر رش را داری ا ا ﺥ وﺝ
و ی ﺥ ا .ای رت را ض
ا دری read() د .ای ا ار ل د ورودی زدenter
. ان ارز ل ورودی از
ا ن رﺵ ه
283. س از د اreadline()ا از ا رااز ر ا
. رت زی ا ن م . اbufferedreader
String readline() throws loexception
. اstring ان ی ه ن
ه را ن ه ای ن راreadline() وbufferedreader د زی
: را یword ده ﺥ ا و ی ی ری
//read a string from console using a bufferedreader.
Import java . io .*;
Class breadlines {
Public static void main (string args[] )
Throws ioexception
{
//create a bufferedreader using system. In
Bufferedreader br = new bufferedeader (new inputsteamreader( system.in
);
String str;
System .out. println( enter lines of text. );
system .out. println(enter `stop` to quit.``);
do {
str =br .readline();
system.out. println(str);
} while(!str.equals (``stop``);
}
}
ای دstring ه از . رای ا ا د ای د ر وی ا زی ی در
د.ی ﺥ ا ١٠٠ د. ا در رای ذﺥ وه ه ﺥ ا و
: ا د اBufferRead ل . ا ﺥ ا ن از `` را یstop`` ای
//A tiny editor
284. Import java.io.*;
Class TinyEdit{
Public static void main(String args[])throws IOException{
//creat a bufferreader using System.in
BufferedReader br = new BufferedReader(new
InputstreamReader(System.in));
String str[] = new String[100];
System.out. println(“enter lines of text.”);
System.out. println(“enter ‘stop’ to quit.”);
For(int i=0;i<100;i++){
Str[i]=br.readLine();
If(str[i].equals(“stop”))break;
}
System.out. println(“nHere is your file:”);
//display the lines
For(int i=0;i<100;i++){
If(str[i].equals(“stop”))break;
System.out. println(str[i]);
}
}
}
: ا ن داد در ذی ا از ﺥ وﺝ
Enter lines of text.
Enter ‘stop’ to quit.
This is line one.
This is line two.
Java makes working with strings easy.
Just creat String objects.
Stop
Here is your file:
This is line one.
This is line two.
Java makes working with strings easy.
Just creat String objects.
ل و ﺵ
288. رت ی ﺥ ا و ن را را از ی ی وا دی اﺥ ا ای ر ه
رت د.در دا -1 ی ا ن از ر . دا د
.د ب IOException ا ﺥ وز ه
داد ی ی ا و د ا ﺥ ا ن ورود ه ا read() زی از در
try/catch ه ر د .د ن ن در ﺥ ان ر د. م ی
ا -ﺥ ﺝ ی اﺝ ا در ا ی ی دو ﺥ ی ا
ان ه ن.از ای روی م ی در ﺥ دن ی ا ش رد ن ی
. ی د نا ﺥ نه د از ر ه ما
/*display a text file.
To use this program ,specify the name
Of the file that you want to see.
For example to see a file called TEST.TXT,
Use the following command line:
Java showFile TEST.TXT
*/
Import java.io.*;
Class showFile{
Public static void main(String args[])throws IOException{
Int I;
FileInputStream fin;
Try{
Fin = new FileInputStream(args[0]);
}catch(FileNotFoundException e){
System.out. println(“file not found”);
Return;
}catch(ArrayIndexoutOfBoundsException e){
System.out. println(“usage: showfile file”);
Return;
}
//read charactors until EOF is encountered
Do{
I=fin.read();
289. If( I != -1)System.out.print((char)i);
}while(I != -1);
Fin.close();
}
}
FileOutputStream س و write() از ا در ی ا
: رت زی ا م ن ی . د د ا ا ی
Void write(int byteval) throws IOException
ی int رت byteval د. ا در یbyteval ی
وز ه رت . در در ی را ه ا ا
دن ی ا write() ل زی از د.در ب IOException ا ﺥ
. ا د ا ی
/*copy a text file.
To use this program specify the name of
The source file and the destination file.
For example to copy a file called EIRST.TXT
To a file called SECOND.TXT ,use the following
Command line.
Java CopyFile FIRST.TXT SECOND.TXT
*/
Import java.io.*;
Class CopyFile{
Public static void main(String args[])throws IOException{
Int I;
FileInputStream fin;
FileOutputStream fout;
Try{
//open input file
Try{
Fin=new FileInputStream(arg[0]);
}catch(FileNotFoundException e){
System.out. println(“file not found”);
306. ا run() آ ا ا . ی ی run() زی را داﺥ ﺝ ی را آيآ
ه ی در ی و د ا ی آ از آ ،ه اﺥ ا را ی رو
ن ه دی اي ورودي run () آ وت در ای ا . ی ن را ا ا
از .د دد ، ی ن run() و آ . ای را اﺝ ا داﺥ
Thread از ع دی ، ی ی آ ، ای د زي را دRunnable ی آ س آ ای
از آ .ی ی را ز ی . Thread زي آ از داﺥ ه ن آ س
: رت زی ا ار د رد ا آ
Thread( Runnable threadOb/ string threadName)
آ زي را دRunnable آ را از آ س ا یthreadOb ، ز درای
ﺝی آ . م ی ، وع ﺥ اه را آ ، ﺝی ز . ای
ن را آstart() روش ز ن از ای د ﺝی .د ThreadName
یstart() ، ذا آ . از وع ﺝ ا ، اﺥ ا ، ن اThread داﺥ
: ن داد ای را در زیstart() آ . روش را اﺝ اrun() اﺥ ا
synchronized void start()
: آ وع د و اﺝ اي ن را ﺝ ی ای د ﻝ وﺝ د دارد آ ی در ای
// Create a second thread.
class NewThread implements Runnable {
Thread t;
NewThread (){
// Create a new/ second thread
t = new Thread(this/ "Demo Thread");
System.out.println("Child thread :" + t);
t.start)(; // Start the thread
}
// This is the entry point for the second thread.
public void run (){
try {
for(int i = 5; i > 0; i )--{
System.out.println("Child Thread :" + i);
307. Thread.sleep(500);
}
} catch( InterruptedException e ){
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo {
public static void main(String args[] ){
new NewThread)(; // create a new thread
try {
for(int i = 5; i > 0; i )--{
System.out.println("Main Thread :" + i);
Thread.sleep(1000);
}
} catch( InterruptedException e ){
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
: ا ي ای د ر د از د ا ﺝ یThread یNew Thread ز درون
t = new Thread(this/ "Demo Thread");
راrun() ﺝ ی روش ﺥ اه ده آ ن ن ر ان اوﻝ this ر دادن
وع آ ر را اﺝ اي د،آ اﺥ ا start() .ی اﺥ ا this روي
از .د ز ز for دآ آ . ای ز run() در روش
آ دد. و main() New Thread ط ز ، start() اﺥ ا
cpu ، ده اﺝ ا را ادا آ . ه دو ﺥ د را واردfor ،د از
ار ای ﻝ . ﺥ وﺝ هی ن ین ای ا اك را
: زی ا
Child thread :Thread[Demo Thread/5/main]
Main Thread :5
308. Child Thread :5
Child Thread :4
Main Thread :4
Child Thread :3
Child Thread :2
Main Thread :3
Child Thread :1
Exiting child thread.
Main Thread :2
Main Thread :1
Main thread exiting.
آ اﺝ ا ی ﺥ ی ا ، آ ، در ی ری ه
،د د ، ین ز آ ی از ای ا ده . ا را ی ن
ا ده آ ن ا . " در یhang" ﻝ اﺝ اي ﺝ وا ا
ق اره ﺙ ١٠٠٠ اي ا د زی ا ین آ ا ﺥ ی
ز دآ . ای ق ﺙ ٥٠٠ ز ﻝ در
ﺥ از ه ا ن از ای ي اي ا ، را د. ﺥ ین ا از زود
. د ﺥ اه ی ین
ی داد و راThread آ ، ای د ی آ س ﺝ ی ا ای د ی دو
ﺥ ی ،آ )( را ﻝrun ی روش ده آ .آ س از ه ن آ س ای د
ﺝی اﺝ اي آ اﺥ ا )( راstart ی . ای آ س ه ﺝی ا اي (entry)
. ده راThread د ای ی را دو ر ی . در ای ز را
// Create a second thread by extending Thread.
class NewThread extends Thread {
NewThread (){
// Create a new/ second thread
super("Demo Thread");
309. System.out.println("Child thread :" + this);
start)(; // Start the thread
}
// This is the entry point for the second thread.
public void run (){
try {
for(int i = 5; i > 0; i )--{
System.out.println("Child Thread :" + i);
Thread.sleep(500);
}
} catch( InterruptedException e ){
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ExtendThread {
public static void main(String args[] ){
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i )--{
System.out.println("Main Thread :" + i);
Thread.sleep(1000);
}
} catch( InterruptedException e ){
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
ز ری آ .ه ﻝ را ه ن ﺥ وﺝ ای
اﺥ ا .د ، ای د Thread آ ازNewThread زي ی
آ ل راTrhead ز ي ی . ای ا دNewThread داﺥsuper ()
.
public thread( string threadName)
310. آ ا بی آ .ا را ، مThread Name در ای
Thread . آ س ا آ ام ز دارد و ای اي ای د ا ﺝ وا دو آ
. از ای رو ﻝ ا ی آ س آ آ ی ی روش را
Runnable آ و ا ه ن رو . ای روش اﻝ اrun() د ی ﻝ ،
آ نآ آ س ی ن ﺝ وا ا ري از . زم ا زي د را
ﺥ اه ه ای ا . ی ، ی نه ح ی ا ل ا ای در و
.دارد د ﺥ ا ی ،ا را ﻝthread ي ی رو از
هيچ ای د
اد ا ، ز .ا و ا : د ای د را ا دو ل
: آ ز ای د ي ،ل ان . ی ه رد ز از
// Create multiple threads.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname ){
name = threadname;
t = new Thread(this/ name);
System.out.println("New thread :" + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run (){
try {
for(int i = 5; i > 0; i )--{
System.out.println(name + " :" + i);
Thread.sleep(1000);
}
} catch( InterruptdException e ){
System.out.println(name + " Interrupted.");
}
311. System.out.println(name + " exiting.");
}
}
class MultiThreadDemo {
public static void main(String args[] ){
new NewThread("One"); // start threads
new NewThread("Two");
new NewThread("Three");
try {
// wait for other threads to end
Thread.sleep(10000);
} catch( InterruptedException e ){
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
: ار زی از ای ﺥ وﺝ
New thread :Thread[One/5/main]
New thread :Thread[Two/5/main]
New thread :Thread[Three/5/main]
One :5
Two :5
Three :5
One :4
Two :4
Three :4
One :3
Three :3
Two :3
One :2
Three :2
Two :2
One :1
Three :1
Two :1
313. join() ي دی ي از .د ین آ روي ن اﺥ ا ای روش
ی اي ی ن ی ﺥ اه آ ز اآ ه ده اﺝ ز وﺝ د دار آ
. ی را آ ﺥص
ن ا د آ د )( اjoin وﺝ د دارد آ از ل از ح ی روای ا در ای
)( راisAlive روش ه د . ای آ ا ﺥ ی ا ده آ
. ده ن
// Using join )(to wait for threads to finish.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname ){
name = threadname;
t = new Thread(this/ name);
System.out.println("New thread :" + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run (){
try {
for(int i = 5; i > 0; i-- ) {
System.out.println(name + " :" + i);
Thread.sleep(1000);
}
} catch( InterruptdException e ){
System.out.println(name + " Interrupted.");
}
System.out.println(name + " exiting.");
}
}
class DemoJoin {
public static void main(String args[] ){
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive :" + ob1.t.isAlive))(;
314. System.out.println("Thread Two is alive :" + ob2.t.isAlive))(;
System.out.println("Thread Three is alive :" + ob3.t.isAlive))(;
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch( InterruptedException e ){
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive :" + ob1.t.isAlive))(;
System.out.println("Thread Two is alive :" + ob2.t.isAlive))(;
System.out.println("Thread Three is alive :" + ob3.t.isAlive))(;
System.out.println("Main thread exiting.");
}
}
: ار زی از ای ﺥ وﺝ
New thread :Thread[One/5/main]
New thread :Thread[Two/5/main]
New thread :Thread[Three/5/main]
Thread One is alive :true
Thread Two is alive :true
Thread Three is alive :true
One :5
Two :5
Three :5
One :4
Two :4
Three :4
One :3
Two :3
Three :3
One :2
Two :2
Three :2
315. One :1
Two :1
Three :1
One exiting.
Two exiting.
Three exiting.
One exiting.
Thread One is alive :false
Thread Two is alive :false
Thread Three is alive :false
Main thread exiting.
resume() وsuspend() د از ا
ان ﺝا د از ی ا ل ان . ی را ﻝ اﺝ اي ی زم ا ه
ن ی ط ، ا د از ا ی آر ن داد. ا را و
ر ا اﺥ .ه د اي ا آر دن ی ،ﻝ ع نه .د ﻝ
از ر ه را ا م ی آ ای و و . رو د اي ا آر ﻝ دی
: رت زی و ی Thread . resume()وsuspend()
final void resume()
final void suspend()
: ده ن را ي ای رو
// Using suspend ()and resume.()
class NewThread implements Runnable {)
String name; // name of thread
Thread t;
NewThread(String threadname ){
name = threadname;
t = new Thread(this/ name);
System.out.println("New thread :" + t);
316. t.start()// Start the thread
}
// This is the entry point for thread.
public void run (){
try {
for(int i = 5; i > 0; i )--{
System.out.println(name + " :" + i);
Thread.sleep(200);
}
} catch( InterruptdException e ){
System.out.println(name + " Interrupted .");
}
System.out.println(name + " exiting.");
}
}
class SuspendResume {
public static void main(String args[] ){
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
try {
Thread.sleep(1000);
ob1.t.suspend)(
System.out.println("Suspending thread One");
Thread.sleep(1000);
ob1.t.resume)(
System.out.println("Resuming thread One");
ob2.t.suspend)(
System.out.println("Suspending thread Two");
Thread.sleep(1000);
ob2.t.resume)(
System.out.println("Resuming thread Two");
} catch( InterruptedException e ){
System.out.println("Main thread Interrupted");
}
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
317. ob1.t.join)(;
ob2.t.join)(;
} catch( InterruptedException e ){
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
: آ ي را ﻝ ﺥ وﺝ ای
New thread :Thread[Two/5/main]
One :15
New thread :Thread[Three/5/main]
Two :15
One :14
Two :14
One :13
Two :13
One :12
Two :12
One :11
Two :11
Suspending thread One
Two :10
Two :9
Two :8
Two :7
Two :6
Resuming thread One
Suspending thread Two
One :10
One :9
One :8
One :7
One :6
Resuming thread Two
Waiting for threads to finish.
332. . د رت زی ر طا ﺥ ا ، ق رر در ا ا و
از ی ا ا از ه ط ق ، ای د ﺥ ر از روش ه ی
.( ی ). اﺝ اء ﺥ اه دی
import java.awt.Graphics;
public class FirstApplet extends
java.applet.Applet
{
public void paint(Graphics g)
{
int y;
y = 10;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;
g.drawLine(10, y, 210, y);
y = y + 25;