cwass191
cwass191

Reputation: 11

calling a set value method from another class

I'm creating a four digit class (like a clock) while calling methods from a two digit class.

The four digit class has two segments. When segment one reaches my set maximum value, the second segment must increase by one.

these are my methods from my first class:

/*
 * method to set the value
 */
public void setValue(int anyValue){
    if((anyValue < TOO_HIGH) && (anyValue >= 0)){
        value =  anyValue;}
}

/*
 * method to set the value plus one
 */
public void setValuePlusOne(){
    int tempValue = value + 1;
    if(tempValue < TOO_HIGH){
        value = tempValue;}
    else{
        value = 0;

        // value = tempValue % TOO_HIGH;}

    }

This is from my second four digit class.

/*
 * method to set the increment
 */
public void setIncrement(int increment){
    rightSegment.setValuePlusOne();
    if(increment == 0)
        leftSegment.setValuePlusOne();  

    }

I think there might be something wrong with my increment == 0. It doesn't compile when I try if(rightsegment.setValuePlusOne()==0)

any advise would help. Thank you!!

Upvotes: 1

Views: 315

Answers (2)

Shuvajit Ghosh
Shuvajit Ghosh

Reputation: 108

Please try the code below. Hope the below code will help you to achieve your implementation. Instead of setting the TOO_HIGH integer value in the if else block of the below given code, you can set it in the RightSegment and LeftSegment classes respectively which is extending the Clock class. Thanks

package stackoverflow;

public class Clock {

private int value;
private int TOO_HIGH;
private Clock rightSegment;
private Clock leftSegment;


/*
 * method to set the value
 */
public void setValue(int anyValue, String position){
    if(position.equals("right")){
        TOO_HIGH = 60;
    }else if(position.equals("left")){
        TOO_HIGH = 13;
    }

    if((anyValue < TOO_HIGH) && (anyValue >= 0)){
        value =  anyValue;}
}

/*
 * method to set the value plus one
 */
public void setValuePlusOne(){
    int tempValue = value + 1;
    if(tempValue < TOO_HIGH){
        value = tempValue;}
    else{
        value = 0;
    }

        // value = tempValue % TOO_HIGH;}

    }


    /*
     * method to set the i`ncrement
     */
    public void setIncrement(int increment, Clock right, Clock left){
        rightSegment = right;
        leftSegment = left;
        //rightSegment = new Clock();
        //leftSegment = new Clock();
        rightSegment.setValuePlusOne();
        if(increment == 0){
            leftSegment.setValuePlusOne();  
        }

    }

    public static void main (String args[]){
        Clock clock = new Clock();
        clock.rightSegment = new Clock();
        clock.leftSegment  = new Clock();
        clock.rightSegment.setValue(12, "right");
        clock.leftSegment.setValue(12, "left");
        clock.rightSegment.setIncrement(0, clock.rightSegment, clock.leftSegment);
        System.out.println("Time "+clock.leftSegment.value+":"+clock.rightSegment.value);
    }

}

Upvotes: 0

Jeremy Hanlon
Jeremy Hanlon

Reputation: 317

setValuePlusOne(...) does not return anything. Call setValuePlusOne before the if and then use (rightsegment.getValue() == 0) for the if.

Upvotes: 1

Related Questions