6 Replies Latest reply: Jun 15, 2008 3:49 AM by 807601 RSS

    Need help with constructors....ASAP!!

    807601
      I'm working on a project for school and I'm can't get my object to instantiate. Below id the code for the Class that holds the data fields, and the Class that prompts the user for input. If anyone can tell me what I'm doing wrong and how to fix it it would be greatly appreciated.

      This is the data field class with constructor.


      {color:#0000ff}public class Delivery
      {
      private int year;
      private int month;
      private int deliveryNum;
      private int finalDeliveryNum;
      private double weight;
      private double fee;
      private int method;
      public int getYear()
      {
      return year;
      }
      public void setYear(int inputYear)
      {
      year = (inputYear - 2000) * 100000;
      }
      public int getMonth()
      {
      return month;
      }
      public void setMonth(int inputMonth)
      {
      month = inputMonth * 1000;
      }
      public int getDeliveryNum()
      {
      return deliveryNum;
      }
      public void setDeliveryNum(int inputDeliveryNum)
      {
      deliveryNum = inputDeliveryNum;
      }
      public int getFinalDeliveryNum()
      {
      return finalDeliveryNum;
      }
      public void setFinalDeliveryNum()
      {
      finalDeliveryNum = year + month + deliveryNum;
      }
      public double getWeight()
      {
      return weight;
      }
      public void setWeight(double inputWeight)
      {
      weight = inputWeight;
      }
      public int getMethod()
      {
      return method;
      }
      public void setMethod(int inputMethod)
      {
      method = inputMethod;
      }
      public double getFee()
      {
      return fee;
      }
      public void setFee(int inputMethod, double inputWeight)
      {
      while(inputMethod == 1)
      {
      if(inputWeight < 5.0)
      fee = 12.00;
      else if(inputWeight >= 5.0 && inputWeight <= 20.0)
      fee = 16.50;
      else
      fee = 22.00;
      }
      while(inputMethod == 2)
      {
      if(inputWeight <= 5.0)
      fee = 35.00;
      else
      fee = 47.95;
      }
      }
      }
      {color}



      and this is the Class that prompts the user for input. After running this application all fields are 0's except for Delivery Area.


      {color:#0000ff}import javax.swing.JOptionPane;
      public class CreateDelivery
      {
      public static void main(String[] args)
      {
      Delivery newDelivery = new Delivery();
      String enterYear, enterMonth, enterDeliveryNum, enterMethod, enterWeight;
      int inputYear, inputMonth, inputDeliveryNum, inputMethod;
      double inputWeight;
      String displayMethod = " ";
      enterYear = JOptionPane.showInputDialog(null, "Please enter the year.\n2001 thru 2009 are the only valid entries.");
      inputYear = Integer.parseInt(enterYear);
      while(inputYear < 2001 || inputYear > 2009)
      {
      enterYear = JOptionPane.showInputDialog(null, "Invalid Entry.\nPlease enter the year.\n2001 thru 2009 are the only valid entries.");
      inputYear = Integer.parseInt(enterYear);
      }
      enterMonth = JOptionPane.showInputDialog(null, "Please enter the month.\n Enter 1 for January, 2 for February, etc..");
      inputMonth = Integer.parseInt(enterMonth);
      while(inputMonth < 1 || inputMonth > 12)
      {
      enterMonth = JOptionPane.showInputDialog(null, "Invalid Entry.\nPlease enter the month.\n Enter 1 for January, 2 for February, etc..");
      inputMonth = Integer.parseInt(enterMonth);
      }
      enterDeliveryNum = JOptionPane.showInputDialog(null, "Please enter the delivery number.\n A valid number is 1 thru 999.");
      inputDeliveryNum = Integer.parseInt(enterDeliveryNum);
      while(inputDeliveryNum < 1 || inputDeliveryNum > 999)
      {
      enterDeliveryNum = JOptionPane.showInputDialog(null, "Invalid Entry.\nPlease enter the delivery number.\n A valid number is 1 thru 999.");
      inputDeliveryNum = Integer.parseInt(enterDeliveryNum);
      }
      enterMethod = JOptionPane.showInputDialog(null, "Please enter the delivery are.\n\n1 - Local\n2 - Long Distance");
      inputMethod = Integer.parseInt(enterMethod);
      while(inputMethod != 1 && inputMethod != 2)
      {
      enterMethod = JOptionPane.showInputDialog(null, "Invalid Entry.\nPlease enter the delivery are.\n\n1 - Local\n2 - Long Distance");
      inputMethod = Integer.parseInt(enterMethod);
      }
      if(inputMethod == 1)
      displayMethod = "Local";
      else
      displayMethod = "Long Distance";
      enterWeight = JOptionPane.showInputDialog(null, "Please enter the weight of the package.\n Enter a wieight from 0.10lbs to 100lbs.");
      inputWeight = Double.parseDouble(enterWeight);
      while(inputWeight < 0.01 || inputWeight > 100)
      {
      enterWeight = JOptionPane.showInputDialog(null, "Invalid Entry.\nPlease enter the weight of the package.\n Enter a wieight from 0.10lbs to 100lbs.");
      inputWeight = Double.parseDouble(enterWeight);
      }
      JOptionPane.showMessageDialog(null, "Delivery Details\n\n" + "Delivery Number: " + newDelivery.getFinalDeliveryNum() + "\nDelivery Distance: " + displayMethod + "\nPackage Weight: " + newDelivery.getWeight() + "\nDelivery Fee: " + newDelivery.getFee());
      }
      }{color}
        • 1. Re: Need help with constructors....ASAP!!
          807601
          Please post your code using the code tags. You put {code} at the start of your code and the same thing again at the end. That way your code is properly formatted when it appears here. (It's a good idea to use spaces rather than tabs to indent the code you post.)
          I'm can't get my object to instantiate. Below id the code for the Class that holds the data fields, and the Class that prompts the user for input.
          You instantiate a class by using the "new" keyword.
          import javax.swing.JOptionPane;
          
          public class Eg {
              private int num;
               
              public int getNum() {
                  return num;
              }
               
              public void setNum(int i) {
                  num = i;
              }
          }
          
          class NewEg {
              public static void main(String[] args) {
                  String str = JOptionPane.showInputDialog(null, "Enter an integer");
                  int i = Integer.parseInt(str);
                  while(i <=0) {
                      str = JOptionPane.showInputDialog(null, "Only positive integers please");
                      i = Integer.parseInt(str);
                  }
                      // use new to instantiate the Eg class
                  Eg test = new Eg();
                      // use setters to set the objects state.
                  test.setNum(i);
          
                  JOptionPane.showMessageDialog(null, "The value is: " + test.getNum());
              }
          }
          This example uses the default no-arg constructor. It would be usual to provide the Eg class with a constructor that sets the object's state at the time it is instantiated.
          import javax.swing.JOptionPane;
          
          public class Eg {
              private int num;
               
                  // the constructor will set the value of num
              public Eg(int num) {
                  this.num = num;
              }
               
              public int getNum() {
                  return num;
              }
               
              public void setNum(int i) {
                  num = i;
              }
          }
          
          class NewEg {
              public static void main(String[] args) {
                  String str = JOptionPane.showInputDialog(null, "Enter an integer");
                  int i = Integer.parseInt(str);
                  while(i <=0) {
                      str = JOptionPane.showInputDialog(null, "Only positive integers please");
                      i = Integer.parseInt(str);
                  }
                      // the purpose built constructor eliminates the need to call
                      // the setter methods at the time the object is instantiated
                  Eg test = new Eg(i);
          
                  JOptionPane.showMessageDialog(null, "The value is: " + test.getNum());
              }
          }
          • 2. Re: Need help with constructors....ASAP!!
            807601
            Ok, I didn't know how to post the code in that format, so I will repost it here to make it easier to read. And that did not really answer my question. I have 2 seperate Classes, one for data fields and one for input. They both compile fine, but when i run the CreateDelivery class I get 0's where I should get values. I am new to Java so I have not learned all of the terminology yet. Anyway, here is the code again.

            The Delivery Class, for storing the data.

            public class Delivery
            {
                private int year;
                private int month;
                private int deliveryNum;
                private int finalDeliveryNum;
                private double weight;
                private double fee;
                private int method;
                public Delivery()
                {
                    year = 0;
                    month = 0;
                    deliveryNum = 0;
                    method = 0;        
                    weight = 0;
                }
                public int getYear()
                {
                    return year;
                }
                public void setYear(int inputYear)
                {
                    year = (inputYear - 2000) * 100000;
                }
                public int getMonth()
                {
                    return month;
                }
                public void setMonth(int inputMonth)
                {
                    month = inputMonth * 1000;
                }
                public int getDeliveryNum()
                {
                    return deliveryNum;
                }
                public void setDeliveryNum(int inputDeliveryNum)
                {
                    deliveryNum = inputDeliveryNum;
                }
                public int getFinalDeliveryNum()
                {
                    return finalDeliveryNum;
                }
                public void setFinalDeliveryNum()
                {
                    finalDeliveryNum = year + month + deliveryNum;
                }
                public double getWeight()
                {
                    return weight;
                }
                public void setWeight(double inputWeight)
                {
                    weight = inputWeight;
                }
                public int getMethod()
                {
                    return method;
                }
                public void setMethod(int inputMethod)
                {
                    method = inputMethod;
                }
                public double getFee()
                {
                    return fee;
                }
                public void setFee(int inputMethod, double inputWeight)
                {
                    while(inputMethod == 1)
                    {
                        if(inputWeight < 5.0)
                            fee = 12.00;
                        else if(inputWeight >= 5.0 && inputWeight <= 20.0)
                            fee = 16.50;
                        else
                            fee = 22.00;
                    }
                    while(inputMethod == 2)
                    {
                        if(inputWeight <= 5.0)
                            fee = 35.00;
                        else
                            fee = 47.95;
                    }
                }
            
            }
            The CreateDelivery Class, for entering the data.
            import javax.swing.JOptionPane;
            public class CreateDelivery
            {
                public static void main(String[] args)
                {
                    Delivery newDelivery = new Delivery();
                    String enterYear, enterMonth, enterDeliveryNum, enterMethod, enterWeight;
                    int inputYear, inputMonth, inputDeliveryNum, inputMethod;
                    double inputWeight;
                    String displayMethod  = " ";
                    enterYear = JOptionPane.showInputDialog(null, "Please enter the year.\n2001 thru 2009 are the only valid entries.");
                    inputYear = Integer.parseInt(enterYear);
                    while(inputYear < 2001 || inputYear > 2009)
                    {
                        enterYear = JOptionPane.showInputDialog(null, "Invalid Entry.\nPlease enter the year.\n2001 thru 2009 are the only valid entries.");
                        inputYear = Integer.parseInt(enterYear);
                    }
                    enterMonth = JOptionPane.showInputDialog(null, "Please enter the month.\n Enter 1 for January, 2 for February, etc..");
                    inputMonth = Integer.parseInt(enterMonth);
                    while(inputMonth < 1 || inputMonth > 12)
                    {
                        enterMonth = JOptionPane.showInputDialog(null, "Invalid Entry.\nPlease enter the month.\n Enter 1 for January, 2 for February, etc..");
                        inputMonth = Integer.parseInt(enterMonth);
                    }
                    enterDeliveryNum = JOptionPane.showInputDialog(null, "Please enter the delivery number.\n A valid number is 1 thru 999.");
                    inputDeliveryNum = Integer.parseInt(enterDeliveryNum);
                    while(inputDeliveryNum < 1 || inputDeliveryNum > 999)
                    {
                        enterDeliveryNum = JOptionPane.showInputDialog(null, "Invalid Entry.\nPlease enter the delivery number.\n A valid number is 1 thru 999.");
                        inputDeliveryNum = Integer.parseInt(enterDeliveryNum);
                    }
                    enterMethod = JOptionPane.showInputDialog(null, "Please enter the delivery are.\n\n1 - Local\n2 - Long Distance");
                    inputMethod = Integer.parseInt(enterMethod);
                    while(inputMethod != 1 && inputMethod != 2)
                    {
                        enterMethod = JOptionPane.showInputDialog(null, "Invalid Entry.\nPlease enter the delivery are.\n\n1 - Local\n2 - Long Distance");
                        inputMethod = Integer.parseInt(enterMethod);
                    }
                        if(inputMethod == 1)
                            displayMethod = "Local";
                        else
                            displayMethod = "Long Distance";
                    enterWeight = JOptionPane.showInputDialog(null, "Please enter the weight of the package.\n Enter a wieight from 0.10lbs to 100lbs.");
                    inputWeight = Double.parseDouble(enterWeight);
                    while(inputWeight < 0.01 || inputWeight > 100)
                    {
                        enterWeight = JOptionPane.showInputDialog(null, "Invalid Entry.\nPlease enter the weight of the package.\n Enter a wieight from 0.10lbs to 100lbs.");
                        inputWeight = Double.parseDouble(enterWeight);
                    }
                    JOptionPane.showMessageDialog(null, "Delivery Details\n\n" + "Delivery Number: " + newDelivery.getFinalDeliveryNum() + "\nDelivery Distance: " + displayMethod + "\nPackage Weight: " + newDelivery.getWeight() + "\nDelivery Fee: " + newDelivery.getFee());
                }
            }
            • 3. Re: Need help with constructors....ASAP!!
              807601
              when i run the CreateDelivery class I get 0's where I should get values.
              That's because in the main() method of CreateDelivery you create a delivery (with "new Delivery()") but you never use the setter methods of the Delivery class to set the state of the delivery you created.

              Compare this with the first version of NewEg that I posted. It includes both instantiation using the new keyword, and a call to the object's setter method.
              • 4. Re: Need help with constructors....ASAP!!
                807601
                Ok, almost there. Everything works now except the set/get for the Fee method. When I get to the end of the app the last dialog box closes and it then goes into and indefinate loop. That part of the code is posted below. I can't see where the loop is occurring.

                public void setFee(int inputMethod, double inputWeight)
                     {
                          fee = fee;
                     }
                     public double getFee()
                     {
                          while(method == 1)
                          {
                               if(weight < 5.0)
                                    fee = 12.00;
                               else if(weight >= 5.0 && weight <= 20.0)
                                    fee = 16.50;
                               else
                                    fee = 22.00;
                          }
                          while(method == 2)
                          {
                               if(weight <= 5.0)
                                    fee = 35.00;
                               else
                                    fee = 47.95;
                          }
                          return fee;
                     }
                • 5. Re: Need help with constructors....ASAP!!
                  807601
                  TrailorParkKid wrote:
                  Ok, almost there. Everything works now except the set/get for the Fee method. When I get to the end of the app the last dialog box closes and it then goes into and indefinate loop. That part of the code is posted below. I can't see where the loop is occurring.

                  public void setFee(int inputMethod, double inputWeight)
                       {
                            fee = fee;
                  That line does nothing. Setting a variable equal to itself accomplishes nothing.
                       public double getFee()
                       {
                            while(method == 1)
                            {
                                 if(weight < 5.0)
                                      fee = 12.00;
                                 else if(weight >= 5.0 && weight <= 20.0)
                                      fee = 16.50;
                                 else
                                      fee = 22.00;
                            }
                  What would have to be true for that while loop to execute? method would have to equal 1, right? A while loop says, "Mr. Java, please keep executing the code inside my braces over and over again until the while condition is no longer true. Therefore, if the code inside that while loop starts executing, that means method equals 1. But how will the while loop stop? You told Mr. Java to keep executing the loop over and over again as long as method is equal to 1. Since the code inside the while loop never changes the value of method, method will always equal 1, so the loop will execute over and over again until the sun implodes and sucks the Earth into a black hole. A while loop is not the same thing as an if statement. An if statement executes only once if the conditional is true.
                  • 6. Re: Need help with constructors....ASAP!!
                    807601
                    You know, I can't believe I didn't see that. But like I said, I've been staring at this screen all day saying some not so nice things to my computer. Completely fixed now, Thanks for all the help.