A little help on my java programming homework?

Status
Not open for further replies.
Hey all, we are supposed to do an assigment for class and no one has figured it out yet. I was hoping you could give a little guidence (NOT JUST GIVE THE CODE ANSWER) :D

Assigment
Write a program application called Midpoint that prompts the user for a
series of numeric inputs until the user inputs either "m" OR "midpoint", at
which point the program reports the "midpoint" of all the numbers
entered, where midpoint is defined to be the middle real (i.e.,
decimal precision) number on a real number line between the lowest and
highest numbers.

E.g., if I input the following:

1, 4, -21, 5, 70, 12, 9, 0, 44, 2, "m"

Midpoint will respond:
"The midpoint is: 24.5" (Correction!!!!!)

Notice how the program looped indefinitely waiting for me to enter
either "m" or "midpoint". The program should use JOptionPane utilities
to accomplish its input/output operations

Notice also that the midpoint (as I've defined it) is not necessarily a
number you have input and that the midpoint can have a fractional (i.e.,
decimal expansion) part. You will need to treat the inputs as
floating-point (i.e., decimal precision) numbers, so you will need to use
either doubles or floats (I recommend using doubles)

My code thus far...

Code:
import javax.swing.JOptionPane;

public class Midpoint {

   public static void main(String args[]) {

      	String userInput;
	double doubleInput; 
	double large = -9999;
        double small = 9999;
	double midpoint = 0;


	userInput = JOptionPane.showInputDialog("Enter a number (type 'Midpoint' or 'm'"+"\nfor the midpoint of entered numbers).");


	while (!(userInput.equalsIgnoreCase("Midpoint") ||    userInput.equalsIgnoreCase("m")) ) {


	doubleInput = Double.parseDouble(userInput);
  

        if (doubleInput > large)

        large = doubleInput;

	else if (doubleInput < small)
		
             small = doubleInput;

	     else;

	userInput = JOptionPane.showInputDialog( "Enter a number (type 'Midpoint' or 'm'"+
         "\nfor the midpoint of entered numbers).");

        midpoint = (large + small) / 2;
        
	}

	JOptionPane.showMessageDialog(null, "The Midpoint is: "+midpoint);

	System.exit(0);
}
}

Now, first off, it doesnt work anyways.... but even if it did, I could only use numbers from -9999 to 9999...

Can someone point me in the right direction?

Thanks
Anthony

p.s. pardon the formatting... i couldnt get it all lined up :)
 

gumbo

Ars Tribunus Angusticlavius
6,107
The code works fine for me, using the values listed in the problem. Does it fail for others?

Edit: NM, 1,2,3,4,5,6 gives me 5000. :D

Edit: I kind of figured it out.

When you set 1, large is 1.0, and small is 9999.0. Midpoint is 5000.
When you enter 2, large is 2.0, but small is still 9999.0. If the number is larger than small, and larger than large, then the current large should be set to the current small, and the current value should become the new large.
Whew! Did you understand that? Much can be learned by entering simple numbers, and watching the variables change in a debugger.

I have no clue how the original example works.

gubmo

[This message was edited by gubmo on April 10, 2002 at 19:57.]
 

KarlShea

Ars Tribunus Militum
2,277
Subscriptor++
Ok, this is what I see so far. (note: I will not have access to a jvm for another hour, so I can't try to run anything, so I don't know what errors/exceptions you're getting)

When you have a while loop, the condition is checked first (like an if statement). So if it were me, I would use a do..while loop because I would want the dialog to display the message at least once. In a do..while loop the loop runs once, and then the condition is checked. So you would get a dialog before it would check the condition.

Second, I would put the dialog first in the loop. You can't do anything until you have the user's input, so you want to get that first.

Then, make sure that before you parse the number, you check to see if they entered 'midpoint' or 'm' like you do in the while loop condition. This way you're not trying to parse a string into a number, I'll bet you get exceptions when you try to do this. Also, check to see if the parseDouble throws an exception. If it does, you should put a try..catch around it and fall through the loop.

Code:
try {
   doubleInput = Double.parseDouble(userInput); }
catch(Exception e) {
   continue;
}

would be the syntax I beleive.

For large and small to work, I would make a new bool (boolean? I forget. Stupid c++), call it firstRun, and set it to true. Then, right before you do your large and small check, do this:

Code:
if(firstRun) {
  large = doubleInput;
  small = doubleInput;
  firstRun = false;
}

That way you won't have any extra numbers when you do your midpoint calculation.

I also would do the midpoint calculation outside of the loop, you only need to do it once, right before you show it to the user.

Now a tip for just starting with java: USE THE API!! It is incredibly useful to be able and click String and see all of the methods available to you in the string class, with descriptions. It's a lot easier than paging through a textbook saying "What the hell am I even looking for"

Java 1.4 API, can get to it from google by searching for "Java 1.4 API"
 
Originally posted by gubmo:
The code works fine for me, using the values listed in the problem. Does it fail for others?

Edit: NM, 1,2,3,4,5,6 gives me 5000. :D

Edit: I kind of figured it out.

When you set 1, large is 1.0, and small is 9999.0. Midpoint is 5000.
When you enter 2, large is 2.0, _ but small is still 9999.0_. If the number is larger than small, and larger than large, then the current large should be set to the current small, and the current value should become the new large.
Whew! Did you understand that? Much can be learned by entering simple numbers, and watching the variables change in a debugger.

I have no clue how the original example works.

gubmo

[This message was edited by gubmo on April 10, 2002 at 19:57.]


That wont work :) At least if I am understanding you right.... Supposed I put -2 and 2 as my inputs.. -2 is smallest, 2 is largest... lets say I then input 4... 4 becomes largest and -2 is STILL smallest... The was you are saying it would make 4 the new large and 2 the new small... I am in class right now, I will check periodically.
 

gumbo

Ars Tribunus Angusticlavius
6,107
Well, I didn't really think it through - that's for you to do. ;)

Anyway, what I meant was this:

Input 2 and -2.

large would be 2, and small would be -2.

But if I put in 2 and 4, large would be 4, and small would be 9999.

This is because large was 2, and then became 4. What happened to the large=2?
That is the basic problem I can see.

You need to have a solution that accounts for the relative change in places. Note that the given example worked because it contained negative numbers. Once one negative number is input, it seems to correct the problem. Try starting small at 0.

Edit: An untested solution:
Code:
if (doubleInput < large) {
    if (small = 9999) {
        small = large;
    }
}
I didn't test it, but if doubleInput is greater than large, but large is less than small (I would think checking for 999 would work, since you should have a valid value otherwise), then large must be "saveD" in small before it is overwritten.

Edit: My solution won't fix my identified problem, after I clarified it to myself in the bold statement. :) I'll try to find one tonight.

gubmo

[This message was edited by gubmo on April 10, 2002 at 20:51.]

[This message was edited by gubmo on April 10, 2002 at 20:52.]
 
this is where I am now...

Code:
import javax.swing.JOptionPane;

public class Midpoint {

   public static void main(String args[]) {

      	String userInput;
	double doubleInput = 0; 
	double large = doubleInput;
        double small = doubleInput;
	double midpoint;

	userInput = JOptionPane.showInputDialog(
         "Enter a number (type 'Midpoint' or 'm'"+
         "\nfor the midpoint of entered numbers).");

        doubleInput = Double.parseDouble(userInput);
        
	while (!(userInput.equalsIgnoreCase("Midpoint") ||
                   userInput.equalsIgnoreCase("m")) ) {

	doubleInput = Double.parseDouble(userInput);
  
        if (doubleInput > large) {
		
        large = doubleInput;
        }
        else {
            }
            
        if (doubleInput < small){
		
        small = doubleInput;
            }
      
            else {
	userInput = JOptionPane.showInputDialog( "Enter a number (type 'Midpoint' or 'm'"+
        "\nfor the midpoint of entered numbers)."); }
        }

        midpoint = (large + small) / 2;
        
	JOptionPane.showMessageDialog(null, ("The Midpoint is: "+midpoint+"\nLarge = "+large+"\nSmall = "+small ) );

	System.exit(0);
}
}

I added the display of large and small to see my problem... I am recognizing large properly after multiple inputs, but yes, small still = 0.
 

MikeApp

Ars Centurion
257
Subscriptor
Code:
import javax.swing.JOptionPane;

public class Midpoint {

   public static void main(String args[]) {

    String userInput = "";
    Double doubleInput = null;  
    Double large = null;
    Double small = null;
    double midpoint;

	while (true)  {
        
  userInput = JOptionPane.showInputDialog( "Enter a number (type 'Midpoint' or 'm'"+
        	"\nfor the midpoint of entered numbers).");

   if ( userInput.equals("m") || userInput.equals("Midpoint") ) {
		 		break;  // Exit the loop.
		 }

		 try {
			doubleInput = Double.valueOf(userInput);
  
        	if ( (large == null)  || (doubleInput.compareTo(large) > 0) ) {
        		large = doubleInput;
        	}

            
              if ( (small == null) || (doubleInput.compareTo(small) < 0)  ){
       			  small = doubleInput;
	      }
       }	catch (NumberFormatException nfe) {
				System.err.println(nfe);
       }
      

      }

      midpoint = (large.doubleValue() + small.doubleValue()) / 2;   
      JOptionPane.showMessageDialog(null, ("The Midpoint is: "+midpoint+"\nLarge = "+large+"\nSmall = "+small ) );
      System.exit(0);
	}
}

Edit: this doesn't account for someone entering 'm' on the first go, so you would need to check for this condition ( large==null ) when you assign midpoint and display an error message.
 
Sorry been so long since I've been on, moving sucks!

Anyways, I solved the problem, here is the code if anyone cares...

Code:
import javax.swing.JOptionPane;

public class Midpoint {

   public static void main(String args[]) {

      	String userInput;
	double doubleInput = 0; 
	double large;
        double small;
	double midpoint;
	double doubleInput2 = 0;

	userInput = JOptionPane.showInputDialog(
         "Enter a number (type 'Midpoint' or 'm'"+
         "\nfor the midpoint of entered numbers).");

        doubleInput = Double.parseDouble(userInput);
        
	large = doubleInput;

	small = doubleInput;

	while (!(userInput.equalsIgnoreCase("Midpoint") ||
                   userInput.equalsIgnoreCase("m")) ) {

	doubleInput = Double.parseDouble(userInput);
        
        if (doubleInput > large) {
		
        large = doubleInput;
        }
        else {
            }
            
        doubleInput2 = doubleInput;
        
        if (doubleInput2 < small){
		
        small = doubleInput2;
        
            }
      
            else {
            }

	userInput = JOptionPane.showInputDialog( "Enter a number (type 'Midpoint' or 'm'"+
        "\nfor the midpoint of entered numbers)."); 
        }

        midpoint = (large + small) / 2;
        
	JOptionPane.showMessageDialog(null, ("The Midpoint is: "+midpoint+"\nLarge = "+large+"\nSmall = "+small ) );

	System.exit(0);
}
}
 

j3

Ars Tribunus Militum
2,226
I guess you already solved it, but for referece...

Am I the only one who sees this screaming for a linked list insertion sort? It would be totally easy to code (I'm sure I have about 10 iterations of a similar concept) and easy to follow.

-j3

EDIT:
Ummm, ok, I just bothered to look at the actual "midpoint" operation. It is just the largest + smallest over 2? That is pretty simplistic. I guess a list would be overkill.
 
Status
Not open for further replies.