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

Can someone help me? I am taking computer science I and need help with example 8

ID: 3810186 • Letter: C

Question

Can someone help me? I am taking computer science I and need help with example 8-2 and 8-8 in the DS Malik 5th edition programming book. I've done the code for 8-2 but it's not working. Please fix it for me. Thanks!

import java.util.*;

public class TestProgClock
{

static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
Clock myClock = new Clock(5, 4, 30);
Clock yourClock = new Clock();
  
int hours;
int minutes;
int seconds;
  
System.out.print("Line 6: myClock: ");
myClock.printTime();
System.out.println();
System.out.print("Line 9: yourClock: ");
yourClock.printTime();
System.out.println();
  
yourClock.setTime(5, 45, 16);
  
System.out.print("Line 13: After setting " + "the time, yourClock: ");
yourClock.printTime();
System.out.println();
  
if (myClock.equals(yourClock))
System.out.println("Line 17: Both the " + "time are equal.");

else
System.out.println("Line 19: The two " + "time are not" + "equal.");

System.out.print("Line 20: Enter hours, " + "minutes, and seconds: ");
hours = console.nextInt();
minutes = console.nextInt();
seconds = console.nextInt();
System.out.println();
  
myClock.setTime(hours, minutes, seconds);
  
System.out.print("Line 26: New time of " + "myClock: ");
  
myClock.printTime();
System.out.println();
  
myClock.incrementSeconds();
  
System.out.print("Line 30: After" + "incrementing the time by " + "one second, myClock: ");
myClock.printTime();
System.println();
  
yourClock.makeCopy(myClock);
  
System.out.print("Line 34: After copying" + "myClock into yourClock, " + " yourClock: ");
yourClock.printTime();
system.out.println();
}
}

Explanation / Answer

// Clock.java
public class Clock
{
private int hours;
private int minutes;
private int seconds;

// constrcutor
public Clock()
{
hours = 0;
minutes = 0;
seconds = 0;
}

// parameterized constructor
public Clock(int h, int m, int s)
{
setTime(h, m, s);
}

// methid to set time
public void setTime(int h, int m, int s)
{
if (h >= 0 && h < 24)
hours = h;
else
hours = 0;

if (m >= 0 && m < 60)
minutes = m;
else
minutes = 0;

if (s >= 0 && s < 60)
seconds = s;
else
seconds = 0;
}

// method to print time
public void printTime()
{
if (hours < 10)
{
System.out.print("0");
}
System.out.print(hours + ":");

if (minutes < 10)
{
System.out.print("0");
}
System.out.print(minutes + ":");

if (seconds < 10)
{
System.out.print("0");
}
System.out.print(seconds);
}

// incrmeent hours
public void incrementHours()
{
hours++;

if (hours > 23)
hours = 0;
}
  
// increment minutes
public void incrementMinutes()
{
minutes++;

if (minutes > 59)
{
minutes = 0;
incrementHours();
}
}

// method to increment seconds
public void incrementSeconds()
{
seconds++;

if (seconds > 59)
{
seconds = 0;
incrementMinutes();
}
}

// compare if two clocks are equals
public boolean equals(Clock obj)
{
return (hours == obj.hours && minutes == obj.minutes && seconds == obj.seconds);
}

// make copy of a clock
public void makeCopy(Clock obj)
{
hours = obj.hours;
minutes = obj.minutes;
seconds = obj.seconds;
}

}




// TestProgClock.java
import java.util.*;
public class TestProgClock
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
Clock myClock = new Clock(5, 4, 30);
Clock yourClock = new Clock();
  
int hours;
int minutes;
int seconds;
  
System.out.print("Line 6: myClock: ");
myClock.printTime();
System.out.println();
System.out.print("Line 9: yourClock: ");
yourClock.printTime();
System.out.println();
  
yourClock.setTime(5, 45, 16);
  
System.out.print("Line 13: After setting " + "the time, yourClock: ");
yourClock.printTime();
System.out.println();
  
if (myClock.equals(yourClock))
System.out.println("Line 17: Both the " + "time are equal.");

else
System.out.println("Line 19: The two " + "time are not" + "equal.");

System.out.print("Line 20: Enter hours, " + "minutes, and seconds: ");
hours = console.nextInt();
minutes = console.nextInt();
seconds = console.nextInt();
System.out.println();
  
myClock.setTime(hours, minutes, seconds);
  
System.out.print("Line 26: New time of " + "myClock: ");
  
myClock.printTime();
System.out.println();
  
myClock.incrementSeconds();
  
System.out.print("Line 30: After" + "incrementing the time by " + "one second, myClock: ");
myClock.printTime();
System.out.println();
  
yourClock.makeCopy(myClock);
  
System.out.print("Line 34: After copying" + "myClock into yourClock, " + " yourClock: ");
yourClock.printTime();
System.out.println();
}
}

/*
output:

Line 6: myClock: 05:04:30
Line 9: yourClock: 00:00:00
Line 13: After setting the time, yourClock: 05:45:16
Line 19: The two time are notequal.
Line 20: Enter hours, minutes, and seconds: 12 2 45

Line 26: New time of myClock: 12:02:45
Line 30: Afterincrementing the time by one second, myClock: 12:02:46
Line 34: After copyingmyClock into yourClock, yourClock: 12:02:46


*/