class Car
{
void fullThrottle()
{
System.out.println("The car is going as fast as it can!");
}
void speed(int maxSpeed)
{
System.out.println("Max speed is: " + maxSpeed);
}
}
/*
javac Car.java
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
class Test
{
public static void main(String[] args)
{
Car myCar = new Car(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.speed(200); // Call the speed() method with argument `200'
}
}
/*
javac Car.java
javac Test.java
java Test
The car is going as fast as it can!
Max speed is: 200
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
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.
Open the terminal in this folder, which contains the folder overload.
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/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
/** Shows constructor and method overloading. */
package overload;
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
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
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/
*/