• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

In a way, I feel you are validating his point.

If you want to discount probably the most important sector of our entire field, sure.

I guess I should expound upon it. When I got out of college I despised Java. Pretty much for all of the same reasons. When I started to work professionally I realized that you could have more real world productivity with Java and the available tools than pretty much anything other than a handful of aspects of .NET. For all of it's flaws it's got a ton of perks. But you sort of have to put on your project manager/business person hat to appreciate it.
 
Total newbie here. I'm doing excel VBA stuff at the moment.

I've got this form-like thing on sheet 1. After the user has written all kinds of stuff in it, I want the "Done!" button to make a new sheet with that same form in it, but empty.

I've got a done button and it's making a new sheet, but I'm scratching my head over the get-the-form-from-the-last-page-also bit.

What have you tried so far? Usually with excel VBA you want to use the Sheet.Range() (or whatever that's called) function to pull values and then copy them in to the new one that way but without seeing some code it's hard to know where you're stuck.

GAF, I need your help. I'm trying to implement a template class in Java and I get this error 'The operator < is undefined for the argument type(s) T, T' in Eclipse. I'm fairly new to Java and come from a C++ background so I have no idea what that means. I tried looking it up and thought using Comparable would fix it but that seems to be something used to compare Generics instead of unknown types within an array. Bolded is the error.

Since Java has no operator overloading there's no way to use the < or > operators on objects. What you want is the compareTo() method, which is defined in the Comparable interface. If a.compareTo(b) returns:
< 0 means a < b
0 means a == b
> 0 means a > b.

If you want to use your own defined objects in this way you need to implement the Comparable interface and provide your own implementation of compareTo.
 

Jokab

Member
:p

I used Java in college for a lot of stuff, including in several programming competitions. I thought I was really effective. All the non-trivial stuff I had done was either in Java or C++. Then I graduated and started using other languages. I'll never go back to Java, unless it's for a massive pay-raise. Getting anything done takes ages, although I guess that it's good for some domains.

Also, I don't have any problem with the style of interfaces in Java, though I think Go handles them much better. Screw multiple-inheritance. I almost never use inheritance of any kind anyway. Imo you can use composition more effectively most of the time to solve the same problems.

Out of curiosity, which languages did you learn which made you that more productive? I only know Java, and fiddled a bit in Python, so I have very little point of reference.

The little I tried in Python didn't make me feel very powerful, but I suppose that comes with experience.
 
Out of curiosity, which languages did you learn which made you that more productive? I only know Java, and fiddled a bit in Python, so I have very little point of reference.

The little I tried in Python didn't make me feel very powerful, but I suppose that comes with experience.

I'm on the same position as him, right now 99% of my code is either Python, Ruby or CoffeScript. I can't go back to Java for any large project; I don't mind writing a small class here or there but the whole development ecosystem is so toxic, ugh.
 
I'm on the same position as him, right now 99% of my code is either Python, Ruby or CoffeScript. I can't go back to Java for any large project; I don't mind writing a small class here or there but the whole development ecosystem is so toxic, ugh.

Curious to know what you consider a large project? I think the "toxic ecosystem" will very much depend on what you are trying to do.

In a way, I feel you are validating his point.

If you want to discount probably the most important sector of our entire field, sure.

You are both pretty much right. You could argue that Java is the worst language of all time or that it is missing a huge amount of important features. This is probably true for some contexts. But in terms of producing something commercially, it is all about the tools and libraries you are plugging together. In this regard? The syntax and other details are pretty irrelevant, and Java is more or less unmatched.
 

squidyj

Member
GAF, I need your help. I'm trying to implement a template class in Java and I get this error 'The operator < is undefined for the argument type(s) T, T' in Eclipse. I'm fairly new to Java and come from a C++ background so I have no idea what that means. I tried looking it up and thought using Comparable would fix it but that seems to be something used to compare Generics instead of unknown types within an array. Bolded is the error.

Code:
public class PriorityQ<T>
{
	PriorityQ(){}
	
	void Sort()
	{
		int varPos = m_queue.size() - 1;
		int swap = 1;
		
		while(swap == 1 && varPos > 0)
		{
			if([b]m_queue.get(varPos) < m_queue.get(varPos - 1)[/b])
			{	
			}
			else
				swap = 0;
		}
	}
	
	private ArrayList<T> m_queue;
}

so, T is an Object, operators like < are only defined in java for primitives, like int and float and whatnot.

If you want to compare two objects of type T you're going to need to guarantee that you can so you should make a stronger claim, or restriction on what T is. You'll want to use

public class PriorityQ<T extends Comparable<T>>

which says that whatever object T is, it must extend the Comparable interface. Then you can use the compareTo method to compare two objects returning an int that represents their difference. The standard is such that
a.compareTo(b) = a.relevantfield - b.relevantfield;
so, negative if the value of b is greater than a, and positive if b is less than a, and 0 if they are equal.
and if you want to use your own custom classes they will need to extend Comparable and implement the compareTo method.
 
I am programming a website for class using html and css. There are multiple pages in this website. Do i need to create a separate css for each page?
 

Zoe

Member
I am programming a website for class using html and css. There are multiple pages in this website. Do i need to create a separate css for each page?

If you're referencing a css file, then no, you only need one. If you're writing it out in the header, then you have to do it over and over and over again for each page.
 
Ordinarily, good practice would be to use a single site wide css for your main theme, with page specific css files for specific sections. Remember that because they cascade, you can use as many as you'd like on a page and only the most recent attribute value will be applied.

So you might have something like:

main.css
Code:
div#content {
    width: 50%;
    color: green;
}

alt.css
Code:
div#content {
    width: 75%;
}

Then you would include main.css in all your pages and the content div would be 50% wide. If you wanted the 75% to apply in one place you would include alt.css after main.css and the 75% would override the 50% but the colour would still be that horrible green.
 

usea

Member
Out of curiosity, which languages did you learn which made you that more productive? I only know Java, and fiddled a bit in Python, so I have very little point of reference.

The little I tried in Python didn't make me feel very powerful, but I suppose that comes with experience.
I am not an amazing programmer or anything. Most of what I do right now is in C#. But even though C# is very similar to Java, it has made me way more productive. Due to Visual Studio, the better standard library, wayyy better language features, etc.

Other languages I use from time to time are javascript, Go, Rust. I'm bad at them though. I believe in the right tool for the job. I think for a lot of jobs, python, ruby, javascript etc are going to be way better tools that Java.

For me, often the best tool is the one I'm most familiar with. So I tend to solve a lot of my problems in C#.
 

Tamanon

Banned
Speaking of Visual Studio. I'm cutting my teeth now on Python, learning how to use it(noob to programming). Should I stick to Notepad++ or Aptana and completely bypass Visual Studio? i noticed it has soem Python modules but I suspect it might make things overly confusing for me. I can download it for free through my school.
 

Septimius

Junior Member
Speaking of Visual Studio. I'm cutting my teeth now on Python, learning how to use it(noob to programming). Should I stick to Notepad++ or Aptana and completely bypass Visual Studio? i noticed it has soem Python modules but I suspect it might make things overly confusing for me. I can download it for free through my school.

I thiiiiink the Eclipse plugin is better, if I remember correctly. Maybe not.

If you're in school, I'm of the thought that your first semester should be with notepad++ equivalents. It's good to just learn to be thorough and not leaning on a program telling you your errors as you make them. It's not highly important to be super-efficient when you're starting out programming, as there's more than enough information to soak in. When you have a good foundation, moving to a nice IDE will be great.
 
Speaking of Visual Studio. I'm cutting my teeth now on Python, learning how to use it(noob to programming). Should I stick to Notepad++ or Aptana and completely bypass Visual Studio? i noticed it has soem Python modules but I suspect it might make things overly confusing for me. I can download it for free through my school.

For Python, there is an (official) addon for VS, IronPython. It's pretty cool and it lets you work with the whole .NET framework, but you don't really need that at the start, same for debugger, profiler or intellisense (auto-complete for method names etc.). If you feel like you have the basics of programming down, you can start working with an IDE.

edit: As for whether VS or Eclipse are better for Python.. I don't know. I've tried both, but most of the stuff I do in Python is small enough that Sublime Text is sufficient. I think VS is much better as an IDE in general and since you can get it for free, I'd take advantage of that. The one disadvatage that VS has, specificlally for Python, at least last time I checked, IronPython only worked for Python 2.7.

Edit 2: A question about Scala, I'm currently doing the Functional Programming class on Coursera and I'm stuck on the second assignment, which is to implement a purely functional set (a set being represented by its characteristic function, meaing a function Int => Boolean that for each integer, says if it is in the set or not). My problem is the method for constructing a singleton set (a set that only contains one integer). The function signature is this:
Code:
def singletonSet(elem: Int): Set
So, it returns a set that contains only elem. My first approach was to do
Code:
def singletonSet(elem: Int): Set = elem => true
but that doesn't work since it returns true for every integer. It should be so simple but somehow I can't wrap my head around it. I've only got a function called contains which takes a set and an integer as input and returns whether the set contains the integer or not, it looks like that: (it's already been implemented from the start)
Code:
def contains(s: Set, elem: Int): Boolean = s(elem)
 

Kimaka

Member
Since Java has no operator overloading there's no way to use the < or > operators on objects. What you want is the compareTo() method, which is defined in the Comparable interface. If a.compareTo(b) returns:
< 0 means a < b
0 means a == b
> 0 means a > b.

If you want to use your own defined objects in this way you need to implement the Comparable interface and provide your own implementation of compareTo.

so, T is an Object, operators like < are only defined in java for primitives, like int and float and whatnot.

If you want to compare two objects of type T you're going to need to guarantee that you can so you should make a stronger claim, or restriction on what T is. You'll want to use

public class PriorityQ<T extends Comparable<T>>

which says that whatever object T is, it must extend the Comparable interface. Then you can use the compareTo method to compare two objects returning an int that represents their difference. The standard is such that
a.compareTo(b) = a.relevantfield - b.relevantfield;
so, negative if the value of b is greater than a, and positive if b is less than a, and 0 if they are equal.
and if you want to use your own custom classes they will need to extend Comparable and implement the compareTo method.

Thank you both. Now its finally working.
 
Edit 2: A question about Scala, I'm currently doing the Functional Programming class on Coursera and I'm stuck on the second assignment, which is to implement a purely functional set (a set being represented by its characteristic function, meaing a function Int => Boolean that for each integer, says if it is in the set or not). My problem is the method for constructing a singleton set (a set that only contains one integer). The function signature is this:
Code:
def singletonSet(elem: Int): Set
So, it returns a set that contains only elem. My first approach was to do
Code:
def singletonSet(elem: Int): Set = elem => true
but that doesn't work since it returns true for every integer. It should be so simple but somehow I can't wrap my head around it. I've only got a function called contains which takes a set and an integer as input and returns whether the set contains the integer or not, it looks like that: (it's already been implemented from the start)
Code:
def contains(s: Set, elem: Int): Boolean = s(elem)


The reason that's not working is that you're defining the singletonSet to be a function that takes an int and returns true. What you want is something like this:

Code:
def singletonSet(elem: Int): Set = x => if (x == elem) true else false

In this case, x is the argument to the function that singletonSet returns. If that x is equal to the elem then the result is true. What this is in functional programming terms is a closure. The returned function closes over the value of elem so that you can compare other things (such as the argument x) to it later.

Disclaimer: I'm ok with FP in general but I know SFA about Scala, there might be a more idiomatic way to approach this.
 
The reason that's not working is that you're defining the singletonSet to be a function that takes an int and returns true. What you want is something like this:

Code:
def singletonSet(elem: Int): Set = x => if (x == elem) true else false

In this case, x is the argument to the function that singletonSet returns. If that x is equal to the elem then the result is true. What this is in functional programming terms is a closure. The returned function closes over the value of elem so that you can compare other things (such as the argument x) to it later.

Disclaimer: I'm ok with FP in general but I know SFA about Scala, there might be a more idiomatic way to approach this.

In the meantime I figured it out, but thanks anyways. Thinking about what the function has to look like helped me in the end. (a boolean return type means the body of the function has to be some sort of predicate, like i % 2 == 0) I have the following now
Code:
def singletonSet(elem: Int): Set = i => elem == i
 
Anyway to do a "return" in a for loop and continue with the loop after it returns something?

Hmm.. my intuitive approach would be to add the return value you want out of the loop to some sort of list or array in each iteration, but I don't know enough about what you need it for to really say. Could be completely unhelpful. On an unrelated note, I should go to bed. Spent all night playing Dota and working on the Scala assignment (which is really fun, I've dabbled in functional programming a little bit, actually bought a F# book as well, but I never got very far with it).
 
Okay, getting hang of it pretty fast...

My goal is to make a simple Snake Game with a twist of Portal :)

haha.. one of my assignments this semester is a Snakes game, to which i'm adding a touch of portal. i may have to chase you up after i've finished it and see how yours went.

Anyway to do a "return" in a for loop and continue with the loop after it returns something?

the whole idea of Return is that it "returns" program execution to the calling function. Passing parameters is a secondary function. have a think about what you're trying to accomplish: do you need to do something with the value in parallel with the loop executing? in that case you might want to use threading.

Can the loop pause while the returned value is acted on, and resume when that is done? then call a method from within the loop. returning from that method will drop you back into the loop.

Can the value be acted on after the loop is done? Store it and wait for the loop to finish.

Do you need to pass a variety of information back, including that value and maybe some others? create a class or data structure to hold all the required bits and return that at the end.

basically return will always end execution of that block, i don't think it's the mechanism you're looking for. maybe if you described better what it is you are trying to do?
 

pompidu

Member
Hmm.. my intuitive approach would be to add the return value you want out of the loop to some sort of list or array in each iteration, but I don't know enough about what you need it for to really say. Could be completely unhelpful. On an unrelated note, I should go to bed. Spent all night playing Dota and working on the Scala assignment (which is really fun, I've dabbled in functional programming a little bit, actually bought a F# book as well, but I never got very far with it).

I dont think so, if theres some sort of value that you want for later usage I would suggest creating a global variable, putting whatever test statements in your for loop and saving it.

Hmm.. my intuitive approach would be to add the return value you want out of the loop to some sort of list or array in each iteration, but I don't know enough about what you need it for to really say. Could be completely unhelpful. On an unrelated note, I should go to bed. Spent all night playing Dota and working on the Scala assignment (which is really fun, I've dabbled in functional programming a little bit, actually bought a F# book as well, but I never got very far with it).

I dont think so, if theres some sort of value that you want for later usage I would suggest creating a global variable, putting whatever test statements in your for loop and saving it.

Use threading and report back each time you find what you need? (complex solution!)

Does Java have the equivalent of a background worker (c#)?

I might be approaching the code the wrong way, so ignoring that;
I have to determine the boolean value of comparison operators (<, <=, >, ....) using 1 for true and 0 for false in unison with infix/postfix calculations. My problem is they have to be strings and I dont know how to get each individual string from a Token, my teacher used regular operators (+, - , * ...) and used char to get the one at 0. I have no idea how to do that for a string. Hard to explain and might not be able to help me so Il just show the code. If you cant help no biggie, just dont know wtf to do cause he doesnt teach this stuff.
Code:
private Token calculate(Token opr, Token opd1, Token opd2)
    {
        // Get the first char from opr, it is the operator: +, -, ...
        char ch = opr.getBody().charAt(0);
        
        //Get the two operands by converting from String to int
        int op1 = Integer.parseInt(opd1.getBody());
        int op2 = Integer.parseInt(opd2.getBody());
        
        //Default return value, in case an error occurs
        int res = Integer.MAX_VALUE;
        
        //Perform the operation, and set a value for res
        
        switch (ch)
        {
            case '+' : res = op1+op2;break;   
            case '-' : res = op1-op2;break;  
            case '*' : res = op1*op2;break;
            case '/' : if (op2 != 0)
                        res = op1/op2;
                       else 
                       System.out.println("Division by zero error in"+
                       " PostfixEvaluator.calculate().");
                       break;  
            case '%' : if (op2 != 0)
                        res = op1%op2;
                       else 
                       System.out.println("Division by zero error in"+
                       " PostfixEvaluator.calculate().");
                       break;  
            default: System.out.println("Illegal Operator in "+
                "PostfixEvaluator.calculate()");
        }
        //Convert res into a Token and return it.
        return new Token(""+res);
    }
 

Aeris130

Member
You want ch to contain "<=" etc? Just call substring on the body, assuming it's a string.

Code:
String ch = opr.getBody().substring(int beginIndex, int endIndex)
//Returns a new string that is a substring of this string.

It means ch needs to be a String, but those can be checked using switch() cases as well.
 
looks like the Token class function getBody() returns a string.

On any string you can call charAt(int position), for example:

Code:
String myString = "hello";
Token myToken = new Token(myString);
System.out.println(myToken.getBody().charAt(2));

based on the code above, this should print the first "l" from "hello".

you can use the substring method to take more than one char, and the string length value to ensure you don't try to take from an index that doesn't exist.
 
You want ch to contain "<=" etc? Just call substring on the body, assuming it's a string.

Code:
String ch = opr.getBody().substring(int beginIndex, int endIndex)
//Returns a new string that is a substring of this string.

It means ch needs to be a String, but those can be checked using switch() cases as well.

Yeah I think this is what you mean, than you would just have to check if the 2nd character in that string is an int, etc.
 

pompidu

Member
You want ch to contain "<=" etc? Just call substring on the body, assuming it's a string.

Code:
String ch = opr.getBody().substring(int beginIndex, int endIndex)
//Returns a new string that is a substring of this string.

It means ch needs to be a String, but those can be checked using switch() cases as well.

looks like the Token class function getBody() returns a string.

On any string you can call charAt(int position), for example:

Code:
String myString = "hello";
Token myToken = new Token(myString);
System.out.println(myToken.getBody().charAt(2));

based on the code above, this should print the first "l" from "hello".

you can use the substring method to take more than one char, and the string length value to ensure you don't try to take from an index that doesn't exist.

Tried this using your guys advice,
Code:
String token = opr.getBody().substring(0);
And by stepping it does take in the string correctly. Now eclipse is tell me that Strings cannot do switch statements? is this correct? Am I stuck doing all this in if statements?
 
Tried this using your guys advice,
Code:
String token = opr.getBody().substring(0);
And by stepping it does take in the string correctly. Now eclipse is tell me that Strings cannot do switch statements? is this correct? Am I stuck doing all this in if statements?

Lol, just checked and not really unfortunately. Either do it with if statements or you have to do this workaround. Just search "switch statements with string" for the later. I would just do it with if statements honestly.
 

pompidu

Member
Lol, just checked and not really unfortunately. Either do it with if statements or you have to do this workaround. Just search "switch statements with string" for the later. I would just do it with if statements honestly.

Aight cool. Thanks everybody for your help. The rest of this is impossible to try and post here and I won't figure out myself so I'm going to go have to bitch to my professor Thursday about him not teaching us any of this.
 

pompidu

Member
JDK 7 will finally allow String switch statements
yeah i did see that but i doubt my school will update to it. This my last programming class for my degree and i cant wait. I enjoy learning to program but my professors have a hard time relating what is being taught to students.
 
Does anyone have experience with Java frameworks such as Struts, Hibernate and Spring? Are they easy/fast to learn?

I can help you there. Check out http://www.mkyong.com/ as he has excellent tutorials and examples to get you started in each.

Easy/fast to learn? That depends. You can be up and running with simple examples of each very quickly. For example a login page with some struts or spring MVC tags backed up by Hibernate for DB access within a few hours (depending how much you know about JAVA etc of course).

To learn all of what they can do and why, especially with Spring? You are going to be putting in a heck of a lot of work.
 
I can help you there. Check out http://www.mkyong.com/ as he has excellent tutorials and examples to get you started in each.

Easy/fast to learn? That depends. You can be up and running with simple examples of each very quickly. For example a login page with some struts or spring MVC tags backed up by Hibernate for DB access within a few hours (depending how much you know about JAVA etc of course).

To learn all of what they can do and why, especially with Spring? You are going to be putting in a heck of a lot of work.

Thanks, I asked if they were easy or fast to learn because the people interviewing me want me to have an idea of how they work and what they do, due to my lack of experience with them.
 

Snowdrift

Member
Speaking of Visual Studio. I'm cutting my teeth now on Python, learning how to use it(noob to programming). Should I stick to Notepad++ or Aptana and completely bypass Visual Studio? i noticed it has soem Python modules but I suspect it might make things overly confusing for me. I can download it for free through my school.

Wing IDE 101 is worth taking a look at as well. It's what I have been learning on at school. Being able to test out code in the shell is a great feature when I just want to try something out, or when I need to look up some documentation really quickly.
 
Thanks, I asked if they were easy or fast to learn because the people interviewing me want me to have an idea of how they work and what they do, due to my lack of experience with them.

Fair enough. I actually went through that way back when. The company had Spring/Hibernate/Struts on the PD so I had to skill up within a day so it looked like I knew what I was talking about ;) Those tutorials should get you out of trouble.

If you are really keen, also throw $10 at:

http://www.smashwords.com/books/view/45989

Which covers those three techs specifically in terms of what you might be asked in an interview. You wouldn't have to know all of it I wouldn't think, but if you at least get some grounding it'll show you are keen.
 

Seanbob11

Member
Is there an easy way for the user to state how many elements will be in an Array in C++? I understand that arrays values can only be defined by constants so this code wouldn't work:

Code:
//Vairables 
	int input;
	int hold;
	
	cin >> "Input how many numbers in the row: ";
	cin >> input;

	const int numOfElements = input;

	int marksArray [numOfElements];

Would it be easier to use vectors? We haven't come across them in class and thus I would have to learn vectors.
 

arit

Member
Is there an easy way for the user to state how many elements will be in an Array in C++? I understand that arrays values can only be defined by constants so this code wouldn't work:

Code:
//Vairables 
	int input;
	int hold;
	
	cin >> "Input how many numbers in the row: ";
	cin >> input;

	const int numOfElements = input;

	int marksArray [numOfElements];

Would it be easier to use vectors? We haven't come across them in class and thus I would have to learn vectors.

That would be dynamically allocating memory on the heap:
Code:
int *marksArray = new int[input]; // allocate input * sizeof(int) bytes of memory
.
.
.
delete[] marksArray; // release it after use to prevent memory leaks

Access to the elements does not change due to language magic(or at least that is how I think it is), so just new and delete and you are fine.

Vectors would be useful if the size of the array should change during runtime and it has some handy functions.
 
Top Bottom