Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8377129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:35:14+00:00 2026-06-09T15:35:14+00:00

Exercise: Complex numbers are numbers that contain two components: a real part and an

  • 0

Exercise:

“Complex numbers are numbers that contain two components: a real part and an
imaginary part. If a is the real component and b is the imaginary component, this
notation is used to represent the number:
a + b i
Write an Objective-C program that defines a new class called Complex. Following
the paradigm established for the Fraction class, define the following methods for
your new class:

-(void) setReal: (double) a;
-(void) setImaginary: (double) b;
-(void) print; // display as a + bi
-(double) real;
-(double) imaginary;

Write a test program to test your new class and methods.”

Here is my solution that doesn’t work:

    #import <Foundation/Foundation.h>

@iterface Complex:NSObject
{
    double a, b;
}

-(void)setReal: (double) a;
-(void)setImaginary: (double) b;
-(void) print;
-(double) real;
-(double) imaginary;

@end

@implementation Complex
-(void)setReal
{
    scanf(@"Set real value %f",&a);
}
-(void)setImaginary
{
    scanf(@"Set imaginary value %f", &b);
}
-(void) print
{
    Nslog(@"Your number is %f",a+bi);
}
-(double)real
{
    Nslog(@"Real number is %f",a);
}
-(double)imaginary
{
    NSlog(@"Imaginary number is %f",b)
}

@end



int main (int argc, char *argv[])
{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    Complex*num=[[complex alloc] init];
    [num setReal:3];
    [num setImaginary:4];
    Nslog(@"The complex number is %i",[num print]);
    [num release];
    [pool drain];
    return 0;
}

Please, what is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:35:16+00:00Added an answer on June 9, 2026 at 3:35 pm

    There are a few obvious flaws that I can see. First, (and this may be a copy/paste error), you misspelled interface as iterface.

    Second, your print method does not write to the NSLog correctly. You’re trying to force an expression a+bi as the result to the format specifier %f. Instead, you’ll want to have two arguments, with both a and b being passed separately to the NSLog call. Thus you’d have:

        NSLog(@"Your number is %f + %fi", a, b);
    

    Lastly, your methods real and imaginary are supposed to be ‘getters’ for your instance variables, not functions that print to NSLog. Thus, you’ll instead just want the function bodies to be return a; and return b; respectively. For the former (in full):

        -(double)real
        {
            return a;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this small exercise that I am trying to finish, but I get
The exercise in this tutorial says: Generate a 10 x 3 array of random
The exercise says: Create a Text class that contains a string object to hold
This is an academic exercise, I'm new to Reactive Extensions and trying to get
As part of an exercise, am implementing an ArrayList which will support Enumerations. Following
I'm looking few exercise from university about C++ and I found out this exercise:
Got this exercise on a python exam. Trying to return a deep o copy
As an exercise I'm trying to create a function that returns a generic list
As part learning exercise, part hobby project, I am implementing my own interpretation of
This is my Exercise class @class Question; @interface Exercise : NSManagedObject @property (nonatomic, retain)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.