Anda di halaman 1dari 3

Exercise 1: Writing, Compiling, and Testing a Basic

Program
The objective of this exercise is for you to become familiar with the steps necessary to
write (or modify), compile, and test (execute) your Java technology programs.

Task 1 – Compiling and Executing a Java


Technology Program
In this task, you compile and execute your first Java technology program. Follow these
steps to compile and execute a ShirtTest program:

1. Go to the getstarted directory.

2. Open an editor, and enter in the Java technology syntax for the Shirt class
shown earlier in this module (the program source code).

Note – A copy of this code is provided at the end of these instructions.

3. Save the file.

4. Open an editor, and enter the ShirtTest class shown earlier in this module
(the program test file source code).

Note – A copy of this code is provided at the end of these instructions.

5. Save and close the file.

6. Open a terminal window.

7. Type the following command to compile the program into an executable bytecode
file.

javac Shirt.java

The file Shirt.class is created.

8. Type the following on the command line to compile the program into an
executable bytecode file.

javac ShirtTest.java

The file ShirtTest.class is created.


9. When the prompt reappears, type the following to run the program:

java ShirtTest

10. Open the Shirt.java file again.

11. Change the value of the price variable to 14.99 and the shirtID to
112244.

12. Save and close the file.

13. Compile the Shirt.java file again.

javac Shirt.java

14. Run the ShirtTest.class file again.

java ShirtTest

Task 2 – Creating, Compiling, and Executing Another


Java Technology Program
In this task, you create, compile, and execute your second Java technology program.
Follow these steps to create a class called Quotation that prints a quotation:

15. Open a text editor, and enter the following class:

public class Quotation {

String quote = "Welcome to Sun!";

public void display() {

System.out.println(quote);

}
}

16. Save the file as Quotation.java.

Note – Be sure to indent as shown; it makes the program much easier to read and
easier to debug.

17. Enter the following in the terminal window to compile the program:

javac QuotationTest.java
18. If no error messages appear, enter the following in the terminal window to run the
program:

java QuotationTest

19. Open the Quotation.java file again, and change the “Welcome toSun!”
text to your own favorite quotation. Be sure to leave the quotation marks at the
beginning and the end.

20. Compile and execute the program again.

Shirt.java
public class Shirt {

public int shirtID = 0; // Default ID for the shirt


public String description = "-description required-"; // default

// The color codes are R=Red, B=Blue, G=Green, U=Unset


public char colorCode = 'U';

public double price = 0.0; // Default price for all shirts

public int quantityInStock = 0; // Default quantity for all shirts

// This method displays the values for an item


public void displayShirtInformation() {

System.out.println("Shirt ID: " + shirtID);


System.out.println("Shirt description:" + description);
System.out.println("Color Code: " + colorCode);
System.out.println("Shirt price: " + price);
System.out.println("Quantity in stock: " + quantityInStock);

} // end of display method


} // end of class

ShirtTest.java
public class ShirtTest {

public static void main (String args[]) {

Shirt myShirt = new Shirt();

myShirt.displayShirtInformation();

}
}

Anda mungkin juga menyukai