A new class is defined as follows: public class TimeSpan { private int hours; //
ID: 3909759 • Letter: A
Question
A new class is defined as follows:public class TimeSpan { private int hours; //can be any non-negative numbers private int minutes; //must be in the range [0, 60)
public TimeSpan(int hours, int minutes) { this.hours = 0; this.minutes = 0; add(hours, minutes); } public void add(int hours, int minutes) { //for negative numbers, set to 0 //you are required to implement this method } public int getHours() { return hours;} public int getMinutes() { return minutes;} public String toString() { return hours + "h" + minutes + "m"; // Returns a String for this time span such as "6h15m". } }
Please implement the add method so hours and minutes are set properly. For example: TimeSpan ts=new TimeSpan(0, 90); ts.getHours() should return 1, and ts.getMinutes() should return 30.
You are required to implement public void add(int hours, int minutes) method only.
A new class is defined as follows:
public class TimeSpan { private int hours; //can be any non-negative numbers private int minutes; //must be in the range [0, 60)
public TimeSpan(int hours, int minutes) { this.hours = 0; this.minutes = 0; add(hours, minutes); } public void add(int hours, int minutes) { //for negative numbers, set to 0 //you are required to implement this method } public int getHours() { return hours;} public int getMinutes() { return minutes;} public String toString() { return hours + "h" + minutes + "m"; // Returns a String for this time span such as "6h15m". } }
Please implement the add method so hours and minutes are set properly. For example: TimeSpan ts=new TimeSpan(0, 90); ts.getHours() should return 1, and ts.getMinutes() should return 30.
You are required to implement public void add(int hours, int minutes) method only.
A new class is defined as follows:
public class TimeSpan { private int hours; //can be any non-negative numbers private int minutes; //must be in the range [0, 60)
public TimeSpan(int hours, int minutes) { this.hours = 0; this.minutes = 0; add(hours, minutes); } public void add(int hours, int minutes) { //for negative numbers, set to 0 //you are required to implement this method } public int getHours() { return hours;} public int getMinutes() { return minutes;} public String toString() { return hours + "h" + minutes + "m"; // Returns a String for this time span such as "6h15m". } }
Please implement the add method so hours and minutes are set properly. For example: TimeSpan ts=new TimeSpan(0, 90); ts.getHours() should return 1, and ts.getMinutes() should return 30.
You are required to implement public void add(int hours, int minutes) method only.
Explanation / Answer
public class TimeSpan {
private int hours; //can be any non-negative numbers
private int minutes; //must be in the range [0, 60)
public TimeSpan(int hours, int minutes) {
this.hours = 0;
this.minutes = 0;
add(hours, minutes);
}
public void add(int hours, int minutes)
{
this.hours = ( hours < 0 ? 1 : this.hours);
this.hours += hours;
this.minute += minute;
this.hours += this.minutes / 60;
this.minute += this.minutes % 60;
}
public int getHours()
{ return hours;}
public int getMinutes()
{ return minutes;}
public String toString() {
return hours + "h" + minutes + "m"; // Returns a String for this time span such as "6h15m".
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.