Exercise 3-16 (Print, Overloading, Javadoc)

Chapter_3     Exercise_3-15     Package_Access Chapter_4







Exercise 3-16     TIJ, p. 62


Exercise 3-16: (1) In Chapter_6 (Initialization & Cleanup), locate the Overloading.java example and add Javadoc documentation. Extract this comment documentation into an HTML file using Javadoc and view it with your Web browser.




CONTENTS:     readme.txt     folder_overload   (package-info.java,  Print.java,  Overloading.java)




readme.txt


Folder ex16 (Exercise 16) contains the file readme.txt and folder overload (folder
doc is created when generating the javadoc). Open the terminal in folder ex16.

Compile Print.java:
javac overload/Print.java

Compile Overloading.java:
javac overload/Overloading.java

Run Overloading:
java overload/Overloading
java overload.Overloading

Generate Javadoc:
javadoc overload/Print.java overload/Overloading.java -d doc -package \
-link https://docs.oracle.com/en/java/javase/21/docs/api/











folder overload     (package-info.java,  Print.java,  Overloading.java)




package-info.java


/** Shows constructor and method overloading. */
package overload;





Print.java     TIJ, p. 151 (Chapter_7)


package overload;

import java.io.PrintStream;
/**
* Print methods that can be used without qualifiers,
* using Java SE5 static imports (see {@link overload.Overloading}).
* Utility class.
*/
public class Print
{
/** Print with a newline. */
public static void print(Object obj)
{
System.out.println(obj);
}
/** Print a newline by itself. */
public static void print()
{
System.out.println();
}
/** Print with no line break. */
public static void printnb(Object obj)
{
System.out.print(obj);
}
/** The new Java SE5 printf() (from C). */
public static PrintStream printf(String format, Object... args)
{
return System.out.printf(format, args);
}
}
/*
javac overload/Print.java
*/











Overloading.java     TIJ, p. 109-110 (Chapter_6)


package overload;

import static overload.Print.*;
/**
* Demonstration of both constructor
* and ordinary method overloading.
* A tree is defined by its height.
*/
class Tree
{
/** The height of the tree. */
int height;
/**
* Default constructor (no args).
* Prints information about planting the tree.
* Initial height is 0.
*/
Tree()
{
print("Planting a seedling");
height = 0;
}
/**
* Non-default constructor (one arg).
* Prints information about the new tree.
* @param initialHeight Initial height of the tree.
*/
Tree(int initialHeight)
{
height = initialHeight;
print("Creating new Tree that is " +
height + " feet tall");
}

/** Prints the current height of the tree. */
void info()
{
print("Tree is " + height + " feet tall");
}
/**
* Prints a message and the current height of the tree.
* @param s Message to be printed
*/
void info(String s)
{
print(s + ": Tree is " + height + " feet tall");
}
}

/**
* Main class of the program.
* Prints information about created trees.<br />
* Uses static import (of static methods) from class {@link overload.Print}:
* <pre>import static overload.Print.*;</pre>
*/
public class Overloading
{
/**
* Prints information about several trees.
* It acceesses both types of constructors and methods
* of {@link overload.Tree Tree}.
* @param args Command line arguments
* as an array of {@link java.lang.String String}s.
*/
public static void main(String[] args)
{
for(int i = 0; i < 5; i++)
{
Tree t = new Tree(i);
t.info();
t.info("overloaded method");
}

new Tree(); // Overloaded (default) constructor
}
}
/*
javac overload/Overloading.java
java overload/Overloading
java overload.Overloading
Creating new Tree that is 0 feet tall
Tree is 0 feet tall
overloaded method: Tree is 0 feet tall
Creating new Tree that is 1 feet tall
Tree is 1 feet tall
overloaded method: Tree is 1 feet tall
Creating new Tree that is 2 feet tall
Tree is 2 feet tall
overloaded method: Tree is 2 feet tall
Creating new Tree that is 3 feet tall
Tree is 3 feet tall
overloaded method: Tree is 3 feet tall
Creating new Tree that is 4 feet tall
Tree is 4 feet tall
overloaded method: Tree is 4 feet tall
Planting a seedling

javadoc overload/Print.java overload/Overloading.java -d doc -package \
-link https://docs.oracle.com/en/java/javase/21/docs/api/
*/




Notes:

For Print, see also Exercise_4-1 in Chapter_4.
For Javadoc, see the Notes at the end of Exercise_3-12.









Chapter_3     Exercise_3-15     Package_Access BACK_TO_TOP Chapter_4



Comments

Popular posts from this blog

Contents