Professional Documents
Culture Documents
Exercise 1: Writing, Compiling, and Testing A Basic Program
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.
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
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.
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(); } }