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

Thread: Most of my code works when executed except for the Min and Max calculations

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Most of my code works when executed except for the Min and Max calculations

    Everything works exactly like its suppose to but when its suppose to show the max and min values out of the 3 numbers, it just returns 0.

    //23.4
    import java.awt.Graphics;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;
     
    public class Comparisons extends JApplet
    {
    	public double sum;
    	public double average;
    	public double product;
    	public double smallest;
    	public double largest;
    	public double minimumValue;
    	public double maximumValue;
     
    	public void init()
    	{
    		String firstNumber = JOptionPane.showInputDialog( "Enter the first floating-point value" );
     
    		String secondNumber = JOptionPane.showInputDialog( "Enter the second floating-point value" );
     
    		String thirdNumber = JOptionPane.showInputDialog( "Enter the third floating-point value" );
     
    		double number1 = Double.parseDouble( firstNumber );
    		double number2 = Double.parseDouble( secondNumber );
    		double number3 = Double.parseDouble( thirdNumber );
     
    		sum = number1 + number2 + number3;
     
    		average = (number1 + number2 + number3) / 3;
     
    		product = number1 * number2 * number3;
     
    	}	
    		public static double minimum( double number1, double number2, double number3 )
    		{
    			double minimumValue = number1;
     
    			if( number2 < minimumValue )
    				minimumValue = number2;
     
    			if( number3 < minimumValue )
    				minimumValue = number3;
    			return minimumValue;
    		}
    		public static double maximum(double number1, double number2, double number3)
    		{
    			double maximumValue = number1;
     
    			if( number2 > maximumValue )
    				maximumValue = number2;
    			if( number3 > maximumValue )
    				maximumValue = number3;
    				return maximumValue;
    		}
    		public void paint(Graphics g)
    		{
    			super.paint(g);
     
    			g.drawRect(15, 10, 270, 20);
     
    			g.drawString("The sum is " + sum, 25, 25);
     
    			g.drawRect(15, 30, 270, 20);
     
    			g.drawString("The average is: " + average, 25, 45);
     
    			g.drawRect(15, 50, 270, 20);
     
    			g.drawString("The product is " + product, 25, 63);
     
    			g.drawRect(15, 70, 270, 20);
     
    			g.drawString("The smallest value is " + minimumValue, 25, 83);
     
    			g.drawRect(15, 90, 270, 20);
     
    			g.drawString("The largest value is " + maximumValue, 25, 103);
     
    		}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Most of my code works when executed except for the Min and Max calculations

    Where does the code set the value of the variables to the max or min values?
    Is the code where that happens executed?
    Add a call to the println method to print a message when the methods are executed to see if they are in fact executed. If nothing prints, that would say the method is NOT executed. If the method is not executed the values of the variables won't be changed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Most of my code works when executed except for the Min and Max calculations

    yes it executes but it just says the min value is 0 and the max value is 0

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Most of my code works when executed except for the Min and Max calculations

    Did you add the println call in the methods where the min and max variables are given values?
    Was anything printed?
    If nothing prints, that would say the method is NOT executed. If the method is not executed the values of the variables won't be changed and will still be 0.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Most of my code works when executed except for the Min and Max calculations

    even with the println call it still says the max and min are both 0.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Most of my code works when executed except for the Min and Max calculations

    The println statements won't change the values of min and max. They are to tell you if the methods are executed.
    Was anything printed?
    If nothing prints, that would say the method is NOT executed. If the method is not executed the values of the variables won't be changed and will still be 0.

    --- Update ---

    The println statements won't change the values of min and max. They are to tell you if the methods are executed.
    Was anything printed?
    If nothing prints, that would say the method is NOT executed. If the method is not executed the values of the variables won't be changed and will still be 0.

    --- Update ---

    The println statements won't change the values of min and max. They are to tell you if the methods are executed.
    Was anything printed?
    If nothing prints, that would say the method is NOT executed. If the method is not executed the values of the variables won't be changed and will still be 0.

    --- Update ---

    The println statements won't change the values of min and max. They are to tell you if the methods are executed.
    Was anything printed?
    If nothing prints, that would say the method is NOT executed. If the method is not executed the values of the variables won't be changed and will still be 0.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Most of my code works when executed except for the Min and Max calculations

    it printed "The smallest value is 0" for the min and "The largest value is 0" for the max.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Most of my code works when executed except for the Min and Max calculations

    Did you add some new printlns to all the methods as I requested earlier in post#2?

    Did any of them print out a message?

    Add this to all the methods in the code:
    System.out.println("in <name of method here> ");
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Most of my code works when executed except for the Min and Max calculations

    i only added the println to the min and max because those are the only two that aren't working properly.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Most of my code works when executed except for the Min and Max calculations

    Did you add some new printlns to all the methods as I requested earlier in post#2?

    Did any of them print out a message?

    Add this to all the methods in the code:
    System.out.println("in <name of method here> ");


    --- Update ---

    Please post the code that shows where you added the new println statements.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)
    By TheWhopper858 in forum Collections and Generics
    Replies: 1
    Last Post: November 6th, 2011, 07:50 PM
  2. Need help with C++ min and max heaps.
    By javapenguin in forum Other Programming Languages
    Replies: 6
    Last Post: October 19th, 2011, 11:08 AM
  3. Replies: 5
    Last Post: May 24th, 2011, 10:39 AM
  4. Help with Finding Max and Min Values of ArrayList
    By CheekySpoon in forum Collections and Generics
    Replies: 3
    Last Post: March 2nd, 2011, 07:57 PM
  5. Printing the Max and Min in an Array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2010, 08:28 AM