You are on page 1of 24

file name: array.

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:

public class array{


public static void main(string[] args){
int [] a = new int [5];
int [] b = new int [5];
for (int x=0; x<a.length; ++x){
a[x] = integer.parseint(args[x]);
b[x] = a[x] * a[x];
system.out.println(b[x]);
}
}
}

file name: array2.java


terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, june 28, 2007, 11:01:12 am
description: this program uses array
output: multiplying a number to itself

program:

import java.io.*;

public class array2{


public static void main(string[] args) throws ioexception{
inputstreamreader isr = new inputstreamreader(system.in);
bufferedreader br = new bufferedreader(isr);
int[] a = new int[5];
int[] b = new int[5];
string s;
for(int x=0; x<a.length;++x){
system.out.print("enter" + (x+1) + "number: ");
s = br.readline();
a[x] = integer.parseint(s);
}
system.out.println();

for(int x=0;x<a.length;++x){
b[x] = a[x] * a[x];
system.out.println(b[x]);
}
}
}

file name: array3.java


terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, june 21, 2007, 1:23:23 pm
description: this program combined inputstreamreader and bufferedreader in one
line
output: multiplying a number to itself

program:

import java.io.*;

public class array3{

public static void main(string[] args) throws ioexception{


bufferedreader br = new bufferedreader(new inputstreamreader(system.in));
int[] a = new int[5];
int[] b = new int[5];
string s;
for(int x=0; x<a.length;++x){
system.out.print("enter " + (x+1) + " number: ");
a [x] = integer.parseint(br.readline());
}
system.out.println();
for(int x=0;x<a.length;++x){
b[x] = a[x] * a[x];
system.out.println(b[x]);
}
}
}

file name: switchtest.java


terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, july 05, 2007, 1:04:22 pm
description:
output: displaying a number

program:

public class switchtest{

public static void main(string args[]){

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:

public class sample{


public static void displaymessage(string st){
system.out.println("\n" + "this message is all about numbers: "+
st);
}

public static void main(string[] args){


sample sample = new sample();
sample.displaymessage("\n" + "\n" +
" hitchyfreaks@yahoo.com" + "\n" +
" jm | kamikuto" +
"\n" + "\n" +
" one" + "\n" +
" two" + "\n" +
" three" + "\n" +
" four" + "\n" +
"\n" + "\n" +
" jm | kamikuto" + "\n" +
" hitchyfreaks@yahoo.com");
}
}

file name: month.java


terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, june 28, 2007, 11:45:41 am
description:
output: displaying months in proper order

program:

public class month{

public static void main(string args[]){

string[] month = {"january","february","march","april"};


for(int i = 0;i<month.length;++i){
system.out.println(month[i]);

}
}
}

file name: month2.java


terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, june 28, 2007, 11:53:38 am
description:
output:

program:

public class month2{

public static void main(string args[]){


//string[] month={"jan","feb","mar","apr"}
int[] month = {1,2,3,4,5};
for (int i=0;i<month.length;++i){
system.out.println(month[i]);
}
}
}

file name: volume1.java


terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, june 28, 2007, 12:28:10 pm
description:
output: displaying the volume of radius and height

program:

import java.io.*;

public class volume1{

public static void main(string args[])throws java.io.ioexception{


inputstreamreader isr = new inputstreamreader(system.in);
bufferedreader br = new bufferedreader(isr);
double volume,radius,height;
string output;
final double pi = 3.1416;

system.out.print("enter radius: ");


radius = double.parsedouble(br.readline());

system.out.print("enter height: ");


height = double.parsedouble(br.readline());
volume = pi * radius * radius * height;

output ="for a cylinder of radius" + radius;


output +="and a height of" + height;
output +="\nthe volume is" + volume;

system.out.println(output);
}
}

file name: kamikuto1.java


terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, july 05, 2007, 1:44:01 pm
description: this program uses switch statements and multiple sub-classes to
covert numbers to words
output: converting numbers to words

program:

public class kamikuto1{

private double getplace( string number ){


switch( number.length() ){

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

private string getword( int number ){


switch( number ){
case 1:
return "one";
case 2:
return "two";
case 3:
return "three";
case 4:
return "four";
case 5:
return "five";
case 6:
return "six";
case 7:
return "seven";
case 8:
return "eight";
case 9:
return "nine";
case 0:
return "zero";
case 10:
return "ten";
case 11:
return "eleven";
case 12:
return "twelve";
case 13:
return "thirteen";
case 14:
return "forteen";
case 15:
return "fifteen";
case 16:
return "sixteen";
case 17:
return "seventeen";
case 18:
return "eighteen";
case 19:
return "ninteen";
case 20:
return "twenty";
case 30:
return "thirty";
case 40:
return "forty";
case 50:
return "fifty";
case 60:
return "sixty";
case 70:
return "seventy";
case 80:
return "eighty";
case 90:
return "ninty";
case 100:
return "hundred";
} //switch
return "";
} //getword

private string cleannumber( string number ){


string cleanednumber = "";

cleanednumber = number.replace( '.', ' ' ).replaceall( " ", "" );


cleanednumber = cleanednumber.replace( ',', ' ' ).replaceall( " ",
"" );
if( cleanednumber.startswith( "0" ) )
cleanednumber = cleanednumber.replacefirst( "0", "" );

return cleanednumber;
} //cleannumber

public string convertnumber( string number ){


number = cleannumber( number );
double num = 0.0;
try{
num = double.parsedouble( number );
}catch( exception e ){
return "invalid number sent to convert";
} //catch

string returnvalue = "";


while( num > 0 ){
number = "" + (int)num;
double place = getplace(number);

if( place == defineplace.tens || place == defineplace.tenthousands || place


== defineplace.tenmillions || place == defineplace.tenbillions ){
//if( place == defineplace.tens || place == defineplace.tenthousands ||
place == //defineplace.tenmillions || place == defineplace.tencrores
){

int subnum = integer.parseint( number.charat(0) + "" + number.charat(1) );

if( subnum >= 21 && (subnum%10) != 0 ){


returnvalue += getword( integer.parseint( "" + number.charat(0) ) * 10 ) + "
" + getword( subnum%10 ) ;
} //if
else{
returnvalue += getword(subnum);
}//else

if( place == defineplace.tens ){


num = 0;
}//if
else if( place == defineplace.tenthousands ){
num -= subnum * defineplace.thousands;
returnvalue += " thousand ";

}//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) );

returnvalue += getword( subnum );


if( place == defineplace.units ){
num = 0;
}//if
else if( place == defineplace.hundreds ){
num -= subnum * defineplace.hundreds;
returnvalue += " hundred ";
}//if
else if( place == defineplace.thousands ){
num -= subnum * defineplace.thousands;
returnvalue += " thousand ";

}//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();

if( args.length >= 1 )


{
for( int i=0; i<args.length; i++ )
system.out.println( "given number : " + args[i] + "\nconverted: " +
cv.convertnumber(args[i]) + "\n\n" );
system.exit(0);
}

system.out.println( "given number : 999999999\nconverted: " +


cv.convertnumber("999999999") + "\n\n" );
}//main
} //class

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;

//public static final double lakhs = 10 * tenthousands;


//public static final double tenlakhs = 10 * lakhs;
//public static final double crores = 10 * tenlakhs;
//public static final double tencrores = 10 * crores;
} //class

file name: human.java


terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, august 02, 2007, 11:05:42 am
description: this program uses inheritance
output:

program:

public class human{


public static void main(string[] args){
human human = new human();
cat cat = new cat();
dog dog = new dog();
call(dog);
call(cat);
}

public static void call(animal animal){


animal.cry();
}
}

file name: dog.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:

public class dog extends animal{


public void cry(){
system.out.println("\n" + " cries:" +
"\n" + " aw!!" +
"\n" + " aw!!" +
"\n" + " aw!!");
}
}

file name: cat.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:

public class cat extends animal{


public void cry(){
system.out.println("\n" + " cries:" +
"\n" + " meow!!" +
"\n" + " meow!!" +
"\n" + " meow!!");
}
}
file name: animal.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:

public class animal{


public void run(){
system.out.println("run!!!");
}

public void cry(){


system.out.println("cry!!!");
}
}
file name: kamikuto.java
terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, august 02, 2007, 11:54:07 am
description:
output:

program:

//this program shows all my favorites. music that i am listening,


//music that i admire, and persons that i idolized when it comes to music
//or guitar playing.

public class kamikuto{


public static void main(string[] args){
kamikuto kamikuto = new kamikuto();
one one = new one();
two two = new two();
three three = new three();
four four = new four();
call(one);
call(two);
call(three);
call(four);
}

public static void call(kuto kuto){


kuto.favorites();
}
}

file name: one.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:

public class one extends kuto{


public void favorites(){
system.out.println("\n"
+ "----------hitchyfreaks@yahoo.com----------" + "\n" + "\n"
+ " * favorite music instrument:" + "\n"
+ "\n" + " jazz"
+ "\n" + " reggae"
+ "\n" + " rock"
+ "\n" + " funky"
+ "\n" + " classical"
+ "\n" + " fusion rock"
+ "\n"
+ "\n" + "----------------------------------------");
}
}

file name: two.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:

public class two extends kuto{


public void favorites(){
system.out.println("\n" + " * favorite music instrument:"
+ "\n" + "\n" + " guitar"
+ "\n" + " bass"
+ "\n"
+ "\n" + "---------------------------------------------");
}
}
file name: three.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:

public class three extends kuto{


public void favorites(){
system.out.println("\n" + " * favorite band:"
+ "\n" + "\n" + " bamboo"
+ "\n" + " g3"
+ "\n" + " racer x"
+ "\n" + " nitro"
+ "\n" + " rage against the machine"
+ "\n" + " red hot chilli peppers"
+ "\n" + " p.o.t"
+ "\n" + " 311"
+ "\n" + " dream theater"
+ "\n" + " audioslave"
+ "\n" + " mr.big");
}
}
file name: four.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:

public class four extends kuto{


public void favorites(){
system.out.println("\n"
+ "--------------------------------------------" + "\n" + "\n"
+ " * favorite guitarist and bassist"
+ "\n" + "\n" + " john petrucci [ dream theater, g3 ]"
+ "\n" + " michael angelo batio [ nitro band ]"
+ "\n" + " steve vai [ g3 ]"
+ "\n" + " victor wooten - solo instrumental artist(
bassist )"
+ "\n" + " paul gilbert [ mr.big, racer x ]"
+ "\n" + " joe satriani [ g3 ]"
+ "\n" + " eric johnson [ g3 ]"
+ "\n" + " tuck andress [ classical ] - solo instrumental
artist"
+ "\n" + " greg howe - solo instrumental artist,
sessionist"
+ "\n" + " guthrie govan - solo instrumental artist"
+ "\n"
+ "\n" + "----------------------------------------");
}
}

file name: while.java


terminal no: 9
programmer: delos reyes, jose mari luis c.
date and time created: thursday, july 28, 2007
description:
output:

program:
import javax.swing.*;

public class while{


public static void main(string[] args){
final int maxnum = 4;
string message = "this program will ask you to enter " + maxnum
+ " numbers.";

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;
}

string s = joptionpane.showinputdialog("enter " + msg + " number: ");


double num = double.parsedouble(s);

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:

public class kuto{


public void favorites(){
system.out.println("favorites");
}
public void hobbies(){
system.out.println("hobbies");
}
}
what is java arrays?

(for array.java, array2.java, array3.java)


structure of java arrays
the java array depicted above has 7 elements. each element in an array holds a
distinct value. in the figure, the number above each element shows that element's
index. elements are always referenced by their indices. the first element is
always index 0. this is an important point, so i will repeat it! the first element
is always index 0. given this zero-based numbering, the index of the last element
in the array is always the array's length minus one. so in the array pictured
above, the last element would have index 6 because 7 minus 1 equals 6.
java array declaration
an array variable is declared the same way that any java variable is declared. it
has a type and a valid java identifier. the type is the type of the elements
contained in the array. the [] notation is used to denote that the variable is an
array. some examples:
java array initialization
once an array variable has been declared, memory can be allocated to it. this is
done with the new operator, which allocates memory for objects. (remember arrays
are implicit objects.) the new operator is followed by the type, and finally, the
number of elements to allocate. the number of elements to allocate is placed
within the [] operator.

what is inheritance?

(for human.java(main), cat.java, dog.java, animal.java, kamikuto.java(main),


one.java, two.java, three.java, four.java, kuto.java)

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.

what is buffered reader?

(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));

what is java switch statements?

(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);
}
}

You might also like