Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Applying Methods to Program

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Question Applying Methods to Program

    I'm just not quite getting the whole method thing. I get the general concept and even like, but applying it is another matter for me. Smaller programs, examples of simple method programs I get. But when it comes to 'methodizing' a larger program, I begin to have trouble. Case-in-point, below I'm attempting to add methods to a sequential program I wrote earlier. (The original is posted below the method-attempt.) If anyone cares to chime in, it would really be helpful to me.

    import java.io.*;  
    import java.util.*;  
    import java.text.DecimalFormat;
     
     
     
     
    public class Bank3 
    {
    	public static void main(String[] args) throws IOException
    	{
     
    		String firstLine; 
    		String secondLine;
    		String thirdLine;
    		String fourthLine;
    		String tempLine;  
    		double fifthLine;
    		double monthlyFee = 0;  
    		double totalBalance = 0;  
     
     
     
     
     
    		openRead();
    		outPut();
     
    	}	
     
     
     
    		public static void openRead() throws IOException
    		{
    		File file = new File("AcctInfo.txt");   
    		Scanner inputFile = new Scanner(file);    
    		DecimalFormat formatter = new DecimalFormat("#0.00");  
     
    		while (inputFile.hasNext()) 
    			{
    			String firstLine = inputFile.nextLine();
    			while (!(firstLine.isEmpty()))  
    				{
    				String secondLine = inputFile.nextLine(); 
    				String thirdLine = inputFile.nextLine();
    				String fourthLine = inputFile.nextLine();;
    				String tempLine = inputFile.nextLine();
    				Double fifthLine = Double.parseDouble(tempLine);  
     
    				double monthlyFee = 0;
    				double totalBalance = 0;
     
     
     
    				System.out.println("Account #:\t\t" + firstLine);
    				System.out.println("Account Type:\t\t" + secondLine);
    				System.out.println("Customer Name:\t\t" + thirdLine);
    				System.out.println("Customer Type:\t\t" + fourthLine);
    				System.out.println("Balance:\t\t$" + fifthLine);
     
     
    				if (secondLine.equalsIgnoreCase("savings"))
    					{
    					monthlyFee = 0.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("value") && fifthLine < 1500)
    					{
    					monthlyFee = 5.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("value") && fifthLine >= 1500)
    					{
    					monthlyFee = 0.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("advantage") && fifthLine < 1000)
    					{
    					monthlyFee = 10.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("advantage") && fifthLine >= 1000)
    					{
    					monthlyFee = 0.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("premier") && fifthLine < 25000)
    					{
    					monthlyFee = 30.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("premier") && fifthLine >= 25000)
    					{
    					monthlyFee = 0.00;
     
    					return monthlyFee;
    					}
     
    				}
     
    				}}
    				public static void outPut(double monthlyFee, fifthLine)throws IOException
    				{
    				System.out.println("Monthly Fee:\t\t$" + formatter.format(monthlyFee));  
    				System.out.println("");  
    				totalBalance += fifthLine;  
    				firstLine = inputFile.nextLine(); 
    				System.out.println("The total amount deposited: $" + formatter.format(totalBalance)); 
    										inputFile.close();		
    				}	
     
     
    			}


    Original Code:

    import java.util.*;  
    import java.io.*;  
    import java.text.DecimalFormat;
     
     
     
     
    public class Bank1 
    {
    	public static void main(String[] args) throws IOException
    	{
     
    		String firstLine; 
    		String secondLine;
    		String thirdLine;
    		String fourthLine;
    		String tempLine;  
    		double fifthLine;
    		double monthlyFee = 0;  
    		double totalBalance = 0;  
     
    		File file = new File("AcctInfo.txt");   
    		Scanner inputFile = new Scanner(file);    
    		DecimalFormat formatter = new DecimalFormat("#0.00");  
     
    		while (inputFile.hasNext()) 
    			{
    			firstLine = inputFile.nextLine();
    			while (!(firstLine.isEmpty()))  
    				{
    				secondLine = inputFile.nextLine(); 
    				thirdLine = inputFile.nextLine();
    				fourthLine = inputFile.nextLine();;
    				tempLine = inputFile.nextLine();
    				fifthLine = Double.parseDouble(tempLine);  
     
     
     
    				System.out.println("Account #:\t\t" + firstLine);
    				System.out.println("Account Type:\t\t" + secondLine);
    				System.out.println("Customer Name:\t\t" + thirdLine);
    				System.out.println("Customer Type:\t\t" + fourthLine);
    				System.out.println("Balance:\t\t$" + fifthLine);
     
     
    				if (secondLine.equalsIgnoreCase("savings"))
    					{
    					monthlyFee = 0.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("value") && fifthLine < 1500)
    					{
    					monthlyFee = 5.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("value") && fifthLine >= 1500)
    					{
    					monthlyFee = 0.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("advantage") && fifthLine < 1000)
    					{
    					monthlyFee = 10.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("advantage") && fifthLine >= 1000)
    					{
    					monthlyFee = 0.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("premier") && fifthLine < 25000)
    					{
    					monthlyFee = 30.00;
    					}
    				else if (fourthLine.equalsIgnoreCase("premier") && fifthLine >= 25000)
    					{
    					monthlyFee = 0.00;
    					}
    				System.out.println("Monthly Fee:\t\t$" + formatter.format(monthlyFee));  
    				System.out.println("");  
    				totalBalance += fifthLine;  
    				firstLine = inputFile.nextLine(); 
    				}				
    			}
    		System.out.println("The total amount deposited: $" + formatter.format(totalBalance));  
    		inputFile.close(); 
    	}
    }


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Applying Methods to Program

    Do you have any specific questions about your code?

    How methods work:
    1. You create a method - public static datatype Name(args)
    public static boolean MyMethod(int x, int y){
    }
    2. Declare the type of return for the method aka bool, int, void. etc.
    3. What data types, if any, you are going to pass to the method
    4. Calling a method- MyMethod(5, 8);

    More about methods: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Applying Methods to Program

    Thanks for quick reply and link Zyrion. No particular questions about my code. I suppose I posted it to see if it elicited a "Hey, you have major logic errors" response. But I realize that it's a lot to look at, and such is not readily apparent. Anyway, step 3 in the summary you posted is actually a big reminder to me. May be one of the problems I'm having, i.e., not properly defining my methods. Thanks again Zyrion!

Similar Threads

  1. Having trouble writing the methods for this program
    By michael305rodri in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2012, 11:31 PM
  2. Program not compiling + calling methods help
    By alex067 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 11th, 2012, 07:41 AM
  3. LWJGL texture applying problem
    By DanielJamesCollier in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 16th, 2012, 01:45 PM
  4. Need Help applying discount
    By teeej86 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 22nd, 2011, 08:13 PM
  5. Applying a MIDI instrument change to a Track
    By AnkleSpankle in forum Java Theory & Questions
    Replies: 3
    Last Post: January 23rd, 2011, 03:34 PM