ICS 140. Programming Fundamentals. Lab 3.

 

Your Name:  _____________________________________________________________

Points: 15

Points earned:

 

You must attempt to complete the lab tonight. If you cannot, you may work on it outside and submit it by the beginning of the next class. But I will allow that only if you work on the problem in the assigned lab time. You may work alone or in a group of two. Your score will depend on how far you are able to complete the work and the correctness of the work.

 

Goals: To learn to create a simple GUI application, to learn how to use Math functions, and to learn how to debug a program.

 

Part I, answer questions.

 

  1. List the 4 primitive data types introduced in the class.  Give an example for each data type.

 

 

 

 

 

 

 

  1. What is the value (in the right representation) that is assigned to the variable x?

int m = 5;

int n = 2;

double y = m + n;

double x = y + m /n;

 

 

 

Part II, programming.

 

In this lab section, you are going to learn how to create a java program that uses Graphic User Interface (GUI) to communicate with user.  Java supplies an optional package called swing that helps you to write a GUI program quickly.  To use this package, you need to import it first.  The typical import statement will be

 

            import javax.swing.*;

 

The following is a GUI program that takes your name and one double number.  The correct program should output the square root of the number.

There are one syntax error and one logical error.  You are to correct these errors.

 

 

Step 1, use Textpad to create a java program called FirstGUI.  Save it in the A:\ICS140\Lab3 directory with file name FirstGUI.java.

Here is the program:

 

/**

       Name:

       Program: ICS 140 Lab3

This is a simple exercise to learn how to create a GUI java

program.

*/

 

import javax.swing.*;

 

public class FirstGUI {

       /**

              The main method. This is a must for all Java applications.

       */

       public static void main(String[] args) {

              String name;

              String n1Str;

              String message;

              double double1;

              double squareRoot;

 

name = JOptionPane.showInputDialog(“Please input your name”)

n1Str = JOptionPane.showInputDialog(

“Please input a double number”);

double1 = Double.parseDouble(n1Str);

double1 = double1 * double1;

squareRoot = Math.sqrt(double1);

message = “Your name is: “ + name +

          “\n The number you input is: “ + n1Str +

          “\n The square root of the number is: “ + squareRoot;

 

JOptionPane.showMessageDialog(null, message);

       }

}

 

Step 2, compile the program using Textpad.

Compile the program.  Do the following:

On the Tools menu, select “Compile Java”

Run the program.  Do the following:

On the Tools menu, select “Run Java Application”

 

Do the following before you turn in the lab.

1.      Document what the error are and the steps you taken to correct them.

 

 

 

 

 

 

 

 

 

 

2.  Capture the active window screen capture, which has the output of the program run; insert it into a Microsoft Word page and print it out.  Staple the print out to this lab.

 

Hand to your instructor the following:

1) this document, with screen capture stapled.