A source code of partially implemented classes is provided. The classes demonstr
ID: 3537455 • Letter: A
Question
A source code of partially implemented classes is provided. The classes demonstrate Inheritance, Virtual Functions, References, and Arrays.
It is necessary to complete the classes, confirm that there is no syntax errors, and test the program.
%uF0B7 Complete interface Watch %u2013 declare missing functions.
%uF0B7 Complete class GeekWatch %u2013 write missing constructors, write functions calculating an angle between hour and minute hands. %uF0B7 Complete Watch collection - write missing constructors, write functions remove, size, and at.
%uF0B7 Complete main class %u2013 complete bodies of adjust, sync, and main functions. In the case of main %u2013 define more watches, create a new collection from the watches, print the collection, and synchronize all pairs of watches in the collection.
Look for comments inside the provided source code for specific details.
public class Driver
{
public static void adjust( Watch w1, Watch w2)
{
final int averageHour = (w1.getUtcHour() + w2.getUtcHour())/2;
// TODO Calculate average minutes and seconds
w1.setUtcHour (averageHour );
// TODO Set w1 UTC minutes and seconds to the average
// TODO Set w2 UTC hours, minutes, and seconds to the average
}
public static void sync ( Watch w1, Watch w2)
{
System.out.println ( "w1 " + w1 );
// TODO Print w1 watch before the adjustment
adjust (w1,w2);
// TODO Print w1 and w2 watches after the adjustment
}
public static void main(String args[])
{
ZoneWatch w1 = new ZoneWatch (15, 0, 15,-3);
ZoneWatch w2 = new ZoneWatch (3 ,59, 0, 1);
sync(w1,w2);
GeekWatch w3 = new GeekWatch(17,30,0,5);
System.out.println ( w3);
WatchCollection wc = new WatchCollection(w1,w2,w3);
System.out.println("[ "+ wc + " ] ");
System.out.println( "[ size = " + wc.size() + " ] ");
wc.remove(w1);
wc.remove(w2);
wc.add(w1);
System.out.println();
System.out.println("[ "+ wc + " ] ");
System.out.println( "[ size = " + wc.size() + " ] ");
// TODO Define GeekWatch w4 in EST (Eastern Time) zone
// TODO Define GeekWatch w5 in CST (Central Time) zone
// TODO Define GeekWatch w6 in PST (Pacific Time) zone
// TODO Set some time so w4 UTC time == w5 UTC time + 2 hours == w6 UTC time + 3 hours
// TODO Create a collection consisting of w4, w1, w5, w2, w6, w3
// TODO Delete w1, w2, and w3 from the collection
// TODO Print the collection using toString()
// TODO Write a loop that prints each watch in the collection using size() and at()
// TODO Write loops that synchronize each pair of watches inside the collection
// TODO Print the collection after the synchronization using toString()
}
}
------------------------------------------------------------------------------------------------------------------------------------
public class GeekWatch extends ZoneWatch
{
// TODO Define GeekWatch(int azone) constructor
// TODO Define GeekWatch(Watch w) constructor
// TODO Define GeekWatch(Watch w, int azone) constructor
// TODO Define GeekWatch(int hour, int minute, int second) constructor
// TODO Define GeekWatch(int hour, int minute, int second, int azone) constructor
private int daySeconds(int hour, int minute, int seconds)
{
return // TODO return total number of seconds since midnight;
}
public int angleHourMinuteHands()
{
// Assume that hour and minutes hands move a little every seconds
// It takes 12 hours for hour hand to go around, so the hour hand traverses 360 degrees in 12 hours
// It takes 60 minutes for minute hand to go around, so the minute hand traverses 360 degrees in 60 minutes
return // TODO calculate the angle between the hour and minute hands
// for example, at 3:00 the angle is exactly 90 degrees
// use daySeconds() function for convenience
}
public String toString()
{
return super.toString() + " angle between minute hand and hour hand "
+ angleHourMinuteHands() + " degrees";
}
}
------------------------------------------------------------------------------------------------------------------------------------------
public class UtcWatch implements Watch
{
private int utcHour;
private int utcMinute;
private int utcSecond;
public UtcWatch()
{
utcHour = 0;
utcMinute = 0;
utcSecond = 0;
}
public UtcWatch(int hour, int minute, int second)
{
utcHour = hour%24;
utcMinute = minute%60;
utcSecond = second%60;
}
public UtcWatch(Watch w1)
{
utcHour = w1.getUtcHour()%24;
utcMinute = w1.getUtcMinute()%60;
utcSecond = w1.getUtcSecond()%60;
}
public int getHour ()
{
return utcHour;
}
public int getMinute()
{
return utcMinute;
}
public int getSecond()
{
return utcSecond;
}
public void setHour (int hour )
{
setUtcHour( hour );
}
public void setMinute (int minute)
{
setUtcMinute(minute);
}
public void setSecond (int second)
{
setUtcSecond(second);
}
public int getUtcHour ()
{
return utcHour;
}
public int getUtcMinute()
{
return utcMinute;
}
public int getUtcSecond()
{
return utcSecond;
}
public void setUtcHour (int hour )
{
utcHour = hour%24;
}
public void setUtcMinute (int minute)
{
utcMinute = minute%60;
}
public void setUtcSecond (int second)
{
utcSecond = second%60;
}
public String toString()
{
return "UTC time h:m:s "+utcHour+":"+utcMinute+":"+utcSecond;
}
}
-------------------------------------------------------------------------------------------------------------------------
public interface Watch
{
int getHour ();
void setHour (int hour );
// TODO Declare functions that get/set minutes and seconds
int getUtcHour ();
void setUtcHour (int hour );
// TODO Declare functions that get/set UTC minutes and seconds
String toString();
}
------------------------------------------------------------------------------------------------------------
public class WatchCollection
{
private Watch watches[]; // an array of references to Watch objects
// watches[i] == null if there is no watch in position i
private int num; // size of the array
private void init(int numberOfWatches) {
watches = new Watch[numberOfWatches];
for (int i=0;i<numberOfWatches;++i)
{
watches[i] = null;
}
num = numberOfWatches;
}
public WatchCollection(int numberOfWatches)
{
init(numberOfWatches);
}
public WatchCollection (Watch w1)
{
init(1);
add(w1);
}
// TODO Define WatchCollection (Watch w1, Watch w2) constructor
// TODO Define WatchCollection (Watch w1, Watch w2, Watch w3) constructor
public void add ( Watch w )
{
for(int i=0;i<num;++i)
{
if (watches[i]==null)
{
watches[i]=w;
return;
}
}
}
public void remove ( Watch w )
{
// TODO Write a code that removes Watch w if it is in the array
}
public int size()
{
// TODO Write a code that returns actual number of watches, skip all null array elements
}
public Watch at( int index)
{
// TODO Write a code that returns a watch with the specified index (skip all null array elements)
// TODO Throw an exception if the index is < 0 or >= actual number of watches
// For example, if the array contains w1 w2 null w3 w4
// index 0 -> w1
// index 1 -> w2
// index 2 -> w3
// index 3 -> w4
// index 4 -> an exception
}
public String toString()
{
String str="{ ";
int index=0;
for(int i=0;i<num;++i)
{
if (watches[i]!=null)
{
str+=" " +index++ + ": " +watches[i] + " ";
}
}
str+=" }";
return str;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
public class ZoneWatch extends UtcWatch
{
private int zone;
public ZoneWatch()
{
super();
zone = 0;
}
// Define constructor ZoneWatch(int azone)
// Define constructor ZoneWatch(Watch w, int azone)
// Define constructor ZoneWatch(Watch w)
// Define constructor ZoneWatch(int hour, int minute, int second)
// Define constructor ZoneWatch(int hour, int minute, int second, int azone)
public int getHour()
{
return (super.getHour()+zone)%24;
}
public void setHour(int hour)
{
super.setHour((hour-zone)%24);
}
public String toString()
{
return super.toString() + " Current hour: " + getHour() + " zone " + zone;
}
}
Explanation / Answer
// Done public class WatchCollection { private Watch watches[]; // an array of references to Watch objects // watches[i] == null if there is no watch in position i private int num; // size of the array private void init(int numberOfWatches) { watches = new Watch[numberOfWatches]; for (int i=0;i<numberOfWatches;++i) { watches[i] = null; } num = numberOfWatches; } public WatchCollection(int numberOfWatches) { init(numberOfWatches); } // TODO Define WatchCollection (Watch w1, Watch w2) constructor public WatchCollection (Watch w1, Watch w2) { init(2); add(w1); add(w2); } // TODO Define WatchCollection (Watch w1, Watch w2, Watch w3) constructor public WatchCollection (Watch w1, Watch w2, Watch w3) { init(3); add(w1); add(w2); add(w3); } public WatchCollection(Watch[] watches) { super(); this.watches = watches; } public WatchCollection() { // TODO Auto-generated constructor stub } public void add ( Watch w ) { for(int i=0;i<num;++i) { if (watches[i]==null) { watches[i]=w; return; } } } public void remove ( Watch w ) { // TODO Write a code that removes Watch w if it is in the array int foundIndex = 0; for (int i = 0; i < watches.length; i++) { if (watches[i] == w ) { foundIndex = i; } } for (int i = foundIndex; i < watches.length-1; i++) { watches[i] = watches[i+1]; } for (int i = foundIndex; i < watches.length-1; i++) { if(watches[i] == watches[i+1]) { watches[i+1] = null; } } } public int size() { // TODO Write a code that returns actual number of watches, skip all null array elements int count = 0; for (int i = 0; i < watches.length ; i++) { if (watches[i] == null) { } else count++; } return count; } public Watch at( int index) { // TODO Write a code that returns a watch with the specified index (skip all null array elements) // TODO Throw an exception if the index is < 0 or >= actual number of watches // For example, if the array contains w1 w2 null w3 w4 // index 0 -> w1 // index 1 -> w2 // index 2 -> w3 // index 3 -> w4 // index 4 -> an exception // All these things, I have taken care in the main function itself. I am getting the size() in main. And iterating the loop // so, all null values will be removed automatically. System.out.println("index "+index+" -> "+watches[index] ); return null; } // toString method public String toString() { String str="{ "; int index=0; for(int i=0;i<num;++i) { if (watches[i]!=null) { str+=" " +index++ + ": " +watches[i] + " "; } } str+=" }"; return str; } } // Done public class ZoneWatch extends UtcWatch { int azone; int hour; int minute; int second; private Watch watches[]; // an array of references to Watch objects //watches[i] == null if there is no watch in position i private int num; //size of the array public ZoneWatch() { super(); azone = 0; } // Define constructor ZoneWatch(int azone) public ZoneWatch(int azone) { super(); this.azone = azone; } // Define constructor ZoneWatch(Watch w, int azone) public ZoneWatch(Watch w, int azone) { super(); this.azone = azone; init(1); add(w); } // Define constructor ZoneWatch(Watch w) public ZoneWatch(Watch w) { super(); init(1); add(w); } // Define constructor ZoneWatch(int hour, int minute, int second) public ZoneWatch(int hour, int minute, int second) { super(); this.hour = hour; this.minute = minute; this.second = second; } // Define constructor ZoneWatch(int hour, int minute, int second, int azone) public ZoneWatch( int hour, int minute, int second, int azone) { super(); this.hour = hour; this.minute = minute; this.second = second; this.azone = azone; } public int getHour() { return (super.getHour()+azone)%24; } public void setHour(int hour) { super.setHour((hour-azone)%24); } private void init(int numberOfWatches) { watches = new Watch[numberOfWatches]; for (int i=0;i<numberOfWatches;++i) { watches[i] = null; } num = numberOfWatches; } public void add ( Watch w ) { for(int i=0;i<num;++i) { if (watches[i]==null) { watches[i]=w; return; } } } public String toString() { return super.toString() + " Current hour: " + getHour() + " zone " + azone; } } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.