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!

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."

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

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.

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.

``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);`

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.

> 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.

...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.

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;
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.

> 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.

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

...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. :-)

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)

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.

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.

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.

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;

[B]public Animal(int h){
height = h;
}[/B]

}``````

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[[B]10[/B]];``

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.

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!

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 would not 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.

Your first code box, if you put
...new Animal(2); and
...new Animal(2, 4);

the second one would automatically acess the one that also contains the int w, as there are 2 values, and skip over the first method right? while as the first one would skip the second method?

Or does the first one just have a w value of 0 as it is default?

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

What is "this.height" and weight?
And how would you go about to adress using these values in addition or subtraction appropriately? I know I can just shove it in there, and do the calculations, but I also know that there must probably be a specific protocol in which you use to do it.

Thanks, you have been very helpful and friendly

Your first code box, if you put
...new Animal(2); and
...new Animal(2, 4);

the second one would automatically acess the one that also contains the int w, as there are 2 values, and skip over the first method right? while as the first one would skip the second method?

Or does the first one just have a w value of 0 as it is default?

Considering the class,

``````public class Animal{
int height = 0;
int weight = 0;

// ... our methods down here
}``````

Think of it like this. Every time you construct a new Animal, that Animal initially has a height = 0 and a weight = 0. Then, if you say Animal myAnimal = new Animal(2), what Java does is it looks for the method that matches. So it will look for a method called "Animal" that has one integer as its parameter. It will find this method:

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

Then, it will call that method. It will set height to 2, but it will not change weight, since there is no code there to change weight. So now we will have height = 2 and weight = 0.

Oh, and I have read, but do not understand what, how, or the purpose of a no argument constructor.

What exactly is it? a method containing no body?

yeah, I'll begin writing and practicing these using what I know, but I still need to further understand private, no-argument.

So doing calculations would be something like:

``````public getDifference (int height){
heightDifference = myAnimal2.height - myAnimal1.height;
return heightDifference;
}``````

would that be correct?

Haha Winterbreak = forgetting a lot of what you did during school >_<

EDIT: Ahh, yeah, that's how I figured it would choose the methods, sorry, post overlapping hehe.

Also, this.whatever is usually not necessary. It is just a way to clarify what you are referring to. For example, consider the confusion that the following code might produce:

``````public class Animal{
[U][I]int height = 0;[/I][/U]

public int doSomethingPointless(int height){
int temp = [B]height [/B]- 5;
return temp;
}

}``````

Which height does 'height' (where I bolded) refer to? The parameter (int height), or the instance variable? Obviously, this is confusing. So 'this.' refers to the instance variable (underlined & in italics)

Oh, and I have read, but do not understand what, how, or the purpose of a no argument constructor.

What exactly is it? a method containing no body?

No, it is a constructor that had no arguments/parameters. The method can contain whatever body/code you want it to. A no argument constructor is just, for example, if we had a constructor with the method header: public Animal()

yeah, I'll begin writing and practicing these using what I know, but I still need to further understand private, no-argument.

So doing calculations would be something like:

``````public getDifference (int height){
heightDifference = myAnimal2.height - myAnimal1.height;
return heightDifference;
}``````

would that be correct?

You have the right idea. But keep in mind that if you actually wrote that exact code, Java would not know what 'myAnimal2' and 'myAnimal1' are. See my example of how to do it below.

Rewriting your code above so that it would work:

``````public getDifference(Animal a, Animal b){
int difference = a.height - b.height;
return difference;
}``````

And rewriting it again, so that it is even better

``````public getDifference(Animal other){
int difference = this.height - other.height;
return difference;
}``````

^ Note the 'this' again. 'this' refers to the Animal Object that called the method. If that doesn't make sense, consider the way that you actually would call the getDifference method. You would have some code like this:

Animal myAnimal = new Animal(2);
Animal otherAnimal = new Animal(1);

int theDifference = myAnimal.getDifference(otherAnimal);
System.out.println(theDifference); // Prints "1"

Let me know if that makes sense or not.

commented: Nice job helping him out +9

What is the point of a no argument?

``````public getDifference(Animal other){
int difference = this.height - other.height;
return difference;.

}``````

Where is "Animal other" from?
and for the "this" how would it know which of the 2 it refers too, as it was not stated.

I don't think I should use the this. anyways, since our teacher doesn't like us going ahead and using unlearned code

Yeah I understand the topic mainly now, just need to work around the small details that confuse Java.

What is the point of a no argument?

``````public getDifference(Animal other){
int difference = this.height - other.height;
return difference;.

}``````

Where is "Animal other" from?

How would you compare the heights of two Animals if you didn't have two Animal Objects?

and for the "this" how would it know which of the 2 it refers too, as it was not stated.
'this' always refers to the calling Object (if the method isn't a constructor. If the method is a constructor, then this refers to the Object you are in the process of creating/modifying). So if you had two Animals called myAnimal and other, and you said other.getDifference(myAnimal), then inside the getDifference method, 'this.height' would refer to other's height.

I don't think I should use the this. anyways, since our teacher doesn't like us going ahead and using unlearned code

That's fine. I mentioned it because I'm positive that you will see it over and over again in Java. I doubt your teacher would have a problem with you using it, but it's up to you.

Yeah I understand the topic mainly now, just need to work around the small details that confuse Java.

fghgfdfgh

Ahh, I understand the "this" statement now, but when you used (Animal other) and other." "
is other a statement, is it just a placeholder you are using to refer to the other object, NOT "myAnimal" Object, or does it refer to otherAnimal somehow?

For more clarification, is "other" in other.getDifference(myAnimal) etc targeting your "otherAnimal" [which wouldn't it be otherAnimal.getDifference(myAnimal) then? ]

or is it something else?
in reference to

``````Animal myAnimal = new Animal(2);
Animal otherAnimal = new Animal(1);

public getDifference(Animal other){
int difference = this.height - other.height;
return difference;
}``````

EDIT:

And as for no-argument:

would

``````Animal myAnimal = new Animal();

public sendNoArgument() {
System.out.println ( "No values given, or whatever");
}``````

be the purpose of a No argument?

I'm not sure what you're asking, so here's two examples.

Animal myAnimal = new Animal(2);
Animal otherAnimal = new Animal(2);

If we call myAnimal.getDifference(otherAnimal), if you recall, the method parameter was simply called 'other'. So every time it says 'other' in the method's code, it will refer to otherAnimal. We could also say

Animal myAnimal = new Animal(2);
Animal other = new Animal(2);

myAnimal.getDifference(other), and it would not make a difference. The only important thing to remember is that whatever is between the () is going to be referred to in the method by 'other'.

It's probably above what you're studying at this point, but once you fully understand that, you might want to look into how Java passes-by-value.

Blah Blah blah, no I'm stupid, I forgot we were working with Constructors, I was thinking about a simple method, where the parameters int he () are what it is recieving from other methods, my fault sorry!

How was my no argument constructor example?

Thanks for all this help man, appreciate it!
My teacher prefers girls = [

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.