|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On October 05 2016 09:44 travis wrote: I have directions on my newest project in java 2 class has me create a GUI and then it does some stuff (what it does is unimportant).
They want me to implement one of my ActionListeners with an inner class, which I do. And in the constructor I add the listener to my button.
They also want me to implement one of my ActionListeners as an anonymous class. I am not sure how I go about doing this. Would I put my anonymous class inside my constructor.. like this?
member.addActionListener(new ActionListener() {
//ActionListener methods/code goes here
}; ); Yes, that is an annonymous inner class. Here is a slightly larger example: + Show Spoiler +import java.awt.EventQueue; import java.awt.event.WindowEvent; import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class TestApp { public static void main(String[] args) { EventQueue.invokeLater(() -> new TestApp()); } private JFrame frame; public TestApp() { frame = new JFrame("Test App"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(640, 480); frame.setLocationRelativeTo(null); // This is an annonymous inner class of type WindowListener frame.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { System.out.println("TestApp.windowOpened()"); } @Override public void windowIconified(WindowEvent e) { System.out.println("TestApp.windowIconified()"); } @Override public void windowDeiconified(WindowEvent e) { System.out.println("TestApp.windowDeiconified()"); } @Override public void windowDeactivated(WindowEvent e) { System.out.println("TestApp.windowDeactivated()"); } @Override public void windowClosing(WindowEvent e) { System.out.println("TestApp.windowClosing()"); } @Override public void windowClosed(WindowEvent e) { System.out.println("TestApp.windowClosed()"); } @Override public void windowActivated(WindowEvent e) { System.out.println("TestApp.windowActivated()"); } });// End of the annonymous inner class. frame.setVisible(true); } }
On October 05 2016 10:30 RCCar wrote: Hihi~ I was just wondering if there was anyway to set 1 string equal to another in java! I'm not talking about the .equals method, which only seems to return a boolean value, but a method that will directly transfer/copy String 1 to String 2! What exactly do you want to do? Copy the contents of one String object to another? Strings in Java are immutable. Once created their contents will never ever change. You can do this:
String b = "Test"; String a = new String(b); Which will create a new String object with the same contents as the String object created before, but this does not do anything useful at all (at least not that I can think of). If you just do:
String b = "Test"; String a = b You will have only a single String object referenced by both variable but since the String object is immutable it should never make a difference in your code (unless you do something really really wrong).
|
On October 05 2016 13:50 Wrath wrote:That was interesting read and described my suffering to learn front-end development :D This paragraph caught my interest: Show nested quote +No one does at the beginning. Look, you just need to know that functional programming is better than OOP and that’s what we should be using in 2016 Why? I don't know what FP is, I'll do some research on it but I never thought that OOP would become out dated one day. Is it really on a decline?
This is a satire article, "functional programming is better than OOP" is about as uselessly vague a statement as they come . Most of the languages nowadays have elements of both paradigms. And if anything, in practical languages there is more of a continuum than a clear barrier between pure functional and absolute OO programming. I'd say you're better off mastering the better concepts of both.
|
Russian Federation4235 Posts
OOP is great because it more or less mirrors how computers actually work (persistent state and functions to manipulate it) and how humans think about data manipulation as well. FP is great because it makes it very easy to construct provable statements and guarantees about your code (because pure functions have no side effects, you only need to look at the signature and the return type).
Maybe OOP is dying for JavaScript, I don't really know, but it's not a dated concept at all for more performance-critical languages. A major tilt in cpu design has to happen for that to change.
|
OOP and functional are not mutually exclusive. I think Robert C. Martin once put it like that: Structured programming is the removal of GOTO. Object oriented programming is the removal of function pointers. Functional programming is the removal of assignments.
All of these can be integrated into a single language, and many functional languages already are both structured and object oriented. Support for functional programming in the OOP languages like C# is still limited (afaik we don't have much in the way of optimizations for recursion yet), but it's getting better.
The future will be FP + OOP, not either of these.
|
On October 06 2016 01:44 spinesheath wrote: OOP and functional are not mutually exclusive. I think Robert C. Martin once put it like that: Structured programming is the removal of GOTO. Object oriented programming is the removal of function pointers. Functional programming is the removal of assignments.
All of these can be integrated into a single language, and many functional languages already are both structured and object oriented. Support for functional programming in the OOP languages like C# is still limited (afaik we don't have much in the way of optimizations for recursion yet), but it's getting better.
The future will be FP + OOP, not either of these. many scripting languages (python, ruby, perl, php) have functional and oop aspects. In fact, Haskell, one of the ur-languages for functional programming, is at core a scripting language, and it has its own weird idea of objects (monads).
Functional aspects are being increasingly adopted into classical OOP languages. The future is very much a mix, and probably some further innovation that I don't even know about yet.
|
Did any of you do "hackathons" or coding competitions while in school?
If so, how were they? If I go to a coding competition as a newer programmer, knowing only java, am I gonna get owned by whatever the question[s] are?
|
On October 06 2016 01:44 spinesheath wrote: OOP and functional are not mutually exclusive. I think Robert C. Martin once put it like that: Structured programming is the removal of GOTO. Object oriented programming is the removal of function pointers. Functional programming is the removal of assignments.
All of these can be integrated into a single language, and many functional languages already are both structured and object oriented. Support for functional programming in the OOP languages like C# is still limited (afaik we don't have much in the way of optimizations for recursion yet), but it's getting better.
The future will be FP + OOP, not either of these.
Funny that you mention recursion optimization (I guess you mean stuff like TRE here). I've read an article by Guido van Rossum recently in which he defended his position on consciously not including recursion optimization in Python, despite people pointing out that it would be relatively easy to do.
He stated that Python isn't about performance but about an ease of debugging and as soon as you implement TRE you lose your full stack trace, thus rendering it unsuitable for the language. He's even against lambdas (map, filter, reduce), even though they've been implemented into the language by the request from Lisp community, and is a strong proponent of using list comprehensions when dealing with data structures.
|
Hyrule18967 Posts
On October 06 2016 06:38 travis wrote: Did any of you do "hackathons" or coding competitions while in school?
If so, how were they? If I go to a coding competition as a newer programmer, knowing only java, am I gonna get owned by whatever the question[s] are? I did a competition (6 hours), but my team was me, a math person, and another guy who was math/programming. Jerks ended up solving the problems in the first hour then just spent the rest of the time hanging out in the work area (separated from the competition floor) and never relieved me. Shit, I had to run up to them (on the second floor) every time I finished one problem so I could get the solution to the next.
bleh
|
On October 06 2016 06:45 Manit0u wrote:Show nested quote +On October 06 2016 01:44 spinesheath wrote: OOP and functional are not mutually exclusive. I think Robert C. Martin once put it like that: Structured programming is the removal of GOTO. Object oriented programming is the removal of function pointers. Functional programming is the removal of assignments.
All of these can be integrated into a single language, and many functional languages already are both structured and object oriented. Support for functional programming in the OOP languages like C# is still limited (afaik we don't have much in the way of optimizations for recursion yet), but it's getting better.
The future will be FP + OOP, not either of these. Funny that you mention recursion optimization (I guess you mean stuff like TRE here). I've read an article by Guido van Rossum recently in which he defended his position on consciously not including recursion optimization in Python, despite people pointing out that it would be relatively easy to do. He stated that Python isn't about performance but about an ease of debugging and as soon as you implement TRE you lose your full stack trace, thus rendering it unsuitable for the language. He's even against lambdas (map, filter, reduce), even though they've been implemented into the language by the request from Lisp community, and is a strong proponent of using list comprehensions when dealing with data structures. There's a number of fancy optimizations the better functional languages have in store (apparently not Scala). I'm not familiar with all of them but I certainly know that they are essential if you want to have good performance with recursion. And that is kinda important if you're doing functional.
He has a point though I guess. Not every language needs to be able to everything. But there's also a point to be made about making features optional.
|
On October 06 2016 06:38 travis wrote: Did any of you do "hackathons" or coding competitions while in school?
If so, how were they? If I go to a coding competition as a newer programmer, knowing only java, am I gonna get owned by whatever the question[s] are?
(Only speaking to coding competitions, as I haven't done any hackathons) If you haven't done any more "mathy" computer science, like an Algorithms course, coding competitions can be difficult. All of the ones that I have done have basically been an Algorithms final where you have to implement your solutions, but typically the actual coding is much less complex than coming up with the answer.
I know there's a number of websites where you can do coding competition problems at your leisure, but I don't know the best. Now that I've graduated, the only competition I do regularly is Google's Code Jam. All of the problems for those are available online, and I've enjoyed them, so it might be a fun place to start.
For the record, I really enjoy coding competitions, because they make fun brain teasers, if nothing else. But that may be predicated on having a general idea as to what the common approaches to solutions are.
|
On October 05 2016 23:44 BluzMan wrote: OOP is great because it more or less mirrors how computers actually work (persistent state and functions to manipulate it) and how humans think about data manipulation as well. FP is great because it makes it very easy to construct provable statements and guarantees about your code (because pure functions have no side effects, you only need to look at the signature and the return type).
Maybe OOP is dying for JavaScript, I don't really know, but it's not a dated concept at all for more performance-critical languages. A major tilt in cpu design has to happen for that to change.
I wouldn't say dying as such, that's certainly not really true.
But I think there is a shift. It's more that the common problems you solve with javascript (webapps) don't really lend themselves much to a rigid object oriented approach. Especially because you can leverage weak types and non-rigid objects to avoid strictly modeling objects.
One of the things that more functional approach in Javascript also solves is constantly having to duplicate the same state. Instead of having the state come in from the endpoint (as JSON most likely) -> turn it into some type of object model -> turn that into stateful DOM and keep all three in active in memory & your code (maybe not the JSON, but that's still living in terms of what happens when you receive new data from your endpoints) more functional leaning approach can help to give you a singular source of truth of the applications state.
|
On October 06 2016 07:02 spinesheath wrote:Show nested quote +On October 06 2016 06:45 Manit0u wrote:On October 06 2016 01:44 spinesheath wrote: OOP and functional are not mutually exclusive. I think Robert C. Martin once put it like that: Structured programming is the removal of GOTO. Object oriented programming is the removal of function pointers. Functional programming is the removal of assignments.
All of these can be integrated into a single language, and many functional languages already are both structured and object oriented. Support for functional programming in the OOP languages like C# is still limited (afaik we don't have much in the way of optimizations for recursion yet), but it's getting better.
The future will be FP + OOP, not either of these. Funny that you mention recursion optimization (I guess you mean stuff like TRE here). I've read an article by Guido van Rossum recently in which he defended his position on consciously not including recursion optimization in Python, despite people pointing out that it would be relatively easy to do. He stated that Python isn't about performance but about an ease of debugging and as soon as you implement TRE you lose your full stack trace, thus rendering it unsuitable for the language. He's even against lambdas (map, filter, reduce), even though they've been implemented into the language by the request from Lisp community, and is a strong proponent of using list comprehensions when dealing with data structures. There's a number of fancy optimizations the better functional languages have in store (apparently not Scala). I'm not familiar with all of them but I certainly know that they are essential if you want to have good performance with recursion. And that is kinda important if you're doing functional. Scala is build on top of Java if I remember correctly. In that case it most likely wont optimize recursion in any way which would change the Stack Trace. Keeping the stack trace intact is pretty important in java because java allows all kinds of fancy dynamic / runtime analysis. Anything that would change the stack trace would break those features.
However there is still JIT optimizations that could be done. I am not entirely sure how well the JIT can optimize for recursion but it does some crazy good stuff with loops.
Edit: Talking about JIT optimizations in java; this is fucking amazing: https://blog.jooq.org/2016/07/19/the-java-jit-compiler-is-darn-good-in-optimization/
The linked article shows how the JIT will optimize the testIterator method from this class:
public class Test { static int res = 0; public static void main(String[] args) { for (int i = 0; i < 100000; i++) { res += testIterator(Collections.singletonList("x")); } System.out.println(res); }
static int testIterator(List<String> list) { int sum = 0; Iterator<String> it = list.iterator(); while(it.hasNext()) { String s = it.next(); sum += s.length(); } return sum; } }
into machine code which does something like this pseudo code:
if (list.class != Collections$SingletonList) { goto SLOW_PATH; // interprete the byte code } str = ((Collections$SingletonList)list).element; if (str.class != String) { goto EXCEPTIONAL_PATH; // throw exception } return ((String)str).value.length; This is not theory, this is the actual output from the JIT optimization.
|
god, I have to do this lab and the only requirement is to just make it work
and my code seems sooooooooooooooooo bad
the class is of a type "triple" which holds 3 generic objects, and the objects have to implement compareTo
and I have to write an iterator for it, that when next() is called returns the 3 generic objects from "smallest" to "largest" (i guess, using compareTo).
This is my code for my next() method:
(sorry I still don't know how to put it in "code" format or w/e on the tl forums even though you guys have told me how before)
+ Show Spoiler + public T next() { if (this.hasNext()) { int current = cursor; cursor++; //Time to sort them T sortedLeft, sortedMid = null, sortedRight; if(left.compareTo(middle) < 0){ sortedLeft = left; } else { sortedLeft = middle; } if(right.compareTo(sortedLeft) < 0){ sortedLeft = right; } if(left.compareTo(middle) > 0){ sortedRight = left; } else { sortedRight = middle; } if(right.compareTo(sortedRight) > 0){ sortedRight = right; } if(middle.compareTo(sortedRight) == 0 && middle.compareTo(sortedLeft) == 0) { sortedMid = middle; } if(middle != sortedLeft && middle != sortedRight){ sortedMid = middle; } if(left != sortedLeft && left != sortedRight){ sortedMid = left; } if(right != sortedLeft && right != sortedRight){ sortedMid = middle; } if(sortedMid == null){ sortedMid = middle; } if(current == 0 ) { return sortedLeft; } if(current == 1) { return sortedMid; } if(current == 2) { return sortedRight; } } throw new NoSuchElementException(); }
Seems horrible. Like, really bad. It works and I pass the test but clearly there must be a much more elegant way. Also, java forced me to initialize sortedMid to null... but I thought objects were automatically null when they are declared?
edit: ok, put it in the code tag
|
Hyrule18967 Posts
[code] is a tag, use it :X
|
If you replace "spoiler" with "code" in what you posted then you'll have the code formatting. Member variables are automatically initialized to null when the object's constructor is called, but your variables are not member variable. The other two variables are guaranteed to be initialized because you have if/else statements so the compiler didn't complain about them, but the sortedMid variable only has if's which could all be false.
As for sorting, you can read about quick sort, bubble sort, heap sort, and merge sort. You've got so few elements that it really isn't worthwhile to optimize though honestly.
|
@travis: You should really work on your code formatting man...
Also, this "triple" of yours seems to simply be a collection of objects. All you need to do then is to implement a comparator and use Collections.sort on it.
At least that's what I think but I suck at Java.
Something like that I believe:
public static Iterator sortedIterator(Iterator it, Comparator comparator) { List list = new ArrayList();
while (it.hasNext()) { list.add(it.next()); }
Collections.sort(list, comparator);
return list.iterator(); } }
|
What's so bad about the formatting? Some of it is a bit off because I didn't think I would ever go back to the code but in general I thought it was pretty ok.
I thought about adding them to a list and sorting them... now I don't know why I didn't. Also I feel like I am going crazy trying to understand iterable/iterator comparator/comparable and their proper usages (I kind of get it.. it's just difficult keeping it all straight)
|
Hope no one minds me asking a few C++ questions every now and then :S So, I'm wondering about an excersise from the book C++ Primer.
"What does this code do?"
const char ca[] = { 'h', 'e', 'y' }; const char *cp = ca;
while (*cp) { cout << *cp << endl; ++cp; }
Sooo, cp is a pointer to &ca[0] and we keep incrementing it until we encounter *cp == 0. But... ca[] is not null-terminated. I was thinking the while loop should eventually show undefined behaviour, but everytime I run this code it seems to work fine.
Is it undefined? If not, what am I missing?
|
I think you're right. Perhaps the example has a mistake and is working for some other reason that's not seen in the source? It might work because of how the program is set up in memory before it's started? For example, static data structures might be aligned at 32-bit or 64-bit boundaries. There might then be a gap behind that array and the next piece of static data, and that gap might be filled with a zero by default.
|
Thanks a lot, that's reassuring  Yea, everything's possible with undefined behaviour. I was just really scared I'm missing something obvious.
|
|
|
|