Big Java / Java Concepts Lab 1

Becoming Familiar with your Computer

1.

To achieve flexibility, a computer must be programmed to >perform each task. A computer itself is a machine that stores data (numbers, words, pictures), interacts with devices (the monitor screen, the sound system, the printer), and executes programs. Programs are sequences of instructions and decisions that the computer carries out to achieve a task.

What are some examples of computer programs? List program types, not specific names.


2.

Use your operating system to locate the file HelloTester.java and look at its contents. (Here, we assume that the companion code for your text book is installed on your computer.)

What did you do to find HelloTester.java?


3.

Look inside the HelloTester.java file. How did you open the file?


4.

The program that you'll be running to write computer programs is your text editor, which sometimes is part of an integrated compiler environment. If you are working in a computer lab, ask your lab guide how to start the editor. If you purchased and installed your own compiler, then follow the vendor's instructions. Go ahead and start the text editor now.

What did you do to start your text editor?


5.

Compiling and Running Programs

Frequently in these labs you will be asked to compile a sample program. Below is a copy of a Java program that displays a drawing. Copy and paste it into your compiler's text editor. Save it as Art.java.

/**
    Displays an 'art' drawing
*/            
public class Art
{
     public static void main(String[] argv)
     {
      String s1 = " *   *   *   *   *   * ";
      String s2 = "   *   *   *   *   *   ";
      String s3 = "__________________________________\\n";
      String s4 = "_________________________________________________________\\n";          

      System.out.print(s4 + s1 + s3 + s2 + s3);      
      System.out.print(s1 + s3 + s2 + s3);      
      System.out.print(s1 + s3 + s2 + s3);      
      System.out.print(s1 + s3 + s2 + s3);      
      System.out.print(s4 + s4 + s4 + s4 + s4);
   }
}

Describe what you did to create the file Art.java.


6.

Once you have typed in (or pasted in) a program, you need to compile it to create an executable file. Again, these steps depend on your compilation environment. Determine the steps for your computer system, then go ahead and compile Art.java.

Describe what you did.


7.

Finally, it is time to execute the program. Once again, the steps depend on your computer system.

Describe what you did to execute the program.


8.

Describe what happened when the program executed.

Writing Simple Programs


9.

Your initial Java programs will be contained entirely in one file. There are some elements that all the programs will share because of the requirements of the Java language. When you build a program, your compiler looks for code of the form:

public class ClassName
{
    public static void main(String[] args)
    {
       /*
       your work goes here
    */
    }
}

The textbook has a program that prints the message Hello, World! on the screen.

Try changing it to display Hello, Universe!

Type the program into your compiler's editor, compile and test.

What program did you produce?

Detecting Syntax and Logic Errors


10.

There are numerous opportunities for errors in any program, often in places that seem too simple to require close attention.

What do you think the following program is designed to do?
public class Cube
{
    public static void main()
    {
       double height = 3.0; \\ inches
       double cubeVolume = height * height * height;
       double surfaceArea = 8 * height
       System.out.print("Volume = "
       System.out.println(cubeVolume);
       System.out.print("Surface area = );
       System.out.println(surfaceArea);
}


11.

Will it work as shown? If not, what problems can you identify?


12.

Try compiling the program. What were the results? (Supply the specific error messages that the compiler reported.)


13.

Fix the syntax errors. What program did you compile successfully?


14.

The program has two logic errors. Fix them and supply the corrected program.