Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
What does it mean by object? in 2.1 PPC the same code works perfectly. I very often just use JOptionPanes for lone ints. I think i'll just get used to the long way.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
What does it mean by object? in 2.1 PPC the same code works perfectly. I very often just use JOptionPanes for lone ints. I think i'll just get used to the long way.

If you don't know the difference between a primitive type and an Object the you are in real trouble. A primitive type cannot have methods called on it, cannot be subclassed... It's not an object. If a method calls for an Object then passing an int should not work.

Note that Java 1.5 has introduced a new language feature to save the lazy like yourself called Autoboxing which will automatically insert the code to turn an int into an Integer in situations like this. Personally I think this is a bad idea as it leads to a lack of understanding and clarity about what an Object is.

If you installed Java 1.5 on your PPC machine and were using that but have not installed it on your Intel machine this would explain why it works on one machine but not the other. In an exam situation you should do it the correct way (which is not to rely on autoboxing).
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
robbieduncan said:
If you don't know the difference between a primitive type and an Object the you are in real trouble. A primitive type cannot have methods called on it, cannot be subclassed... It's not an object. If a method calls for an Object then passing an int should not work.

Note that Java 1.5 has introduced a new language feature to save the lazy like yourself called Autoboxing which will automatically insert the code to turn an int into an Integer in situations like this. Personally I think this is a bad idea as it leads to a lack of understanding and clarity about what an Object is.

If you installed Java 1.5 on your PPC machine and were using that but have not installed it on your Intel machine this would explain why it works on one machine but not the other. In an exam situation you should do it the correct way (which is not to rely on autoboxing).

My teacher must really suck. He hasn't taght anything on methods and sub classing. Yes. the basics are lacking... and primitive classes:eek: I am so lost!

I have only been taught the "short hand" way i have never used Integer i; rather just int i; and I think that my grade 10 exam won't be that detailed. some guys in my class can only just barely declare variables. It makes me angry having such sub-standard education!
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Grade 10? Like School, not University? I'm from Scotland so I don't understand how old that makes you but the lack of understanding as to what is going on makes a lot more sense. I assumed you were taking an intro to programming style class at Uni or college...

One more thing: primitives are not Classes. That's kind of their point! A lot of people think primitives are a pretty silly idea in Java and that everything should have been an Object.
 

dextertangocci

macrumors 68000
Apr 2, 2006
1,766
1
Grade 10? Like School, not University? I'm from Scotland so I don't understand how old that makes you but the lack of understanding as to what is going on makes a lot more sense. I assumed you were taking an intro to programming style class at Uni or college...

He is about 16.... 2 more years of school, if he passes this year:p :D

I know nothing about Java, so sorry I can't help:eek: :p

But good luck for your exam tomorrow!!!:)
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
Yeah, this is the problem with using Java to teach a beginner's programming course. There are too many things that would have to be glossed over, and the students are not going to get a very good foundation.

My sister took a Java programming class in grade 10. She hated it.

When I was in high school, procedural languages were it, starting with QuickBasic in grade 10 and Pascal in grades 11 and 12. Frankly I think they should still be teaching some variant of QuickBasic for the real beginner courses.

Heck, it took me several years of procedural language programming (granted, these were my early teens) before I finally clued in to what object-orientation was all about.
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
Yes! That works, is there any reasoning behind that or is it just the way it is?

Aha. Yes, you need to brush up on your understanding of object-oriented languages and the difference between objects and primitive types.

A primitive type is just a piece of data (almost always resolves to just a number). Like an integer, a floating point number, a character (which is really just represented as a number anyway), a bool (true/false, which can also be represented as a number).

You can't really do much with primitive types other than assign them to variables, copy them, manipulate them mathematically.

Then there are Objects, which are instances of Classes. Objects can contain data (which can be instances of other objects, or a bunch of primitive types), they can have methods, constructors, destructors, all kinds of code.

It's like the difference between a simple number and a dialog. A number is a number. You can't do much with it. You can't contain data within it. A dialog is far more complicated -- they can be created, destroyed, displayed, resized, they can contain data, messages, etc.

When a method is expecting some kind of object, and you give it a number, it doesn't like that. That's why you were getting an error before.

The solution? Java provides a class called Integer, which encapsulates an integer. (It's an object that contains exactly one piece of data -- an integer number). That's what you're doing in the version of the code that works -- you are creating a new object which contains the integer you want. This satisfies the method that expected an object.

So when you have a method that expects an object, and the thing you're actually trying to send it is a number, you just create a new object that contains that number, and then you can pass it to the method.
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
Yes! That works, is there any reasoning behind that or is it just the way it is?
Sorry, I just saw this thread or I would have posted earlier.

The reason it works is that, as explained above, the showMessageDialog method requires a Component, which could be null, and an Object which is the message.

An int is not an object (i.e., Object or something derived from Object) (easiest way to tell: if it starts with a lowercase letter - like int, char, etc. - it isn't an object), and so it won't work. As explained above, Java 5 will automatically create an object of the correct type (Integer for an int) when you put the primitive in the method call, so it does the "new Integer(i)" stuff behind the scenes. Previous versions of Java don't do this. I agree with others that, in general, it's best to do this explicitly with the "new Integer(i)" code because it creates the least amount of confusion. :)

Edit: or just see above; I took too long getting a piece of pie before answering. ;)
 

DrEasy

macrumors regular
Jan 12, 2004
100
0
That method expects an Object as a parameter so that it can call the toString() method on it. toString() creates a String representation of that object, needed in order to be displayed in that OptionPane of yours. toString() is implemented by default in the mother-of-all-classes called Object, but is overridden (i.e. replaced) in most subclasses.

"new Integer(i)" gives you an object of class Integer, so the toString() method that will be invoked is the one defined in the Integer class. Alternatively, you could use:

i+"" would also work, as Java would interpret "+" as a string concatenation operation between the string representation of i and the empty string, and would result in a String object. In this case the toString() method of class String would be used.

Java 5 ends indeed this silliness by converting i to an object when needed. But it also muddies the distinction between primitive types and objects, making Java even less suitable as a language for teaching programming.
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
Damn, you guys are so smart. the exam was simple except this question:

Write a programme that will allow a user to type the no. of rows to display. The following output will be given by only using for loops.

Amount of rows to display?
User entered:5
Output:
1
22
333
4444
55555

I know the basic structure is a nested for loop but the rest leaves me blank!

here's what I did:
Code:
	String q=JOptionPane.showInputDialog(null,"choose your lines");
		int a=Integer.parseInt(q);
		
		for (int i = 1; i<=a; i++)		
		{
			for (int j = 1; j<=i; j++)     
			{
				System.out.print(i);
		   	}
			
			System.out.println();
		}
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
Damn, you guys are so smart. the exam was simple except this question:

Write a programme that will allow a user to type the no. of rows to display. The following output will be given by only using for loops.

Amount of rows to display?
User entered:5
Output:
1
22
333
4444
55555

I know the basic structure is a nested for loop but the rest leaves me blank!

here's what I did:
Code:
	String q=JOptionPane.showInputDialog(null,"choose your lines");
		int a=Integer.parseInt(q);
		
		for (int i = 1; i<=a; i++)		
		{
			for (int j = 1; j<=i; j++)     
			{
				System.out.print(i);
		   	}
			
			System.out.println();
		}
Sounds like you did well. Congratulations. The code for the question you gave looks correct to me.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Damn, you guys are so smart. the exam was simple except this question:

Write a programme that will allow a user to type the no. of rows to display. The following output will be given by only using for loops.

Amount of rows to display?
User entered:5
Output:
1
22
333
4444
55555

I know the basic structure is a nested for loop but the rest leaves me blank!

here's what I did:
Code:
	String q=JOptionPane.showInputDialog(null,"choose your lines");
		int a=Integer.parseInt(q);
		
		for (int i = 1; i<=a; i++)		
		{
			for (int j = 1; j<=i; j++)     
			{
				System.out.print(i);
		   	}
			
			System.out.println();
		}

That code will work, but there's one subtle thing that I would change:

It's normal for C and Java programmers to count from 0 not 1. So I'd alter the loops to for (int i=0; i<a;i++). This will run unnoticeabley faster. It'll also match the examples you'd read in books, and look more like the code a pro would write!

Other than that well done :)

Edit to add: you should probably catch the potential exception from parseInt but at your level they probably have not taught you about this yet :D
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
Only one problem though: The code should've been displayed in a JOptionPane so I got that wrong... I used printlines.

The question said to start at one so starting at 0 would be wrong.

And: I suck at catching errors using the Try catch method. any tips?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.