In this assignment, you\'ll write a Java class with instance variables and metho
ID: 3561913 • Letter: I
Question
In this assignment, you'll write a Java class with instance variables and methods, create objects of the class, and invoke methods on them. Your class will provide an equals method to allow comparison of two objects to see if they contain identical values.
Write a Java class called MyTime that represents a 24 hour clock (i.e. ranges from 00:00:00 to 23:59:59). It should have private integer instance variables for hour, minute, and second, and should have the following public methods:
public void setTime(int hour, int minute, int second) - this method will set the object's instance variables to the specified 24 hour time.. It must first test its parameter values for a valid time being supplied by the calling code, according to the following rules:,
If any of the parameters are outside these limits, it must set all three instance variables to zero (representing midnight). If the parameters represent a valid time, then it sets the instance variables to that time.
public int getHour() - this method returns the object's hour value.
public int getMinute() - this method returns the object's minute value.
public int getSecond() - this method returns the object's second value.
public boolean equals(MyTime other) - this method compares the time represented by two MyTime objects. It compares the time represented by this MyTime object with the time represented by the parameter other MyTime object, returning true if they represent the same time. The parameter other is a reference to another MyTime object passed into this method by the calling code. To check for equality, this method must compare the three instance variables of this MyTime object with the three instance variables of the other MyTime object, returning true if all three match, and false otherwise. This method is required to compare the value of two MyTime objects, as the == operator compares the two operand's object references (memory addresses) to see if they refer to the same object (which is not what we want).
public String toString() - this method returns the object's time value formatted as a String of the form hh:mm:ss AM or PM. The minutes and seconds must be two digits (ie. if less than 10, add a leading zero), but the hour doesn't have a leading zero. Call the twoDigits method to convert the minutes and seconds values to strings. The displayed hour is in the range 1 to 12. Midnight is returned as 12:00:00 AM, noon as 12:00:00 PM, 9 AM as 9:00:00 AM, 3 PM as 3:00:00 PM. (Note - there is no 0 AM or 0 PM in this format, it must be 12 AM (midnight) or 12 PM (noon)). This method should not change the instance variables. Use a local variable to hold the hour value that must range from 1 to 12.
public String toUniversalString() - this method returns the object's time value formatted as a String of the form hh:mm:ss in 24 hour format (military time). The hours, minutes, and seconds must all be two digits. Call the twoDigits method to convert each value to a string. Midnight is returned as 00:00:00, noon as 12:00:00, 9 AM as 09:00:00, 3 PM as 15:00:00. This method should not change the instance variables.
private String twoDigits(int val) - this private method returns it's int parameter value formatted as a two digit String (e.g calling it with the value 5 returns the string "05", calling it with the value 20 returns the string "20"). It can use either the String.valueOf(int) method or Integer.toString(int) method to do the conversion. Call twoDigits() from the toString and toUniversalString methods in order to format the individual time fields as two digits where required. This method is made private so it cannot be called by methods in other classes.
Note: the main() method for testing this class is supplied for you in the MyTimeDemo.java file attached to this assignment. Do NOT write your own main() method for this homework.
Sample output
Enter hour, minute, and second (-1 to exit)
10 20 30
Values read were: 10 20 30
10:20:30 AM
10:20:30
hour is 10 minute is 20 second is 30
Enter hour, minute, and second (-1 to exit)
13 5 5
Values read were: 13 5 5
1:05:05 PM
13:05:05
hour is 13 minute is 5 second is 5
Enter hour, minute, and second (-1 to exit)
25 10 20
Values read were: 25 10 20
12:00:00 AM
00:00:00
hour is 0 minute is 0 second is 0
Enter hour, minute, and second (-1 to exit)
-1
Test Data
Use all the following test data, followed by some of your own examples.
10 20 30
1 1 1
13 1 1
0 0 0
23 59 59
12 0 0
6 30 0
24 30 15
12 60 15
12 30 60
MyTimeDemo with main() test code
This homework has two .java files you'll need to download.
Use the attached MyTime.java source file as a starting template for writing your MyTime class. It contains an outline of the class and methods needed, and you'll add the implementation code for each of the methods.
Use the attached MyTimeDemo.java source file to test your completed MyTime.java class. It contains just the supplied main() method test code for testing your MyTime class
Compile your MyTime.java file first, then the MyTimeDemo.java file next.
Note: you must re-compile the MyTimeDemo.java class whenever you change any method signatures (method name, parameters, return type) in your MyTime class.
When using NetBeans (or other IDE) for this homework, you'll need to copy the downloaded files into the project's source folder. Do the following:
Getting Started
Use the attached MyTime.java source file as a starting template for writing your MyTime class. This provides the basic class structure to get you started writing this Java class.
The setTime() method first checks its parameters to see if they represent a valid time. If not, it sets all three of its instance variables to zero and returns. Otherwise, it sets the three instance variables to the time specified by the three parameter variables.
The toString() method must format this MyTime object's 24 hour time value into AM/PM time string. Once again, outline the logic needed to determine the hour and AM/PM value to be returned before you start writing Java code, something like:
if the mHour instance value is 0 or 12,
the formatted hour is 12 (either midnight or noon).
else
the formatted hour is mHour modulo 12
if the mHour instance value is less than 12
the time is AM (00:00:00 is 12 o'clock AM - midnight)
else
the time is PM (12:00:00 is 12 o'clock PM - noon)
Format the string to be returned using the hour value calculated above, the mMinute and mSecond instance values, and the AM/PM calculated above. Call twoDigits() on the mMinute and mSecond values to ensure they are formatted as two digits.
The twoDigits() method is an internal (private) 'helper' method called by the toString() and toUniversalString() methods when formatting the time value into string format. It converts its int parameter value into a string, adds a leading "0" if needed to make the string two digits long, then returns the string.. Again, outline the logic needed, something like:
If the parameter value is less than 10
return "0" concatenated with the parameter value
converted to a string using Integer.toString(val)
else
return the parameter value converted to string
using Integer.toString(val)
It's common for classes to define private methods containing code that several other of their methods call.
Explanation / Answer
please rate - thanks
any questions ask
sample RUN
----jGRASP exec: java MyTimeDemo
Enter hour, minute, and second (-1 to exit)
10 20 30
Values read were: 10 20 30
10:20:30 AM
10:20:30
hour is 10 minute is 20 second is 30
Enter hour, minute, and second (-1 to exit)
1 1 1
Values read were: 1 1 1
1:01:01 AM
01:01:01
hour is 1 minute is 1 second is 1
Enter hour, minute, and second (-1 to exit)
13 1 1
Values read were: 13 1 1
1:01:01 PM
13:01:01
hour is 13 minute is 1 second is 1
Enter hour, minute, and second (-1 to exit)
0 0 0
Values read were: 0 0 0
12:00:00 AM
00:00:00
hour is 0 minute is 0 second is 0
Enter hour, minute, and second (-1 to exit)
23 59 59
Values read were: 23 59 59
11:59:59 PM
23:59:59
hour is 23 minute is 59 second is 59
Enter hour, minute, and second (-1 to exit)
12 0 0
Values read were: 12 0 0
12:00:00 PM
12:00:00
hour is 12 minute is 0 second is 0
Enter hour, minute, and second (-1 to exit)
6 30 0
Values read were: 6 30 0
6:30:00 AM
06:30:00
hour is 6 minute is 30 second is 0
Enter hour, minute, and second (-1 to exit)
24 30 15
Values read were: 24 30 15
12:00:00 AM
00:00:00
hour is 0 minute is 0 second is 0
Enter hour, minute, and second (-1 to exit)
12 60 15
Values read were: 12 60 15
12:00:00 AM
00:00:00
hour is 0 minute is 0 second is 0
Enter hour, minute, and second (-1 to exit)
12 30 60
Values read were: 12 30 60
12:00:00 AM
00:00:00
hour is 0 minute is 0 second is 0
Enter hour, minute, and second (-1 to exit)
-1
t=t1? false
t1=t2? true
----jGRASP: operation complete.
CODE
test program I used
import java.util.*;
class MyTimeDemo{
public static void main(String[] args)
{int h,m,s;
MyTime t=new MyTime();
MyTime t1=new MyTime();
MyTime t2=new MyTime();
Scanner in=new Scanner(System.in);
System.out.println("Enter hour, minute, and second (-1 to exit)");
h=in.nextInt();
while(h!=-1)
{m=in.nextInt();
s=in.nextInt();
System.out.println(" Values read were: "+h+" "+m+" "+s);
t.setTime(h,m,s);
System.out.println(t.toString());
System.out.println(t.toUniversalString());
System.out.println("hour is "+t.getHour()+" minute is "+t.getMinute()
+" second is "+t.getSecond()+" ");
System.out.println("Enter hour, minute, and second (-1 to exit)");
h=in.nextInt();
}
t1.setTime(12,23,45);
t2.setTime(12,23,45);
System.out.println("t=t1? "+t1.equals(t));
System.out.println("t1=t2? "+t1.equals(t2));
}
}
-----------------------------------------------
class MyTime
{
private int hour,minute,second;
public MyTime()
{setTime(0,0,0); //default constructor sets all to 0
}
public void setTime(int hour, int minute,int second)
{if(hour<0||hour>23||minute<0||minute>59||second>59||second<0)
{this.hour=0; //anything invalid set all to 0
this.minute=0;
this.second=0;
}
else
{this.hour=hour; //nothing invalid use values
this.minute=minute;
this.second=second;
}
}
public int getHour()
{
return hour;
}
public int getMinute()
{
return minute;
}
public int getSecond()
{
return second;
}
public boolean equals(MyTime other) //for them to be same all values must be equal
{if(hour==other.hour&&minute==other.minute&&second==other.second)
return true;
else
return false;
}
public String toString()
{String time;
if(hour==0) //midnight hour=0, so must change to 12
return 12+":"+twoDigits(minute)+":"+twoDigits(second)+" AM";
else if(hour<12) //AM
return hour+":"+twoDigits(minute)+":"+twoDigits(second)+" AM";
else if(hour==12)//noon is PM
return (hour)+":"+twoDigits(minute)+":"+twoDigits(second)+" PM";
else //change to 1-12
return (hour-12)+":"+twoDigits(minute)+":"+twoDigits(second)+" PM";
}
public String toUniversalString() //already have correct time just format
{
return twoDigits(hour)+":"+twoDigits(minute)+":"+twoDigits(second);
}
private String twoDigits(int val)
{if(val>=10) //already 2 digits make into a string
return Integer.toString(val);
else //if 1 digit, make into 2 digits
return "0"+Integer.toString(val);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.