+ Reply to Thread
Results 1 to 16 of 16

Thread: Need java help...     submit to reddit submit to twitter

  1. #1

    Need java help...

    Have a bit of a question about java if anyone knows.

    This is the problem that I have to do:

    "Many devices in a manufacturing plant are controlled by computers. One example is a valve. Based on certain inputs, a computer program will calculate a new valve position(how far open it should be) and send a signal to the valve to adjust it. A problem would arise though if the signal called for the valve to open or close more then it could. To prevent this, these programs often contain a "clamp" method that accepts an input value and then "clamps it between an upper and lower limit. The safe, clamped value is then sent to the valve. In this exercise, you are going to create a "Clamp" class that performs this clamping operation.

    The Clamp class will need 3 attributes: upper limit, lower limit, and current position. The class should also have two methods: a print method that displays the current valve position, and a setPosition method. The setPosition method should accept a value and check to see if it is within the limits for the valve. If the inputted value is not within the limits, a warning message should be printed out. Otherwise, the position of the valve should be set to the inputted value.

    Use the lower and upper limits of 0 and 100, respectively. In your ClampTest class, create one object of the Clamp class and use it 3 consecutive times. Each time print out a message that states what you're attempting to do, use the object's setPosition method, and then use the object's print method to see what happened."




    The problem I'm getting as is the creating the methods in the Clamp class. I don't know what I should input for the return statement for this. This is what I have so far:

    public class Clamp
    {
    double upperLimit = 100.00;
    double lowerLimit = 0.00;
    double currentPosition;
    double valveValue;

    public double setPosition()
    {
    if(valveValue < lowerLimit)
    {
    System.out.println("***WARNING*** The specified value of "+ valveValue +" exceeds the lower limit of "+ lowerLimit);
    }
    if(valveValue > upperLimit)
    {
    System.out.println("***WARNING*** The specified value of "+ valveValue +" exceeds the upper limit of " +upperLimit);
    }
    else
    {
    currentPosition = valveValue;
    }
    }
    public String printValue()
    {
    return("The current valve position is "+ currentPosition);
    }
    }



    Would appreciate any help I could get on this.

  2. #2

    Can you just add numbers directly into a string like that without any kind of other operators?

    I know C++, but have never ever compiled anything Java, so I don't know. Just maybe someplace to start looking.

  3. #3

    Well, I have to do it as the directions say to do. I could do this a lot easier if I did it all in the test class, but I'm just confused on what kind of return statement I could use for these methods.

  4. #4

    registered just to reply to this

    well you have a few problems with your code, so first i'll basically just give you the code you need and explain it afterwards.

    Code:
    public class Clamp
    {
       double upperLimit = 100.00;
       double lowerLimit = 0.00;
       double currentPosition;
    
       public void setPosition(double valveValue)
       {
          if(valveValue < lowerLimit)
          {
             System.out.println("***WARNING*** The specified value of "+ valveValue +" exceeds the lower limit of "+ lowerLimit);
          }
          else if(valveValue > upperLimit)
          {
             System.out.println("***WARNING*** The specified value of "+ valveValue +" exceeds the upper limit of " +upperLimit);
          }
          else
          {
             currentPosition = valveValue;
          }
       }
    
       public void printValue()
       {
          System.out.println("The current valve position is "+ currentPosition);
       }
    }
    first problem is with setPosition(). you'll put a double before a method name when you are returning a double, and an int before the name when returning an int, ect. but you won't be returning any doubles in setPosition(), in fact you won't be returning anything. what i assumed you were trying to do with that is accept a variable when using the method, in which case you would need to declare a variable within the parantheses of setPosition() as i put. now to use setPosition() later on, you would do something like this:

    Code:
    setPostion(95.5);
    this means that the valveValue variable will be 95.5, and currentPosition will be set to 95.5. so there is no need to declare valveValue at the beginning of the class.

    MOVING ON, the reason that there is an else if() instead of a regular if() is because when you use else below the second if(), it does not take the first if() into consideration. meaning that if valveValue is below lowerLimit, it will display the warning message AND set currentPosition to it. another solution to this instead of using else if() would be to add return; after the warning message.

    almost there. in printValue(), you aren't returning a String, even though it seems like it. you actually won't be returning anything yet again, so we set the method to void. and since we won't be returning anything, that return is useless, so throw that out and just put in a simple output.

    the last paragraph of the question confuses me to all hell, maybe because i haven't seen ClampTest.

    hope this helps ya :D

    Edit: read your second post about using return statements.
    since you're not going to be returning any real values for these methods, just put a simple return;
    here's an example with that first if() statement:

    Code:
          if(valveValue < lowerLimit)
          {
             System.out.println("***WARNING*** The specified value of "+ valveValue +" exceeds the lower limit of "+ lowerLimit);
             return;
          }
    this would keep out that else if() if you think it's ugly or something, otherwise you don't really need any return statements that i can see here.

  5. #5

    Finished that assignment(Although if you could tell me how I could get there to be two 0's after the decimal point instead of one for the output, that'd be helpful. Currently it only shows one 0 after the decimal point in the output, I.E. the user puts in 24 and the screen shows 24.0, and I'm looking for 24.00), although stuck on the last one.

    Problem states:


    "In this exercise you will create a TicTacToe game using an array to represent the board. Even though the board is 2-dimensional, we can still use a 1-dimensional array to represent it as you will see. Create a TicTacToe class and give it one attribute that is a 1-dimensional array of char. Create an init method that creates a 9 element array for this attribute and initializes each element to a blank space. Create a second method named SetSquare that accepts the index number of a square and a character to place there. This method should check to see if a valid number has been entered for the square(0-8) and whether the square has already been filled. If either of those is true, the player loses their turn. If good values have been passed in, the square should be set to the character that was passed in. Finally, create a third method called display. This method should print out the game board.

    This is what I have so far. I just feel like I'm going in the completely wrong direction for this and I'm clueless as to how to fix it(besides that it is incomplete code).

    Code:
    public class TicTacToe
    {
    	public int gameBoard;
    	public int endNumber = 8;
    	
    	public void init()
    	{
    		int[][]gameBoard = new int[3][3];
    	}
    	public void setSquare(int squareNumber, int player)
    	{
    		if((squareNumber < 0)&&(squareNumber > endNumber))
    		{
    			System.out.println("Bad square--lose turn");
    		}
    		if((squareNumber == 0)&&(player == 1))
    		{
    			gameBoard[0,0]=X;
    		}
    		if((squareNumber == 1)&&(player == 1))
    		{
    			gameBoard[0,1]=X;
    		}
    		if((squareNumber == 2)&&(player == 1))
    		{
    			gameBoard[0,2]=X;
    		}
    		if((squareNumber == 3)&&(player == 1))
    		{
    			gameBoard[1,0]=X;
    		}
    		if((squareNumber == 4)&&(player == 1))
    		{
    			gameBoard[1,1]=X;
    		}
    		if((squareNumber == 5)&&(player == 1))
    		{
    			gameBoard[1,2]=X;
    		}
    		if((squareNumber == 6)&&(player == 1))
    		{
    			gameBoard[2,0]=X;
    		}
    		if((squareNumber == 7)&&(player == 1))
    		{
    			gameBoard[2,1]=X;
    		}
    		if((squareNumber == 8)&&(player == 1))
    		{
    			gameBoard[2,2]=X;
    		}
    		if((squareNumber == 0)&&(player == 2))
    		{
    			gameBoard[0,0]=O;
    		}
    		if((squareNumber == 1)&&(player == 2))
    		{
    			gameBoard[0,1]=O;
    		}
    		if((squareNumber == 2)&&(player == 2))
    		{
    			gameBoard[0,2]=O;
    		}
    		if((squareNumber == 3)&&(player == 2))
    		{
    			gameBoard[1,0]=O;
    		}
    		if((squareNumber == 4)&&(player == 2))
    		{
    			gameBoard[1,1]=O;
    		}
    		if((squareNumber == 5)&&(player == 2))
    		{
    			gameBoard[1,2]=O;
    		}
    		if((squareNumber == 6)&&(player == 2))
    		{
    			gameBoard[2,0]=O;
    		}
    		if((squareNumber == 7)&&(player == 2))
    		{
    			gameBoard[2,1]=O;
    		}
    		if((squareNumber == 8)&&(player == 2))
    		{
    			gameBoard[2,2]=O;
    		}

  6. #6

    ..... don't toss all your issues with not being able to do your assignments on here. -.-

  7. #7

    Do your own homework ><

  8. #8

    Hey hey, I was just asking for advice. If I was asking you to do my homework I'd just post the problem and then ask for you to do the work.

  9. #9

    Have you even tried to compile that code? Honestly. Syntax errors out the yingyang; I don't even have to compile it myself and I can see that. That tells me that you're not even trying. You're just posting here asking "help me".

    Let me help you by bolding what you seem to have missed in the problem description:

    "In this exercise you will create a TicTacToe game using an array to represent the board. Even though the board is 2-dimensional, we can still use a 1-dimensional array to represent it as you will see. Create a TicTacToe class and give it one attribute that is a 1-dimensional array of char. Create an init method that creates a 9 element array for this attribute and initializes each element to a blank space. Create a second method named SetSquare that accepts the index number of a square and a character to place there. This method should check to see if a valid number has been entered for the square(0-8) and whether the square has already been filled. If either of those is true, the player loses their turn. If good values have been passed in, the square should be set to the character that was passed in. Finally, create a third method called display. This method should print out the game board."

  10. #10

    well i got no life so i'm gonna scab and help.

    2 decimal places:
    Code:
    NumberFormat f = new DecimalFormat("0.00");
    System.out.println(f.format(valveValue));
    whenever you want to output valveValue with 2 decimal places, use that there (f.format(valveValue)). you're going to need to import a class though, java.text.DecimalFormat.

    this assignment's so confusing. dear god. aurik was right about the highlighting there, you put a 2-dimensional array instead of 1-dimensional like it asks. it also says that it wants the array for the class itself, so you're gonna wanna declare it at the class, and that init() method has errors all ova the place anyway. so the beginning of the document should look like this: (note: i am no expert in java particularly, so i'm a little rusty with arrays myself)
    Code:
    public class TicTacToe
    {
       public int[] gameBoard;
    
       public void init()
       {
          gameBoard = new int[9];
       }
    now... i'm just gonna put what i'd do for the second and third method:
    Code:
       public void setSquare(int squareNumber, int insert)
       {
          if (insert > 9 && insert < 0)
          {
             System.out.println (insert + " is not a valid number! You lose a turn!");
             return;
          }
          if (gameBoard[squareNumber] != 0)
          {
             System.out.println ("The " + squareNumber + " square is already filled! You lose a turn!");
             return;
          }
    
          gameBoard[squareNumber] = insert;
       }
    
       public void display()
       {
          System.out.println (gameBoard[0] + " " + gameBoard[1] + " " + gameBoard[2]);
          System.out.println (gameBoard[3] + " " + gameBoard[4] + " " + gameBoard[5]);
          System.out.println (gameBoard[6] + " " + gameBoard[7] + " " + gameBoard[8]);
       }
    i'll be honest here, i'm not even 100% that this is going to work with your assignment here, because it seems like i haven't seen the whole assignment. you can add any any input you need from the user which it seems like you'll need.

    i compiled my code, no errors, but i don't have time to make my own example now

  11. #11

    Way to contribute to his education by giving him the answer without making him think.

    BTW, it should be an array of characters, not ints, even though it makes sense to encode it as a number.

  12. #12
    Salvage Bans
    Join Date
    Apr 2005
    Posts
    797
    BG Level
    5
    FFXI Server
    Lakshmi

    You could use printf instead of decimal format which is way lot easier then again this applies for java 1.5 and above only if you're going to use printf

  13. #13

    Quote Originally Posted by aurik
    Way to contribute to his education by giving him the answer without making him think.

    BTW, it should be an array of characters, not ints, even though it makes sense to encode it as a number.
    If it makes you feel any better, I'm going to a tutor on this material since I have to do more work on it.

    And thank you Haelian. I normally wouldn't ask for help on this, but the deadline for it was coming really close to submit it.

  14. #14

    If you're having trouble with this stuff, no offense, but drop the course.

  15. #15

    Quote Originally Posted by aurik
    If you're having trouble with this stuff, no offense, but drop the course.
    drop a course just because they don't understand it at first? it's obvious that he's just starting on the fundamentals, everyone would have the same difficulty. really bad motto there, quit at the first sign of hardship.

    i wouldn't really condone getting help like this but i like to help with stuff, who doesn't?

    edit: and oh ya the assignment asked for char not int... oh well, shouldn't matter.

  16. #16

    Aurik, I started out shaky at the beginning of the quarter. I fully believe I am capable of learning all this material and I have already learned a great deal. I refuse to drop any of my courses right now. I don't intend to ask for help like this on a regular basis. My schedule got very messed up the past few days which put me in a very bad predicament with getting this work in, which is why I asked for help on it here. And thanks again Haelian.

Similar Threads

  1. Hey computer builder people, need some help. ;(
    By Anyway in forum General Discussion
    Replies: 8
    Last Post: 2006-05-02, 13:00
  2. need math help again :D
    By untouchable in forum General Discussion
    Replies: 11
    Last Post: 2006-02-28, 20:34
  3. need math help part2
    By untouchable in forum General Discussion
    Replies: 12
    Last Post: 2006-02-01, 12:58
  4. I need some help finding a link...
    By Ninjadik in forum General Discussion
    Replies: 5
    Last Post: 2005-11-04, 21:53
  5. Need more help with my project
    By Amarao in forum General Discussion
    Replies: 17
    Last Post: 2005-09-26, 10:05
  6. Hmmm, need some help
    By Demosthenes11 in forum General Discussion
    Replies: 23
    Last Post: 2005-07-01, 18:50
  7. Need some help~
    By Ankiseth in forum General Discussion
    Replies: 23
    Last Post: 2005-06-11, 02:31
  8. Liltwin I need Some Help From u In BOneCrafting ^^ PLease
    By viper in forum General Discussion
    Replies: 2
    Last Post: 2005-04-10, 05:50
  9. Ondori, I need your help...
    By Antura in forum General Discussion
    Replies: 2
    Last Post: 2005-04-01, 16:33