Question

In: Computer Science

public class Clock { private int hr; private int min; private int sec; public Clock() {...

public class Clock
{
  private int hr;
  private int min;
  private int sec;
  
  public Clock()
  {
    setTime(0, 0, 0);
  }
  
  public Clock(int hours, int minutes, int seconds)
  {
    setTime(hours, minutes, seconds);
  }
  
  public void setTime(int hours, int minutes, int seconds)
  {
    if (0 <= hours && hours < 24)
      hr = hours;
    else
      hr = 0;
    
    if (0 <= minutes && minutes < 60)
      min = minutes;
    else
      min = 0;
    
    if(0 <= seconds && seconds < 60)
      sec = seconds;
    else
      sec = 0;
  }
  
  public int getHours()
  {
    return hr;
  }
  
  public int getMinutes()
  {
    return min;
  }
  
  public int getSeconds()
  {
    return sec;
  }
  
  public void printTime()
  {
    if (hr < 10)
      System.out.print("0");
    System.out.print(hr + ":");
    
    if (min < 10)
      System.out.print("0");
    System.out.print(min + ":");
    
    if (sec < 10)
      System.out.print("0");
    System.out.print(sec);
  }
  
  public void incrementSeconds()
  {
    sec++;
    if (sec > 59)
    {
      sec = 0;
      incrementMinutes();
    }
  }
  
  public void incrementMinutes()
  {
    min++;
    if (min > 59)
    {
      min = 0;
      incrementHours();
    }
  }
  
  public void incrementHours()
  {
    hr++;
    if(hr > 23)
      hr = 0;
  }
  
  public boolean equals(Clock otherClock)
  {
    return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec);
  }
  
  public void makeCopy(Clock otherClock)
  {
    hr =  otherClock.hr;
    min = otherClock.min;
    sec = otherClock.sec;
  }
  
  public Clock getCopy()
  {
    Clock temp = new Clock();
    
    temp.hr = hr;
    temp.min = min;
    temp.sec = sec;
    
    return temp;
  }
}

We learned the above class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following:

  1. Derive the class ExtClock from the class Clock by adding a data member to store the time zone. Add necessary methods and constructors to make the class functional. Also write the definitions of the methods and constructors.
  2. Write a test program to test your new class.

Solutions

Expert Solution

This java code contains ExtClock class and it is tested in main function .

import java.util.Calendar;
import java.util.TimeZone;
class Clock
{
private int hr;
private int min;
private int sec;
  
public Clock()
{
setTime(0, 0, 0);
}
  
public Clock(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}
  
public void setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
  
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
  
if(0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
  
public int getHours()
{
return hr;
}
  
public int getMinutes()
{
return min;
}
  
public int getSeconds()
{
return sec;
}
  
public void printTime()
{
if (hr < 10)
System.out.print("0");
System.out.print(hr + ":");
  
if (min < 10)
System.out.print("0");
System.out.print(min + ":");
  
if (sec < 10)
System.out.print("0");
System.out.print(sec);
}
  
public void incrementSeconds()
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes();
}
}
  
public void incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours();
}
}
  
public void incrementHours()
{
hr++;
if(hr > 23)
hr = 0;
}
  
public boolean equals(Clock otherClock)
{
return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec);
}
  
public void makeCopy(Clock otherClock)
{
hr = otherClock.hr;
min = otherClock.min;
sec = otherClock.sec;
}
  
public Clock getCopy()
{
Clock temp = new Clock();
  
temp.hr = hr;
temp.min = min;
temp.sec = sec;
  
return temp;
}

}
/* creation of new class ExtClock that is derived from class Clock */
class ExtClock extends Clock
{
public String Tzone; //data member to store the timezone
public void getTzone() //method which displays the current timezone
{
Calendar n = Calendar.getInstance(); //this is to get calendar instance
/* java Calendar class and Timezone class are imported at the top of the code */
/* getTimeZone method of Calendar class is used to retrieve current timezone */
TimeZone tzone = n.getTimeZone();
Tzone=tzone.getDisplayName(); /*to store the timezone to Tzone variable using getDisplayName method of TimeZone class */
System.out.println("current timezone is : " +Tzone );
  
}
}
public class Main{
public static void main(String[] args) {
ExtClock c = new ExtClock();
c.getTzone(); //This displays the time zone
}
}

//This code will give the correct output

Output

current timezone is : Coordinated Universal Time

NB

Test program is also included with that code.Its is tested successfully and out put is obtained.

if test program needed , then here it is

(before that make changes to the java code by making the classes Clock and ExtClock to a package named Pclock by simply writing "package Pclock; " at top of the code ie top of importing calendar class and save it with file name of that class . Remove void main code from that package)

program to test the ExtClock class

import Pclock.*; //to import the Pclock package

public class Main{
public static void main(String[] args) {
ExtClock c = new ExtClock();
c.getTzone(); //This displays the time zone
}
}


Related Solutions

public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Question: Use Eclipse to create a clockType with hr, min, sec as private members. You shall...
Question: Use Eclipse to create a clockType with hr, min, sec as private members. You shall have 3 files: clock.h clock.cpp and lab1main.cpp lab1main.cpp shall support the following statements: clockType c1(15, 45, 30), c2(3, 20); // hour, min, sec cout << c1; // add whatever to beautify it cout << c2; cout << c1+c2; c2 = c1+c1; cout << c2; Need help please!
public class IntNode               {            private int data;            pri
public class IntNode               {            private int data;            private IntNode link;            public IntNode(int data, IntNode link){.. }            public int     getData( )          {.. }            public IntNode getLink( )           {.. }            public void    setData(int data)     {.. }            public void    setLink(IntNode link) {.. }         } All questions are based on the above class, and the following declaration.   // Declare an empty, singly linked list with a head and a tail reference. // you need to make sure that head always points to...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m = new int[x][y]; } public Matrix(int x, int y, int z) { m = new int[x][y]; for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { m[i][j] = z; } } } public int rowsum(int i) throws IndexOutOfBoundsException { if (i < 0 || i > m.length-1) { throw new IndexOutOfBoundsException("Invalid Row"); } int sum =...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /** * Constructor for objects of class Classroom */ public Classroom() { this.capacity = 0; }    /** * Constructor for objects of class Classroom * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Classroom(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room...
In C++, Implement the following class that represents a clock. Clock - hour: int - minute:...
In C++, Implement the following class that represents a clock. Clock - hour: int - minute: int - meridiem: string + Clock() + Clock(hr: int, min: int, mer: string) + setTime(hr: int, min: int, mer: string): void + setHour(hr: int): void + setMinute(min: int): void + setMeridiem(mer: string): void + getHour(): int + getMinute(): int + getMeridiem(): string + void tick() + string asString() + string asStandard() Implementation Details: • The default constructor should set the clock to midnight (12:00...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT