Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Apok's general Java thread
Closed Thread
Old 03-18-2008, 12:50 PM   #11 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: Apok's general Java thread

I restructured your program, but I did not change your calculation logic. It was easier to do this than write an explanation. Read the code, and if you have questions, ask.

Code:
import java.io.*;
public class Account
{
    private static final String QUIT = "quit";

    private static double getInput(BufferedReader stdin, String msg)
    {
        while (true)
        {
            try
            {
                System.out.println(msg);
                String input = stdin.readLine();
                if (input == QUIT)
                    System.exit(0);
                return Double.parseDouble(input);
            }
            catch (Exception e)
            {
                System.err.println("Invalid number");
            }
        }
    }
	
    public static void main(String[] args)
    {
        BufferedReader stdin = null;
        try
        {
            stdin = new BufferedReader(new InputStreamReader(System.in));
            double accountBalance = getInput(stdin, "Initial deposit amount:");
            double interestRate = getInput(stdin, "Interest PPA, eg 6.0 for 6.0%:");
            double monthlyDeposit = getInput(stdin, "Monthly deposit amount:");
			
            System.out.println();
            System.out.println("Savings growth over 6 months:");
			
            double mintr = interestRate / 12 / 100;
            for (int month=1; month<=6; month++)
            {
                accountBalance *= monthlyDeposit;
                double mint = accountBalance * mintr;
                accountBalance += mint;
                System.out.println();
                System.out.println("Month " + month);
                System.out.printf("Balance =$%.2f\n",accountBalance);
                System.out.printf("Interest = $%.2f\n",mint);
            }
        }
        catch (Exception e)
        {
            System.err.println(e.getMessage());
        }
        finally
        {
            try
            {
                if (stdin != null)
                    stdin.close();
            }
            catch(Exception e) {}
        }
    }
}


Last edited by jaeusm; 03-18-2008 at 12:55 PM.
jaeusm is offline  
Old 03-18-2008, 08:19 PM   #12 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,737

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

Thanks, but I want this code to be mine (as in, I don't want you to write code for me; just help me understand how to do it)

But I will have a look through it to figure out how it works
__________________

1 + 1 = 3 if you define 3 as a result of 1 + 1

Last edited by Apokalipse; 03-18-2008 at 08:26 PM.
Apokalipse is offline  
Old 03-19-2008, 12:24 AM   #13 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,737

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

I got my program working!
Code:
import java.io.*;
public class Account
{

    public static void main(String[] args)
    {
    BufferedReader stdin = new BufferedReader
            (new InputStreamReader (System.in));

    double acctb = 0.0, intrt = 0.0, mda = 0.0, mint = 0.0;
// account balance, yearly interest rate, monthly deposit amount, monthly interest rate

    boolean valid = true;            // used in case of an IOException

    do {                    // input initial account balance
    try {
        valid = true;
    System.out.println("Initial deposit:");
    acctb = Double.parseDouble(stdin.readLine());
    }
    catch(Exception e) {
        System.out.println("Not a valid number");
        valid = false;
    }
    }
    while (!valid);

    do {                    // input interest rate
    try {
        valid = true;
    System.out.println("Annual interest rate %");
    intrt = Double.parseDouble(stdin.readLine());
    }
    catch(Exception e) {
        System.out.println("Not a valid number");
        valid = false;
    }
    }
    while (!valid);

    do {                    // input monthly deposit amount
    try {
        valid = true;
    System.out.println("monthly deposit:");
    mda = Double.parseDouble(stdin.readLine());
    }
    catch(Exception e) {
        System.out.println("Not a valid number");
        valid = false;
    }
    }
    while (!valid);

    System.out.println();
    System.out.println("Savings growth over 6 months:");

    double mintr = (intrt / 12) / 100;    // interest rate for *one* month

    for (int month=1; month<=6; month++)
    {
        acctb = acctb + mda; // monthly deposit
        mint = acctb * mintr; // monthly interest
        acctb = acctb + mint; // new account balance

    System.out.println();
    System.out.printf("Month %s\n",month);
    System.out.printf("Balance =$%.2f\n",acctb);
    System.out.printf("Interest = $%.2f\n",mint);
    }
    } // end of main method
}
not using any methods (except for main)
altered input style

I understood a few things better (in general) from reading your program, anyway
__________________

1 + 1 = 3 if you define 3 as a result of 1 + 1

Last edited by Apokalipse; 03-19-2008 at 03:18 AM.
Apokalipse is offline  
Old 03-19-2008, 09:40 AM   #14 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: Apok's general Java thread

Quote:
Thanks, but I want this code to be mine (as in, I don't want you to write code for me; just help me understand how to do it)
Right, my code is there to illustrate the points I made earlier, like factoring chunks of repeating code into methods. So instead of writing all those do-while loops to get input from the user, you should move it into its own method. In my example, it's the getInput method.
jaeusm is offline  
Old 03-28-2008, 05:59 AM   #15 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,737

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

After much tweaking, I have a program I'm happy with, and a lot better understanding of making programs.
I also modified it to reject input of negative numbers.

Now, I'm onto my second assignment which isn't due till about mid May, but I want to see if I can get a working program by next week.

One part of the assignment is asking me to make a program that determines if a number (integer) is prime or not.
Now, I am confident I can write the code for it. I'm just not sure what logic to use to make the determination.

*edit*
nevermind, I've come up with a way I'm pretty sure will work:
- input primecandidate (integer)
- p = primecandidate % n (initially 2)
- if (p = 0), not prime
- if (p > 0 && n != (primecandidate - 1)), loop, increase n by 1
- if (P > 0 && n = (primecandidate - 1)), number is prime

__________________

1 + 1 = 3 if you define 3 as a result of 1 + 1

Last edited by Apokalipse; 03-28-2008 at 07:27 AM.
Apokalipse is offline  
Old 03-29-2008, 03:45 AM   #16 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,737

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

I finished the second assignment, and with well over a month to spare!

Now, I have even more time to get ahead of the rest of the students.

One thing I want to have another go at is using methods.

*edit*
what the?
http://apokalipse.googlepages.com/whatthe.PNG
__________________

1 + 1 = 3 if you define 3 as a result of 1 + 1

Last edited by Apokalipse; 03-29-2008 at 03:50 AM.
Apokalipse is offline  
Old 03-29-2008, 07:56 PM   #17 (permalink)
Trotter's Avatar
 

Join Date: Jan 2005

Location: The South

Posts: 19,933

Trotter is a name known to allTrotter is a name known to allTrotter is a name known to allTrotter is a name known to allTrotter is a name known to allTrotter is a name known to all

Default Re: Apok's general Java thread

Only you, only you.

Here's a site I found when I was having some funky compiler errors... turns out it has a lot of good Java stuff...

compile time error messages : Java Glossary

Main Java page:
Canadian Mind Products Java & Internet Glossary
__________________
R.I.P. Danny L. Trotter , 14 Nov 1945 - 4 Sept 2009




DFI LanParty-UT SLI-D - Windows 7 64-bit - AMD Athlon X2 4200+ w/CNPS9500
4GB RAM(4x1GB) - Razer Lachesis - EVGA GTX 260 Core 216 896MB


>>>> I am looking for donated DDR2 (link) <<<<

< < < < < If I've been helpful, rep me. . . .
Trotter is offline  
Old 03-30-2008, 07:02 AM   #18 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,737

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

Quote:
Originally Posted by Trotter View Post
Only you, only you.

Here's a site I found when I was having some funky compiler errors... turns out it has a lot of good Java stuff...

compile time error messages : Java Glossary

Main Java page:
Canadian Mind Products Java & Internet Glossary
Thanks, I'll keep those pages in mind if I run across errors that I haven't seen.

I've just been playing around with multiple methods.
I know how to make methods, but haven't got linking them to each other yet.
Specifically, getting variables in one method to be used in another method.

like in this:
Code:
import java.io.*;
public class George
{
    public static void somemethod()
    {
    BufferedReader stdin = new BufferedReader
            (new InputStreamReader (System.in));
    String s = "";
    System.out.printf("\nAAAAAAAAAAAAAAAAAA\n");
    s = stdin.readLine();    
    }


    public static void someothermethod()
    {
    System.out.printf("Input was: %s\n",s);
    }


    public static void main(String[] args)
    {
    somemethod();
    someothermethod();
    }
}
jaeusm said that I need to pass them as parameters, which I googled and came up with this:
Passing Parameters to a Remote Method (Java Developers Almanac Example)

but it doesn't exactly explain things clearly.
__________________

1 + 1 = 3 if you define 3 as a result of 1 + 1
Apokalipse is offline  
Old 04-03-2008, 02:05 AM   #19 (permalink)
countrydude's Avatar
 
Junior Techie

Join Date: Jan 2008

Location: Alberta, Canada

Posts: 65

countrydude is on a distinguished road

Send a message via MSN to countrydude
Default Re: Apok's general Java thread

Quote:
Originally Posted by Apokalipse View Post
jaeusm said that I need to pass them as parameters, which I googled and came up with this:

but it doesn't exactly explain things clearly.
What I think jaeusm was meaning is that in your main method when you call your methods above you want to pass that method some parameters so that the method can use whatever you pass it. Ill give you an example of some code here.

Code:
 
public class George
{
    public static void somemethod(int x)
    { 
          int y = x;
    }

    public static void main(String[] args)
    {
    somemethod(54);
    }
}
So what I did above is this: In the method called "somemethod" I added what parameters it can take in. That is in red. So now it can take in integers and store it into variable x. Then I took variable x and stored it into variable y.

Then in the main method when I called the "somemethod" I passed it the parameter "54". The parameters go inside the parenthesis. It can only pass parameters of type "int" because that is what the method "somemethod" can only take in.

Well I hope this helps and that this is what your looking for.

EDIT: If you want to use one variable throughout your whole code then you can just declare an instance field at the beginning before any methods like this: private int z; Now that variable can be accessed in any method in that file. Though that variable can be overwritten anywhere in the code.

Please correct me anyone if I am mistaken about any of this! : )
__________________
My Personal Site CraigSite.ca
My current specs
-Power Color HD 4850 512MB
-MSI K8N platinum SLI Mobo
-2x1GB Generic DDR RAM
-250GB, 1TB SATA HDD Seagate
-Athlon 64 X2 4200+ Socket 939 oc'ed to 2.8Ghz

Last edited by countrydude; 04-03-2008 at 02:10 AM.
countrydude is offline  
Old 04-03-2008, 03:55 AM   #20 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,737

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

Quote:
Originally Posted by countrydude View Post
What I think jaeusm was meaning is that in your main method when you call your methods above you want to pass that method some parameters so that the method can use whatever you pass it. Ill give you an example of some code here.

Code:
 
public class George
{
    public static void somemethod(int x)
    { 
          int y = x;
    }

    public static void main(String[] args)
    {
    somemethod(54);
    }
}
So what I did above is this: In the method called "somemethod" I added what parameters it can take in. That is in red. So now it can take in integers and store it into variable x. Then I took variable x and stored it into variable y.

Then in the main method when I called the "somemethod" I passed it the parameter "54". The parameters go inside the parenthesis. It can only pass parameters of type "int" because that is what the method "somemethod" can only take in.

Well I hope this helps and that this is what your looking for.

EDIT: If you want to use one variable throughout your whole code then you can just declare an instance field at the beginning before any methods like this: private int z; Now that variable can be accessed in any method in that file. Though that variable can be overwritten anywhere in the code.

Please correct me anyone if I am mistaken about any of this! : )
Thanks, that actually does explain it.

but I have another question: why is the parameter '54' when you call the method inside main?
__________________

1 + 1 = 3 if you define 3 as a result of 1 + 1
Apokalipse is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Potentially the longest thread in history... Dave Off Topic Discussion 15101 Yesterday 10:36 PM
Yak thread? nOcLuE98 Site Feedback & Suggestions 17 02-18-2008 01:45 AM
(Java) Problem creating and passing an Integer object? Toshiro Programming Discussions 1 10-17-2007 08:00 PM
Dangerous Java Flaw Threatens Virtually Everything Osiris Virus - Spyware Protection / Detection 1 07-13-2007 04:44 PM
Java Game Yek Programming Discussions 5 06-16-2007 04:29 AM