6 Replies Latest reply: Dec 1, 2004 9:04 PM by 807587 RSS

    Package creation problems

    807587
      (This is a question relating to a homework question... so not too much help please!)

      I've created (what I hope is) a working package, and it compiles fine with no errors. I've then created a test program that calls the package. But, when I try to compile the test program it returns with the error (package does not exist).

      Here are my two .java files.

      The Package:
      package uk.ac.cf.cs.scm2lem.PoundOunce;
      
      class PoundOunce {
      
           int lbOne, ozOne, lbTwo, ozTwo, ozOneTotal, ozTwoTotal, ozFinalTotal, lbResult, ozResult;
      
           public void main(String args[]) {
      
                lbOne = Integer.parseInt(args[0]);
                ozOne = Integer.parseInt(args[1]);
                lbTwo = Integer.parseInt(args[3]);
                ozTwo = Integer.parseInt(args[4]);
      
                ozOneTotal = (lbOne * 16) + ozOne;
                ozTwoTotal = (lbTwo * 16) + ozTwo;
      
                if (args[2] == "a") {
                     addition(ozOneTotal, ozTwoTotal);
                }
      
                else if (args[2] == "s") {
                     subtraction(ozOneTotal, ozTwoTotal);
                }
                else if (args[2] == "m") {
                     multiplication(ozOneTotal, ozTwoTotal);
                }
                else division(ozOneTotal, ozTwoTotal);
      
                lbResult = ozFinalTotal / 16;
                ozResult = ozFinalTotal % 16;
      
                System.out.println("Answer: " + lbResult + "lbs and " + ozResult + "oz");
           }
      
           int addition(int ozOneTotal, int ozTwoTotal) {
      
                ozFinalTotal = ozOneTotal + ozTwoTotal;
                return ozFinalTotal;
           }
      
           int subtraction(int ozOneTotal, int ozTwoTotal) {
      
                ozFinalTotal = ozOneTotal - ozTwoTotal;
                return ozFinalTotal;
           }
      
           int multiplication(int ozOneTotal, int ozTwoTotal) {
      
                ozFinalTotal = ozOneTotal * ozTwoTotal;
                return ozFinalTotal;
           }
      
           int division(int ozOneTotal, int ozTwoTotal) {
      
                ozFinalTotal = ozOneTotal / ozTwoTotal;
                return ozFinalTotal;
           }
      }
      Here is my test program:
      import uk.ac.cf.cs.scm2lem.PoundOunce;
      
      class PoundOunceTest {
      
           public static void main(String args) {
      
                PoundOunce test = new PoundOunce();
           }
      }
      Now, I've been told to compile my package by using the command javac -d PackageName.java and this just doesn't work. I've compiled it into a .class file and also put this into a .jar file (Don't ask why, because I don't have a clue!) and it still comes with errors.

      How do I make my package available to my test file? I'm truly stumped!

      Thanks for ANY help.

      fakelvis
        • 1. Re: Package creation problems
          807587
          Make sure you have the file structure too. Since your test program is assuming there is a folder for each of the names in the package.

          ex. uk.ac.cf.cs.scm2lem.PoundOunce

          folder named: uk
          folder named: ac inside the above folder
          folder named: cf inside the above folder
          etc...

          hope this helps you
          • 2. Re: Package creation problems
            807587
            I have discard the package and change the sentence :
            import PoundOunce;
            I thimk that is very simple the solution :
            class PoundOunceTest:

            public class PoundOunceTest{
            private PoundOunce t;

            public static void main ( String[] args )
            {
            PoundOunce test = new PoundOunce();
            }

            }

            and the PoundOunce.java as follow :
            class PoundOunce {
            int lbOne, ozOne, lbTwo, ozTwo, ozOneTotal, ozTwoTotal, ozFinalTotal, lbResult, ozResult;

            public void main(String args[]) {

            lbOne = Integer.parseInt(args[0]);
            ozOne = Integer.parseInt(args[1]);
            lbTwo = Integer.parseInt(args[3]);
            ozTwo = Integer.parseInt(args[4]);

            ozOneTotal = (lbOne * 16) + ozOne;
            ozTwoTotal = (lbTwo * 16) + ozTwo;

            if (args[2] == "a") {
            addition(ozOneTotal, ozTwoTotal);
            }

            else if (args[2] == "s") {
            subtraction(ozOneTotal, ozTwoTotal);
            }
            else if (args[2] == "m") {
            multiplication(ozOneTotal, ozTwoTotal);
            }
            else division(ozOneTotal, ozTwoTotal);

            lbResult = ozFinalTotal / 16;
            ozResult = ozFinalTotal % 16;

            System.out.println("Answer: " + lbResult + "lbs and " + ozResult + "oz");
            }

            int addition(int ozOneTotal, int ozTwoTotal) {

            ozFinalTotal = ozOneTotal + ozTwoTotal;
            return ozFinalTotal;
            }

            int subtraction(int ozOneTotal, int ozTwoTotal) {

            ozFinalTotal = ozOneTotal - ozTwoTotal;
            return ozFinalTotal;
            }

            int multiplication(int ozOneTotal, int ozTwoTotal) {

            ozFinalTotal = ozOneTotal * ozTwoTotal;
            return ozFinalTotal;
            }

            int division(int ozOneTotal, int ozTwoTotal) {

            ozFinalTotal = ozOneTotal / ozTwoTotal;
            return ozFinalTotal;
            }
            }

            You have seen that no more chandes have been made . Well now you can :
            javac PoundOunce.java
            javac PoundOunceTest.java

            and continue :
            java PoundOunceTest

            but you have ommited "static" in tne
            public void main ( String[] args )
            I think that it's wrong
            Miguel
            • 3. Re: Package creation problems
              807587
              I have compiled your application but when I have done I removed the name of package and use
              javac PoundOunce.java
              and change your Test :

              public class PoundOunceTest{

              them javac PoundOunceTest.java

              In this point I ask to miself what I will obtain using the PoundOunce application
              Your main is wrong I ve used :
              public static void main in my application what is your idea ?
              I can't understand if you need agree two parameters :
              java PoundOunce a1,a2
              Answer me please

              Good luck
              Miguel
              • 4. Re: Package creation problems
                807587
                Hi.

                Thanks for the replies. I'm not totally sure what the problem was, but I have now got it sorted at last.

                Sorry I can't be much more help by informing you what I did, because I'm only really an amateur programmer - and the messiest one at that!

                Thanks anyway though!
                • 5. Re: Package creation problems
                  807592
                  Pounce class must be a public class, then only it can accessed outside the package
                  • 6. Re: Package creation problems
                    Darryl Burke
                    {color:#000080}And fakelvis has been waiting all of 2 years, 10 months and 11 days for you to tell him that.<sarcasm/>

                    Learn to read dates.

                    db{color}

                    <h2>{color:red}THIS THREAD IS DEAD{color}</h2>