The thing about people who rally against OO because it's "powerful and you can hurt yourself with it" tend to be academic types who haven't actually written real software in their entire life. Yes, you can write shit code with it. I can write shit functional code with Lisp too. And I can write shit procedural code with C. Over years of experience doing OO, you learn how to avoid writing that shit code. You learn when you're "overdoing it" (we call this over engineering, it has a real term). You learn to sniff out very quickly when you've architected a system to be too complex or not complex enough.
The reason that academics (in particular certain PL folk) don't like OO is because it precisely leads to overly complex code whereas functional styles lead to more concise, elegant code.
There's a whole bunch of features mixed into the debate when it is phrased as "OO vs. functional" (like mutation, ADTs, polymorphism, etc.) so its not a cut-and-dry argument. However, after being in industry and academia now, I'm firmly in the functional side of things.
I'm on the "correct tool for the job" side of things. In most cases, that ends up being a mixture of OO and functional design patterns. They actually mix incredibly well if you know what you're doing.
The problem is too many people graduate from college and think they know what they are doing: They don't. It takes college + years of experience to actually know what you're doing, and the part that is optional is the college, not the years of experience. Not that college is bad, I recommend it for everyone. People just need to understand what a comp sci program is actually going to give you, and what it's not going to give you.
Yep, I'm in that camp too. But in my experience is that, by far, functional programming --- namely immutability and all the great reasoning benefits you get from it --- is (should be) the common case. And then once you start going down that road, you find that OO programming on its own offers little additional benefit.
@TwitchTV, @Youtube: master-level zerg ladder/customs, commentary, and random miscellany.
Does anyone here know much about Java GUIs? I always tend to lump as much stuff into constructors as I can, and I'm wondering if that's considered good or bad practice. Say I'm making a Frame that holds a bunch of Panels, I'll create and add the Panels in the Frame's constructor. Then if those Panels hold Buttons and TextAreas, I'll create and position those in the Panel's constructors.
It feels a bit weird that 90% of my code is in the constructors for these objects, but it makes sense to me, because making these objects and positioning them is a one-time event that needs to happen at start-up, so constructors seem right to me.
Is there a convention for this, either way?
0
GnomeTankWhat the what?Portland, OregonRegistered Userregular
I've done plent of BSTs in C before, but this non-binary is killer, and I could use some help making it a reality:
I have to make a pseudo ID3 Decision Tree. These are used to indicate pathways in decision making such as diagnosing a car or the Ken Flowchart and all that.
Luckily I've been given a fixed set of decisions so it doesn't have to be generic. Each rule is called a Feature, and I have five of them. I put them in String Arrays, and then put those array in a shuffled Array List.
public class main
{
public static void main(String[] args)throws Exception
{
//We are explicitly given the examples to use, so we create the features,
//and the examples, by brute force. The tree will be more dynamic.
//1. Make the light feature
String[] lightstatus = new String[5];
{
lightstatus[0] ="Dim";
lightstatus[1] ="Normal";
}
//2. Make the sound feature
String[] soundstatus = new String[3];
{
soundstatus[0] ="Normal";
soundstatus[1] ="Howl";
soundstatus[2] ="Screech";
soundstatus[3] ="Click";
}
//3. Make the turnover feature
String[] turn = new String[1];
{
turn[0] ="no";
turn[1] ="yes";
}
//4. Make the fuel gauge feature
String[] fuelstats = new String[1];
{
fuelstats[0] ="not-empty";
fuelstats[1] ="empty";
}
//5. Make the smell feature
String[] scents = new String[1];
{
scents[0] ="normal";
scents[1] ="gas";
}
//6. Make the problem feature
String[] problems = new String[4];
{
problems[0] ="battery";
problems[1] ="starter";
problems[2] ="solenoid";
problems[3] ="out-of-gas";
problems[4] ="flooding";
}
ArrayList<String[]> Features = new ArrayList<String[]>();
Features.add(lightstatus);
Features.add(soundstatus);
Features.add(turn);
Features.add(fuelstats);
Features.add(scents);
//We have created the six features. We must now make
//the examples based on the instructions. We have five of them.
Example example1 = new Example(lightstatus[0], soundstatus[1], turn[1], fuelstats[0], scents[0], problems[0]);
Example example2 = new Example(lightstatus[1], soundstatus[2], turn[0], fuelstats[0], scents[0], problems[1]);
Example example3 = new Example(lightstatus[1], soundstatus[3], turn[0], fuelstats[0], scents[0], problems[2]);
Example example4 = new Example(lightstatus[1], soundstatus[0], turn[1], fuelstats[1], scents[0], problems[3]);
Example example5 = new Example(lightstatus[1], soundstatus[0], turn[1], fuelstats[0], scents[1], problems[4]);
//Now we put these five examples in an ArrayList.
//make an array list of Examples.
ArrayList<Example> Examples = new ArrayList<Example>();
Examples.add(example1);
Examples.add(example2);
Examples.add(example3);
Examples.add(example4);
Examples.add(example5);
// Shuffle the elements in the list
Collections.shuffle(Features);
//The Features Array List is now a shuffled tree.
//We will do a single loop that will serve as our stack.
//First we take the top of the list and assign it to the root.
//This line is not right
Tree id3 = new Tree((Feature)Features.get(0)));
The Features get shuffled, and then I take the first Feature (rule) and make it the root. All the different options of the feature need to become children. So if the first feature is "lightstatus", lightstatus is the root, and it can just have two children. One for Normal and one for Dim. If there are four options like with Sound Status, there needs to be four children. I've swapped the BST's left and right child architecture for an Array List of Nodes. But now I'm having trouble no what I need to do next. At this point I know which feature the root would be, but I'm getting my parameterization mixed up. The selected feature's variable name would be the root and it's array list of strings would be all the children. How do I specify that? I feel like I'm so close but I'm doing something retarded.
This is my Node.
import java.util.*;
public class Node
{
public Node()
{
children = new ArrayList<Node>();
}
public ArrayList<Node> getChildren()
{
return children;
}
public void addChild(Node n)
{
children.add(n);
}
public String getphrase()
{
return phrase;
}
public void setphrase(String newphrase)
{
phrase = newphrase;
}
private ArrayList<Node> children;
String phrase;
private boolean isUsed;
Node parent;
public void setUsed(boolean isUsed)
{
this.isUsed = isUsed;
}
public boolean isUsed()
{
return isUsed;
}
//This method states if the node is a leaf
public boolean isLeaf()
{
if (this.getChildren() == null)
return true;
else
return false;
}
}
This is the tree so far.
public class Tree
{
public Node root;
public Node current = new Node();
Integer internalNumber = 0;
Integer height;
//Constructors
public Tree()
{
root = current;
root.parent = null;
}
public Tree(Feature feature)
{
root = current;
root.phrase = feature;
root.parent = null;
}
//This method creates a tree with one root node,
//but it also creates the children too.
public void Create(ArrayList<Feature> checklist)
{
root.phrase = name;
root.parent = null;
}//Finished
//This method checks if a node is the root
public boolean isRoot(Node nodeChecked)
{
if(nodeChecked.parent == null)
{
return true;
}
else
{
return false;
}
}//finished
//This method saves the current node after Find
public void SavePosition(Node saved)
{
saved = current;
}
@Cantido - it really seems like you want to be using Enums instead of String arrays for this. Using Enum objects would let you do everything the Arrays are doing, and let you add more, like possibly a 'getName' method for filling out the tree.
Oh, and just one nitpick: in order for a BST to earn the 'S' in its name, I'm pretty sure it has to be sorted. It's not clear from your code above that any kind of sorting is happening, or even desired.
Edit: wait, why are you representing this as a tree? If I'm understanding right, the nodes at each Feature level are identical, right? (I mean, with your "lightstatus" example above, will there be any difference at all between the children of "normal" and "dim"?) So far it sounds more like you're finding the right point in N-dimensional (or 'Feature'-dimensional) space for the diagnosis.
@Cantido - it really seems like you want to be using Enums instead of String arrays for this. Using Enum objects would let you do everything the Arrays are doing, and let you add more, like possibly a 'getName' method for filling out the tree.
Oh, and just one nitpick: in order for a BST to earn the 'S' in its name, I'm pretty sure it has to be sorted. It's not clear from your code above that any kind of sorting is happening, or even desired.
Edit: wait, why are you representing this as a tree? If I'm understanding right, the nodes at each Feature level are identical, right? (I mean, with your "lightstatus" example above, will there be any difference at all between the children of "normal" and "dim"?) So far it sounds more like you're finding the right point in N-dimensional (or 'Feature'-dimensional) space for the diagnosis.
You're right about the enum, that might do the trick. As for the "Search" part, I don't think it's desired. The idea is to try different Features for roots, and see what makes the tree largest and smallest. The algorithm takes them in at random first, and later I will force in different roots and count the total nodes. The lesson is that good indexing can make or break Inductive Machine Learning (at the undergrad level anyway)
I will pseudo out the algorithm:
1. Take the first Feature, make it the root, and it's children are the different options.
2. If there are more than one node at this level, create the same number of branches for each node at this level
3.for (all examples in the Examples List)
{
*"parse" the Example down the path whose branch agrees with its value(s) for the applicable Feature
* If all the Examples gathered at the same location have the same "solution," STOP
* Continue
}
4. Take the next Feature, which will be used to obtain the next level, and return to 2.
As the levels get added, examples follow the newly created children each time, so it quickly forges reliable case based reasoning.
That enum class is awesome, but I never really got the point of doing that. That could be done via a regular old class right? Stupid Java and their stupidly powerful enums.
Yeah if you didn't have a String constructor I guess. Planet Earth = new Planet("Earth"); then do a switch statement on the string or do a bunch of ifs.
public class main
{
public enum lightstatus {Dim, Normal}
public enum soundstatus {Normal, Howl, Screech, Click}
public enum turn {no, yes}
public enum fuelstats {notempty, empty}
public enum scents {normal, gas}
public enum problems {battery, starter, solenoid, outofgas, flooding}
public static void main(String[] args)throws Exception
{
ArrayList<Enum> Features = new ArrayList<Enum>();
Features.add(lightstatus);
Features.add(soundstatus);
Features.add(turn);
Features.add(fuelstats);
Features.add(scents);
//We have created the six features. We must now make
//the examples based on the instructions. We have five of them.
Example example1 = new Example(lightstatus.Dim, soundstatus.Howl, turn.yes, fuelstats.notempty, scents.normal, problems.battery);
Example example2 = new Example(lightstatus.Normal, soundstatus.Screech, turn.no, fuelstats.notempty, scents.normal, problems.starter);
Example example3 = new Example(lightstatus.Normal, soundstatus.Click, turn.no, fuelstats.notempty, scents.normal, problems.solenoid);
Example example4 = new Example(lightstatus.Normal, soundstatus.Normal, turn.yes, fuelstats.empty, scents.normal, problems.outofgas);
Example example5 = new Example(lightstatus.Normal, soundstatus.Normal, turn.yes, fuelstats.notempty, scents.gas, problems.flooding);
//Now we put these five examples in an ArrayList.
//make an array list of Examples.
ArrayList<Example> Examples = new ArrayList<Example>();
Examples.add(example1);
Examples.add(example2);
Examples.add(example3);
Examples.add(example4);
Examples.add(example5);
// Shuffle the elements in the list
Collections.shuffle(Features);
The thing is I need to redefine my Example class. I just need it to take in one value from each enum, but how do I specify that? After all, they are no longer Strings, and I don't think I'm allowed to specify that it takes in enums.
public class Example
{
protected String lights;
protected String sound;
protected String turnover;
protected String fuelgauge;
protected String smell;
protected String problem;}
//Constructor for a parameter
public Example (main.lightstatus, main.soundstatus, main.turn, main.fuelstats, main.scents, main.problems)
{
this.lights= lights;
this.sound= sound;
this.turnover= turnover;
this.fuelgauge= fuelgauge;
this.smell= smell;
this.problem= problem;
}
}
Edit... the C# extension method version is pretty cool though
Except it's really not that much more elegant? It's just syntactic sugar around the same thing. The biggest differences are 'class' instead of 'enum' and the Values property, which isn't even technically necessary, you could do the same thing with simple reflection.
I don't care if C# adds the feature, it just annoys me when people say "This isn't possible in C#!!!!!"...when it is completely possible, you just have to think about it for a second.
We ran into this on a result set created by MSSQL mail and tabs fixed the issue.
You are right though that with me standalone testing a simple .csv file I just created by hand it is properly opening up. Not sure what the issue is.
I figured it out.
If the file is ASCII: Excel opens .csv files that are comma delimited and properly breaks the fields out in multiple columns.
If the file is Unicode: Excel opens .csv files that are comma delimited and puts all the fields in one column.
If the file is Unicode: Excel opens .csv files that are tab delimited and properly breaks the fields out in multiple columns.
Ugh the newest update to Netbeans does something very annoying. When you do "Fix Imports" and you have two or more in the same package it imports the entire package.
Mike Danger"Diane..."a place both wonderful and strangeRegistered Userregular
Rails question: what exactly does it mean when I define Users as a resource? This is one of the thing that was explained in the book and that I have now forgotten.
Rails question: what exactly does it mean when I define Users as a resource? This is one of the thing that was explained in the book and that I have now forgotten.
It means you've done "map.resource" in your route file, and that it's now going to treat that route as a complete resource path. That means the controller needs to support get, put, index, delete, etc. You can tweak what verbs actually need to be supported at the time of mapping, but without specifying, it's going to assume all the CRUD operations are there. Essentially, resource mapping is the heart of RESTful in rails.
Mike Danger"Diane..."a place both wonderful and strangeRegistered Userregular
Solid. I'll probably be back here in a few hours with more Rails questions...I'm almost done with the Hartl tutorial now and I think I'm ready to start wrapping my brain around this app idea that I had.
@Cantido - it really seems like you want to be using Enums instead of String arrays for this. Using Enum objects would let you do everything the Arrays are doing, and let you add more, like possibly a 'getName' method for filling out the tree.
Oh, and just one nitpick: in order for a BST to earn the 'S' in its name, I'm pretty sure it has to be sorted. It's not clear from your code above that any kind of sorting is happening, or even desired.
Edit: wait, why are you representing this as a tree? If I'm understanding right, the nodes at each Feature level are identical, right? (I mean, with your "lightstatus" example above, will there be any difference at all between the children of "normal" and "dim"?) So far it sounds more like you're finding the right point in N-dimensional (or 'Feature'-dimensional) space for the diagnosis.
@wildwood I made an enum version of the six Features so they look like this. Instead of put them in main I gave each Enum it's own file so I wouldn't have to do main.enumname everywhere. One final thing I want to do is put those enums in an ArrayList and shuffle them, because I will take the order and build a tree with it. But
ArrayList<Enum> Features = new ArrayList<Enum>();
Is not allowed.
public class main
{
public static void main(String[] args)throws Exception
{
ArrayList<Enum> Features = new ArrayList<Enum>();
Features.add(lightstatus);
Features.add(soundstatus);
Features.add(turn);
Features.add(fuelstats);
Features.add(scents);
//We have created the six features. We must now make
//the examples based on the instructions. We have been given five different examples.
Example example1 = new Example(lightstatus.Dim, soundstatus.Howl, turn.yes, fuelstats.notempty, scents.normal, problems.battery);
Example example2 = new Example(lightstatus.Normal, soundstatus.Screech, turn.no, fuelstats.notempty, scents.normal, problems.starter);
Example example3 = new Example(lightstatus.Normal, soundstatus.Click, turn.no, fuelstats.notempty, scents.normal, problems.solenoid);
Example example4 = new Example(lightstatus.Normal, soundstatus.Normal, turn.yes, fuelstats.empty, scents.normal, problems.outofgas);
Example example5 = new Example(lightstatus.Normal, soundstatus.Normal, turn.yes, fuelstats.notempty, scents.gas, problems.flooding);
This is my new example class. This works out really well.
* Each Example has one value for the six different features that exist.
*/
public class Example
{
protected lightstatus lights;
protected soundstatus sound;
protected turn turnover;
protected fuelstats fuelgauge;
protected scents smell;
protected problems problem;
//Constructor for a parameter
public Example (lightstatus lights, soundstatus sound, turn turnover, fuelstats fuelgauge, scents smell, problems problem)
{
this.lights= lights;
this.sound= sound;
this.turnover= turnover;
this.fuelgauge= fuelgauge;
this.smell= smell;
this.problem= problem;
}
}
EDIT - making changes....
Cantido on
3DS Friendcode 5413-1311-3767
0
GnomeTankWhat the what?Portland, OregonRegistered Userregular
Solid. I'll probably be back here in a few hours with more Rails questions...I'm almost done with the Hartl tutorial now and I think I'm ready to start wrapping my brain around this app idea that I had.
Rails is pretty sweet. Welcome to awesomeville when it comes to writing RESTful MVC webapps.
GnomeTankWhat the what?Portland, OregonRegistered Userregular
Yeah, if the pay wasn't shit compared to what I do in the C#/line-of-business world, I'd switch my career to rails...but I just can't make what I make in that field.
Posts
Yep, I'm in that camp too. But in my experience is that, by far, functional programming --- namely immutability and all the great reasoning benefits you get from it --- is (should be) the common case. And then once you start going down that road, you find that OO programming on its own offers little additional benefit.
Nobody needs more than a single petaflop!
It feels a bit weird that 90% of my code is in the constructors for these objects, but it makes sense to me, because making these objects and positioning them is a one-time event that needs to happen at start-up, so constructors seem right to me.
Is there a convention for this, either way?
Looks like Fortran.
pickBASIC (my lovely pit of hell)
I had to ask because it didn't look quite like a Fortran program, and sure enough, a Basic variant. Well, it could always be worse, but I feel for ya.
I have to make a pseudo ID3 Decision Tree. These are used to indicate pathways in decision making such as diagnosing a car or the Ken Flowchart and all that.
Luckily I've been given a fixed set of decisions so it doesn't have to be generic. Each rule is called a Feature, and I have five of them. I put them in String Arrays, and then put those array in a shuffled Array List.
The Features get shuffled, and then I take the first Feature (rule) and make it the root. All the different options of the feature need to become children. So if the first feature is "lightstatus", lightstatus is the root, and it can just have two children. One for Normal and one for Dim. If there are four options like with Sound Status, there needs to be four children. I've swapped the BST's left and right child architecture for an Array List of Nodes. But now I'm having trouble no what I need to do next. At this point I know which feature the root would be, but I'm getting my parameterization mixed up. The selected feature's variable name would be the root and it's array list of strings would be all the children. How do I specify that? I feel like I'm so close but I'm doing something retarded.
This is my Node.
This is the tree so far.
One interesting example of Enums in a hierarchy: java.dzone.com/articles/enum-tricks-hierarchical-data
Oh, and just one nitpick: in order for a BST to earn the 'S' in its name, I'm pretty sure it has to be sorted. It's not clear from your code above that any kind of sorting is happening, or even desired.
Edit: wait, why are you representing this as a tree? If I'm understanding right, the nodes at each Feature level are identical, right? (I mean, with your "lightstatus" example above, will there be any difference at all between the children of "normal" and "dim"?) So far it sounds more like you're finding the right point in N-dimensional (or 'Feature'-dimensional) space for the diagnosis.
You're right about the enum, that might do the trick. As for the "Search" part, I don't think it's desired. The idea is to try different Features for roots, and see what makes the tree largest and smallest. The algorithm takes them in at random first, and later I will force in different roots and count the total nodes. The lesson is that good indexing can make or break Inductive Machine Learning (at the undergrad level anyway)
I will pseudo out the algorithm:
As the levels get added, examples follow the newly created children each time, so it quickly forges reliable case based reasoning.
Planet.Earth is different from Planet Earth = new Planet(...);
Oh and BTW I learned today that Excel will only open .csv files properly formatted if they are actually tab delimited :rotate: O_o
Nintendo ID: Incindium
PSN: IncindiumX
Something like this:
Typical CSV file I see:
Tab delimited file
How a CSV should be:
We ran into this on a result set created by MSSQL mail and tabs fixed the issue.
You are right though that with me standalone testing a simple .csv file I just created by hand it is properly opening up. Not sure what the issue is.
Nintendo ID: Incindium
PSN: IncindiumX
Yeah but the Java version is more elegant in this case
contrast/compare:
http://stackoverflow.com/questions/469287/c-sharp-vs-java-enum-for-those-new-to-c
Edit... the C# extension method version is pretty cool though
Nintendo ID: Incindium
PSN: IncindiumX
Except it's really not that much more elegant? It's just syntactic sugar around the same thing. The biggest differences are 'class' instead of 'enum' and the Values property, which isn't even technically necessary, you could do the same thing with simple reflection.
I don't care if C# adds the feature, it just annoys me when people say "This isn't possible in C#!!!!!"...when it is completely possible, you just have to think about it for a second.
I figured it out.
If the file is ASCII: Excel opens .csv files that are comma delimited and properly breaks the fields out in multiple columns.
If the file is Unicode: Excel opens .csv files that are comma delimited and puts all the fields in one column.
If the file is Unicode: Excel opens .csv files that are tab delimited and properly breaks the fields out in multiple columns.
Nintendo ID: Incindium
PSN: IncindiumX
I use .csv's all the time with commas in excel. Something else must be a miss.
EDIT: Whoops you figured it out already.
So instead of:
"import javax.swing.JFrame;
import javax.swing.JTabbedPanes;"
It just does: "import javax.swing.*;"
It means you've done "map.resource" in your route file, and that it's now going to treat that route as a complete resource path. That means the controller needs to support get, put, index, delete, etc. You can tweak what verbs actually need to be supported at the time of mapping, but without specifying, it's going to assume all the CRUD operations are there. Essentially, resource mapping is the heart of RESTful in rails.
@wildwood I made an enum version of the six Features so they look like this. Instead of put them in main I gave each Enum it's own file so I wouldn't have to do main.enumname everywhere. One final thing I want to do is put those enums in an ArrayList and shuffle them, because I will take the order and build a tree with it. But
ArrayList<Enum> Features = new ArrayList<Enum>();
Is not allowed.
This is my new example class. This works out really well.
EDIT - making changes....
Rails is pretty sweet. Welcome to awesomeville when it comes to writing RESTful MVC webapps.