view release on metacpan or search on metacpan
examples/corpus/AbstractShapeIncremental.java view on Meta::CPAN
abstract protected double area();
abstract protected double circumference();
}
abstract class Polygon extends Shape {
protected int numVertices;
protected boolean starShaped;
}
abstract class curvedShape extends Shape {
abstract public void polygonalApprox();
}
class Circle extends curvedShape {
protected double r;
protected static double PI = 3.14159;
public Circle() { r = 1.0; }
public Circle( double r ) { this.r = r; }
public double area() { return PI*r*r; }
public double circumference() { return 2 * PI * r; }
public double getRadius() {return r;}
public void polygonalApprox() {
System.out.println(
"polygonal approximation code goes here");
}
}
class Rectangle extends Polygon {
double w, h;
public Rectangle() {
w=0.0; h = 0.0; numVertices = 0; starShaped = true;
}
public Rectangle( double w, double h ) {
this.w = w;
this.h = h;
numVertices = 4;
starShaped = true;
}
public double area() { return w * h; }
public double circumference() { return 2 * (w + h); }
public double getWidth() { return w; }
public double getHeight() { return h; }
}
class Test {
public static void main( String[] args )
{
Shape[] shapes = new Shape[ 3 ];
shapes[0] = new Circle( 2.0 );
shapes[1] = new Rectangle( 1.0, 3.0 );
shapes[2] = new Rectangle( 4.0, 2.0 );
double total_area = 0;
for (int i=0; i < shapes.length; i++ )
total_area += shapes[i].area();
System.out.println("Total area = " + total_area);
examples/corpus/AddArray.java view on Meta::CPAN
// For further information regarding the book, please visit
//
// http://programming-with-objects.com
//
//AddArray.java
public class AddArray { //(A)
public static void main( String[] args ) //(B)
{
int[] data = { 0, 1, 2, 3, 4, 5, 9, 8, 7, 6 }; //(C)
System.out.println( "The sum is: " //(D)
+ addArray(data) );
}
public static int addArray( int[] a ) { //(E)
int sum = 0;
for ( int i=0; i < a.length; i++ )
sum += a[i];
return sum;
}
}
examples/corpus/Animator.java view on Meta::CPAN
// http://programming-with-objects.com
//
//Animator.java
import java.applet.*;
import java.awt.*;
public class Animator extends Applet implements Runnable {
private String imageNameBase; //(A)
private int imageCount; //(B)
private Thread runner; //(C)
private Image image = null; //(D)
private Image[] imageArr; //(E)
public void init() { //(F)
imageNameBase = getParameter( "imagename" ); //(G)
imageCount = Integer.parseInt(
getParameter( "imagecount" ) ); //(H)
imageArr = new Image[ imageCount ];
int i = 0;
while ( i < imageCount ) {
String imageName = imageNameBase + i + ".gif";
imageArr[i] =
getImage( getDocumentBase(), imageName ); //(I)
MediaTracker tracker = new MediaTracker( this );
tracker.addImage( imageArr[i], 0 );
try {
tracker.waitForID( 0 );
} catch( InterruptedException e ) {}
i++;
}
}
public void start() { //(J)
runner = new Thread( this ); //(K)
runner.start(); //(L)
}
public void stop() { //(M)
runner = null; //(N)
}
public void paint( Graphics g ) { //(O)
if ( image == null ) return;
g.drawImage( image, 100, 100, this );
}
public void run() { //(P)
int i = 0;
while ( true ) {
image = imageArr[i];
i = ++i % imageCount;
try {
Thread.sleep( 200 );
} catch( InterruptedException e ){}
repaint(); //(Q)
}
}
examples/corpus/ArrayBasic.java view on Meta::CPAN
//ArrayBasic.java
class User {
String name;
int age;
public User( String nam, int yy ) {
name = nam;
age = yy;
}
}
class Test {
public static void main( String[] args ) {
User[] user_list = new User[ 4 ];
for ( int i=0; i<user_list.length; i++ )
System.out.print( user_list[ i ] + " " );
// null null null null
}
}
examples/corpus/ArraysFill.java view on Meta::CPAN
//ArraysFill.java
import java.util.*;
class Test {
public static void main( String[] args ) {
int[] intArr = new int[4];
Arrays.fill( intArr, 99 ); //(A)
for ( int i=0; i<intArr.length; i++ )
System.out.print( intArr[ i ] + " " ); // 99 99 99 99
System.out.println();
double[] dbArr = new double[4];
Arrays.fill( dbArr, 2, 3, 9.9 ); //(B)
for ( int i=0; i<dbArr.length; i++ )
examples/corpus/ArraysShuffle.java view on Meta::CPAN
//ArraysShuffle.java
import java.util.*;
class Test {
public static void main( String[] args ) {
Integer[] intArr2 = new Integer[10]; //(A)
for ( int i=0; i<intArr2.length; i++ ) //(B)
intArr2[i] = new Integer(i);
List list = Arrays.asList( intArr2 ); //(C)
Collections.shuffle( list ); //(D)
examples/corpus/AssignTest.java view on Meta::CPAN
// For further information regarding the book, please visit
//
// http://programming-with-objects.com
//
//AssignTest.java
class User {
public String name;
public int age;
public User( String str, int n ) { name = str; age = n; }
}
class Test {
public static void main( String[] args )
{
User u1 = new User( "ariel", 112 );
System.out.println( u1.name ); // ariel
User u2 = u1; //(A)
u2.name = "muriel"; //(B)
System.out.println( u1.name ); // muriel
}
}
examples/corpus/BlockInheritance.java view on Meta::CPAN
//
//BlockInheritance.java
class User {
private String name;
private int age;
public User( String str, int yy ) { name = str; age = yy; }
public void print() {
System.out.print( "name: " + name + " age: " + age );
}
}
//StudentUser cannot be extended
final class StudentUser extends User { //(A)
private String schoolEnrolled;
public StudentUser( String nam, int y, String sch ) {
super(nam, y);
schoolEnrolled = sch;
}
public void print() {
super.print();
System.out.println( " school: " + schoolEnrolled );
}
}
//Wrong:
//class UndergradStudentUser extends StudentUser { } //(B)
class Test {
public static void main( String[] args ) {
StudentUser us = new StudentUser(
"Zaphlet", 10, "Cosmology" );
us.print();
}
}
examples/corpus/BlockInheritance2.java view on Meta::CPAN
//
//BlockInheritance2.java
class User {
private String name;
private int age;
public User( String str, int yy ) { name = str; age = yy; }
//cannot be overridden:
final public void print() { //(C)
System.out.print( "name: " + name + " age: " + age );
}
}
//cannot be extended:
final class StudentUser extends User { //(D)
private String schoolEnrolled;
public StudentUser( String nam, int y, String sch ) {
super(nam, y);
schoolEnrolled = sch;
}
/*
public void print() { // ERROR //(E)
super.print();
System.out.println( "school: " + schoolEnrolled );
}
*/
}
class Test {
public static void main( String[] args ) {
StudentUser us = new StudentUser(
"Zaphlet", 10, "Cosmology" );
us.print(); //(F)
}
}
examples/corpus/BorderLayoutTest.java view on Meta::CPAN
//BorderLayoutTest.java
//additional files needed: snlowflake.gif, zwthr14.gif,
//thunderstormanim.gif, sunanim.gif
import java.awt.*; // for Container, BorderLayout
import java.awt.event.*; // for WindowAdapter
import javax.swing.*;
import javax.swing.border.*; // Border, BorderFactory
public class BorderLayoutTest {
public static void main( String[] args ) {
JFrame f = new JFrame( "BorderLayoutTest" );
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = f.getContentPane();
//the following is unnecessary since BorderLayout is default:
// contentPane.setLayout( new BorderLayout() );
//NORTH:
examples/corpus/BoxLayoutTest.java view on Meta::CPAN
//
//BoxLayoutTest.java
import java.awt.*; // for Container, BorderLayout
import java.awt.event.*; // for WindowAdapter
import javax.swing.*;
public class BoxLayoutTest {
public static void main( String[] args ) {
JFrame f = new JFrame( "BoxLayoutTest" );
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = f.getContentPane(); //(A)
String[] data = {"sunny", "hot", "stormy", "balmy", //(B)
"cold", "frigid", "rainy", "windy",
"snowy", "blistery", "blizzardy"};
examples/corpus/CardLayoutTest.java view on Meta::CPAN
//CardLayoutTest.java
import java.awt.*; // for Container, BorderLayout
import java.awt.event.*; // for WindowAdapter
import javax.swing.*;
import javax.swing.border.*; // for Border, BorderFactory
public class CardLayoutTest extends JFrame implements ItemListener {
JPanel cards;
final static String[] comboBoxItems
= {"frigid","balmy","stormy","sunny" };
public CardLayoutTest() {
Container contentPane = getContentPane();
JPanel comboPanel = new JPanel();
JComboBox c = new JComboBox( comboBoxItems ); //(A)
c.setEditable( false );
c.addItemListener( this ); //(B)
c.setBorder(
BorderFactory.createEmptyBorder( 20, 20, 20, 20 ) ); //(C)
comboPanel.add( c ); //(D)
contentPane.add( comboPanel, BorderLayout.NORTH );
cards = new JPanel() {
public Dimension getPreferredSize() { //(E)
Dimension size = super.getPreferredSize();
size.width = 200;
size.height = 200;
return size;
}
};
cards.setLayout( new CardLayout() );
//Card 1:
examples/corpus/CardLayoutTest.java view on Meta::CPAN
JLabel.CENTER );
fourthLabel.setVerticalTextPosition( JLabel.BOTTOM );
fourthLabel.setHorizontalTextPosition( JLabel.CENTER );
fourthLabel.setBorder(
BorderFactory.createLineBorder( Color.white ) );
cards.add( fourthLabel, "sunny" );
contentPane.add( cards, BorderLayout.CENTER );
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void itemStateChanged( ItemEvent evt ) { //(F)
CardLayout cl = (CardLayout) ( cards.getLayout() );
cl.show( cards, (String) evt.getItem() );
}
public static void main( String[] args ) {
CardLayoutTest window = new CardLayoutTest();
window.setTitle( "CardLayoutTest" );
window.setLocation( 200, 300 );
window.pack();
window.setVisible( true );
}
}
examples/corpus/CharEscapes.java view on Meta::CPAN
//
// http://programming-with-objects.com
//
//CharEscapes.java
class Test {
public static void main( String[] args ) {
String y1 = "a\u0062";
print( "y1:\t" + y1 ); // Printed output: ab
String y2 = "a\n";
print( "y2:\t" + y2 ); // Printed output: a
String y3 = "a\nbcdef";
print( "y3:\t"+ y3 ); // Printed output: a
// bcdef
examples/corpus/ChatServer.java view on Meta::CPAN
//
//ChatServer.java
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
public static List clientList = new ArrayList();
public static void main( String[] args ) {
try {
ServerSocket server = new ServerSocket( 5000 );
for (;;) {
Socket socket = server.accept();
System.out.print( "A new client checked in: " );
ClientHandler clh = new ClientHandler( socket );
clientList.add( clh );
clh.start();
}
} catch( Exception e ) { System.out.println( e ); }
examples/corpus/ChatServer.java view on Meta::CPAN
}
///////////////// class ClientHandler extends Thread ////////////////
class ClientHandler extends Thread {
private String userName;
private Socket sock;
private static List chatStore = new ArrayList();
private BufferedReader buff_reader = null;
private PrintWriter out = null;
public ClientHandler( Socket s ) {
try {
sock = s;
out = new PrintWriter( sock.getOutputStream() );
InputStream in_stream = sock.getInputStream();
InputStreamReader in_reader =
new InputStreamReader( in_stream );
buff_reader = new BufferedReader( in_reader );
// ask for user name
out.println( "\n\nWelcome to Avi Kak's chatroom");
examples/corpus/ChatServer.java view on Meta::CPAN
ListIterator iter = chatStore.listIterator();
while ( iter.hasNext() ) {
out.println( (String) iter.next() );
}
out.print("\n\n");
out.flush();
}
} catch( Exception e ) {}
}
public void run() {
try {
boolean done = false;
while ( !done ) {
out.print( userName + ": " );
out.flush();
String str = buff_reader.readLine();
if ( str.equals( "bye" ) ) {
str = userName + " signed off";
done = true;
examples/corpus/ClientSocket.java view on Meta::CPAN
//ClientSocket.java
import java.io.*;
import java.net.*;
class ClientSocket {
public static void main( String[] args )
{
try {
String webAddress = args[0];
String hostHeader = "Host: " + webAddress;
Socket socket = new Socket( webAddress, 80 );
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream( os, true );
examples/corpus/ClonableX.java view on Meta::CPAN
// For further information regarding the book, please visit
//
// http://programming-with-objects.com
//
//ClonableX.java
class X implements Cloneable {
public int n;
public X() { n = 3; }
public static void main( String[] args )
{
X xobj = new X();
X xobj_clone = null;
try {
xobj_clone = (X) xobj.clone(); //(A)
} catch (CloneNotSupportedException e){}
System.out.println( xobj.n ); // 3
System.out.println( xobj_clone.n ); // 3
examples/corpus/CloneArray.java view on Meta::CPAN
//
//CloneArray.java
import java.util.*; // for Random
class X implements Cloneable {
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
// For further information regarding the book, please visit
//
// http://programming-with-objects.com
//
//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;
try {
xobj_clone = (X) xobj.clone();
} catch (CloneNotSupportedException e){}
System.out.println( xobj.n ); // 3
System.out.println( xobj_clone.n ); // 3
examples/corpus/CloneClassTypeArr.java view on Meta::CPAN
// For further information regarding the book, please visit
//
// 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/CollectionMax.java view on Meta::CPAN
//
//CollectionMax.java
// code by Bracha, Odersky, Stoutamire, and Wadler
// with inconsequential changes by the author
interface Comparator {
public int compare( Object x, Object y );
}
class IntComparator implements Comparator {
public int compare( Object x, Object y ) {
return ( (Integer) x ).intValue() - ( (Integer) y ).intValue();
}
}
class Collections {
public static Object max( Collection xs, Comparator comp ) { //(A)
Iterator it = xs.iterator();
Object max = it.next();
while ( it.hasNext() ) {
Object next = it.next();
if ( comp.compare( max, next ) < 0 ) max = next;
}
return max;
}
}
class Test {
public static void main( String[] args ) {
// int list with int comparator:
LinkedList intList = new LinkedList(); //(B)
intList.add( new Integer( 0 ) );
intList.add( new Integer( 10 ) );
Integer max =
(Integer) Collections.max( intList, new IntComparator() );
System.out.println( "Max value: " + max.intValue() );
// string list with int comparator:
LinkedList stringList = new LinkedList();
examples/corpus/CollectionMaxGeneric.java view on Meta::CPAN
//
//CollectionMaxGeneric.java
// code by Bracha, Odersky, Stoutamire, and Wadler
// with inconsequential changes by the author
interface Comparator<T> {
public int compare( Object x, Object y ); //(A)
}
class IntComparator implements Comparator<Integer> { //(B)
public int compare( Object x, Object y ) {
return ( (Integer) x ).intValue() - ( (Integer) y ).intValue();
}
}
class Collections {
public static <T> T
max( Collection<T> coll, Comparator<T> comp ) { //(C)
Iterator<T> it = coll.iterator();
T max = it.next();
while ( it.hasNext() ) {
T next = it.next();
if ( comp.compare( max, next ) < 0 ) max = next;
}
return max;
}
}
class Test {
public static void main( String[] args ) {
// int list with int comparator
LinkedList<Integer> intList = new LinkedList<Integer>();
intList.add( new Integer( 0 ) );
intList.add( new Integer( 1 ) );
Integer m =
Collections.max( intList, new IntComparator() );
System.out.println( "Max value: " + m ); // 1
// string list with int comparator
examples/corpus/ConstructorOrderFoo.java view on Meta::CPAN
// For further information regarding the book, please visit
//
// http://programming-with-objects.com
//
//ConstructorOrderFoo.java
class Base {
public void foo(){ System.out.println( "Base's foo invoked" ); }
public Base() { foo(); }
}
class Derived extends Base {
public void foo(){ System.out.println( "Derived's foo invoked" ); }
public Derived() {}
}
class Test {
public static void main( String[] args )
{
Derived d = new Derived(); //Derived's foo() invoked
}
}
examples/corpus/CrazyWindow.java view on Meta::CPAN
import javax.swing.*; // for JTextArea, JPanel, etc.
import javax.swing.event.*; // for DocumentListener
import javax.swing.text.*; // for Document interface
import java.awt.*; // for Graphics, GridLayout, etc.
import java.awt.event.*; // for WindowAdapter
class CrazyWindow extends JFrame {
MyTextPanel panel1; //(A)
MyDrawPanel panel2; //(B)
public CrazyWindow() {
super( "Crazy Window" );
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit( 0 ) ;
}
});
JPanel contentPane = new JPanel();
contentPane.setLayout(new GridLayout(1, 2));
panel1 = new MyTextPanel(); //(C)
panel2 = new MyDrawPanel(); //(D)
contentPane.add( panel1 );
contentPane.add( panel2 );
setContentPane( contentPane );
}
class MyTextPanel extends JPanel {
class MyDocumentListener implements DocumentListener { //(E)
int lengthText;
StringBuffer word = new StringBuffer(""); //(F)
public void insertUpdate( DocumentEvent e ) { //(G)
Document doc = (Document) e.getDocument();
try {
lengthText = doc.getLength();
String currentChar =
doc.getText( lengthText - 1, 1 );
char ch =
currentChar.charAt( currentChar.length() - 1 );
if ( currentChar.equals( " " ) || ch == '\n' ) {
if ( word.toString().equals( "red" ) ) { //(H)
panel2.drawColoredSquare( "red" ); //(I)
examples/corpus/CrazyWindow.java view on Meta::CPAN
panel2.drawColoredSquare( "orange" );
}
word = new StringBuffer();
}
else //(J)
word = word.append( currentChar );
} catch( BadLocationException bad ) {
bad.printStackTrace();
}
}
public void removeUpdate( DocumentEvent e ) { //(K)
try {
Document doc = (Document) e.getDocument();
lengthText = doc.getLength();
String currentChar =
doc.getText( lengthText - 1, 1 );
char ch =
currentChar.charAt( currentChar.length() - 1 );
if ( currentChar.equals( " " ) || ch == '\n' ) {
word = new StringBuffer(); //(L)
}
else if ( word.length() >= 1 )
word =
word.deleteCharAt( word.length() - 1 ); //(M)
} catch( BadLocationException bad ) {
bad.printStackTrace();
}
}
public void changedUpdate( DocumentEvent e ) {} //(N)
}
public MyTextPanel() {
JTextArea ta = new JTextArea( 100, 60);
ta.getDocument().addDocumentListener(
new MyDocumentListener() );
ta.setEditable(true);
JScrollPane jsp = new JScrollPane( ta );
jsp.setPreferredSize(new Dimension( 150, 150));
add(jsp, "Center");
setBorder( BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("My Text Window"),
BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ) );
}
}
// panel2
class MyDrawPanel extends JPanel {
protected void paintComponent( Graphics g ) { }
public void drawColoredSquare( String color ) { //(O)
Graphics g = getGraphics();
g.translate( getInsets().left, getInsets().top );
int width = getBounds().width;
int height = getBounds().height;
if ( color.equals( "red" ) ) g.setColor( Color.red );
if ( color.equals( "green" ) ) g.setColor( Color.green );
if ( color.equals( "blue" ) ) g.setColor( Color.blue );
if ( color.equals( "orange" ) ) g.setColor( Color.orange );
if ( color.equals("magenta") ) g.setColor( Color.magenta );
int x = (int) ( Math.random() * width );
int y = (int) ( Math.random() * height );
if ( x > width - 30 ) x = x - 30;
if ( y > height - 30 ) y = y - 30;
g.fillRect( x, y, 30, 30 );
paintComponent( g ); //(P)
}
}
public static void main(String[] args)
{
JFrame wg = new CrazyWindow();
wg.setSize( 500, 400 );
wg.show();
}
}
examples/corpus/DBFriends1.java view on Meta::CPAN
// http://programming-with-objects.com
//
//DBFriends1.java
import java.sql.*;
class DBFriends1 {
public static void main( String[] args )
{
try {
Class.forName( "org.gjt.mm.mysql.Driver").newInstance();
String url = "jdbc:mysql:///test";
Connection con = DriverManager.getConnection( url );
Statement stmt = con.createStatement();
stmt.executeQuery( "SET AUTOCOMMIT=1" );
stmt.executeQuery( "DROP TABLE IF EXISTS Friends" );
stmt.executeQuery( "DROP TABLE IF EXISTS Rovers" );
examples/corpus/DBFriends2.java view on Meta::CPAN
// http://programming-with-objects.com
//
//DBFriends2.java
import java.sql.*;
class DBFriends2 {
public static void main( String[] args )
{
try {
Class.forName( "org.gjt.mm.mysql.Driver").newInstance();
String url = "jdbc:mysql:///test";
Connection con = DriverManager.getConnection( url );
Statement stmt = con.createStatement();
stmt.executeQuery( "SET AUTOCOMMIT=1" );
stmt.executeQuery( "DROP TABLE IF EXISTS Friends" );
stmt.executeQuery( "DROP TABLE IF EXISTS SportsClub" );
examples/corpus/Date.java view on Meta::CPAN
//
//Date.java
class Date {
private int d, m, y;
private static Date today = new Date( 31, 10, 2001 ); //(A)
public Date( int dd, int mm, int yy ) { //(B)
d = dd;
m = mm;
y = yy;
}
public Date( int dd, int mm ) { //(C)
d = dd;
m = mm;
y = today.y;
}
public Date( int dd ) { //(D)
d = dd;
m = today.m;
y = today.y;
}
public Date() { //(E)
d = today.d;
m = today.m;
y = today.y;
}
public static void setToday( int dd, int mm, int yy ) { //(F)
today = new Date(dd, mm, yy);
}
public void print() {
System.out.println( "day: " + d + " month: " + m
+ " year: " + y );
}
public static void main( String[] args ) {
Date d1 = new Date( 1, 1, 1970 );
d1.print(); // day: 1 month: 1 year: 1970
Date d2 = new Date( 2 );
d2.print(); // day: 2 month: 10 year: 2001
setToday(3, 4, 2000); //(G)
today.print(); // day: 3 month: 4 year: 2000
Date d3 = new Date( 7 );
d3.print(); // day: 7 month: 4 year: 2000
Date d4 = new Date();
d4.print(); // day: 3 month: 4 year: 2000
examples/corpus/DefaultInit.java view on Meta::CPAN
// http://programming-with-objects.com
//
//DefaultInit.java
class User {
private String name;
private int age;
public User() { name = "John Doe"; age = 25; }
public String toString() { return name + " " + age; }
}
class Test {
public static void main( String[] args ) {
//u1 declared but not defined:
User u1; //(A)
// System.out.println( u1 ); // ERROR //(B)
//u2 defined and initialized:
User u2 = new User(); //(C)
System.out.println( u2 ); // John Doe 25 //(D)
}
}
examples/corpus/DefaultInit2.java view on Meta::CPAN
// http://programming-with-objects.com
//
//DefaultInit2.java
class User {
public String name;
public int age;
public User() { name = "John Doe"; age = 25; } //(A)
public String toString() { return name + " " + age; } //(B)
}
class UserGroup {
public String groupName;
public User chief;
public int priority;
public User[] members;
public String toString() { //(C)
return groupName + " " + chief + " "
+ priority + " " + members ;
}
}
class Test {
public static void main( String[] args ) {
UserGroup ug = new UserGroup(); //(D)
System.out.println( ug ); // null null 0 null //(E)
}
}
examples/corpus/DefaultInit3.java view on Meta::CPAN
//
//DefaultInit3.java
class User {
public String name = "John Doe"; //(A)
public int age = 25; //(B)
public String toString() { return name + " " + age; }
}
class Test {
public static void main( String[] args ) {
User u = new User(); //(C)
System.out.println( u ); // John Doe 25 //(D)
}
}
examples/corpus/EnclosingClassAccess.java view on Meta::CPAN
//EnclosingClassAccess.java
class X {
private int regularIntEnclosing; //(A)
private static int staticIntEnclosing = 300; //(B)
public static class Y{
private int m;
private int n;
Y( X xref ) {
m = xref.regularIntEnclosing; //(C)
n = staticIntEnclosing; //(D)
}
}
public X( int n ) { regularIntEnclosing = n; } //(E)
}
class Test {
public static void main( String[] args ) {
X x = new X( 100 );
X.Y y = new X.Y( x ); //(F)
}
}