Sunday, January 15, 2012

Lab 3: Windows

In this lab you will take the code you wrote in the previous lab, or my solution if you could not do it, and change it so that it uses a popup window instead of the console. Listing 2.12 of the textbook shows you how to do this. Also, the code below will pop up a window asking for the user's name followed by another window with the "Your name is.." message.

/**
 * @author Jose M Vidal 
 * JOptionPane minimal example
 */

import javax.swing.JOptionPane;

public class MainJoptionpane {

   public static void main(String[] args) {
    String name = JOptionPane.showInputDialog("Enter your name:");
    JOptionPane.showMessageDialog(null, "Your name is " + name);
    System.exit(0);
   }
}




After you get it working with the code from Lab 2, you will extend the program so that it accepts a double as input, which represents the number of dollars and cents, and then prints the amount of paper currency and coins the person will get. For example, if the person asks for 1.1 he will get 1 dollar and 1 dime. If he asks for deci-cents or less (for example, .001) you can just truncate those off.

The two screenshots shown on the right show you how your program should look like when run.

Hint: To get a line break in a String (so the next character show in the next line down) use the \n special character. For example, in the screenshot the first line comes from the string "You will get:\n".

As always, when you are done turn in your .java file in dropbox.cse.sc.edu.

Instructional Videos


The video below also shows how to build an application with JoptionPane




Optional video, just for learning: If you want to email the applications that you build in this class to other people so they can just double-click on them and run them (on windows, macs, and linux) then check out the video below which shows you how to export the apps you build with eclipse.



No comments: