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-13-2008, 05:30 AM   #1 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,739

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Apok's general Java thread

I've just started programming in Java, and I want to do more of it.
So I'm making a thread, so I can ask general questions about Java functions, and how I can make things more efficient. Basically, how to do more stuff.
[edit]I don't want anybody to write code for me. I just want to understand java concepts better.[/edit]

Today I finished a pretty simple (but repetitive) program to calculate account growth over a 6 month period:
Code:
import java.io*;
public class Account
{
    public static void main(String[] args) throws IOException
    {
    BufferedReader stdin = new BufferedReader
                (new InputStreamReader (System.in));
    String acctb, intrt, mda;
    double acctbD, intrtD, mir, intD, mdaD;

    System.out.println("Initial deposit amount:");
    acctb = stdline.readLine();
    acctbD = Double.parseDouble(acctb);

    System.out.println("Interest PPA, eg 6.0 for 6.0%:");
    intrt = stdin.readLine();
    intrtD = Double.parseDouble(intrt);

    System.out.println("Monthly deposit amount:");
    mda = stdin.readLine();
    mdaD = Double.parseDouble(intrt);

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

    mir = (intrtD / 12) / 100;

    acctbD = acctbD * mir;
    intD = acctbD * mir;
    acctbD = acctbD + intD;

    System.out.println("");
    System.out.println("First month:");
    System.out.printf("Balance =$%.2f",acctbD);
    System.out.println("");
    System.out.printf("Interest = $%.2f",intD);
    System.out.println("");

    acctbD = acctbD * mir;
    intD = acctbD * mir;
    acctbD = acctbD + intD;

    System.out.println("");
    System.out.println("Second month:");
    System.out.printf("Balance =$%.2f",acctbD);
    System.out.println("");
    System.out.printf("Interest = $%.2f",intD);
    System.out.println("");

    acctbD = acctbD * mir;
    intD = acctbD * mir;
    acctbD = acctbD + intD;

    System.out.println("");
    System.out.println("Third month:");
    System.out.printf("Balance =$%.2f",acctbD);
    System.out.println("");
    System.out.printf("Interest = $%.2f",intD);
    System.out.println("");

    acctbD = acctbD * mir;
    intD = acctbD * mir;
    acctbD = acctbD + intD;

    System.out.println("");
    System.out.println("Forth month:");
    System.out.printf("Balance =$%.2f",acctbD);
    System.out.println("");
    System.out.printf("Interest = $%.2f",intD);
    System.out.println("");

    acctbD = acctbD * mir;
    intD = acctbD * mir;
    acctbD = acctbD + intD;

    System.out.println("");
    System.out.println("Fifth month:");
    System.out.printf("Balance =$%.2f",acctbD);
    System.out.println("");
    System.out.printf("Interest = $%.2f",intD);
    System.out.println("");

    acctbD = acctbD * mir;
    intD = acctbD * mir;
    acctbD = acctbD + intD;

    System.out.println("");
    System.out.println("Sixth month:");
    System.out.printf("Balance =$%.2f",acctbD);
    System.out.println("");
    System.out.printf("Interest = $%.2f",intD);
    System.out.println("");

    }
}
I just have a few questions:

1. Is there a more efficient way of making blank lines, rather than using a whole bunch of System.out.println("")?

2. How can I catch IO errors? Is it possible to just make it print a custom error message, then go back to prompt for a (hopefully) correct input?

Are there ways I can reduce the amount of code, when there's a lot of repetition (like what's in there now)?
__________________

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

Last edited by Apokalipse; 03-19-2008 at 03:20 AM.
Apokalipse is offline  
Old 03-13-2008, 08:53 AM   #2 (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:
Is there a more efficient way of making blank lines, rather than using a whole bunch of System.out.println("")
Using System.out.println() is fine, but you could append the new line character (and carriage return in Windows) to the ends of your strings. However, I think using println() is more readable.

Quote:
How can I catch IO errors?
Wrap the code in try-catch blocks.

Quote:
Are there ways I can reduce the amount of code, when there's a lot of repetition (like what's in there now)?
Factor repetitive code into methods. If you find yourself rewriting a block of code that already exists, you should move that code into a method and then just call the method every time you need that functionality.

Google "Java style guidelines" for more information on some of your questions.
jaeusm is offline  
Old 03-13-2008, 10:54 AM   #3 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,739

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

I tried modifying it, but I'm not really sure how to use classes, methods etc (yet).
Code:
import java.io*;
public class Account
{
    public static void main(String[] args)
    {
    BufferedReader stdin = new BufferedReader
                (new InputStreamReader (System.in));
    String acctb, intrt, mda;
    double acctbD, intrtD, mir, intD, mdaD;

    public static void firstDeposit( ) throws IOException
    {
    try
    {
        System.out.println("Initial deposit amount:");
        acctb = stdline.readLine();
        acctbD = Double.parseDouble(acctb);
    }

        catch(IOException io)
    {
        System.out.println("You did not enter a valid number");
        firstDeposit();
    }
    }

    public static void Interest( ) throws IOException
    {
    try
    {
        System.out.println("Interest PPA, eg 6.0 for 6.0%:");
        intrt = stdin.readLine();
        intrtD = Double.parseDouble(intrt);
    }

        catch(IOException io)
    {
        System.out.println("You did not enter a valid number");
        Interest();
    }
    }

    public static void mDeposit( ) throws IOException
    {
    try
    {
        System.out.println("Monthly deposit amount:");
        mda = stdin.readLine();
        mdaD = Double.parseDouble(intrt);
    }

        catch(IOException io)
    {
        System.out.println("You did not enter a valid number");
        mDeposit();
    }
    }

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

    mir = (intrtD / 12) / 100;

    public static nextMonth( )
    {
        acctbD = acctbD * mir;
        intD = acctbD * mir;
        acctbD = acctbD + intD;
        System.out.println("");
    }
    public static dispBI( )
    {
        System.out.printf("Balance =$%.2f",acctbD);
        System.out.println("");
        System.out.printf("Interest = $%.2f",intD);
        System.out.println("");
    }

    nextMonth();
    System.out.println("First month:");
    dispBI();

    nextMonth();
    System.out.println("Second month:");
    dispBI();

    nextMonth();
    System.out.println("Third month:");
    dispBI();

    nextMonth();
    System.out.println("Forth month:");
    dispBI();

    nextMonth();
    System.out.println("Fifth month:");
    dispBI();

    nextMonth();
    System.out.println("Sixth month:");
    dispBI();

    }
}
If I figure this out, I should be well ahead of what they're teaching us so far at uni
__________________

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

Last edited by Apokalipse; 03-13-2008 at 10:57 AM.
Apokalipse is offline  
Old 03-13-2008, 12:21 PM   #4 (permalink)
Trotter's Avatar
 

Join Date: Jan 2005

Location: The South

Posts: 19,969

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

It is comforting to know another shares my pain...
__________________
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-13-2008, 11:10 PM   #5 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,739

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

I think this is worth it though; I'm trying to get ahead of what they're teaching us.

The program in the first post works, I just want to make it better/more efficient.

Right now, I'm just trying to figure out how to use methods.
I wrote this little program (it isn't work I have to do):
Code:
import java.io.BufferedReader;
public class Test
{
    BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
    String Something;
    public static aMethod()
    {
    System.out.print("Input something:");
    Something = stdin.readLine();
    }    

    public static void main(String[] args)
    {
    System.out.print("Something before aMethod");
    aMethod();
    System.out.printf("%s",Something);
    }
}
It doesn't compile properly (using javac in DOS):
H:\Test.java:6: invalid method declaration; return type required
public.static.aMethod
(there's a "^" under the a in aMethod)

*edit*
Quote:
Originally Posted by Apokalipse View Post
I tried modifying it, but I'm not really sure how to use classes, methods etc (yet).
[program]
If I figure this out, I should be well ahead of what they're teaching us so far at uni
I found that if I changed it from using classes to normal methods (changing the words 'class' to 'static'), it only got 2 errors from the compiler.
__________________

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

Last edited by Apokalipse; 03-13-2008 at 11:26 PM.
Apokalipse is offline  
Old 03-14-2008, 08:37 AM   #6 (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:
Right now, I'm just trying to figure out how to use methods.
Read your text book.

Quote:
It doesn't compile properly (using javac in DOS):
H:\Test.java:6: invalid method declaration; return type required
public.static.aMethod (there's a "^" under the a in aMethod)
The compiler will tell you exactly why your code won't compile. In this case, your method doesn't specify a return type.

Quote:
I found that if I changed it from using classes to normal methods (changing the words 'class' to 'static'), it only got 2 errors from the compiler.
The number of errors your compiler finds is sometimes misleading. In many situations, as soon as you fix those errors, the compiler can further parse your code and find even more errors.

If you want to get ahead of your class, then read your book.
jaeusm is offline  
Old 03-14-2008, 06:28 PM   #7 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,739

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

Quote:
Originally Posted by jaeusm View Post
Read your text book.
Yeah mate, that's what I've been doing.
But you can't ask a book specific questions, or to elaborate certain things, if they're not completely clear. That's why I made this thread.
__________________

1 + 1 = 3 if you define 3 as a result of 1 + 1
Apokalipse is offline  
Old 03-18-2008, 07:07 AM   #8 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,739

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

okay, I've revised the program now, and got this:
Code:
import java.io.*;
public class Account
{

    public static double input( ) throws IOException // method for inputting initial values
    {
    boolean valid = true;                 // used to prompt input again if an IOException occurs

    string acctb, intrt, mda;
    double acctbD, intrtD, mir, mint, mdaD;

    BufferedReader stdin = new BufferedReader
            (new InputStreamReader (System.in));

    do                         // input initial account balance
    {
    try
    {
        valid = true;
        System.out.println("Initial deposit amount:");
        acctb = stdline.readLine();
        acctbD = Double.parseDouble(acctb);
    }
    catch(IOException io)
    {
        System.out.println("You did not enter a valid number");
        valid = false;
    }
    }
    while (!valid);

    do                         // input interest rate
    {
    try
    {
        valid = true;
        System.out.println("Interest PPA, eg 6.0 for 6.0%:");
        intrt = stdin.readLine();
        intrtD = Double.parseDouble(intrt);
    }
    catch(IOException io)
    {
        System.out.println("You did not enter a valid number");
        valid = false;
    }
    }
    while (!valid);

    do                         // input monthly deposit amount
    {
    try
    {
        valid = true;
        System.out.println("Monthly deposit amount:");
        mda = stdin.readLine();
        mdaD = Double.parseDouble(mda);
    }
    catch(IOException io)
    {
        System.out.println("You did not enter a valid number");
        valid = false;
    }
    }
    while (!valid);
    }

    public static void nextMonth( )             // calculates new values each month
    {
        acctbD = acctbD * mdaD;
        mint = acctbD * mir;
        acctbD = acctbD + mint;
        System.out.println("");
    }

    public static void dispBI( )             // displays balance and interest each month
    {
        System.out.printf("Balance =$%.2f\n",acctbD);
        System.out.printf("Interest = $%.2f\n",mint);
    }

    public static void main(String[] args)
    {
    input();

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

    mir = (intrtD / 12) / 100;             // calculatess interest rate for *one* month


    nextMonth();
    System.out.println("First Month:");
    dispBI();

    nextMonth();
    System.out.println("Second month:");
    dispBI();

    nextMonth();
    System.out.println("Third month:");
    dispBI();

    nextMonth();
    System.out.println("Forth month:");
    dispBI();

    nextMonth();
    System.out.println("Fifth month:");
    dispBI();

    nextMonth();
    System.out.println("Sixth month:");
    dispBI();

    }
}
But it doesn't compile. I get a whole bunch of "cannot find symbol" errors.
__________________

1 + 1 = 3 if you define 3 as a result of 1 + 1
Apokalipse is offline  
Old 03-18-2008, 08:51 AM   #9 (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:
I get a whole bunch of "cannot find symbol" errors.
Post your compiler output, as it tells you exactly what your problems are.

The "nextMonth" method will definitely have "cannot find symbol" errors. How does it know what those variables are? You didn't define them in the method, nor did you pass them in as parameters. They are defined in a local scope in a different method.

I wasn't trying to be a jerk earlier when I told you to read your book, but that's exactly what you need to do. You have huge gaps in your understanding, as this code sample illustrates.

Quote:
But you can't ask a book specific questions
You're getting ahead of yourself. You need more foundational knowledge before attempting to find answers to your questions. Be patient. You'll learn it.
jaeusm is offline  
Old 03-18-2008, 10:35 AM   #10 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,739

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

Quote:
Originally Posted by jaeusm View Post
Post your compiler output, as it tells you exactly what your problems are.


Quote:
The "nextMonth" method will definitely have "cannot find symbol" errors. How does it know what those variables are? You didn't define them in the method, nor did you pass them in as parameters.
okay, how do I pass the variables as parameters?
I assume they go in the brackets when declaring the method:
public static void aMethod(parameters here)

Quote:
I wasn't trying to be a jerk earlier
I know.

Quote:
when I told you to read your book, but that's exactly what you need to do.
Yeah, I know. And I have been.
Quote:
You have huge gaps in your understanding, as this code sample illustrates.
Yes, I have a lot of gaps in my understanding. That's actually why I've jumped into the deep-end. I just seem to be able to learn better, and faster this way.
Quote:
You're getting ahead of yourself.
I don't want to sound naiive, but I don't think I am.
I've intentionally "bitten off more than I can chew", because:
1. I found the pace they're teaching us to be kinda slow
2. it makes it easier to highlight what I need to learn
3. I'm eager

Anyway, I understand basically everything that I've written in my program so far. What's missing is what I don't understand (yet)

*edit*
updated the program:
Code:
import java.io.*;
public class Account
{

    public static void nextMonth( )        // calculates new values each month
    {
        acctbD = acctbD * mdaD;
        mint = acctbD * mintr;
        acctbD = acctbD + mint;
        System.out.println("");
    }

    public static void dispBI( )        // displays balance and interest each month
    {
        System.out.printf("Balance =$%.2f\n",acctbD);
        System.out.printf("Interest = $%.2f\n",mint);
    }

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

    String acctb, intrt, mda;        // account balance, yearly interest rate, monthly deposit amount
    double acctbD, intrtD, mint, mdaD;    // doubles of all above values, and monthly interest rate
    boolean valid = true;            // true by default, but false if an IOException occurs

    do                    // input initial account balance
    {
    try
    {
        valid = true;
        System.out.println("Initial deposit amount:");
        acctb = stdin.readLine();
        acctbD = Double.parseDouble(acctb);
    }
    catch(IOException io)
    {
        System.out.println("You did not enter a valid number");
        valid = false;
    }
    }
    while (!valid);

    do                    // input interest rate
    {
    try
    {
        valid = true;
        System.out.println("Interest PPA, eg 6.0 for 6.0%:");
        intrt = stdin.readLine();
        intrtD = Double.parseDouble(intrt);
    }
    catch(IOException io)
    {
        System.out.println("You did not enter a valid number");
        valid = false;
    }
    }
    while (!valid);

    do                    // input monthly deposit amount
    {
    try
    {
        valid = true;
        System.out.println("Monthly deposit amount:");
        mda = stdin.readLine();
        mdaD = Double.parseDouble(mda);
    }
    catch(IOException io)
    {
        System.out.println("You did not enter a valid number");
        valid = false;
    }
    }
    while (!valid);

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

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

    for (int month=1; month<=6; month++)
    {
    nextMonth();
    System.out.println("");
    System.out.println("Month "+ month);
    dispBI();
    }
    } // end of main method
}
I've gotten rid of the Input method, and put the code from that inside the main method (the input didn't really need its own method)
It now uses a loop to print the results for 6 months
*edit*
Just thought of something: does the initial value for the month integer need to be 0, or 1, in order to loop 6 times?
__________________

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

Last edited by Apokalipse; 03-18-2008 at 11:09 AM.
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 15121 Today 08:43 AM
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