Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 17, 2012, 05:29 AM   #1
cs.iris
macrumors newbie
 
Join Date: Jan 2012
converting Java to Objective-C

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. 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:
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:
Code:
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'."

Last edited by robbieduncan; Jan 17, 2012 at 05:50 AM. Reason: Use code tags not italic tags
cs.iris is offline   0 Reply With Quote
Old Jan 17, 2012, 05:48 AM   #2
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
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/m.../NSArray_Class and http://developer.apple.com/library/m...bleArray_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.
jiminaus is offline   2 Reply With Quote
Old Jan 17, 2012, 09:12 AM   #3
DESNOS
Banned
 
Join Date: Aug 2011
Quote:
Originally Posted by cs.iris View Post
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. 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:
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:
Code:
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...
DESNOS is offline   2 Reply With Quote
Old Jan 17, 2012, 11:31 AM   #4
cs.iris
Thread Starter
macrumors newbie
 
Join Date: Jan 2012
Quote:
Originally Posted by jiminaus View Post
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/m.../NSArray_Class and http://developer.apple.com/library/m...bleArray_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
cs.iris is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Some apps prompt to install Java 6 even when Java 7 is installed MacMan988 OS X Mavericks (10.9) 1 Apr 18, 2014 08:56 AM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 04:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 10:13 AM
Objective-C / Java Question Mvkoe Mac Programming 3 Dec 8, 2012 09:27 AM
java to Objective-C spilakalb Alternatives to iOS and iOS Devices 0 Oct 1, 2012 12:17 AM

Forum Jump

All times are GMT -5. The time now is 10:51 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC