954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with starting this code

Hi, I you probably have much annoyance with homework help, but I'm a first year student in high school doing a AP comp sci class.

I'm currently working on my eighth program, the past 7 were all solved by me, and I helped others with it, so I'm not trying to leech a quick answer, just grab some help.

Anywho,

My assignment is
Create a class called Complex for performing arithmetic operations with complex numbers. Complex numbers have the form

realPart + imaginaryPart * i, where i = square root of -1

Write a program to test your class. Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialize when it is declared. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform the following operations:

a) Add two Complex numbers: The real parts are added together and the imaginary parts are added together.

b) Subtract two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary art of the left operand.

c) Print Complex numbers in the form a + bi, where a is the real part and b is the imaginary part.

I really don't understand private methods, constructors, and calling different classes.

I have had experience with using methods, control loops, and all the basics up to arrays, but this topic has gotten me puzzled. I've looked through my huge thousand page java book with no help.

Normally I'd ask a friend or the teacher to work with, but in this case, We have a 2 week break, and it's due when we get back, so that's not a choice.

We were also provided a driver file

/*
   Purpose: The purpose of this class is to test the Complex class. */

public class ComputeComplex {

	public static void main (String[] args) {
	
		Complex num1 = new Complex( 3, 2 );
		Complex num2 = new Complex( 4, 9 );
		Complex num3 = new Complex();
		Complex num4 = new Complex( -5, 7);
		Complex num5;
		
		num5 = num1.subtract( num2 );
		System.out.println( num5 );
		
		System.out.println ( num2.subtract( new Complex(4, -2) ));
		System.out.println ( num1.subtract( num1 ) );
		
		System.out.println ( num2.add( num4 ) );
		System.out.println ( "num3 = " + num3 );
		
		System.out.println ( num1.subtract( num2 ).add(num4) );
		
		System.exit( 0 );
	}//main
}//ComputeComplex


Any help or discussion appreciated. We can talk about this over AIM or email if you can help me.

Thanks and merry Christmas!

Eggplant
Newbie Poster
14 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Use a suitable tool for creating software. Use free IDE NetBeans. http://www.netbeans.org/downloads/

quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 123
Solved Threads: 106
 
Use a suitable tool for creating software. Use free IDE NetBeans. http://www.netbeans.org/downloads/


Uhh... how is this helpful?

I'm sure your book must show you examples of constructors, private methods and their uses. Anyways a private method is a method that can only be used in the containing class. They are generally called support methods because they are used only within the class.

If you do not provide a constructor, Java automatically uses a default constructor, which is a no argument constructor

//default constructor, no argument
public Complex(){
}

// a constructor that takes arguments
public Complex(double one, double two){

}


To go from there I'll need to know exactly what you are having trouble on, besides a vague, "private methods, constructors, and calling different classes."

jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
 

Reading this would give you a detailed idea of the things you want. All the topics of interest are listed there.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 
Use a suitable tool for creating software. Use free IDE NetBeans. http://www.netbeans.org/downloads/

ignore this post if you intend to actually learn something. start off with Notepad. it's not the most "flashy" tool, but it's as good as any idea (for a beginner that is, and it's even better)

look up the java syntax, re-read your notes on "how to create objects" and give it a go.
if it doesn't work, come back here with your own code and show us that, we'll help you out from there.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

ignore this post if you intend to actually learn something. start off with Notepad. it's not the most "flashy" tool, but it's as good as any idea (for a beginner that is, and it's even better)

look up the java syntax, re-read your notes on "how to create objects" and give it a go. if it doesn't work, come back here with your own code and show us that, we'll help you out from there.

ditto, not only is the solution mentioned here by quuba irrelevant in this case it is also wrong in the case that it would apply. Using an IDE just because you don't or don't want to remember langauge syntax and API is totally useless, it should rather be used to assist in your development when much of the basic work is very well known to you and you need to waste your time repeating that.
As a personal suggestion IDEs should never be used unless until you have spent enough time typing the actual langauge, this way you will remember & learn more.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 
num5 = num1.subtract( num2 );


Also when you create your class and declare the private variables, you will need to implement the methods described.
Example, with the above you will have a public method he take an argument a Complex object.
Inside the method you will use the get methods of object:num2 to get the real and imaginery part, do the subtraction with the real and imaginery part of num1 and with the results create a new Complex object and return it.

I once started to create some sort of library for Complex numbers (for fun) but I got bored. It was designed to handle even calculations like: cosh(Complex x) or arctan(Complex x)

Just have get, set methods for the variables and use your Math book for the calculations.

Also you will need to implement the toString() method:

public String toString() {

}


It is automatically called whenever you do this: System.out.println(num1);
or System.out.println("num 3: "+num3);

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
Using an IDE just because you don't or don't want to remember langauge syntax and API is totally useless,


While I do agree with your post in its entirety I think using certain IDE's, such as Eclipse, can be very useful in learning the API. In Eclipse, whenever you access a method, it gives you a summary of what the method does, as given in the API. This is a great opportunity to really explore the API.

jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
 

> whenever you access a method, it gives you a summary of what the
> method does, as given in the API

...so do the online java docs. But in most of the cases we end up with a bunch of CTRL + SPACE programmers who find it too troublesome to even remember the method signatures of the most commonly used methods out there.

As someone who earns from programming, I find such tools to be really useful in boosting ones' productivity and getting the job at hand done faster, it can't be denied that knowing the basics of the language you develop in of prime importance. I am pretty sure none of us would want to hire someone who is completely lost without an IDE; after all, it's a Java developer we are looking for and not just a Eclipse/Netbeans/IntelliJ user.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
...it can't be denied that knowing the basics of the language you develop in of prime importance.


It is important, that is why I agree that IDE's should not be used when first learning a language. But my point was certain IDE's can, if you truly want to learn, help you learn common method signatures, what commonly used methods do. They will only help beginners if the user actually types everything out themselves, instead of using code completion, code generation et cetera.I am pretty sure none of us would want to hire someone who is completely lost without an IDE; after all, it's a Java developer we are looking for and not just a Eclipse/Netbeans/IntelliJ user.
Which is what happens when beginners are incorrectly taught to always use and IDE (which is why they should not use them), instead of using it when they are ready.

jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
 

I'm quite lost on the whole idea of this program.

So he is asking for a file with defines Complex, as a certain type of variable?

Complex num1 = new Complex( 3, 2 );


So, normally what would be int num1 = ... or String num1 = .... He's asking for the creation a type called "Complex" ?

And I run the given tester which does test calculations, from a file which I write that defines the "Complex".

and in the line above, the ( x , y ) is an array containing 2 numbers? the first one ( 0 ) being 3 and second, (1 ) being 2?

And what do they mean by like

// the Bicycle class has one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }


To me this piece of code just looks like a method that sets values for gear, cadence, and speed. (this is from a pervious link with tutorial on obj based programming.

Thanks, trying to get started, and a bit confused.

Eggplant
Newbie Poster
14 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

> whenever you access a method, it gives you a summary of what the > method does, as given in the API

...so do the online java docs. But in most of the cases we end up with a bunch of CTRL + SPACE programmers who find it too troublesome to even remember the method signatures of the most commonly used methods out there.

As someone who earns from programming, I find such tools to be really useful in boosting ones' productivity and getting the job at hand done faster, it can't be denied that knowing the basics of the language you develop in of prime importance. I am pretty sure none of us would want to hire someone who is completely lost without an IDE; after all, it's a Java developer we are looking for and not just a Eclipse/Netbeans/IntelliJ user.


I don't think that anyone in academics would agree with such a broad statement. Of course, it is important to be knowledgeable without the use of resources - otherwise, you're useless - but knowing how to properly use your resources is probably just as important.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

> I don't think that anyone in academics would agree with such a
> broad statement.

...something which I was expecting all along. ;-)

JFTR, how many good java programmers have you seen who are not good with the tools they work? And how many programmers have you seen who struggle with the basics of Java programming language but still are focused on learning the tools? As for me, none of the former and a lot of latter.

> but knowing how to properly use your resources is probably just
> as important.

From a practical standpoint, anyone can learn every feature in Eclipse related to Java development in a day; so yes, tools are important but not something which need focus, you just work in them and their usage becomes pretty much second nature. This is something which can't be said about understanding the language basics.

But it seems almost everyone in this thread agrees that basics come first, tools second, so now, back to helping out the OP. :-)

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Yes, we're definitely agreed that understanding the basics is the most important thing. Otherwise, one wouldn't understand the information the IDE spits out at all, which is why I only half agree with what you said about people who can "only use the IDE". Also, forgive the freepost, just making my previous statement clear - we're agreed on the first and half agreed on the second.

(feel free to erase and apologies if this is clouding the thread)

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

Off topic threads are full of win =]

Anyways, so I'm beginning to understand the constructor thing, but to clear this up, is a constructor just a term used for a method in which it does specific [ constructory type ] things?

Or is a constructor a..like an array, or a control, or a Java statement?

Readying through my example code given to us was pretty confusing, as certain class files called on others with slightly adjusted variable names, was a little lost.

Eggplant
Newbie Poster
14 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

A constructor is a name for a method that 'constructs' an Object, or creates an instance of its class type. The name of a constructor has to be the same of the name as the class it is in (although it can have any parameters). For example, if my class is as follows

public class Animal{

int height = 0;

   public Animal(int h){
      height = h;
   }

}


The method "Animal(int h)" is a constructor. If you say Animal myAnimal = new Animal(2); you are creating an Animal object, also referred to with various other terminology...


I just gave you the most basic description of what a constructor is. If you want to know more specific details then google Java Sun constructor or ask more specific questions here.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

So you can access Animal as a different type of variable?

IE, Animal is now, like an int, or a float, but as what the method Animal states it is?
where does int h come from? or is that called from something else assumed already stated??

What specifically would:
Animal myAnimal = new Animal(2);

do? create "myAnimal" array, with 2 different animals of different values of height?

Sorry if I see a little unknowledged, still reading through the guide links.

Eggplant
Newbie Poster
14 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 
IE, Animal is now, like an int, or a float, but as what the method Animal states it is?

You were correct in your connection between Animal and int/float/etc. Yes, they are similar in some ways. But note that there are also very important differences. Animal, and all other types (anything that is declared with the word class) are Objects whereas int/float/etc are not Objects and do not have a class declaration. There are more differences than these, but I'm straying from your question. The other statements you made in the same post as this one, i.e. about it being an array, were not correct.

public class Animal{
int height;

<strong>public Animal(int h){
height = h;
}</strong>


}


In this example, I created a class called 'Animal'. That means that when I say Animal myAnimal = new Animal(2), I am invoking the method that I bolded in my code. You'll notice that the method I'm invoking, Animal, has one parameter: "int h". So when I say new Animal(2), the Animal method is invoked, and 2 is passed in. This means that what will end up happening is height = 2. So after I say Animal myAnimal = new Animal(2), I have created an Animal Object with a height of 2. If I wanted to create an array of Animals, I would have to use the following code:

Animal[] arrayOfAnimals = new Animal[<strong>10</strong>];


There are a couple of other ways to make an array in Java, but that is probably the most common one. An array in Java is created by using the name of the type, in this case Animal, followed by the []. After that declaration, we have created an array capable of holding ten Animals. Note that I say capable of holding ten Animals - at this point, there are NOT ten Animals in it. If you want to put an Animal in it, for example, here is how you could put an Animal in the first index of the array.

arrayOfAnimals[0] = new Animal(5);

This uses my original constructor (the one in bold), creates an Animal with a height of 5, and puts it in the first index of the array. Arrays are indexed 0-whatever, so where I have 10 bolded above, that means the array will have indexes 0, 1, 2, ... 9

Hope that helps.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

Oh, so int h will be defined when you put Animal myAnimal = new Animal(2)
as it will use the Animal method, and store it in the parameters. What if, say the constructor for animal contained 2 integers in the parameters such as:

public Animal(int h, int w){
height = h;
weight=w;
}

would you now:

Animal myAnimal = new Animal(2, 4);

giving now the myAnimal to have height=2 and weight=4 ?

and what would be the appropriate way to access these values?
IE if you also had another animal

Animal theAnimal = new Animal(3);

and you wanted to have a height comparison of the given 2 animals?

beginning to understand the whole concept, thanks!

Eggplant
Newbie Poster
14 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Yes, it seems like you are starting to understand everything. If you had an Animal called myAnimal, myAnimal.height would let you access the height. So if you had the following class

public class Animal{

int height = 0;
int weight = 0;

public Animal(int h){
    height = h;
}

public Animal(int h, int w){
   height = h;
   weight = w;
}

}


And you did the following:
Animal myAnimal1 = new Animal(2);
Animal myAnimal2 = new Animal(2, 4);

myAnimal1 and myAnimal2 would have the same height. However, they wouldnot have the same weight, since myAnimal1 uses a constructor which does not change the Animal's weight, whereas myAnimal2 uses a constructor which does change the weight.

Here are some things you can do w/ the height and weight of the Animals I just created.

//If you run this code, it prints "We have equal heights"

if (myAnimal1.height == myAnimal2.height)
System.out.println("We have equal heights");


//Print myAnimal1's height

System.out.println("Animal's height is " + myAnimal1.height);


As a side note, the two constructors I am writing below do the same exact thing:

public Animal(int h, int w){
   height = h;
   weight = w;
}

public Animal(int height, int weight){
   this.height = height;
   this.weight = weight;
}


You should practice writing some simple classes where you make the instance variables private and use setters and getters to access and change the values. An example of an instance variable, in the class we've been talking about, is height or weight. A setter is a method that changes the values and a getter simply returns the value.

Sometimes it is hard to learn programming because you don't really know what exercises to do, and you either start too simple or start too complex. I'd suggest continuing w/ whatever tutorial you're working on. If that is the case, once you feel like you understand the basic concepts in the tutorial, I can suggest some exercises for you to work on.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You