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 12 of 12

Thread: HELP WITH METHODS!

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HELP WITH METHODS!

    Hi friends, having trouble using a method to do a calculation and then calling it to be printed. What am I doing wrong? (What I am doing is all the way at the bottom below the main method)

    import javax.swing.JOptionPane;
    public class hospital_copy {
     
    	public static void main(String[] args) {
     
    			//Declare variables
    				String patient_last, surgery_location, surgery_type, insurance_fee, surgeon_name, output="";
    				double agh_count=0, upmc_count=0;
    				double fee = 0, agh_fee = 0, upmc_fee = 0, high_fee = 0;
    				int count_knee=0, count_hip=0;
    				String highest_surgeon="";
    				String high_patient="";
    				String high_surgeon="";
    				String high_location="";
    				String more_data = "yes";
    				int Atkins_count=0,Johnson_count=0,Smith_count=0;
     
    				while(more_data.equals("yes"))
    				{
    			//Read the data
    					patient_last=JOptionPane.showInputDialog(null,"Enter Patient's Last Name",
    						"Input Data", JOptionPane.QUESTION_MESSAGE);
     
    					surgery_location=JOptionPane.showInputDialog(null,"Enter the Surgery Location (AGH or UPMC)",
    						"Input Data", JOptionPane.QUESTION_MESSAGE);
     
    					surgery_type=JOptionPane.showInputDialog(null,"Enter the Type of Surgery (knee or hip)",
    						"Input Data", JOptionPane.QUESTION_MESSAGE);
     
    					insurance_fee=JOptionPane.showInputDialog(null,"Enter the Amount of Fee Paid By Insurance",
    							"Input Data", JOptionPane.QUESTION_MESSAGE);
    					//parse insurance fee to double
    					 fee=Double.parseDouble(insurance_fee);
     
    					surgeon_name=JOptionPane.showInputDialog(null,"Enter the Surgeon's Name (Johnson, Atkins, or Smith)",
    							"Input Data", JOptionPane.QUESTION_MESSAGE);	
     
    					//Count number of each type of surgery
    					if(surgery_type.equals("knee"))
    						count_knee=count_knee+1;
    					else
    						count_hip=count_hip+1;
     
    					//Count number of surgeries at each location
    					if(surgery_location.equals("UPMC"))
    					{
    						upmc_count=upmc_count+1;
    						upmc_fee=upmc_fee+fee;
    					}
    					if(surgery_location.equals("AGH"))
    					{
    						agh_count=agh_count+1;
    						agh_fee=agh_fee+fee;				
    					}
     
    					//Count number of surgeries each surgeon did
    					if(surgeon_name.equals("Johnson"))
    						Johnson_count=Johnson_count+1;
    					else
    					if(surgeon_name.equals("Atkins"))
    						Atkins_count=Atkins_count+1;
    					else
    						Smith_count=Smith_count+1;
     
    					//Find surgeon with highest fee
    					if (fee>high_fee)
    					{
    						high_fee=fee;
    						high_patient=patient_last;
    						high_surgeon=surgeon_name;
    						high_location=surgery_location;
    					}
     
    					//Ask if more data
    				 	more_data=JOptionPane.showInputDialog(null,"More Info? Enter yes or no",
    						 "Input Dialog",JOptionPane.QUESTION_MESSAGE);	
    				}//End of while loop
     
     
     
     
     
     
    				//Calculate average fee for each location
     
     
     
    				//Find surgeon with most surgeries
    				if ((Johnson_count > Atkins_count)&(Johnson_count > Smith_count))
    						highest_surgeon = "Johnson";
    				else
    					if ((Atkins_count > Johnson_count)&(Atkins_count > Smith_count))
    						highest_surgeon = "Atkins";
    					else
    						highest_surgeon = "Smith";
     
    				//Output data
    			 	output=output+"Number of Knee Surgeries: "+count_knee+"\n"; 
    				output=output+"Number of Hip Surgeries: "+count_hip+"\n"+"\n";
     
     
    				output=output+"Number of Surgeries Performed by Johnson: "+Johnson_count+"\n";
    				output=output+"Number of Surgeries Performed by Atkins: "+Atkins_count+"\n";
    				output=output+"Number of Surgeries Performed by Smith: "+Smith_count+"\n"+"\n";
    				output=output+"Doctor Who Performed the Most Surgeries: "+highest_surgeon+"\n"+"\n";
    				output=output+"Patient Whose Insurance Paid Highest Fee: "+high_patient+"\n";
    				output=output+"Doctor For Above Patient: "+high_surgeon+"\n";
    				output=output+"Location of Above Patient's Surgery: "+high_location+"\n";
     
     
    				//Display the output dialog box
    				JOptionPane.showMessageDialog(null,output,"Output",JOptionPane.INFORMATION_MESSAGE);
    				System.exit(0);
     
    			}//end class
     
    	public static void calc_average(double avagh, double avUPMC){
    		System.out.println(avUPMC);
    	}
     
     
    	public static double calc_averages(double agh_fee,int agh_count){
    		double avagh = agh_fee / agh_count;
    		return avagh;
    	}
     
    	public static double calc_average(double UPMC_fee, int UPMC_count){
    		double avUPMC = UPMC_fee / UPMC_count;
    		return avUPMC;
    	}
     
     
    		} //end method


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP WITH METHODS!

    What am I doing wrong?
    Please explain what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP WITH METHODS!

    At the very bottom, bellow the main method, I am trying to calculate the average of the fees paid by UPMC (avUPMC) Something is going wrong but I'm getting no errors. Its either its not calculating correctly and not returning anything, or I'm not calling the print method correctly and its just not showing up. This is the part that I am working on and have hit a dead end;

    	public static void print(double avUPMC){
    		System.out.println(avUPMC);
    	}
     
     
    	public static double calc_averages(double agh_fee,int agh_count){
    		double avagh = agh_fee / agh_count;
    		return avagh;
    	}
     
    	public static double calc_average(double UPMC_fee, int UPMC_count){
    		double avUPMC = UPMC_fee / UPMC_count;
    		return avUPMC;
    	}
     
     
    		} //end method

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP WITH METHODS!

    Something is going wrong
    Can you explain what the program does and what is "going wrong"?

    What do you expect to happen when the code executes?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP WITH METHODS!

    It just isnt returning anything with the System.out.println(avUPMC); Its either not calculating and returning avUPMC or I'm not doing the print method correctly. Do I have to initialize anything in the main method or something? I'm completely lost.

    I want this method below to calculate the average fees paid at UPMC.
    public static double calc_average(double upmc_fee, int upmc_count){
    		double avUPMC = upmc_fee / upmc_count;
    		return avUPMC;
    	}

    and i want this to print the results
    	public static void printing(double avUPMC){
    		System.out.println(avUPMC);
    	}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP WITH METHODS!

    Are the methods you want to execute being called? If they are not called, they will not be executed.

    Take a look at the tutorial:
    Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP WITH METHODS!

    This is a very good link, thank you. But I really dont understand what I personally am doing wrong then. If you could answer this...how would you call back "answer" and have it print it in a separate method in this example? Bellow it is how I think I should be doing it.
    public double computePayment(
                      double loanAmt,
                      double rate,
                      double futureValue,
                      int numPeriods) {
        double interest = rate / 100.0;
        double partial1 = Math.pow((1 + interest), 
                        - numPeriods);
        double denominator = (1 - partial1) / interest;
        double answer = (-loanAmt / denominator)
                        - ((futureValue * partial1) / denominator);
        return answer;
    }
    public static void computePayment(double answer){
    System.out.println(answer);
    }


    --- Update ---

    Is there something else I need to do in the unseen main method in order to make it work? I feel like it needs to be initialized in the main method or something. This is only speculative but I'm really pulling my hair out trying to figure out what I'm not doing correctly

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP WITH METHODS!

    When a method returns a value, the calling code needs to assign the returned value to a variable:
    double someVar = computePayment(put needed args here);
    someVar now has the value that was returned by the computePayment() method from what was in the answer variable in the computePayment() method.

    The method you posted will print out the value of the double arg that was passed to it.
    That method has NO relationship with the computePayment() that was coded above it.

    what I'm not doing correctly
    You still have not explained what is wrong and what you expect the program to do differently.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP WITH METHODS!

    double someVar = computePayment(put needed args here);
    Which method is this going in? Main?

    Also what I want it to do is work, when I run it nothing happens and the console is blank. I want a method to take information from the main method and compute the average, then return it to yet another method to print the results.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP WITH METHODS!

    Which method is this going in?
    That statement could be coded in any method that is in the same class as the computePayment() method.

    want a method to take information from the main method and compute the average, then return it to yet another method to print the results.
    Then you need to have a statement that calls the method you want to execute and pass it whatever values it needs.

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: HELP WITH METHODS!

    you didn't call the methods like calc_averages() and calc_average() anywhere then how to print the average in your screen..
    you first call the methods in your class and assign return statement in to variable.

    Finally you print that variable ...
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  12. #12
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: HELP WITH METHODS!

    Your code compiles because it's syntax is correct. However as already mention
    Quote Originally Posted by Norm View Post
    Then you need to have a statement that calls the method you want to execute and pass it whatever values it needs.
    Java Tutorials - Defining Methods

Similar Threads

  1. [SOLVED] Help with Methods
    By Blasfemmy in forum What's Wrong With My Code?
    Replies: 24
    Last Post: October 22nd, 2012, 08:18 PM
  2. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  3. Help with methods?
    By THeAnnonymous in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 09:27 AM
  4. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 11:57 PM
  5. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 05:04 PM