view release on metacpan or search on metacpan
examples/corpus/CloneArray.java view on Meta::CPAN
public int[] arr = new int[5];
public X() {
Random ran = new Random();
int i=0;
while ( i < 5 )
arr[i++] = (ran.nextInt() & 0xffff)%10; //(A)
}
public Object clone() throws CloneNotSupportedException { //(B)
X xob = null;
xob = (X) super.clone();
//now clone the array separately:
xob.arr = (int[]) arr.clone(); //(C)
return xob;
}
public String toString() {
String printstring = "";
for (int i=0; i<arr.length; i++) printstring += " " + arr[i];
return printstring;
}
public static void main( String[] args ) throws Exception {
X xobj = new X();
X xobj_clone = (X) xobj.clone(); //(D)
System.out.println( xobj ); // 0 4 5 2 5
System.out.println( xobj_clone ); // 0 4 5 2 5
xobj.arr[0] = 1000; //(E)
System.out.println( xobj ); // 1000 4 5 2 5
System.out.println( xobj_clone ); // 0 4 5 2 5
examples/corpus/CloneBasic.java view on Meta::CPAN
//CloneBasic.java
class X implements Cloneable {
public int n;
public X() { n = 3; }
public Object clone() throws CloneNotSupportedException { //(A)
return super.clone(); //(B)
}
}
class Test {
public static void main( String[] args )
{
X xobj = new X();
X xobj_clone = null;
examples/corpus/CloneClassTypeArr.java view on Meta::CPAN
// http://programming-with-objects.com
//
//CloneClassTypeArr.java
class X implements Cloneable {
public int p;
public X( int q ) { p = q; }
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public String toString() { return p + ""; }
}
class Y implements Cloneable {
public X x;
public Y( X x ) { this.x = x; }
public Object clone() throws CloneNotSupportedException {
Y clone = (Y) super.clone();
clone.x = (X) x.clone();
return clone;
}
public String toString() { return x + ""; }
}
class Z implements Cloneable {
public Y[] yarr;
public Z( Y[] arr ) { this.yarr = arr; }
public Object clone() throws CloneNotSupportedException { //(A)
Z zclone = (Z) super.clone();
// zclone.yarr = ( Y[] ) yarr.clone(); // WRONG //(B)
Y[] yarrClone = new Y[ yarr.length ]; //(C)
for (int i=0; i < yarr.length; i++ )
yarrClone[i] = (Y) yarr[i].clone(); //(D)
zclone.yarr = yarrClone; //(E)
return zclone;
}
public String toString() {
String superString = "";
for ( int i = 0; i < yarr.length; i++ ) {
superString += yarr[i] + " ";
}
return superString;
}
}
class Test {
public static void main( String[] args ) throws Exception
{
X xobj0 = new X( 5 );
X xobj1 = new X( 7 );
Y yobj0 = new Y( xobj0 );
Y yobj1 = new Y( xobj1 );
Y[] yarr = new Y[2];
yarr[0] = yobj0;
yarr[1] = yobj1;
examples/corpus/ExceptionUsage1.java view on Meta::CPAN
class MyException extends Exception { //(A)
public MyException() { //(B)
super();
}
public MyException( String s ) { //(C)
super( s );
}
}
class Test {
static void f( ) throws MyException { //(D)
throw new MyException( "Exception thrown by function f()" );
}
public static void main( String[] args )
{
try {
f(); //(E)
} catch( MyException e ) {
System.out.println( e.getMessage() ); //(F)
}
}
examples/corpus/ExceptionUsage2.java view on Meta::CPAN
// http://programming-with-objects.com
//
//ExceptionUsage2.java
class MyException extends Exception {}
class Test {
static void f( ) throws MyException {}
public static void main( String[] args ) {
try {
f();
} catch( MyException e ) {}
}
}
examples/corpus/ExceptionUsage3.java view on Meta::CPAN
//
//ExceptionUsage3.java
class MyException extends Exception {}
class Err extends Exception {}
class Test {
static void f( int j ) throws MyException, Err { //(A)
if ( j == 1 ) throw new MyException(); //(B)
if ( j == 2 ) throw new Err(); //(C)
}
public static void main( String[] args )
{
try {
f( 1 );
} catch( MyException e ) {
System.out.println("caught MyException -- arg must be 1");
} catch( Err e ) {
System.out.println("caught Err -- arg must be 2");
examples/corpus/ExceptionUsage4.java view on Meta::CPAN
//
//ExceptionUsage4.java
class MyException extends Exception {}
class Err extends Exception {}
class Test {
static void f() throws MyException { //(A)
throw new MyException();
}
static void g() throws Err { //(B)
throw new Err();
}
static void h() throws MyException, Err { //(C)
f();
g();
}
public static void main( String[] args ) {
try {
h(); //(D)
} catch( MyException e ) {
System.out.println( "caught MyException" );
} catch( Err e ) {
examples/corpus/ExceptionUsage5.java view on Meta::CPAN
//
//ExceptionUsage5.java
import java.io.*;
class Test {
static void foo() throws Exception { throw new Exception(); }
static void bar() throws Exception {
FileReader input = null;
try {
input = new FileReader( "infile" ); //(A)
int ch;
while ( ( ch = input.read() ) != -1 ) { //(B)
if ( ch == 'A' ) {
System.out.println( "found it" );
foo(); //(C)
}
}
examples/corpus/ExceptionUsage6.java view on Meta::CPAN
//
//ExceptionUsage6.java
class MyException extends Exception {}
class Test {
static void f() throws MyException {
throw new MyException();
}
static void g() throws MyException {
try {
f();
} catch( MyException e ) {
System.out.println( "catching and re-throwing in g" );
throw new MyException();
}
}
public static void main( String[] args )
{
try {
g();
} catch( MyException e ) {
System.out.println( "caught MyException in main" );
}
examples/corpus/ExceptionUsage7.java view on Meta::CPAN
//ExceptionUsage7.java
class MyException extends Exception {
String message; //(A)
public MyException( String mess ) { message = mess; } //(B)
}
class Test {
static void f() throws MyException {
throw new MyException( "Hello from f()" ); //(C)
}
public static void main( String[] args )
{
try {
f();
} catch( MyException e ) {
System.out.println( e.message );
}
}
examples/corpus/GC.java view on Meta::CPAN
//GC.java
class X {
int id;
static int nextId = 1;
//constructor:
public X() { id = nextId++; }
protected void finalize() throws Throwable { //(A)
if ( id%1000 == 0 )
System.out.println("Finalization of X object, id = " + id);
super.finalize(); //(B)
}
}
class Test {
public static void main( String[] args ) {
X[] xarray = new X[ 10000 ]; //(C)
for (int i = 0; i < 10000; i++ ) //(D)
examples/corpus/GC_Resurrect.java view on Meta::CPAN
//GC_Resurrect.java
class X {
int id;
static int nextId = 1;
static X[] staticArr = new X[10000]; //(A)
//constructor:
public X() { id = nextId++; }
protected void finalize() throws Throwable {
if ( id%1000 == 0 )
System.out.println("Finalization of X object, id = " + id);
super.finalize();
X resurrect = this; //(B)
putItAway( resurrect ); //(C)
}
public void putItAway( X xobj ) { staticArr[ id ] = this; }
}
examples/corpus/Interleaved.java view on Meta::CPAN
public String toString() { return "\nName: " + name
+ " Age: " + age; }
public String getName() { return name; }
public int getAge() { return age; }
public void print() {
System.out.println( this );
}
public Object clone() throws CloneNotSupportedException {
Dog dog = null;
try {
dog = ( Dog ) super.clone();
} catch( CloneNotSupportedException e ) {}
return dog;
}
}
////////////////////////// class Employee //////////////////////////
examples/corpus/LinkedList.java view on Meta::CPAN
public Iterator iterator() { //(F)
return new Iterator() { //(G)
protected Node ptr = head;
public boolean hasNext() { return ptr != null; }
public Object next() {
if ( ptr != null ) {
Object item = ptr.item;
ptr = ptr.next;
return item;
} else throw new NoSuchElementException();
}
};
}
} // end of class LinkedList
class Test {
public static void main( String[] args ) {
examples/corpus/LinkedListGeneric.java view on Meta::CPAN
return new Iterator<T>() { //(G)
protected Node ptr = head;
public boolean hasNext() { return ptr != null; }
public T next() {
if ( ptr != null ) {
T item = ptr.item;
ptr = ptr.next;
return item;
} else throw new NoSuchElementException();
}
};
}
}
class Test {
public static void main( String[] args ) {
String str = "";
examples/corpus/MapHist.java view on Meta::CPAN
//MapHist.java
import java.io.*;
import java.util.*;
class WordHistogram {
public static void main (String args[]) throws IOException
{
Map histogram = new TreeMap(); //(A)
String allChars = getAllChars( args[0] ); //(B)
StringTokenizer st = new StringTokenizer( allChars ); //(C)
while ( st.hasMoreTokens() ) { //(D)
String word = st.nextToken(); //(E)
Integer count = (Integer) histogram.get( word ); //(F)
histogram.put( word, ( count==null ? new Integer(1)
: new Integer( count.intValue() + 1 ) ) ); //(G)
}
System.out.println( "Total number of DISTINCT words: "
+ histogram.size() ); //(H)
System.out.println( histogram ); //(I)
}
static String getAllChars( String filename ) throws IOException {
String str = "";
int ch;
Reader input = new FileReader( filename );
while ( ( ch = input.read() ) != -1 )
str += (char) ch;
input.close();
return str;
}
}
examples/corpus/ObjectIO.java view on Meta::CPAN
import java.io.*;
class User implements Serializable {
private String name;
private int age;
public User( String nam, int yy ) { name = nam; age = yy; }
public String toString(){return "User: " + name + " " + age;}
public static void main( String[] args ) throws Exception {
User user1 = new User( "Melinda", 33 );
User user2 = new User( "Belinda", 43 );
User user3 = new User( "Tralinda", 53 );
FileOutputStream os = new FileOutputStream( "object.dat" );
ObjectOutputStream out = new ObjectOutputStream( os );
out.writeObject( user1 );
out.writeObject( user2 );
out.writeObject( user3 );
examples/corpus/ReadIntFromFile.java view on Meta::CPAN
//ReadIntFromFile.java
import java.io.*;
class ReadIntFromFile {
public static void main( String[] args ) throws Exception {
int anInt = 123456;
int x;
DataOutputStream dos = new DataOutputStream(
new FileOutputStream( "out.num" ) ); //(A)
dos.writeInt( anInt ); //writes hex 00 01 e2 40 to file //(B)
dos.close();
examples/corpus/ReadStringFromFile.java view on Meta::CPAN
//ReadStringFromFile.java
import java.io.*;
class ReadStringFromFile {
public static void main( String[] args ) throws Exception {
String aString = "hello";
String bString = "there";
String str;
DataOutputStream dos = new DataOutputStream(
new FileOutputStream( "out.dos" ) );
dos.writeUTF( aString ); //hex output: 00 05 68 65 6c 6c 6f
dos.writeUTF( bString ); //hex output: 00 05 74 68 65 72 65
dos.close();
examples/corpus/RuntimeExcep.java view on Meta::CPAN
class MyException extends RuntimeException { //(A)
public MyException() {
super();
}
public MyException( String s ) {
super( s );
}
}
class Test {
static void f( ) throws MyException {
throw new MyException( "Exception thrown by function f()" );
}
public static void main( String[] args ) {
f(); //(B)
}
}
examples/corpus/SpecialInt.java view on Meta::CPAN
//
//SpecialInt.java
class SpecialInt {
int i;
int accumulator;
SpecialInt( int m ) throws Exception {
if ( m > 100 || m < -100 ) throw new Exception();
i = m;
accumulator = m;
}
int getI() { return i; }
SpecialInt plus( SpecialInt sm ) throws Exception {
accumulator += sm.getI();
if ( accumulator > 100 || accumulator < -100 )
throw new Exception();
return this;
}
public static void main( String[] args ) throws Exception {
SpecialInt s1 = new SpecialInt( 4 );
SpecialInt s2 = new SpecialInt( 5 );
SpecialInt s3 = new SpecialInt( 6 );
SpecialInt s4 = new SpecialInt( 7 );
s1.plus( s2 ).plus( s3 ).plus( s4 );
System.out.println( s1.accumulator ); // 22
//SpecialInt s5 = new SpecialInt( 101 ); // range violation
}
}
examples/corpus/ThreadsBasicWithJoin.java view on Meta::CPAN
HelloThread( String message ) { this.message = message; }
public void run() {
//int sleeptime = (int) ( Math.random() * 3000 );
//try {
// sleep( sleeptime );
// } catch( InterruptedException e ){}
System.out.print( message );
}
public static void main(String[] args) throws InterruptedException
{
HelloThread ht1 = new HelloThread( "Good" );
HelloThread ht2 = new HelloThread( " morning" );
HelloThread ht3 = new HelloThread( " to" );
ht1.start();
ht2.start();
ht3.start();
ht1.join(); //(H)
examples/corpus/TryCatch.java view on Meta::CPAN
class Test {
public static void main( String[] args )
{
try {
f(0);
} catch( Err e ) {
System.out.println( "Exception caught in main" );
}
}
static void f(int j) throws Err {
System.out.println( "function f invoked with j = " + j );
if (j == 3) throw new Err();
f( ++j );
}
}
examples/corpus/WriteIntToFile.java view on Meta::CPAN
// Section: Section 6.9 I/O Streams For Java
//
//WriteIntToFile.java
import java.io.*;
class WriteIntToFile {
public static void main( String[] args ) throws Exception {
int anInt = 98; //(A)
FileOutputStream fos = new FileOutputStream( "out.fos" ); //(B)
fos.write( anInt ); //(C)
fos.close();
FileWriter fw = new FileWriter( "out.fw" ); //(D)
fw.write( anInt ); //(E)
fw.close();
examples/corpus/WriteStringToFile.java view on Meta::CPAN
//
// Section: Section 6.9.2 Writing Strings
//
//WriteStringToFile.java
import java.io.*;
class WriteStringToFile {
public static void main( String[] args ) throws Exception {
String aString = "hello"; //(A)
FileWriter fw = new FileWriter( "out.fw" ); //(B)
fw.write( aString ); //(C)
fw.close();
DataOutputStream dos = new DataOutputStream(
new FileOutputStream( "out.dos" ) ); //(D)
dos.writeBytes( aString ); //(E)
examples/corpus_with_java_and_cpp/CloneArray.java view on Meta::CPAN
public int[] arr = new int[5];
public X() {
Random ran = new Random();
int i=0;
while ( i < 5 )
arr[i++] = (ran.nextInt() & 0xffff)%10; //(A)
}
public Object clone() throws CloneNotSupportedException { //(B)
X xob = null;
xob = (X) super.clone();
//now clone the array separately:
xob.arr = (int[]) arr.clone(); //(C)
return xob;
}
public String toString() {
String printstring = "";
for (int i=0; i<arr.length; i++) printstring += " " + arr[i];
return printstring;
}
public static void main( String[] args ) throws Exception {
X xobj = new X();
X xobj_clone = (X) xobj.clone(); //(D)
System.out.println( xobj ); // 0 4 5 2 5
System.out.println( xobj_clone ); // 0 4 5 2 5
xobj.arr[0] = 1000; //(E)
System.out.println( xobj ); // 1000 4 5 2 5
System.out.println( xobj_clone ); // 0 4 5 2 5
examples/corpus_with_java_and_cpp/CloneBasic.java view on Meta::CPAN
//CloneBasic.java
class X implements Cloneable {
public int n;
public X() { n = 3; }
public Object clone() throws CloneNotSupportedException { //(A)
return super.clone(); //(B)
}
}
class Test {
public static void main( String[] args )
{
X xobj = new X();
X xobj_clone = null;
examples/corpus_with_java_and_cpp/CloneClassTypeArr.java view on Meta::CPAN
// http://programming-with-objects.com
//
//CloneClassTypeArr.java
class X implements Cloneable {
public int p;
public X( int q ) { p = q; }
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public String toString() { return p + ""; }
}
class Y implements Cloneable {
public X x;
public Y( X x ) { this.x = x; }
public Object clone() throws CloneNotSupportedException {
Y clone = (Y) super.clone();
clone.x = (X) x.clone();
return clone;
}
public String toString() { return x + ""; }
}
class Z implements Cloneable {
public Y[] yarr;
public Z( Y[] arr ) { this.yarr = arr; }
public Object clone() throws CloneNotSupportedException { //(A)
Z zclone = (Z) super.clone();
// zclone.yarr = ( Y[] ) yarr.clone(); // WRONG //(B)
Y[] yarrClone = new Y[ yarr.length ]; //(C)
for (int i=0; i < yarr.length; i++ )
yarrClone[i] = (Y) yarr[i].clone(); //(D)
zclone.yarr = yarrClone; //(E)
return zclone;
}
public String toString() {
String superString = "";
for ( int i = 0; i < yarr.length; i++ ) {
superString += yarr[i] + " ";
}
return superString;
}
}
class Test {
public static void main( String[] args ) throws Exception
{
X xobj0 = new X( 5 );
X xobj1 = new X( 7 );
Y yobj0 = new Y( xobj0 );
Y yobj1 = new Y( xobj1 );
Y[] yarr = new Y[2];
yarr[0] = yobj0;
yarr[1] = yobj1;
examples/corpus_with_java_and_cpp/ExceptionUsage1.java view on Meta::CPAN
class MyException extends Exception { //(A)
public MyException() { //(B)
super();
}
public MyException( String s ) { //(C)
super( s );
}
}
class Test {
static void f( ) throws MyException { //(D)
throw new MyException( "Exception thrown by function f()" );
}
public static void main( String[] args )
{
try {
f(); //(E)
} catch( MyException e ) {
System.out.println( e.getMessage() ); //(F)
}
}
examples/corpus_with_java_and_cpp/ExceptionUsage2.java view on Meta::CPAN
// http://programming-with-objects.com
//
//ExceptionUsage2.java
class MyException extends Exception {}
class Test {
static void f( ) throws MyException {}
public static void main( String[] args ) {
try {
f();
} catch( MyException e ) {}
}
}