Need help with starting this code

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 14
Reputation: Eggplant is an unknown quantity at this point 
Solved Threads: 0
Eggplant Eggplant is offline Offline
Newbie Poster

Need help with starting this code

 
0
  #1
Dec 25th, 2008
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
  1. /*
  2.   Purpose: The purpose of this class is to test the Complex class. */
  3.  
  4. public class ComputeComplex {
  5.  
  6. public static void main (String[] args) {
  7.  
  8. Complex num1 = new Complex( 3, 2 );
  9. Complex num2 = new Complex( 4, 9 );
  10. Complex num3 = new Complex();
  11. Complex num4 = new Complex( -5, 7);
  12. Complex num5;
  13.  
  14. num5 = num1.subtract( num2 );
  15. System.out.println( num5 );
  16.  
  17. System.out.println ( num2.subtract( new Complex(4, -2) ));
  18. System.out.println ( num1.subtract( num1 ) );
  19.  
  20. System.out.println ( num2.add( num4 ) );
  21. System.out.println ( "num3 = " + num3 );
  22.  
  23. System.out.println ( num1.subtract( num2 ).add(num4) );
  24.  
  25. System.exit( 0 );
  26. }//main
  27. }//ComputeComplex

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

Thanks and merry Christmas!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Need help with starting this code

 
0
  #2
Dec 25th, 2008
Use a suitable tool for creating software. Use free IDE NetBeans.http://www.netbeans.org/downloads/
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Need help with starting this code

 
0
  #3
Dec 25th, 2008
Originally Posted by quuba View Post
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

  1. //default constructor, no argument
  2. public Complex(){
  3. }
  4.  
  5. // a constructor that takes arguments
  6. public Complex(double one, double two){
  7.  
  8. }

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."
Last edited by jasimp; Dec 25th, 2008 at 9:43 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Need help with starting this code

 
0
  #4
Dec 25th, 2008
Reading this would give you a detailed idea of the things you want. All the topics of interest are listed there.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Need help with starting this code

 
0
  #5
Dec 26th, 2008
Originally Posted by quuba View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Need help with starting this code

 
0
  #6
Dec 26th, 2008
Originally Posted by stultuske View Post
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,683
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Need help with starting this code

 
0
  #7
Dec 26th, 2008
  1. 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:
  1. public String toString() {
  2.  
  3. }

It is automatically called whenever you do this:
System.out.println(num1); or
System.out.println("num 3: "+num3);
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Need help with starting this code

 
0
  #8
Dec 26th, 2008
Originally Posted by verruckt24 View Post
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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Need help with starting this code

 
0
  #9
Dec 26th, 2008
> 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 accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Need help with starting this code

 
0
  #10
Dec 26th, 2008
Originally Posted by ~s.o.s~ View Post
...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.

Originally Posted by ~s.o.s~ View Post
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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC