Java Encapsulating

Discussion in 'Mac Programming' started by crtvmac, Apr 2, 2010.

  1. crtvmac Guest

    Joined:
    Aug 14, 2009
    #1
    I am trying to self teach myself to learn Java programming. Will someone please help me with this program. I getting confused about encapsulating. If I am comprehending what I am reading in my Java book, encapsulate means to start my properties with private so they can't be change by the client. Am I correct so far? Also, I know string means, whats inside the " ". With that said, will someone tell me how to go about coding the rest of this program contain in this post? When I run the program, I get error message:
    HTML:
     java.lang.ArrayIndexOutOfBoundsException: 0
    	at EightTypes.main(EightTypes.java:17)
    /**Write a program that creates Boolean, Character, Byte, Short, Integer, Long, Float, and Double objects to encapsulate eight values supplied as command-line arguments. Display the string equivalent of each objects.
    */
    HTML:
    public class EightTypes {
      public static void main(String args[ ]) {
          
        Boolean bool = Boolean.valueOf(args[0]);
        Character c = new Character(args[1].charAt(0));
        Byte b = Byte.valueOf(args[2]);
        Short s = Short.valueOf(args[3]);
        Integer i = Integer.valueOf(args[4]);
        Long l = Long.valueOf(args[5]);
        Float f = Float.valueOf(args[6]);
        Double d = Double.valueOf(args[7]);
        
        System.out.println(bool);
        System.out.println(c);
        System.out.println(b);
        System.out.println(s);
        System.out.println(i);
        System.out.println(l);
        System.out.println(f);
        System.out.println(d);
         
        }
    }
    
    2nd question: When I use the word properties as a designation in my question above, can that designation be interchangeable with the word variable?
     
  2. NightGeometry macrumors regular

    Joined:
    Apr 11, 2004
    #2
    in no way a java expert, but how are you running the .class? Are you passing in command line paramaters? (Sorry, trying to give hints).

    May be worth having a check for how many arguments are being passed in at run time...
     
  3. naples98 macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #3

    I agree, I think it is probably a problem with how you are passing arguments to the program. It worked fine when I supplied the proper arguments. I would also add a check in the program to ensure you have 8 arguments so you don't get another runtime error.

    Encapsulating anything basically means you are hiding your implementation details which allows you to change those implementation details without breaking code that depends on your code.
     
  4. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #4
    With your help I realize I needed to pass arguments for this program to display the string equivalent of each object. I used BlueJ IDE. I pass my arguments doing the following: { "1", "2" , "3", "4", "5", "6", "7", "8"}.

    My output was:
    HTML:
    false
    2
    3
    4
    5
    6
    7.0
    8.0
    Okay, I got that part using BlueJ. Now would someone please assist me in how I would write the coding arguments in the correct syntax for this program, using my Terminal Application? I would like to compile and run this same program to get the same output using my Terminal window? The following output is were I got to this point using my Terminal Application.

    HTML:
     Summer Mac-Pro:JavaLearning Summer$ ls
    Add2Integers.java	        Exercises211N6		examples
    CharacterArray.java	        HelloWorld
    EncapsulateEight.java	        ReadFromConsole.java
    
     
  5. NightGeometry macrumors regular

    Joined:
    Apr 11, 2004
    #5
    javac EightTypes.java
    java EightTypes true z 1 2 3 4 5.23 6.56

    Works for me.

    A book I liked is "Agile Java - Crafting Code with Test Driven Development", if you are looking to learn Java (and TDD), I personally think it's not such a bad starting point.
     
  6. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #6
    Would not compile using command line. When I pulled up my directories and typed 'javac EncapsulateEight.java' the following was returned:

    HTML:
    Last login: Sat Apr  3 10:47:02 on ttys001
    Summer-Mac-Pro:~ Summer$  ls -F
    Desktop/		        Library/		        Public/
    Documents/		Movies/			Send Registration@
    Downloads/		Music/			Sites/
    JavaLearning/		Pictures/
    Summer-Mac-Pro:~ Summer$ cd JavaLearning
    Summer-Mac-Pro:JavaLearning Summer$ ls
    Add2Integers.java	EncapsulateEight.java	ReadFromConsole.java
    CharacterArray.java	Exercises211N6		examples
    EncapsulateEight.class	HelloWorld
    Summer-Mac-Pro:JavaLearning Summer$ javac EncapsulateEight.java
    javac: not a file: EncapsulateEight.java
    Usage: javac <options> <source files>
    use -help for a list of possible options
    Summer-Mac-Pro:JavaLearning Summer$  
    
    Then I tried the command 'java EncapsulateEight' after I used the command 'ls -i' and got the following:

    HTML:
    Summer-Mac-Pro:JavaLearning Summer$  ls -i
    664274 Add2Integers.java	        655367 Exercises211N6
    672729 CharacterArray.java	        395038 HelloWorld
    677095 EncapsulateEight.class	665087 ReadFromConsole.java
    675573 EncapsulateEight.java	393099 examples
    Summer-Mac-Pro:JavaLearning Summer$ java EncapsulateEight
    Exception in thread "main" java.lang.ClassFormatError: Truncated class file
    	at java.lang.ClassLoader.defineClass1(Native Method)
    	at java.lang.ClassLoader.defineClass(ClassLoader.java:698)
    	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    	at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    	at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
    	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398
    Would you please assist me by telling me my error?
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Try this command, and post the output:
    Code:
    ls -l
    
    That's "dash ell", not "dash eye" after the 'ls'.

    The -l option means "long listing", while the -i option means "show inode number". I doubt that inode number is useful here.

    Also post the output of this command:
    Code:
    file *
    
    Obviously, run both commands in your JavaLearning directory.
     
  8. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #8
    Following your directions, the following output was displayed:

    HTML:
    Summer-Mac-Pro:JavaLearning Summer$ ls -l
    total 0
    drwxr-xr-x  11 Summer  staff  374 Mar 31 16:37 Add2Integers.java
    drwxr-xr-x   8 Summer  staff  272 Apr  1 20:19 CharacterArray.java
    -rw-r--r--    1 Summer  staff    0 Apr  3 11:20 EncapsulateEight.class
    drwxr-xr-x    9 Summer  staff  306 Apr  3 10:57 EncapsulateEight.java
    drwxr-xr-x    8 Summer  staff  272 Mar 26 21:59 Exercises211N6
    drwxr-xr-x    9 Summer  staff  306 Mar 26 22:00 HelloWorld
    drwxr-xr-x    9 Summer  staff  306 Mar 30 19:20 ReadFromConsole.java
    drwxr-xr-x@   11 Summer  staff  374 Mar 25 14:14 examples
    
    Summer-Mac-Pro:JavaLearning Summer$ file *
    Add2Integers.java:      directory
    CharacterArray.java:    directory
    EncapsulateEight.class: empty
    EncapsulateEight.java:  directory
    Exercises211N6:         directory
    HelloWorld:             directory
    ReadFromConsole.java:   directory
    examples:               directory
    Summer-Mac-Pro:JavaLearning Summer$ 
    
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Well this is odd, but your "files" whose names end in ".java" are actually directories. Maybe that's a BlueJ thing, or a side-effect of what you were doing before, but it's certainly odd. One doesn't usually name directories to look like filenames, because it's too confusing when looking at directory listings.

    Try this command, and post the output:
    Code:
    ls -la EncapsulateEight.java
    
    I suspect the file you need to compile is contained in the directory, so you have to list its contents to discover its actual name.
     
  10. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #10
    Here is the output from the ls -la command.

    HTML:
    Summer-Mac-Pro:JavaLearning Summer$ ls -la EncapsulateEight.java
    total 48
    drwxr-xr-x   9 Summer  staff   306 Apr  3 10:57 .
    drwxr-xr-x  12 Summer  staff   408 Apr  3 10:59 ..
    -rw-r--r--@ 1  Summer  staff  6148 Apr  2 16:08 .DS_Store
    -rw-r--r--   1 Summer  staff  1603 Apr  3 10:50 EncapsulateEight.class
    -rw-r--r--   1 Summer  staff    86 Apr  3 10:50 EncapsulateEight.ctxt
    -rw-r--r--   1 Summer  staff   926 Apr  3 10:29 EncapsulateEight.java
    -rw-r--r--   1 Summer  staff   471 Apr  2 15:19 README.TXT
    -rw-r--r--   1 Summer  staff     0 Apr  2 15:19 bluej.pkg
    -rw-r--r--   1 Summer  staff     0 Apr  2 15:19 package.bluej
    
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    With those files, try this command:
    Code:
    javac EncapsulateEight.java/EncapsulateEight.java
    
    It might work, or it might fail. If it fails, post the error message, like you've been doing.

    If it works, you should do this command:
    Code:
    file *.class
    
    It should tell you its a compiled Java class (wording might vary). If so, then run it like this:
    Code:
    java EncapsulateEight 1 2 3 4 5 6 7 8
    
    Or run it and have it throw an exception like this:
    Code:
    java EncapsulateEight
    
     
  12. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #12
    Here is the error message:

    HTML:
    Summer-Mac-Pro:JavaLearning Summer$ javac EncapsulateEight.java/EncapsulateEight.java
    Summer-Mac-Pro:JavaLearning Summer$ file *.class
    *.class: cannot open `*.class' (No such file or directory)
     
  13. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #13
    Sorry, I misremembered javac's default, and didn't check the man page before posting.

    By default, javac puts the class file in the same location as the source file.

    Try this command:
    Code:
    javac -d . EncapsulateEight.java/EncapsulateEight.java
    
    Same procedure: if there are errors, post them, otherwise try the other commands I gave before:
    Code:
    file *.class
    java EncapsulateEight 1 2 3 4 5 6 7 8
    
     
  14. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #14
    This is the coding I did before you posted your last post. I went into the Finder and located my 'JavaLearning' directory. I then located the 'EncapsulateEight' directory that had .java attached to it; I then deleted the .java tag on the 'EncapsulateEight' directory. Then I did the following coding and got the program to run and display the correct output based on the pass arguments . Might have been something with the BlueJ application as you suggested, which was causing .java to be attached to my directories and not just my files, which was causing the error message. I also rebooted my computer. Thanks for all the assistance. I would have been banging my head up against the wall all night to located my error.

    HTML:
    Summer-Mac-Pro:JavaLearning Summer$ ls -F
    Add2Integers.java/	Exercises211N6/		ReadFromConsole.java/
    CharacterArray.java/	HelloWorld/		examples/
    EncapsulateEight/	NaturalLogarithm.java/
    Summer-Mac-Pro:JavaLearning Summer$ cd EncapsulateEight
    Summer-Mac-Pro:EncapsulateEight Summer$ ls
    EncapsulateEight.class	EncapsulateEight.java	bluej.pkg
    EncapsulateEight.ctxt	README.TXT		package.bluej
    Summer-Mac-Pro:EncapsulateEight Summer$ javac EncapsulateEight.java
    Summer-Mac-Pro:EncapsulateEight Summer$ java EncapsulateEight 1 2 3 4 5 6 7 8
    false
    2
    3
    4
    5
    6
    7.0
    8.0
    Summer-Mac-Pro:EncapsulateEight Summer$ 
    
     
  15. mrbash macrumors 6502

    Joined:
    Aug 10, 2008
    #15
    Encapsulation is hiding the details of an implementation, but leaving exposed the interface. Think of it like a contract. The contract states that certain work will be done, and specific payment will be accepted for the work. But the contract does not specify how the work will be done.

    Adding a private, or public property does not affect encapsulation. In fact, there is little point in declaring something private for the purposes of encapsulation.
     

Share This Page