javacodelearner2245
javacodelearner2245

Reputation: 11

Questions about creating UML Class diagram from Java program - default constructor? Are there no attributes in this example?

I have a class where I have to manually create a UML diagram for the program below. In order to understand better, I automatically created a UML diagram in Eclipse with ObjectAid. enter image description here

I have a few questions I would like to understand:

  1. Of the three sections class, attributes, and methods, why is "PayrollDialog()" included in the methods section? Is it because you should always include the default constructor?
  2. Are JOptionPane.showInputDialog really not considered for the methods section? Why not? I ask because I notice getting the user name, hours worked, and pay rate are not included.
  3. Are there really no attributes to be listed in the UML for this program? I think this is correct because they are not listed under the public class PayrollDialog.

Is the UML diagram listed really accurate for this program? If not, what should it look like? I find it hard to believe the assignment is that simple.

import javax.swing.JOptionPane;

/**
 4  * This program demonstrates using dialogs
 * with JOptionPane.
 */

public class PayrollDialog
{

    public static void main(String[] args)
    {
        String inputString;     // For reading input
        String name;            // The user's name
        int hours;              // The number of hours worked
        double payRate;         // The user's hourly pay rate
        double grossPay;        // The user's gross pay

        // Get the user's name.
        name = JOptionPane.showInputDialog("What is " +
                "your name? ");

        // Get the hours worked.
        inputString =
                JOptionPane.showInputDialog("How many hours " +
                        "did you work this week? ");

        // Convert the input to an int.
        hours = Integer.parseInt(inputString);

        // Get the hourly pay rate.
        inputString =
                JOptionPane.showInputDialog("What is your " +
                        "hourly pay rate? ");


        // Convert the input to a double.
        payRate = Double.parseDouble(inputString);

        // Calculate the gross pay.
        grossPay = hours * payRate;

        // Display the results.
        JOptionPane.showMessageDialog(null, "Hello " +
                name + ". Your gross pay is $" +
                grossPay);

        // End the program.
        System.exit(0);
    }
}

Upvotes: 1

Views: 427

Answers (1)

cabrillosa
cabrillosa

Reputation: 155

  1. Yes you are basically correct.

  2. JOptionPane is not listed in the Class Diagram because it is found in javax.swing.JOptionPane and its not globally declared as property/attribute or method of your PayrollDialog class.

  3. Based on your source code, there are no global properties/attribute declared.

For example

class PayrollDialog{
   static JFrame f; // this would appear as property in UML
   public static void main(String[] args){..}
}

Upvotes: 2

Related Questions