public class NumberBox<N extends Number> extends Box<N> {
public NumberBox( ) {
super( );
}
// Sum everything in the box
public double sum( ) {
double total = 0;
for (Iterator<N> i = contents.iterator( ); i.hasNext( ); ) {
total = total + i.next( ).doubleValue( );
}
return total;
}
}
You can use this same syntax in method definitions:
public static double sum(Box<? extends Number> box1,
Box<? extends Number> box2) {
double total = 0;
for (Iterator<? extends Number> i = box1.contents.iterator( );
i.hasNext( ); ) {
total = total + i.next( ).doubleValue( );
}
for (Iterator<? extends Number> i = box2.contents.iterator( );
i.hasNext( ); ) {
total = total + i.next( ).doubleValue( );
}
return total;
}
public void testSwitchStatement(PrintStream out) throws IOException {
StringBuffer outputText = new StringBuffer(student1.getFullName( ));
switch (student1.getGrade( )) {
case A:
outputText.append(" excelled with a grade of A");
break;
case B: // fall through to C
case C:
outputText.append(" passed with a grade of ")
.append(student1.getGrade( ).toString( ));
break;
case D: // fall through to F
case F:
outputText.append(" failed with a grade of ")
.append(student1.getGrade( ).toString( ));
break;
case INCOMPLETE:
outputText.append(" did not complete the class.");
break;
}
out.println(outputText.toString( ));
}
// These are the the opcodes that our stack machine can execute.
abstract static enum Opcode {
PUSH(1),
ADD(0),
BEZ(1); // Remember the required semicolon after last enum value
int numOperands;
Opcode(int numOperands) { this.numOperands = numOperands; }
public void perform(StackMachine machine, int[] operands) {
switch(this) {
case PUSH: machine.push(operands[0]); break;
case ADD: machine.push(machine.pop( ) + machine.pop( )); break;
case BEZ: if (machine.pop( ) == 0) machine.setPC(operands[0]); break;
default: throw new AssertionError( );
}
}
}
/**
* Causes the currently executing thread to sleep (cease execution)
* for the specified number of milliseconds plus the specified number
* of nanoseconds. The thread does not lose ownership of any monitors.
*
* @param millis the length of time to sleep in milliseconds.
* @param nanos 0-999999 additional nanoseconds to sleep.
* @exception IllegalArgumentException if the value of millis is
* negative or the value of nanos is not in the range
* 0-999999.
* @exception InterruptedException if another thread has interrupted
* the current thread. The <i>interrupted status</i> of the
* current thread is cleared when this exception is thrown.
* @see java.lang.Object#notify()
*/
public static void sleep(long millis, int nanos)
throws InterruptedException {
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
// Et là , c'est la grosse arnaque!!!
if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
millis++;
}
sleep(millis);
}
package com.destiney.Prime;
class Prime
{
public static void main( String args[] )
{
int x, y, c = 0;
for( x = 2; x < 1000; x++ )
{
if( x % 2 != 0 || x == 2 )
{
for( y = 2; y <= x / 2; y++ )
{
if( x % y == 0 )
{
break;
}
}
if( y > x / 2 )
{
System.out.println( x );
c++;
}
}
}
System.out.println( "\nTotal: " + c );
}
}