0

I am trying to make class that sets the time using three integer variables for amount of hours, minutes, and seconds, and a fourth Boolean variable to set AM or PM. I am able to get the three integer variables to work correctly, but I don't know what I am doing wrong regarding the Boolean variable not being assigned or tested correctly.

public class Time
{
    private int hour;
    private int minute;
    private int second;
    private boolean amPm;

    public Time(int setHour, int setMinute, int setSecond, boolean setAmPm)
    {
        hour = setHour;
        minute = setMinute;
        second = setSecond;
        amPm = setAmPm;
    }

    public String toString()
    {
        if (amPm == true)
        {
            String halfDay = "P.M.";
        }
        else
        {
            String halfDay = "A.M.";
        }
        return hour + ":" + minute + ":" + second + " " + halfDay + ".";
    }
}
arh012
  • 29
  • 3

5 Answers5

3

You can't use halfDay as it's out of the scope

In computer programming, the scope of a name binding – an association of a name to an entity, such as a variable – is the region of a computer program where the binding is valid: where the name can be used to refer to the entity. Such a region is referred to as a scope block. In other parts of the program the name may refer to a different entity (it may have a different binding), or to nothing at all (it may be unbound).

In other world, you declare halfDay in the if/else statement, but it disapear once you close the brackets. If you want to use it for your concatenation, you need to change his scope, by declaring it before.

public class Time
{
    private int hour;
    private int minute;
    private int second;
    private boolean amPm;

    public Time(int setHour, int setMinute, int setSecond, boolean setAmPm)
    {
        hour = setHour;
        minute = setMinute;
        second = setSecond;
        amPm = setAmPm;
    }

    public String toString()
    {
        String halfDay;
        if (amPm == true)
        {
            halfDay = "P.M.";
        }
        else
        {
            halfDay = "A.M.";
        }
        return hour + ":" + minute + ":" + second + " " + halfDay + ".";
    }
}
Kepotx
  • 1,095
  • 12
  • 26
2

The issue you've come across here is one of scoping: You are initializing the halfDay String inside the if/else statement in the toString() function, but it needs to be available outside of that to be used in the return statement.

Your objective can be achieved using a ternary operator like so:

@Override // Overriding the toString() method from Object
public String toString() {
  return hour + ":" + 
    minute + ":" + 
    second + " " + 
    (amPm ? "P.M" : "A.M") + "."
}
A. Bandtock
  • 1,233
  • 8
  • 14
1

The boolean is fine, the halfDay variable doesn't have scope. Give it scope. Like,

public String toString()
{
    String halfDay;
    if (amPm)
    {
        halfDay = "P.M.";
    }
    else
    {
        halfDay = "A.M.";
    }
    return hour + ":" + minute + ":" + second + " " + halfDay + ".";
}

But, I would prefer to not use such a temporary variable. You could do,

public String toString()
{
    if (amPm)
    {
        return hour + ":" + minute + ":" + second + " P.M..";
    }
    return hour + ":" + minute + ":" + second + " A.M..";
}

or with a ternary and a formatted String. Like

public String toString()
{
    return String.format("%d:%d:%d %s.", hour, minute, second, amPm ? "P.M." : "A.M.");
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Simplified boolean check and changed scope of halfDay:

public Time(int setHour, int setMinute, int setSecond, boolean setAmPm)
    {
        hour = setHour;
        minute = setMinute;
        second = setSecond;
        amPm = setAmPm;
    }

    public String toString()
    {
        String halfDay = "A.M.";
        if (amPm)
        {
            halfDay = "P.M.";
        }
        return hour + ":" + minute + ":" + second + " " + halfDay + ".";
    }
0
public String toString()
{

    String halfDay = "";
    if (amPm == true)
    {
        halfDay = "P.M.";
    }
    else
    {
        halfDay = "A.M.";
    }
    return hour + ":" + minute + ":" + second + " " + halfDay + ".";
}

Do this. Your halfDay variable isn't in the scope of your return.

Subbies
  • 246
  • 2
  • 8