Author Topic: Stupid java question  (Read 1680 times)

0 Members and 1 Guest are viewing this topic.

Offline aldo_14

  • Gunnery Control
  • 213
Stupid java question
Right...I really should know this, but I don't :D

Is there a way to limit class access to only be from other classes within the same package?

i.e. I wish to break up a program into modules where the only access point is either an interface class and some designated factory (or other type) creational object.  Obviously it's easy enough to self-check in code, but I'm wondering if there is a way to strongly 'force' it.

This is, incidentally, because I'm planning to port the code to other languages / apps as a learning excercise.  I'm particualrly curious as to whether I can a) get a seperate C++ based graphics module or b)get a mobile-optimized version running with only (again) the graphics module swapped.

EDIT; wouldn't mind a link to a free UML editor plugin for Eclipse if anyone knows of one, either :D
« Last Edit: April 05, 2005, 10:43:50 am by 181 »

 

Offline Styxx

  • 211
    • Hard Light Productions
That's the default access level. Just don't put any access modifier before the class declaration, and it'll default to package access. ;)
Probably away. Contact through email.

 

Offline aldo_14

  • Gunnery Control
  • 213
Really?  :doh: :o

Have to say I've never dropped the public bit before :).

(mind you, I've never wanted to funnel it this tightly before).

Cheers.

 

Offline kode

  • The Swedish Chef
  • 28
  • The Swede
    • http://theswe.de
there's private, public and protected. can't remember exactly what differed between private and protected.
Pray, v. To ask that the laws of the universe be annulled in behalf of a single petitioner confessedly unworthy.
- Ambrose Bierce
<Redfang> You're almost like Stryke 9 or an0n
"Facts do not cease to exist because they are ignored."
- Aldous Huxley
WAR IS PEACE
FREEDOM IS SLAVERY
IGNORANCE IS STRENGTH

 
I think protected allows for 'public-like' read-access, but 'private-like' write-access.

protected sure beats having to write methods to retrieve and set values for variables that you access all the time.

Or at least that's how I think it works.

 

Offline aldo_14

  • Gunnery Control
  • 213
Protected also allows access by inheriting classes; private doesn't.

EDIT; rough analogy;  to external classes (outside the inheritance hierarchy), public is read-write, protected is read-only, private is no read or write.  Although you should always really restrict variable access to get and set methods for consistency control.
« Last Edit: April 05, 2005, 01:58:13 pm by 181 »

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
In C++, I'm guessing it'd be "static". I've never tried that, but that's what's used for functions, so it seems like for consistency's sake they'd use the same thing.
-C

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
This is the kind of thing that gets drilled into you when you study for the programmer certification. Ironically enough they only check if you can actually program at the next level :D
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline aldo_14

  • Gunnery Control
  • 213
Quote
Originally posted by WMCoolmon
In C++, I'm guessing it'd be "static". I've never tried that, but that's what's used for functions, so it seems like for consistency's sake they'd use the same thing.


Which, I'd guess is a completely different meaning of static to java :D

(static variables / methods are accessible upon the class rather than class instance.

i.e. if you have (in class myClass) method public static void nee(), you'd call using myClass.nee(); non-static would be something like myClassInstance = new myClass(); and then myClassInstance.nee().)

In C, according to this book I've just grabbed off my shelf, static has a dual meaning; within a block, static gives a variable fixed duration instead of automatic duration.  Outside a block, it controls scope of a variable - giving it file rather than program scope.

(actually, the latter definition sounds similar to the Java meaning)

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Static works in four ways in C/++.

1) For global variables, it limits the scope to the current file.

2) Inside functions, the value remains unchanged across calls, ie in this function i would start at 0 and continue to grow:

Code: [Select]
void increment_i()
{
static int i = 0;
i++;
}


3) Inside class definitions, it does the same thing as in Java, although the syntax to call the function would be slightly different -
class::Function(argument);

4) For class member variables, they remain the same across all instances of that class.
class::Variables = Value;
-C

 

Offline Grug

  • 211
  • From the ashes...
Kewl.
So who's the learning exercise for?

Slightly off topic but meh:
I've probably only got a medium-average skill level with Java. Mainly for the reason I don't use it enough to know it as well as I should do.
(Last time I touched it was couple months ago)

Anyway, is there any easy way to setup a recurring method to read objects and files then to put them into a JTree?

I spent some time doing this, but the end results I always came up with worked for the most part, but were untidy and slightly hackish.
I always wished there was a method already out there like readTree and generateTree, rather than having to write my own.

The question is in relation to a program I was writing that would read a directory, store its contents in a simplified state (just all the file / folder properties) and then read from the stored file into a JTree for display. Possibly only loading the next level when an item is expanded.

I'm going to have to get back into Java soon I think. Get off my arse and make a living for myself. =/

 

Offline phreak

  • Gun Phreak
  • 211
  • -1
there's also a "package" keyword, but thats a bit redundant.
Offically approved by Ebola Virus Man :wtf:
phreakscp - gtalk
phreak317#7583 - discord

 

Offline aldo_14

  • Gunnery Control
  • 213
Quote
Originally posted by Grug
Kewl.
So who's the learning exercise for?

Slightly off topic but meh:
I've probably only got a medium-average skill level with Java. Mainly for the reason I don't use it enough to know it as well as I should do.
(Last time I touched it was couple months ago)

Anyway, is there any easy way to setup a recurring method to read objects and files then to put them into a JTree?

I spent some time doing this, but the end results I always came up with worked for the most part, but were untidy and slightly hackish.
I always wished there was a method already out there like readTree and generateTree, rather than having to write my own.

The question is in relation to a program I was writing that would read a directory, store its contents in a simplified state (just all the file / folder properties) and then read from the stored file into a JTree for display. Possibly only loading the next level when an item is expanded.

I'm going to have to get back into Java soon I think. Get off my arse and make a living for myself. =/


I'm not sure how much use it is, but a few years back i wrote a program to parse Prolog formatted trees (defined in a text file) and build a binary tree from them; it might be useful (http://aldo14.f2s.com/downloadable_stuff%5Cjava%5CprologTreeParser.zip)

 

Offline Grug

  • 211
  • From the ashes...
Um, 404 error on the download.

Thanks btw. :D

I kind of know the theory behind binary trees, but I'm yet to use them in one of my own programs (that I know of). Speaks true to my amatuerism of Java but my education it was more of a 2yr builder course. Basically learning the basics and how to use the doc's correctly to expand your knowledge on your own.

Binary tree's are used with searches correct?
Man, I'm gonna have to scrub up my skills.
*Blows much dust off old Java book*
« Last Edit: April 05, 2005, 05:48:21 pm by 501 »

 

Offline aldo_14

  • Gunnery Control
  • 213
Ooops... slashes were wrong  http://aldo14.f2s.com/downloadable_stuff/java/prologTreeParser.zip
(been ages since I wrote it; i believe there is some documentation there)

Binary trees reduce search time; basically, searching down a tree means you half the set to search each time you choose a branch.

i.e. searching numbers (spacing this is awkward)

                    ROOT
             /                \
           >5                  <=5
          /      \                /   \
         >10  <=10        <3 >=3

For example;

The binary part comes because the decision of which branch to take is always made out of 2; i.e. true or false / 1 or 0.

Offhand, the search time is quick - O(log n) or something similar, where n is the number of branches.  I can't rememer the addition removal time offhand - I think it's also fairly speed; the main issue is simply presorting and correctly forming the branch points for a perfectly balanced tree (i.e. the same depth of branches on each side; yu need this for the search to work ot optimal efficiency).

 

Offline Grug

  • 211
  • From the ashes...
Thanks for the files. :)

The search system sounds easy enough to understand.
How to parse the data into a binary tree seems to be the killer though.

I'm assuming that along with the formated stored data, you would have another index file that contains the binary tree data? Or is the data itself stored in a format akin to a binary tree?
This is a little big to wrap my head around, but I think I'll get there eventually.

Say I have a simple class that has some basic attributes of a file / folder. name, size, dateModified, location.
With only methods being getter's and setter's. Called say FileInfo.
To read a simple directory and store any files within them as the class from above it would have to be a recurring method.

So that a simple directory would read in as:
ROOT
->Folder1
-->File1
-->File2
->Folder2
-->File1

Each Entry would be a FileInfo.

How would I then sort that into a binary search is a little perplexing to me.
To be able to call searches with either of the variables as flags, how would that work?
Especially with Strings, would you use a String length call?
So trying to find a way to put everything into an Integer that can be placed into a true false clause?

Sorry to be a hassel. :p
Slowly my knowledge grows.
Just skimmed through me old textbook and already found something that would explain alot of screwed up things in my old programs. (Aliasing) *doh*
« Last Edit: April 05, 2005, 07:39:52 pm by 501 »

 

Offline Goober5000

  • HLP Loremaster
  • 214
    • Goober5000 Productions
Quote
Originally posted by aldo_14
i.e. searching numbers (spacing this is awkward)
aldo, you know about the (code)(/code) tag -- replacing () with [] -- right?

Code: [Select]
                ROOT
             /          \
            >5         <=5
          /    \      /   \
         >10  <=10   <3  >=3

 

Offline aldo_14

  • Gunnery Control
  • 213
Quote
Originally posted by Grug
Thanks for the files. :)

The search system sounds easy enough to understand.
How to parse the data into a binary tree seems to be the killer though.

I'm assuming that along with the formated stored data, you would have another index file that contains the binary tree data? Or is the data itself stored in a format akin to a binary tree?
This is a little big to wrap my head around, but I think I'll get there eventually.

Say I have a simple class that has some basic attributes of a file / folder. name, size, dateModified, location.
With only methods being getter's and setter's. Called say FileInfo.
To read a simple directory and store any files within them as the class from above it would have to be a recurring method.

So that a simple directory would read in as:
ROOT
->Folder1
-->File1
-->File2
->Folder2
-->File1

Each Entry would be a FileInfo.

How would I then sort that into a binary search is a little perplexing to me.
To be able to call searches with either of the variables as flags, how would that work?
Especially with Strings, would you use a String length call?
So trying to find a way to put everything into an Integer that can be placed into a true false clause?

Sorry to be a hassel. :p
Slowly my knowledge grows.
Just skimmed through me old textbook and already found something that would explain alot of screwed up things in my old programs. (Aliasing) *doh*


Well... i'd suggest using a recursive parse and store based on hierarchy (probably not ideal for binary trees as you have !=2 files / directories iin each directory).

In all honesty I'd have to do a wee bit reading up to get a proper answer.  However, it might be useful to check out the details of HTML parsing, as browsers format HTML (etc) data in a tree structure (reflecting the nested nature of HTML tags).

But it's definately a case for a recursive call.... IIRC in a 'straight' sorted array youd simply organize it into an ordered data structure.  For a file system, you'd really want to mimic the hierarchy, I think.

I'll probably have a further look later; IIRC I still have my uni notes lying about for this sort of code.

 

Offline Grug

  • 211
  • From the ashes...
I spent ages trying to come up with a way to store the hierarchy in its own format. But always ended up using an iterator of some sort and just using the location property to sort things out when building the Jtree.
There was one way that kind of worked with a hierarchy. But it required using a single file for each file / folder so you'd end up with the same amount of files as the original dir, but just containing the properties rather than  the file data. This would probably be best kept in a zip entry or something.
It just added another layer of complexity that seemed unrequired.

I still can't really think of a system that uses a binary tree to search from a file structure. Unless it was sorted into something other the default hierarchy, say by building a tree from catagories or file types etc. That probably means the tree needs to be generated dynamically. Making it happen on the spot, with the relevant catagories is going to be a pain in the ass though.

  

Offline aldo_14

  • Gunnery Control
  • 213
It wouldn't be a binary tree, no; it's likely be a n-way tree (n branches at each level).  I suspect directory structures aren't entirely suitable for tree-based searching as there's no expectation of balance; if you want to search at binary tree speed for files and still preserve heirarchical details, then you need to fudge it a bit and sort the data yet store the hierarchy.

Offhand, I think a n-tree recursive algorithm could be (pseudocode)

Code: [Select]


public Tree treeBuild(Directory root)  {
 Tree nTree = new Tree(); //some n-way tree structure
 file[] listOfFiles = all files in root dir
 sort(listOfFiles); //for intelligent search - go low->high
 for(int i=0;i   if(listOfFiles[i] is a directory)
      nTree.addBranchTo(   treeBuild((Directory)listOfFiles[i])  );
   }
   else  {
    nTree.addBranchTo( listOfFiles[i] );
    }
return nTree;
}



This really a 10 second effort, though; there's obviously an ordering issue when adding branches to be considered, and I'm not sure if you can intelligently search the resulting structure without further consideration of sorting.