IST 311 Lab Tips
9/25/18
Creating code, compiling and running Java programs:
To create a class named Demonstrate:
Open Notepad, enter code that declares the class Demonstrate and its components, fo example:
Save the code as Demonstrate.java (remember to specify the java extension and save it as file type All Files(*.*) in the jdk1.3\bin directory.
Note: public static void main(String argv[]) is the standard main method of java application code.
To compile the code:
Run the MS-DOS prompt.
CD\
CD jdk1.3\bin
Compile the java code as follows:
javac Demonstrate.java
[If you get no error, the compile is clean. Otherwise, correct the error(s) in Notepad, save the code and compile again.]
To run the program from C:\jdk1.3\bin
java Demonstrate.class
Stepwise refinement:
You can save each step by assigning a new name to the class and its java code. In the example below, each refinement increments the class name and java code file name by one.You can choose any naming strategy and maintain a trail of your changes.
Build a BoxCar class (BoxCar.java) with a main method that performs the calculation and prints the output:
public class BoxCar{
public static void main(String argv[])
{
System.out.print("The Box Car volume is: ");
System.out.println((12*10*50) + " cubic feet");
}// main()
}
Create a new BC1 class (BC1.java) to add class variables to hold length, width and height values, assign values to these variables within the main method and use the variables in the println statement:
public class BC1{
public static int length, width, height;
public static void main(String argv[])
{
length = 12; width = 10; height = 50;
System.out.print("The Box Car volume is: ");
System.out.println((length*width*height) + " cubic feet");
}// main()
}
Create a new BC2 class (BC2.java) to convert the class variables to instance variables. Instantiate BoxCar within the main method and use the variables in the println statement:
public class BC2{
public int length, width, height;
public static void main(String argv[])
{
BC2 b = new BC2();
b.length = 12; b.width = 10; b.height = 50;
System.out.print("The Box Car volume is: ");
System.out.println((b.length*b.width*b.height) + " cubic feet");
}// main()
}
Use Comments/Document Your Progress
//Lab 2 – Part 2public class BC2
{
public int length, width, height; //instance variables
public static void main(String argv[])
{
BC2 b = new BC2(); //create new instance
b.length = 12; b.width = 10; b.height = 50; //assign values
System.out.print("The Box Car volume is: ");
System.out.println((b.length*b.width*b.height) + " cubic feet"); //calculate & print
}// main()
} //BC2