public class Power
{
/**
* @param radix the base for exponentiation
* @param expon the exponent used
* @return the result of radix raised to the expon
* this can be a reciprocal if the the expon is negative
**/
public static int power ( int radix, int expon )
{
if ( expon < 0 )
{
// the answer would be a float
// we are going to return the reciprocal instead
// so we can still return an int
expon *= -1;
}
int rval = 1;
while ( expon > 0 )
{
rval *= radix;
--expon;
}
return rval;
}
/* main does not need block comments */
public static final void main ( String args [] )
{
System.out.printf ( "%d ^ %d = %d\n", 2, 3, power(2,3) );
System.out.printf ( "%d ^ %d = %d\n", 3, 2, power(2,3) );
}
}
return to top
import java.io.*;
import java.util.*;
class ReadLine
{
public static void main ( String[] args )
{
// System.in is the keyboard aka stdin
InputStreamReader isr = new InputStreamReader( System.in );
// input buffering
BufferedReader in = new BufferedReader ( isr );
String curLine = "";
System.out.println( "input now !!" );
try
{
while ( ( curLine = in.readLine() ) != null )
{
//System.out.println( "the line is '" + curLine + "'" );
// break up the line into its words
StringTokenizer st =
new StringTokenizer ( curLine,
" \t\n\f\r,.;:!@" );
while ( st.hasMoreTokens() )
{
String token = st.nextToken();
System.out.println( "the token is '" +
token + "'" );
}
}
}
catch ( IOException dummy_variable )
{
System.out.println( "this is very bad" );
}
}
}
Continuation of the same line of code should also be indented.
StringTokenizer st =
new StringTokenizer ( curLine,
" \t\n\f\r,.;:!@" );
for( int i = 0; i < 10 ; i ++ ) {
tmp += arr[i];
}
Here is an example of east coast which is GOOD.
for( int i = 0; i < 10 ; i ++ )
{
tmp += arr[i];
}
East coast braces are easier to see if you forgot one,
and helps make missing braces easier to spot.
public static bool func( int n )
{
if ( n > 10 )
{
return true;
}
else
{
return false;
}
}
Here is the previous code rewritten to have only one return.
public static bool func( int n )
{
bool rval = false;
if ( n > 10 )
{
rval = true;
}
return rval;
}
This will help you avoid some error conditions in your code.
Many programmers miss every condition that is possible and fail
to always return a value from a function, this will prevent that error.
Most people tend to use continue when re-ordering the conditions
correctly would have solved the problem. All uses of continue must be
clearly comments as to why you are using it.
return to top
JC=javac
JFLAGS=-Xlint
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
CLASSES=Main.class
all: $(CLASSES)
clean:
-rm -f *.class
After putting that in a file named "Makefile", you can run "make" in the
shell, or ":make" in vim and it will build your project. This is
hardcoded to build Main.java, but you could change CLASSES to compile
any source file(s).
foo = y; bar = z;
if ( foo == bar ) x = 5;
Here is the correct form of the above code
foo = y;
bar = z;
if ( foo == bar )
x = 5;