PDA

View Full Version : converting Java to Objective-C




cs.iris
Jan 17, 2012, 04:29 AM
Hello :)
I am a student that is currently converting a java program into Objective-C. It was kinda hard, but I need to do it to pass my subject.;) I have a little problem regarding the ".add" of Java. I read a lot of posts and books just to understand it. But I still have a hard time understanding.:confused: I would like to check if what I did was correct and if someone can lend me a hand in converting. Help please? thanks.:)

Here is the java code:
ArrayList cid = new ArrayList();
ArrayList sid = new ArrayList();
ArrayList<String> sol = new ArrayList<String>();

public mainProgram() {
initComponents(); //.add (cartesian product principle)
sid.add("sink");
cid.add("1 /x");


here is my solution in objective-c:
NSArray *cid = [[NSArray alloc]init];
NSArray *sid = [[NSArray alloc]init];
NSMutableArray *sol = [[NSMutableArray array];
(my first trial in .add) cid addObject: @"1/x"; -----but it shows an error: "Expected ';' before 'addObject'"
(the other trial is) [sid addObject: @"sink"]; -------but it also shows an error:
"Local declaration of sid hides instance variable"
"NSArray may not respond to '-addObject'."



jiminaus
Jan 17, 2012, 04:48 AM
We will not do homework/assignments for you. So don't expect anyone to give you an explicit, straight-up answer here. However do expect people to help you understand enough for you to be able to do it yourself.


Let's start of with two thing.


Firstly, what the difference between NSArray and NSMutableArray? If you don't know, check out the class references at http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class and http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class.

If you're going to try to add an object to an array, which do you think you'll need? What type of objects are sid and cid pointing to? What should they be? How do get them to be what they should be?


Second how do you send a message (call a method) of an object in Objective-C? What's the syntax? How did you violate the syntax in your first attempt?


There's more wrong with your code than this, but let's start with this.

DESNOS
Jan 17, 2012, 08:12 AM
Hello :)
I am a student that is currently converting a java program into Objective-C. It was kinda hard, but I need to do it to pass my subject.;) I have a little problem regarding the ".add" of Java. I read a lot of posts and books just to understand it. But I still have a hard time understanding.:confused: I would like to check if what I did was correct and if someone can lend me a hand in converting. Help please? thanks.:)

Here is the java code:
ArrayList cid = new ArrayList();
ArrayList sid = new ArrayList();
ArrayList<String> sol = new ArrayList<String>();

public mainProgram() {
initComponents(); //.add (cartesian product principle)
sid.add("sink");
cid.add("1 /x");


here is my solution in objective-c:
NSArray *cid = [[NSArray alloc]init];
NSArray *sid = [[NSArray alloc]init];
NSMutableArray *sol = [[NSMutableArray array];
(my first trial in .add) cid addObject: @"1/x"; -----but it shows an error: "Expected ';' before 'addObject'"
(the other trial is) [sid addObject: @"sink"]; -------but it also shows an error:
"Local declaration of sid hides instance variable"
"NSArray may not respond to '-addObject'."


First of all, an NSMutableArray is an array which CAN be modified. Most classes are unmodifiable by default, but have a "Mutable" version which can be modified. Also, you have some serious syntax problems. Remember that any calls to a class must be wrapped in square brackets (Objective-C 2.0 changes this a bit, but we'll pretend that never happened for now). I really don't think you worked too hard on this before asking us...

cs.iris
Jan 17, 2012, 10:31 AM
We will not do homework/assignments for you. So don't expect anyone to give you an explicit, straight-up answer here. However do expect people to help you understand enough for you to be able to do it yourself.


Let's start of with two thing.


Firstly, what the difference between NSArray and NSMutableArray? If you don't know, check out the class references at http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class and http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class.

If you're going to try to add an object to an array, which do you think you'll need? What type of objects are sid and cid pointing to? What should they be? How do get them to be what they should be?


Second how do you send a message (call a method) of an object in Objective-C? What's the syntax? How did you violate the syntax in your first attempt?


There's more wrong with your code than this, but let's start with this.

I'm sorry if you feel that way, though I don't intend for you guys to do it for me. Thanks for the reply, it was helpful in some ways to understand what I should do. And I learned that there were really a lot more errors in the syntax. I've fixed it already. Thanks :)