Skip to content

Instantly share code, notes, and snippets.

@rootux
Created January 30, 2016 18:34
Show Gist options
  • Save rootux/69b4909a090ceae2eb35 to your computer and use it in GitHub Desktop.
Save rootux/69b4909a090ceae2eb35 to your computer and use it in GitHub Desktop.
/**
* Write a description of class BusArrival here.
*
* @author (your name)
* @version (a version number or a date)
*/
/*# Joni: no API documentation */
public class BusArrival
{
// instance variables - replace the example below with your own
private int _lineNumber;
private Time1 _arrivalTime;
private int _noOfPassengers;
/*# Joni: final names should be uppercsae words separated by underscore character '_',
for example
private final int MAX_PASS = 70;
*/
private final int _defaultLineNumber = 1;
private final int _defaultNoOfPassengers = 0;
private final int _maxPass = 70;
private final int _maxLine = 99;
/**
* Constructor for objects of class BusArrival
*/
public BusArrival(int lineNum, int pass, int h, int m, int s)
{
// initialise instance variables
_arrivalTime = new Time1(h,m,s);
initLinePass(lineNum, pass);
}
public BusArrival(int lineNum, int pass, Time1 t)
{
_arrivalTime = new Time1(t);
initLinePass(lineNum, pass);
}
public BusArrival(BusArrival other)
{
_arrivalTime = new Time1(other.getArrivalTime());
_lineNumber = other.getLineNum();
_noOfPassengers = other.getNoOfPass();
}
// constracor helper methods
private boolean passValid(int pass)
{
if ((pass>=0) && (pass <= _maxPass))
{
return true;
}
else
{
return false;
}
}
private boolean lineNumValid(int lineNum)
{
if ((lineNum>=1) && (lineNum <= _maxLine))
{
return true;
}
else
{
return false;
}
}
private void initLinePass(int lineNum, int pass)
{
if (passValid(pass))
{
_noOfPassengers = pass;
}
else
{
_noOfPassengers = _defaultNoOfPassengers;
}
if (lineNumValid(lineNum))
{
_lineNumber = lineNum;
}
else
{
_lineNumber = _defaultLineNumber;
}
}
// Set methods
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public Time1 getArrivalTime()
{
// put your code here
/*# Joni: aliasing */
// return _arrivalTime;
return new Time1(_arrivalTime);
}
public int getLineNum()
{
return _lineNumber;
}
public int getNoOfPass()
{
return _noOfPassengers;
}
// set methods
public void setArrivalTime(Time1 t)
{
_arrivalTime = new Time1(t);
}
public void setLineNum(int num)
{
if (lineNumValid(num))
{
_lineNumber = num;
}
}
public void setNoOfPass (int num)
{
if (passValid(num))
{
_noOfPassengers = num;
}
}
public boolean equals(BusArrival other)
{
return ((_noOfPassengers == other.getNoOfPass()) && (_lineNumber == other.getLineNum()) && (_arrivalTime.equals(other.getArrivalTime())));
}
public String toString()
{
/*# Joni: no need to call String.valueOf(); it is also not allowed */
return ("Bus no. " + String.valueOf(_lineNumber) + " arrived at " + _arrivalTime.toString() + " with " + String.valueOf(_noOfPassengers) + " passengers");
}
public boolean fuller (BusArrival other)
{
return (_noOfPassengers > other.getNoOfPass());
}
public boolean before (BusArrival other)
{
return (_arrivalTime.before(other.getArrivalTime()));
}
public boolean isFull()
{
return (_noOfPassengers == _maxPass);
}
public int elapsedTime (BusArrival other)
{
int deltaTime;
if (before(other))
{
Time1 otherT = other.getArrivalTime();
deltaTime = otherT.difference(_arrivalTime);
}
else
{
deltaTime = _arrivalTime.difference(other.getArrivalTime());
}
return (int)(deltaTime / 60);
}
}
/**
* Write a description of class Time1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
/*# Joni: no API documentation */
public class Time1
{
/*# Joni: missing finals:
private final int HOURS_IN_DAY = 24;
private final int MINUTES_IN_HOUR = 60;
private final int SEC_IN_MINUTE = 60;
*/
// instance variables - replace the example below with your own
private int _hour; // This is the Hour value 0-23
private int _minute;
private int _second;
/**
* Constructor for objects of class Time1
*/
public Time1 (int h, int m, int s)
{
// initialise instance variables
if (!trySetHour(h))
{
_hour = 0;
}
if (!trySetMinute(m))
{
_minute = 0;
}
if (!trySetSecond(s))
{
_second = 0;
}
}
public Time1 (Time1 other)
{
_hour = other.getHour();
_minute = other.getMinute();
_second = other.getSecond();
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
private boolean trySetHour(int num)
{
// put your code here
if ((num >=0 ) && (num <= 23))
{
_hour = num;
return true;
}
else
{
return false;
}
}
public void setHour(int num)
{
// put your code here
trySetHour(num);
}
private boolean trySetMinute(int num)
{
// put your code here
if ((num >=0 ) && (num <= 59))
{
_minute = num;
return true;
}
else
{
return false;
}
}
public void setMinute(int num)
{
// put your code here
trySetMinute(num);
}
private boolean trySetSecond(int num)
{
// put your code here
if ((num >=0 ) && (num <= 59))
{
_second = num;
return true;
}
else
{
return false;
}
}
public void setSecond(int num)
{
// put your code here
trySetSecond(num);
}
public int getHour()
{
return _hour;
}
public int getMinute()
{
return _minute;
}
public int getSecond()
{
return _second;
}
private String myIntToString(int num)
{
String numStr = "";
if (num < 10)
{
numStr = "0";
}
numStr = numStr + String.valueOf(num);
return numStr;
}
public String toString()
{
String my_time_str = myIntToString(_hour) + ":" + myIntToString(_minute) + ":" + myIntToString(_second);
return my_time_str;
}
public long secFromMidnight()
{
return ( ((_hour*60) + _minute) * 60 + _second);
}
public boolean equals (Time1 other)
{
return ( (_hour == other.getHour()) && (_minute == other.getMinute()) && (_second == other.getSecond()));
}
public boolean before (Time1 other)
{
return (secFromMidnight() < other.secFromMidnight());
}
public boolean after (Time1 other)
{
return (other.before(this));
}
public int difference (Time1 other)
{
return ((int)(secFromMidnight() - other.secFromMidnight()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment