Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Minor Problem

SoulsOfBlackSoulsOfBlack Member Posts: 1
Hey

My program basically takes user input (asking by how much to adjust a clamp) and sets the value to whatever the user input is. The thing is, say a person puts in 400 or -300, its supposed to ONLY display a warning message and not replace the current value. So if a user puts in 27 for the first time, thats an acceptable value. Then the next time, if the user puts in 300, I ONLY want a warning message, I do not want the value changed to 300 but STAY at 27 (a valid input value). But I am having trouble figuring out how to do this. Where am I going wrong in my programming?

Here is my test file:
public class ClampTest{

public static void main(String[]args){


Clamp clampObject = new Clamp();
double tempClamp;

double currentPosition;
currentPosition=clampObject.getPosition();
System.out.printf("Attempt to set the clamp to a position of: ");
tempClamp = MyInput.readDouble();
clampObject.setPosition(tempClamp);
System.out.printf("Attempt to set the clamp to a position of: ");
tempClamp = MyInput.readDouble();
clampObject.setPosition(tempClamp);
System.out.printf("Attempt to set the clamp to a position of: ");
tempClamp = MyInput.readDouble();
clampObject.setPosition(tempClamp);

currentPosition=clampObject.getPosition();
System.out.println(currentPosition);


}






}

My function class

public class Clamp{

final int UPPER_LIMIT = 100;
final int LOWER_LIMIT = 0;
double currentPosition;

public void setPosition(double newPosition){



if (newPosition > UPPER_LIMIT)
{

System.out.println("*** WARNING *** The specified value of " + newPosition + " exceeds the upper limit of " + UPPER_LIMIT);

}

else if (newPosition < LOWER_LIMIT)
{

System.out.println("*** WARNING *** The specified value of " + newPosition + " exceeds the lower limit of " + LOWER_LIMIT + "
");

}



}

public double getPosition(){


return currentPosition;


}





}

Comments

  • NCS_OneNCS_One Member Posts: 170
    : Hey
    :
    : My program basically takes user input (asking by how much to adjust a clamp) and sets the value to whatever the user input is. The thing is, say a person puts in 400 or -300, its supposed to ONLY display a warning message and not replace the current value. So if a user puts in 27 for the first time, thats an acceptable value. Then the next time, if the user puts in 300, I ONLY want a warning message, I do not want the value changed to 300 but STAY at 27 (a valid input value). But I am having trouble figuring out how to do this. Where am I going wrong in my programming?
    :
    : Here is my test file:
    : public class ClampTest{
    :
    : public static void main(String[]args){
    :
    :
    : Clamp clampObject = new Clamp();
    : double tempClamp;
    :
    : double currentPosition;
    : currentPosition=clampObject.getPosition();
    : System.out.printf("Attempt to set the clamp to a position of: ");
    : tempClamp = MyInput.readDouble();
    : clampObject.setPosition(tempClamp);
    : System.out.printf("Attempt to set the clamp to a position of: ");
    : tempClamp = MyInput.readDouble();
    : clampObject.setPosition(tempClamp);
    : System.out.printf("Attempt to set the clamp to a position of: ");
    : tempClamp = MyInput.readDouble();
    : clampObject.setPosition(tempClamp);
    :
    : currentPosition=clampObject.getPosition();
    : System.out.println(currentPosition);
    :
    :
    : }
    :
    :
    :
    :
    :
    :
    : }
    :
    : My function class
    :
    : public class Clamp{
    :
    : final int UPPER_LIMIT = 100;
    : final int LOWER_LIMIT = 0;
    : double currentPosition;
    :
    : public void setPosition(double newPosition){
    :
    :
    :
    : if (newPosition > UPPER_LIMIT)
    : {
    :
    : System.out.println("*** WARNING *** The specified value of " + newPosition + " exceeds the upper limit of " + UPPER_LIMIT);
    :
    : }
    :
    : else if (newPosition < LOWER_LIMIT)
    : {
    :
    : System.out.println("*** WARNING *** The specified value of " + newPosition + " exceeds the lower limit of " + LOWER_LIMIT + "
    ");
    :
    : }
    :
    :
    :
    : }
    :
    : public double getPosition(){
    :
    :
    : return currentPosition;
    :
    :
    : }
    :
    :
    :
    :
    :
    : }
    :
    :

    Hi

    Try changing this 2 lines:
    [code]
    If ((newPosition+CurrentPosition) > UPPER_LIMIT)
    and
    If ((newPosition+CurrentPosition) < LOWER_LIMIT)
    [/code]
    Note : i dont know whats the starting position but imagin its 0 so CurrentPosition is equal to 0 if user enters 27 then ((27+0) > 300) its false, and you add the value of the number entered by the user to CurrentPosition : CurrentPosition += newPosition, now CurrentPosition is equal to 27, next the user enter 300 then ((300+27) > 300) its true
    so you show the warning window and do not add the newPosition to the CurrentPosition.

    Hope it helps.
    Any more questions just post it here.
  • VilanyeVilanye Member Posts: 684
    currentPosition in Clamp never gets initialized(it defaults to 0), and there is no way to change the value. setPosition, doesn't set it.

    : Hey
    :
    : My program basically takes user input (asking by how much to adjust a clamp) and sets the value to whatever the user input is. The thing is, say a person puts in 400 or -300, its supposed to ONLY display a warning message and not replace the current value. So if a user puts in 27 for the first time, thats an acceptable value. Then the next time, if the user puts in 300, I ONLY want a warning message, I do not want the value changed to 300 but STAY at 27 (a valid input value). But I am having trouble figuring out how to do this. Where am I going wrong in my programming?
    :
    : Here is my test file:
    : public class ClampTest{
    :
    : public static void main(String[]args){
    :
    :
    : Clamp clampObject = new Clamp();
    : double tempClamp;
    :
    : double currentPosition;
    : currentPosition=clampObject.getPosition();
    : System.out.printf("Attempt to set the clamp to a position of: ");
    : tempClamp = MyInput.readDouble();
    : clampObject.setPosition(tempClamp);
    : System.out.printf("Attempt to set the clamp to a position of: ");
    : tempClamp = MyInput.readDouble();
    : clampObject.setPosition(tempClamp);
    : System.out.printf("Attempt to set the clamp to a position of: ");
    : tempClamp = MyInput.readDouble();
    : clampObject.setPosition(tempClamp);
    :
    : currentPosition=clampObject.getPosition();
    : System.out.println(currentPosition);
    :
    :
    : }
    :
    :
    :
    :
    :
    :
    : }
    :
    : My function class
    :
    : public class Clamp{
    :
    : final int UPPER_LIMIT = 100;
    : final int LOWER_LIMIT = 0;
    : double currentPosition;
    :
    : public void setPosition(double newPosition){
    :
    :
    :
    : if (newPosition > UPPER_LIMIT)
    : {
    :
    : System.out.println("*** WARNING *** The specified value of " + newPosition + " exceeds the upper limit of " + UPPER_LIMIT);
    :
    : }
    :
    : else if (newPosition < LOWER_LIMIT)
    : {
    :
    : System.out.println("*** WARNING *** The specified value of " + newPosition + " exceeds the lower limit of " + LOWER_LIMIT + "
    ");
    :
    : }
    :
    :
    :
    : }
    :
    : public double getPosition(){
    :
    :
    : return currentPosition;
    :
    :
    : }
    :
    :
    :
    :
    :
    : }
    :
    :

    [italic][blue]Just my 2 bits[/blue][/italic]

Sign In or Register to comment.