Professional Documents
Culture Documents
java
terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, july 26, 2007, 11:10:50 am
description: this program uses array
output: multiplying a number to itself with out using input stream reader
program:
program:
import java.io.*;
for(int x=0;x<a.length;++x){
b[x] = a[x] * a[x];
system.out.println(b[x]);
}
}
}
program:
import java.io.*;
program:
int keyvalue = 1;
switch(keyvalue){
case 1:
system.out.println("one");
break;
case 2:
system.out.println("two");
break;
case 3:
system.out.println("three");
break;
default:
system.out.println("more");
break;
}
}
}
file name: sample.java
terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, july 05, 2007, 1:04:22 pm
description: this program uses two sub-class to diplay a message
output:
program:
program:
}
}
}
program:
program:
import java.io.*;
system.out.println(output);
}
}
program:
case 1:
return defineplace.units;
case 2:
return defineplace.tens;
case 3:
return defineplace.hundreds;
case 4:
return defineplace.thousands;
case 5:
return defineplace.tenthousands;
case 6:
return defineplace.millions;
//return defineplace.lakhs;
case 7:
return defineplace.tenmillions;
//return defineplace.tenlakhs;
case 8:
return defineplace.billions;
//return defineplace.crores;
case 9:
return defineplace.tenbillions;
//return defineplace.tencrores;
}//switch
return 0.0;
}// getplace
return cleanednumber;
} //cleannumber
}//if
else if( place == defineplace.tenmillions ){
num -= subnum * defineplace.millions;
returnvalue += " million ";
}//if
else if( place == defineplace.tenbillions ){
num -= subnum * defineplace.billions;
returnvalue += " billion ";
//}//if
//else if( place == defineplace.tenlakhs ){
//num -= subnum * defineplace.lakhs;
//returnvalue += " lakhs ";
//}//if
//else if( place == defineplace.tencrores ){
//num -= subnum * defineplace.crores;
//returnvalue += " crores ";
}//if
}//if
else{
int subnum = integer.parseint( "" + number.charat(0) );
}//if
else if( place == defineplace.millions ){
num -= subnum * defineplace.millions;
returnvalue += " million ";
}//if
else if( place == defineplace.billions ){
num -= subnum * defineplace.billions;
returnvalue += " billion ";
//}//if
//else if( place == defineplace.lakhs ){
//num -= subnum * defineplace.lakhs;
//returnvalue += " lakh ";
//}//if
//else if( place == defineplace.crores ){
//num -= subnum * defineplace.crores;
//returnvalue += " crore ";
}//if
}//else
}//while
return returnvalue;
}//convert number
public static void main( string args[] ){
kamikuto1 cv = new kamikuto1();
class defineplace{
public static final double units = 1;
public static final double tens = 10 * units;
public static final double hundreds = 10 * tens;
public static final double thousands = 10 * hundreds;
public static final double tenthousands = 10 * thousands;
public static final double millions = 10 * tenthousands;
public static final double tenmillions = 10 * millions;
public static final double billions = 10 * tenmillions;
public static final double tenbillions = 10 * billions;
program:
program:
program:
program:
program:
program:
program:
program:
program:
import javax.swing.*;
joptionpane.showmessagedialog(null,message,"while loop",
joptionpane.information_message);
double total=0;
int count=1;
string msg=null;
while(count <= maxnum){
switch(count){
case 1:
msg = "first";
break;
case 2:
msg = "second";
break;
case 3:
msg = "third";
break;
case 4:
msg = "fourth";
break;
}
total +=num;
++count;
}
joptionpane.showmessagedialog(null,"the total of the " + maxnum + " numbers
is " + total,
"result",joptionpane.information_message);
system.exit(0);
}
}
file name: kuto.java
terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, august 02, 2007, 11:05:42 am
description:
output:
program:
what is inheritance?
inheritance syntax
inheritance is such an integral part of java (and oop languages in general) that
it was introduced in chapter 1 and has been used occasionally in chapters before
this one because certain situations required it. in addition, you�re always doing
inheritance when you create a class, because if you don�t say otherwise you
inherit from java�s standard root class object.
the syntax for composition is obvious, but to perform inheritance there�s a
distinctly different form. when you inherit, you say �this new class is like that
old class.� you state this in code by giving the name of the class as usual, but
before the opening brace of the class body, put the keyword extends followed by
the name of the base class . when you do this, you automatically get all the data
members and methods in the base class.
(for volume1.java)
read text from a character-input stream, buffering characters so as to provide for
the efficient reading of characters, arrays, and lines.
the buffer size may be specified, or the default size may be used. the default is
large enough for most purposes.
in general, each read request made of a reader causes a corresponding read request
to be made of the underlying character or byte stream. it is therefore advisable
to wrap a bufferedreader around any reader whose read() operations may be costly,
such as filereaders and inputstreamreaders. for example,
bufferedreader in
= new bufferedreader(new filereader("foo.in"));
will buffer the input from the specified file. without buffering, each invocation
of read() or readline() could cause bytes to be read from the file, converted into
characters, and then returned, which can be very inefficient.
programs that use datainputstreams for textual input can be localized by replacing
each datainputstream with an appropriate bufferedreader.
what is input stream reader?
(for volume1.java)
an inputstreamreader is a bridge from byte streams to character streams: it reads
bytes and decodes them into characters using a specified charset. the charset that
it uses may be specified by name or may be given explicitly, or the platform's
default charset may be accepted.
each invocation of one of an inputstreamreader's read() methods may cause one or
more bytes to be read from the underlying byte-input stream. to enable the
efficient conversion of bytes to characters, more bytes may be read ahead from the
underlying stream than are necessary to satisfy the current read operation.
for top efficiency, consider wrapping an inputstreamreader within a
bufferedreader. for example:
bufferedreader in
= new bufferedreader(new inputstreamreader(system.in));
(for switchtest.java)
the switch statement in java provides a convenient method for branching a program
based on a number of conditionals. this recipe describes the use of the java
switch statement.
in computer programming, a switch statement is a type of control statement that
exists in most modern imperative programming languages (e.g., c, c++, c#, and
java). its purpose is to allow the value of a variable or expression to control
the flow of program execution. in some other programming language, a statement
that is syntactically different but conceptually the same as the switch statement
is known as a case statement or a select statement.
in most languages, a switch statement is defined across many individual
statements. a typical syntax is that the first line contains the actual word
"switch" followed by either the name of a variable or some other expression
allowed by the language's syntax. this variable or expression is usually referred
to as the "control variable" of the switch statement. after this line, following
lines define one or more blocks of code that represent possible branches that
program execution may take.
each block begins with a line containing the case keyword followed a value that
the control variable may have. if the value of the control variable matches this
value, program execution will jump to that block of code. if not, the value
specified in the next block (if present) is examined and the process repeats.
an optional special block is also allowed, which does not specify any value and
which begins with the default keyword instead of the case keyword. if this block
is present and if none of the values listed for any other block matches that of
the control variable, program execution will jump to the statement following the
default keyword.
the method of terminating a block is also of note. typically, a break keyword is
used to signal the end of the block. when encountered, this keyword causes program
execution to continue with the first statement after the series of statements
within the switch statement, thus completing execution of the switch statement. if
no break keyword is present at the end of the block, in many languages program
execution "falls through" to the code associated with the next block in the switch
statement, as if its value also matched the value of the control variable. notable
exceptions include c#, in which fallthrough is not permitted unless the block is
empty and all blocks must be terminated via a break, or by using another keyword.
similarly, almost all basic dialects that feature this type of statement do not
allow fallthrough.
what is while and do-while statements?
(for while.java)
the while and do-while statements
the while statement continually executes a block of statements while a particular
condition is true. its syntax can be expressed as:
while (expression) {
statement(s)
}
the while statement evaluates expression, which must return a boolean value. if
the expression evaluates to true, the while statement executes the statement(s) in
the while block. the while statement continues testing the expression and
executing its block until the expression evaluates to false. using the while
statement to print the values from 1 through 10 can be accomplished as in the
following whiledemo program:
class whiledemo {
public static void main(string[] args){
int count = 1;
while (count < 11) {
system.out.println("count is: " + count);
count++;
}
}
}
you can implement an infinite loop using the while statement as follows:
while (true){
// your code goes here
}
the java programming language also provides a do-while statement, which can be
expressed as follows:
do {
statement(s)
} while (expression);
the difference between do-while and while is that do-while evaluates its
expression at the bottom of the loop instead of the top. therefore, the statements
within the do block are always executed at least once, as shown in the following
dowhiledemo program:
class dowhiledemo {
public static void main(string[] args){
int count = 1;
do {
system.out.println("count is: " + count);
count++;
} while (count <= 11);
}
}