Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help adding in the AM or PM statement in this toString method. Any ideas

ID: 3634660 • Letter: I

Question

I need help adding in the AM or PM statement in this toString method. Any ideas or suggestions on how to set it up so "AM" or "PM" is returned is much appreciated.

/*
* Time.java
*
* A blueprint class to represent one of the times
* maintained by the current time or alarm clock time.
*
* Melanie Famiglietti
* mmf333@bu.edu
*
*/
public class Time {
//fields
private int currentHour;
private int currentMinute;
private boolean amOrPm;

/*
* A constructor with three parameters
*/
public Time(int currentHour, int currentMinute, boolean amOrPm) {
if (currentHour < 1 || currentHour > 12) {
throw new IllegalArgumentException("invalid hour: " + currentHour);
}
this.currentHour = currentHour;

if (currentMinute < 0 || currentMinute > 59) {
throw new IllegalArgumentException("invalid Minute: " + currentMinute);
}
this.currentMinute = currentMinute;


this.amOrPm = amOrPm;
}
/*
* getHour-returns the hour component of a Time object.
*/
public int getHour() {
return this.currentHour;
}
/*
* getMinute- returns the minute component of a Time object.
*/
public int getMinute() {
return this.currentMinute;
}
/*
* isAm- returns true if the Time is AM, and false otherwise.
*/
public boolean isAM() {
if(currentHour< 12){
return (this.amOrPm);
}else{
return false;
}

}

/*
* advanceHour- advances the Time by 1 hour.
*/
public void advanceHour() {
this.currentHour +=1;
if (currentHour > 12) {
this.currentHour = 1;
this.isAM();
}
}
/*
* advanceMinute- advances the Time by 1 minute.
*/
public void advanceMinute() {
this.currentMinute += 1;
if(currentMinute > 59) {
this.currentMinute=0;
advanceHour();
this.isAM();
}
}
/*
* advanceMinute- takes a parameter specifying a number
* of minutes and advances Time by that number of minutes
*/
public void advanceMinute(int tMinute) {
this.currentMinute += tMinute;
if(currentMinute > 59) {
this.currentMinute= (tMinute-60);
advanceHour();
this.isAM();
if(currentMinute < 0) {
this.currentMinute = 0;
this.isAM();
}
}
}
/*
* equals- returns true if the calling object has the same
* components (hour, minute, and AM/PM) as the obeject
* represented by the parameter, and false otherwise.
*/
public boolean equals(Time other) {
return (other != null
&& this.currentHour == other.currentHour
&& this.currentMinute == other.currentMinute
&& this.amOrPm == other.amOrPm);
}
/*
* toString- returns a String of the form HH:MM,
* followed by whether AM or PM.
*/
public String toString() {
String str= "";
if(currentHour < 10 ) {
return this.currentHour + "" + ":" + this.currentMinute +" AM" ;
}

if(currentMinute < 10) {
return this.currentHour + ":" + "0" + this.currentMinute;
} else {
return this.currentHour + ":" + this.currentMinute + " PM";



}
}
}

Explanation / Answer

Dear,

class Time {
//fields
private int currentHour;
private int currentMinute;
private boolean amOrPm;

/*
* A constructor with three parameters
*/
public Time(int currentHour, int currentMinute,boolean amOrPm)
{
if (currentHour < 1 || currentHour > 12)
throw new IllegalArgumentException("invald hour: " + currentHour);

if (currentMinute < 0 || currentMinute > 59)
throw new IllegalArgumentException("invalid Minute: " + currentMinute);

this.currentHour = currentHour;
this.currentMinute = currentMinute;
this.amOrPm=amOrPm;

}


/*
* getHour-returns the hour component of a Time object.
*/
public int getHour() {
return this.currentHour;
}
/*
* getMinute- returns the minute component of a Time object.
*/
public int getMinute() {
return this.currentMinute;
}
/*
* isAm- returns true if the Time is AM, and false otherwise.
*/
public boolean isAM()
{
if(currentHour< 12){
return (this.amOrPm);
}
else{
return false;
}
}

/*
* advanceHour- advances the Time by 1 hour.
*/
public void advanceHour() {
this.currentHour +=1;
if (currentHour > 12) {
this.currentHour = 1;
this.isAM();
}
}
/*
* advanceMinute- advances the Time by 1 minute.
*/
public void advanceMinute() {
this.currentMinute += 1;
if(currentMinute > 59) {
this.currentMinute=0;
advanceHour();
this.isAM();
}
}
/*
* advanceMinute- takes a parameter specifying a number
* of minutes and advances Time by that number of minutes
*/
public void advanceMinute(int tMinute) {
this.currentMinute += tMinute;
if(currentMinute > 59) {
this.currentMinute= (tMinute-60);
advanceHour();
this.isAM();
if(currentMinute < 0) {
this.currentMinute = 0;
this.isAM();
}
}
}
/*
* equals- returns true if the calling object has the same
* components (hour, minute, and AM/PM) as the obeject
* represented by the parameter, and false otherwise.
*/
public boolean equals(Time other) {
return (other != null
&& this.currentHour == other.currentHour
&& this.currentMinute == other.currentMinute
&& this.amOrPm == other.amOrPm);
}
/*
* toString- returns a String of the form HH:MM,
* followed by whether AM or PM.
*/
public String toString() {
String str= "";
if(currentHour < 12 )
str=this.currentHour + "" + ":" + this.currentMinute +" AM";
if(currentHour >12)
str=this.currentHour + ":" + this.currentMinute + " PM";
return str;
}

}
public class TimeDriver {

public static void main(String[] args)
{
Time time1=new Time(5,59,true);
Time time2=new Time(5,59,true);
System.out.println(time1);
System.out.println(time2);
if(time1.isAM())
System.out.print("AM");
else
System.out.print("PM");

if(time1.equals(time2))
System.out.println("Equal");
else
System.out.println("Not -Equal");

}

}

Hope this would helpful to you...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote