[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

create an object of a class

Posted on 2004-11-13
8
Medium Priority
?
211 Views
Last Modified: 2013-11-23
I am writing a program that calls two other programs, I have all of the written but I am having trouble getting the main program to get and send the right information from the other programs and I was hoping for some help. I can not seem to get it to send the numbers to the wage program or the commission program and have it the values sent back.

this is the main program:
// Tami Hall
// Case Chapter 9
// EmployeePay.java
// Nov 5, 2004

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;
import java.text.DecimalFormat;

public class EmployeePay2 extends JApplet implements ActionListener
{
        JButton nameButton = new JButton("Please enter your name:");
      //creates a JButton to prepair to calculate with the label Calculate Annual Appliance Cost
   JLabel outputLabel = new JLabel ();//sets up new label named outputLabel
      JLabel outputLabel2 = new JLabel();
      JTextField tName = new JTextField(20);
      FlowLayout flow = new FlowLayout();
   Container con = getContentPane();// sets up the container con
      String x = "x";
      String X = "X";

   public void init()
   {
      con.setLayout(flow);// row components positioned to the right
      con.add(nameButton);//this tells it to add the calcButton
      con.add(outputLabel);//container for the outputLabel
            con.add(outputLabel2);
            con.add(tName);
      nameButton.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e)
   {
       Object source = e.getSource();
       if (source == nameButton)
     {
         //Converting input to values
         String hours = JOptionPane.showInputDialog(null,"Please enter hours worked or press x to exit:");
            char cHours = hours.charAt(0);
            
            if(Character.isLetter(cHours))
                  {
                        if(hours.equals(x)||hours.equals(X))
                        {
                              System.exit(0);
                        }
                        else
                        {
                        JOptionPane.showMessageDialog(null, "Invalad Entry! Only numeric values or x is valid!");
                        hours = JOptionPane.showInputDialog(null, "Please enter hours worked or press x to exit:");
                        cHours = hours.charAt(0);
                        }
                  }
                        
                  if(Character.isDigit(cHours))
                  {
                        if(cHours> 12)
                        {
                        JOptionPane.showMessageDialog(null, "Hours worked may not be more than 12. Please reenter hours.");
                        hours = JOptionPane.showInputDialog(null, "Please enter hours worked or press x to exit:");
                        }
                        else
                        {
                        double dHours = Double.parseDouble(hours);//Converts the strings into Doubles
                        }
                  }
                        
            }
                        
         String sales = JOptionPane.showInputDialog(null,"Please enter total amount of sales or press x to exit:");
            char cSales = sales.charAt(0);
            
            if(Character.isLetter(cSales))
                  {
                        if(sales.equals(x)||sales.equals(X))
                        {
                              System.exit(0);
                        }
                        else
                        {
                        JOptionPane.showMessageDialog(null, "Invalad Entry! Only numeric values or x is valid!");
                        sales = JOptionPane.showInputDialog(null,"Please enter total amout of sales or press x to exit:");
                        cSales = sales.charAt(0);
                        }
                  }
            Double tempSales = Double.valueOf(sales);            
            double dSales = Double.parseDouble(sales);//Converts the strings into Doubles
            
            CalculateWage sentWage = new CalculateWage();
            CalculateCommission sentComm = new CalculateCommission();
            //create an object from the CalculateWage class
            
            sentComm.getSales(dSales);
            //to pass the variable dSales to the getSales method in the CalculateCommission class
            
            sentWage.getHours(dHours);
            //to pass the variable dHours to the getHours method in the CalculateWage class
            
          remove (tName);
             remove(nameButton);
             repaint();
             
             FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
             con.setLayout(flow);
             
         outputLabel.setLocation(0,0);    
      //      outputLabel.setText (wageAmt + "\t\t hours worked");
            outputLabel2.setLocation(100,100);

            outputLabel2.setText (sentComm + "\t\t sales");
             
     
   }
}

this is the next program:
//CalculateCommission.java

public class CalculateCommission
{
      double commRate;
      double saleAmt;
      double Sales;

      public void getSales(double s)//method to the the sales amount
      {
            saleAmt = s;
            //gets the sale amount that was passed to it from EmployeePay class
      }


      public double commission()
      {
            if(saleAmt>=750.00)
            commRate = .15;

            else if(saleAmt>100.00)
            commRate = .10;

            else commRate = 0.0;
            
            return commRate;
      }
}
 and the next:
//CalculateWage.java

public class CalculateWage
{
      double rate = 10;
      double Hours;
      double overtimePay;
      double regularPay;
      double calPay;
      double pay;
      
      
      public void getHours(double hours)//method to the the sales amount
      {
            calPay = hours;
            //gets the sale amount that was passed to it from EmployeePay class
      }


      public double wages()
      {
      
            if(calPay > 8)
            {
                  regularPay = 8*rate;
                  overtimePay = (calPay - 8) * 1.5 * rate;
                  //time and a half for hours over 8
            }
            else
            {
                  regularPay = calPay * rate;
                  overtimePay = 0.0;
            }
            
            return wages()
            ;
      }
}
0
Comment
Question by:tamihall
7 Comments
 
LVL 2

Expert Comment

by:kjayaraman
ID: 12577307
Rectify the following errors and check

CalculateWage.java
Change the return statement in wages() to
return regularPay+overtimePay ;

EmployeePay2.java

The main problem is here
if (source == nameButton)
     {
        //Converting input to values
        String hours = JOptionPane.showInputDialog(null,"Please enter hours worked or press x to exit:");
         char cHours = hours.charAt(0);
         
          if(Character.isLetter(cHours))
               {
                    if(hours.equals(x)||hours.equals(X))
                    {
                         System.exit(0);
                    }
                    else
                    {
                    JOptionPane.showMessageDialog(null, "Invalad Entry! Only numeric values or x is valid!");
                    hours = JOptionPane.showInputDialog(null, "Please enter hours worked or press x to exit:");
                    cHours = hours.charAt(0);
                    }
               }
                   
               if(Character.isDigit(cHours))
               {
                    /*** This is the error line **/ if(cHours> 12)
                    {
                    JOptionPane.showMessageDialog(null, "Hours worked may not be more than 12. Please reenter hours.");
                    hours = JOptionPane.showInputDialog(null, "Please enter hours worked or press x to exit:");
                    /** Add return so that further processing is stopped after a validation fails **/
                    }
                    else
                    {

                    /** This is error number 2 **/double dHours = Double.parseDouble(hours);//Converts the strings into Doubles
                    }
               }
                   
          }

You are checking for if (cHours >12). Note cHours is a character and it converts cHours to the ascii value and this would always be greater than 12. Anywayz, convert it to an integer. I dont understand your logic to use a double. If you insist on double use it [hopefully people work 10.875 hrs ;) ] So, convert hours to double, the one you are doing in the else part before the if [to check if hours is greater than 12.] And for the next error. Take the dHours outside the if so that it is accessible by other part of the code. Note if you declare a variable inside a block it becomes local to the block and cannot be accessed ouside the {}/

The fixed code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;
import java.text.DecimalFormat;

public class EmployeePay2 extends JApplet implements ActionListener
{
       JButton nameButton = new JButton("Please enter your name:");
     //creates a JButton to prepair to calculate with the label Calculate Annual Appliance Cost
   JLabel outputLabel = new JLabel ();//sets up new label named outputLabel
     JLabel outputLabel2 = new JLabel();
     JTextField tName = new JTextField(20);
     FlowLayout flow = new FlowLayout();
   Container con = getContentPane();// sets up the container con
     String x = "x";
     String X = "X";

   public void init()
   {
      con.setLayout(flow);// row components positioned to the right
      con.add(nameButton);//this tells it to add the calcButton
      con.add(outputLabel);//container for the outputLabel
          con.add(outputLabel2);
          con.add(tName);
      nameButton.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e)
   {
      Object source = e.getSource();
         double dHours = 0;
      if (source == nameButton)
     {
        //Converting input to values
        String hours = JOptionPane.showInputDialog(null,"Please enter hours worked or press x to exit:");
          char cHours = hours.charAt(0);
         
          if(Character.isLetter(cHours))
               {
                    if(hours.equals(x)||hours.equals(X))
                    {
                         System.exit(0);
                    }
                    else
                    {
                    JOptionPane.showMessageDialog(null, "Invalad Entry! Only numeric values or x is valid!");
                    hours = JOptionPane.showInputDialog(null, "Please enter hours worked or press x to exit:");
                              cHours = hours.charAt(0);
                    }
               }
                   
               if(Character.isDigit(cHours))
               {
                   dHours = Double.parseDouble(hours);//Converts the strings into Doubles
                              if(dHours> 12)
                    {
                    JOptionPane.showMessageDialog(null, "Hours worked may not be more than 12. Please reenter hours.");
                    hours = JOptionPane.showInputDialog(null, "Please enter hours worked or press x to exit:");
                              cHours = hours.charAt(0);
                    }
                   
               }
                   
          }
                   
        String sales = JOptionPane.showInputDialog(null,"Please enter total amount of sales or press x to exit:");
          char cSales = sales.charAt(0);
         
          if(Character.isLetter(cSales))
               {
                    if(sales.equals(x)||sales.equals(X))
                    {
                         System.exit(0);
                    }
                    else
                    {
                              /* Change this logic. Note, if the user enters an invalid entry again, the check is not done.
                              Might want to use a while loop instead of 'if'. Or do a 'return' to break further processing */
                    JOptionPane.showMessageDialog(null, "Invalad Entry! Only numeric values or x is valid!");
                    sales = JOptionPane.showInputDialog(null,"Please enter total amout of sales or press x to exit:");
                    cSales = sales.charAt(0);
                    }
               }
          Double tempSales = Double.valueOf(sales);          
          double dSales = Double.parseDouble(sales);//Converts the strings into Doubles
          CalculateWage sentWage = new CalculateWage();
          CalculateCommission sentComm = new CalculateCommission();
          //create an object from the CalculateWage class
         
          sentComm.getSales(dSales);
          //to pass the variable dSales to the getSales method in the CalculateCommission class
         
          sentWage.getHours(dHours);
          //to pass the variable dHours to the getHours method in the CalculateWage class
         
         remove (tName);
           remove(nameButton);
           repaint();
           
           FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
           con.setLayout(flow);
           
        outputLabel.setLocation(0,0);    
          outputLabel.setText (sentWage.wages() + "\t\t hours worked");
          outputLabel2.setLocation(100,100);

          outputLabel2.setText (sentComm.commission() + "\t\t sales");
           
     
   }
}


public class CalculateWage
{
     double rate = 10;
     double Hours;
     double overtimePay;
     double regularPay;
     double calPay;
     double pay;
     
     
     public void getHours(double hours)//method to the the sales amount
     {
          calPay = hours;
          //gets the sale amount that was passed to it from EmployeePay class
     }


     public double wages()
     {
     
          if(calPay > 8)
          {
               regularPay = 8*rate;
               overtimePay = (calPay - 8) * 1.5 * rate;
               //time and a half for hours over 8
          }
          else
          {
               regularPay = calPay * rate;
               overtimePay = 0.0;
          }
         
          return regularPay+overtimePay ;
     }
}



Some more bugs in the code. To be continued ... ;)


0
 
LVL 2

Expert Comment

by:kjayaraman
ID: 12577313
Good now that your code might have some limited functionality. Try to correct the validation strategy.

Try adding 100 for hours, It throws you an error message... Now reenter 100... :)) Now your code silently accepts it assuming that the user enters the correct number of hours for the second time. Work on your 'if' clauses that you use for validations. Try out a strategy like this
while (condition is false) { Show error message; Get the data again from the user; set the paramters so that the condition in the whle is checked with the new data }
Or..
if (condition is false) { show error message; stop processing, rollback all changes and revert to the initial state from which the user can restart the whole process }

Sounds good?
0
 

Author Comment

by:tamihall
ID: 12579248
This is great, but when I get ready to display it am I going to be able to show BOTH overtime and regular pay? I need to show what is what. This might seem like a silly question but I am really new at this.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tamihall
ID: 12579923
Thanks for your help but I got it myself. I was able to fix what I needed to fix.
0
 

Author Comment

by:tamihall
ID: 12579956
I had to totally redo the wage program and seperate the amounts into two different methods.
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12582172
So you say you do not need the question any more?
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12739460
Closed, 250 points refunded.

modulo
Community Support Moderator
Experts Exchange
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question