Code compiles, but I need the right degrees on the clock when it out puts public
ID: 3537566 • Letter: C
Question
Code compiles, but I need the right degrees on the clock when it out puts
public double angleHourMinuteHands() IN GEEK WATCH
// 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
// TODO calculate the angle between the hour and minute hands
// for example, at 3:00 the angle is exactly 90 degrees
public Watch at(int index) IN WATCH CoLLECTION CLASS ( NOT SURE HOW TO FIX) HELP
public class Driver {
public static void adjust(Watch w1, Watch w2) {
System.out.println("hi I am watch w1 :" + w1.getUtcHour());
System.out.println("hi I am watch w2 :" + w2.getUtcHour());
final int averageHour = (w1.getUtcHour() + w2.getUtcHour()) / 2;
// TODO Calculate average minutes and seconds
final int averageMinutes = (w1.getUtcMinute() + w2.getUtcMinute()) / 2;
final int averageSeconds = (w1.getUtcSecond() + w2.getUtcSecond()) / 2;
w1.setUtcHour(averageHour);
// TODO Set w1 UTC minutes and seconds to the average
w1.setUtcMinute(averageMinutes);
w1.setUtcSecond(averageSeconds);
// TODO Set w2 UTC hours, minutes, and seconds to the average
w2.setUtcHour(averageHour);
w2.setUtcMinute(averageMinutes);
w2.setUtcSecond(averageSeconds);
}
public static void sync(Watch w1, Watch w2) {
System.out.println("w1 " + w1);
System.out.println("w2 " + w2);
// TODO Print w1 watch before the adjustment
adjust(w1, w2);
// TODO Print w1 and w2 watches after the adjustment
System.out.println("Watch after adjustment " + w1.getUtcHour() + " : "
+ w1.getUtcMinute() + " : " + w1.getUtcSecond());
System.out.println("Watch after adjustment " + w2.getUtcHour() + " : "
+ w2.getUtcMinute() + " : " + w2.getUtcSecond());
}
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
GeekWatch w4 = new GeekWatch(14, 02, 20, -5);
// TODO Define GeekWatch w5 in CST (Central Time) zone
GeekWatch w5 = new GeekWatch(10, 03, 20, -6);
// TODO Define GeekWatch w6 in PST (Pacific Time) zone
GeekWatch w6 = new GeekWatch(13, 02, 16, -8);
// TODO Set some time so w4 UTC time == w5 UTC time + 2 hours == w6 UTC
// time + 3 hours
w4 = new GeekWatch(17, 02, 20, -5);
// TODO Create a collection consisting of w4, w1, w5, w2, w6, w3
Watch[] watches = { w4, w1, w5, w2, w6, w3 };
WatchCollection w = new WatchCollection(watches);
// TODO Delete w1, w2, and w3 from the collection
w.remove(w1);
w.remove(w2);
w.remove(w3);
// TODO Print the collection using toString()
for (int i = 0; i < watches.length; i++) {
if (watches[i] == null) {
} else
System.out
.println("Printing the Watch Collection using toString() :"
+ watches[i].toString());
}
// TODO Write a loop that prints each watch in the collection using
// size() and at()
int size = w.size();
System.out.println("The size is :" + size);
for (int i = 0; i < size; i++) {
w.at(i);
}
// TODO Write loops that synchronise each pair of watches inside the
// collection
System.out.println(" ");
for (int i = 0; i <= size; ++i) {
if (i == size - 1) {
break;
} else {
UtcWatch utcWatch = new UtcWatch();
UtcWatch utcWatch1 = new UtcWatch();
utcWatch = (UtcWatch) watches[i];
utcWatch.setUtcHour(watches[i].getHour());
utcWatch.setUtcMinute(watches[i].getMinute());
utcWatch.setUtcSecond(watches[i].getSecond());
utcWatch1 = (UtcWatch) watches[++i];
utcWatch1.setUtcHour(watches[i].getHour());
utcWatch1.setUtcMinute(watches[i].getMinute());
utcWatch1.setUtcSecond(watches[i].getSecond());
--i;
sync(utcWatch, utcWatch1);
}
}
// TODO Print the collection after the synchronisation using toString()
System.out.println(" ");
for (int i = 0; i < size; i++) {
System.out.println(watches[i].toString());
}
}
}
-------------------------------------------------------------------------------------------------
import java.util.Calendar;
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
public GeekWatch(int azone){
super(azone);
}
public GeekWatch(Watch w){
super(w);
}
public GeekWatch(Watch w, int azone){
super(w, azone);
}
public GeekWatch(int hour, int minute, int second){
super(hour, minute , second);
}
public GeekWatch(int hour, int minute, int second, int azone){
super(hour, minute , second, azone);
}
private int daySeconds(int hour, int minute, int seconds)
{
Calendar calendarMidnight = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendarMidnight.set(Calendar.HOUR_OF_DAY, 0);
calendarMidnight.set(Calendar.MINUTE, 0);
calendarMidnight.set(Calendar.SECOND, 0);
// calendarMidnight.set(Calendar.MILLISECOND, 0);
calendar2.set(Calendar.HOUR_OF_DAY, hour);
calendar2.set(Calendar.MINUTE, minute);
calendar2.set(Calendar.SECOND, seconds);
// calendar2.set(Calendar.MILLISECOND, 0);
int milliseconds1 = (int) calendarMidnight.getTimeInMillis();
int milliseconds2 = (int) calendar2.getTimeInMillis();
int diff = milliseconds2 - milliseconds1;
int secondsdif = diff / 1000;
return secondsdif;// TODO return total number of seconds since midnight;
}
public double angleHourMinuteHands()
{
int time = daySeconds( getHour(), getMinute(), 0);
if( time > 43200){
time = time - 43200;
}
double hAngle = 0.5D * ((time/3600) * 60 + ((time%3600)/ 60));
double mAngle = 6 * ((time%3600)/ 60);
System.out.println(hAngle);
System.out.println(mAngle);
double angle = Math.abs(hAngle + mAngle);
// angle = Math.min(angle, 360 - angle);
System.out.println(angle);
return angle;
}
// 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
// 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";
}
}
Explanation / Answer
//POSTING THE REQUIRED FUNCTION
// INSERT WHEREVER REQUIRED
//PLEASE CHECK THE DEGREE OUTPUT, IT IS CORRECT
public double 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
// TODO calculate the angle between the hour and minute hands
// for example, at 3:00 the angle is exactly 90 degrees
/* THE FUNCTIONS getHour() and getMinute() ARE DEFINED IN THE PARENT CLASSES OF GEEK WATCH (Zone Watch and UTCWatch)
*/
int H = getHour();
int M = getMinute();
if(H>12) { H=H-12; }
double angle= 0.5*((60*H)-(11*M));
//make positive
if (angle<0 ) { angle = angle*(-1) ; }
//make least
if(angle>180) { angle = 360.0 - angle; }
return angle;
}