| | |
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:
Solved Threads: 0
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
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
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 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 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
Java Syntax (Toggle Plain Text)
/* 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!
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
Use a suitable tool for creating software. Use free IDE NetBeans.http://www.netbeans.org/downloads/
•
•
•
•
Use a suitable tool for creating software. Use free IDE NetBeans.http://www.netbeans.org/downloads/
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
java Syntax (Toggle Plain Text)
//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."
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
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 !!!
•
•
•
•
Use a suitable tool for creating software. Use free IDE NetBeans.http://www.netbeans.org/downloads/
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.
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 !!!
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
public String toString() { }
It is automatically called whenever you do this:
System.out.println(num1); orSystem.out.println("num 3: "+num3); Check out my New Bike at my Public Profile at the "About Me" tab
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
> 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.
> 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.
•
•
•
•
...it can't be denied that knowing the basics of the language you develop in of prime importance.
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
![]() |
Similar Threads
- reference for starting to code in java (Java)
- RUNDLL error when starting Windows XP; "missing bridge.dll" (Viruses, Spyware and other Nasties)
- Helping yourself: What to do before starting a new thread or posting a HiJackThis log (Viruses, Spyware and other Nasties)
- The Move.....Visual Basic 6, Visual Basic .NET ? (VB.NET)
- Erroe messgae when starting windows ME (Windows 95 / 98 / Me)
- Error code on desktop (Windows 95 / 98 / Me)
Other Threads in the Java Forum
- Previous Thread: I'm new to this please help!
- Next Thread: Overwriting the text in the file
| Thread Tools | Search this Thread |
android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database db defaultmethod development dice dragging draw ebook eclipse error event exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx oracle parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows






