I'm making a program that extends the clock to feature the names of the time zones. The derived class needs to have a static String array data member with values: EST, CST, MST, PST, EDT, CDT, MDT, PDT, a zone data member, a default constructor, a constructor with parameters, the setZone() method, the getZone() method, the printTime() method, the toString(), the equals() method, a makeCopy() method, and a getCopy() method.
public class Clock {
private int hr;
private int min;
private int sec;
public Clock() {
hr = 0;
min = 0;
sec = 0;
}
public Clock(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 Clock(Clock otherClock) {
hr = otherClock.hr;
min = otherClock.min;
sec = otherClock.sec;
}
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 incrementHours() {
hr++;
if (hr > 23) {
hr = 0;
}
}
public void incrementMinutes() {
min++;
if (min > 59) {
min = 0;
incrementHours();
}
}
public void incrementSeconds() {
sec++;
if (sec > 59) {
sec = 0;
incrementMinutes();
}
}
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;
}
public String toString() {
String str = "";
if (hr < 10) {
str = "0";
}
str += hr + ":";
if (min < 10) {
str += "0";
}
str += min + ":";
if (sec < 10) {
str += "0";
}
str += sec;
return str;
}
}
class ExtClock extends Clock {
static String[] timeZone = {"EST", "CST", "MST", "PST", "EDT", "CDT", "MDT", "PDT"};
private String zone;
public ExtClock() {
super();
zone = "";
}
public ExtClock(int hours, int minutes, int seconds, String tz) {
super(hours, minutes, seconds);
zone = tz;
}
public void setZone(int hours, int minutes, int seconds, String tz) {
setTime(hours, minutes, seconds);
zone = tz;
}
public String getZone() {
return zone;
}
public void printTime() {
super.printTime();
System.out.println(" " + zone);
}
public String toString() {
return super.toString() + " " + zone;
}
public boolean equals(ExtClock otherClock) {
return super.equals(otherClock) && zone.equalsIgnoreCase(otherClock.zone);
}
}
public class ExtClockTest {
public static void main(String[] args) {
ExtClock myExtClock = new ExtClock(5,4,30,"EST");
ExtClock yourExtClock = new ExtClock(0,0,0,"");
setZone.yourExtClock(5,45,16,"CDT");
}
}
The derived class compiles fine, but the ExtClockTest program wouldn't compile because it says that it cannot find the symbol. Am I doing something wrong?
You have put the method before the object.
setZone.yourExtClock(5,45,16,"CDT");
It should be:
Obj.method()
yourExtClock.setZone(5,45,16,"CDT");
Related
I'm running the program for 5 minutes. It shows no errors but when it runs the time remaining stays the same
I am working on a time-driven single server or multiple server queue system with
following specifications:
Calls arrive at an average rate of 20 calls per minute.
When a call arrives (random callID), the call is placed in a queue.
The service time for a call varies from 3 seconds to 16 seconds (use random number and computer clock time)
Calls are served at a maximum of 7 seconds at once, if a call required service time more
than 7 seconds, for the remaining service time the call is placed back on the queue
(enqueue) again
Here's my coding:
package callcenter;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class CallCenter extends TimerTask {
ArrayList<Caller> Server1 = new ArrayList<>();
ArrayList<Caller> Server2 = new ArrayList<>();
ArrayList<Caller> Queue = new ArrayList<>();
Random rand = new Random();
int count = 7,
count1 = 7,
time = rand.nextInt(301) + 300;
public static int firstattempt = 0;
public static int queued1 = 0;
public static int queued2 = 0;
public static int attempt = 0;
public static int callcounter = 0;
public static int Processed = 0;
public static void main(String[] args) {
TimerTask task = new CallCenter();
Timer timer = new Timer();
timer.scheduleAtFixedRate(task,0,1000);
}
#Override
public void run() {
if ((Server1.isEmpty() == false) || (Server2.isEmpty() == false)){
ReceiveCall();
callcounter++;
if (count > 0){
if(Server1.get(0).getServiceTime() > 1){
Server1.get(0).setServiceTime();
count--;
}
else{
count = 7;
attempt = Server1.get(0).getAttempt();
Server1.remove(0);
if(attempt == 1){
firstattempt++;
}
else if(attempt == 2){
queued1++;
}
else if(attempt == 3){
queued2++;
}
if(Queue.size() > 0){
Server1.add(Queue.get(0));
Queue.remove(0);
Processed++;
}
}
}
else{
Server1.get(0).setAttempt();
Queue.add(Server1.get(0));
Server1.remove(0);
if(Queue.size() > 0){
Server1.add(Queue.get(0));
Queue.remove(0);
Processed++;
}
count = 7;
}
if (count1 > 0){
if(Server2.get(0).getServiceTime() > 1){
Server2.get(0).setServiceTime();
count1--;
}
else{
count1 = 7;
attempt = Server2.get(0).getAttempt();
Server2.remove(0);
if(attempt == 1){
firstattempt++;
}
else if(attempt == 2){
queued1++;
}
else if(attempt == 3){
queued2++;
}
if(Queue.size() > 0){
Server2.add(Queue.get(0));
Queue.remove(0);
Processed++;
}
}
}
else{
Server2.get(0).setAttempt();
Queue.add(Server2.get(0));
Server2.remove(0);
if(Queue.size() > 0){
Server2.add(Queue.get(0));
Queue.remove(0);
Processed++;
}
count1 = 7;
}
}
else if (Queue.size() > 0) {
if(Server1.isEmpty() == false) {
Server2.add(Queue.get(0));
Queue.remove(0);
Processed++;
}
else{
Server1.add(Queue.get(0));
Queue.remove(0);
Processed++;
}
}
else {
ReceiveCall();
callcounter++;
}
Format();
}
public void ReceiveCall(){
int SAssign;
Caller c = new Caller();
c.setCallerID();
c.setRandomTime();
c.setAttempt();
SAssign = rand.nextInt(2) + 1;
if (SAssign == 1){
if(Server1.isEmpty() == false){
Queue.add(c);
}
else {
Server1.add(c);
Processed++;
}
}
else if(SAssign== 2){
if(Server2.isEmpty() == false){
Queue.add(c);
}
else {
Server2.add(c);
Processed++;
}
}
}
public void CountDown(){
if (this.time != 0) {
--this.time;
} else {
System.exit(0);
}
}
public void Format (){
int x = 0;
String Service = null;
String Time1= null;
String Service2 = null;
String Time2 = null;
if (Server1.size() > 0){
Service = Server1.get(0).toString();
Time1 = Integer.toString(Server1.get(0).getServiceTime());
}
if (Server2.size() > 0){
Service2 = Server2.get(0).toString();
Time2 = Integer.toString(Server2.get(0).getServiceTime());
}
System.out.println("Time Remaining: " + time + " seconds");
System.out.println("Server 1: \n" + Service + " | " + Time1);
System.out.println("Server 2: \n" + Service2 + " | " + Time2);
System.out.println("Queue: ");
if (Queue.size() > 0){
while(x < Queue.size()){
System.out.println(Queue.get(x));
x++;
}
}
if (time == 0){
System.out.println("Total number of calls processed are : "+ Processed);
System.out.println("Average number of calls processed per minute is : "+ (double)(Processed/60.0));
System.out.println("Average arrival rate per minute is : "+ (double)(callcounter/60.0));
System.out.println("Number of calls processed in first attempt : "+ firstattempt);
System.out.println("Number of calls had to be requeued once : "+ queued1);
System.out.println("Number of calls had to be requeued twice : "+ queued2);
}
}
}
Caller.java
package callcenter;
import java.util.Random;
public class Caller {
Random rand = new Random();
int CallerID;
int ServiceTime;
int Attempt = 0;
String Name;
public Caller() {
this.CallerID = CallerID;
}
#Override
public String toString() {
Name = Integer.toString(CallerID);
return Name;
}
public void setCallerID() {
this.CallerID = rand.nextInt(3000) + 2000;
}
public int getCallerID() {
return CallerID;
}
public int getServiceTime() {
return ServiceTime;
}
public void setRandomTime() {
this.ServiceTime = rand.nextInt(14) + 3;
}
public void setServiceTime() {
this.ServiceTime = ServiceTime - 1;
}
public void setAttempt() {
this.Attempt++;
}
public int getAttempt() {
return Attempt;
}
}
I have project in java.
Create CTime class with the following specifications
Attributes: Hour, minute and second
Hour >=0 and <= 23, minute >=0 and <=59 , second >=0 and <= 59
Methods
Constructor that updates the CTime attributes
set and get methods for each attribute
tick method that add 1 second to the CTime object and returns nothing
toString method that creates time string with the following format HH:mm:ss e.g. 22:15:01
and I did this
public class CTime {
int hour;
int min;
int sec;
public CTime(int h, int m, int s) {
hour = h;
min = m;
sec = s;
}
public void setHour(int h) {
hour = h;
}
public int getHour() {
return hour;
}
public void setMin(int m) {
min = m;
}
public int getMin() {
return min;
}
public void setSec(int s) {
sec = s;
}
public int getSec() {
return sec;
}
public void tick() {
sec++;
if (sec >= 59) {
sec = 0;
min++;
if (min >= 59) {
min = 0;
}
hour++;
}
}
public String toString() {
String s = hour + ":" + min + ":" + sec;
return s;
}
}
How to make my hour and min and sec with 2 digit, e.g. 05:02:09?
And my code is it correct or not?
If you insist on doing this kind of stuff with Strings the way you are, you could do something like the following:
public String toString() {
String s = (hour > 9 ? hour : "0" + hour) + ":" + (min > 9 ? min : "0" + min) + ":" + (sec > 9 ? sec : "0" + sec);
return s;
}
Just use String.format()
public String toString() {
String s = String.format("%02d:%02d:%02d", hour, min, sec);
return s;
}
One more link here
And your code, yes it is correct. To make it a live clock, all you have to do is call tick() method every second. Take a look at this SO post to learn how to do that.
Under the comment version 4, i am trying to create a method named equals that will test the hours, minutes, and seconds. The formal parameter is used again in the return statement. I know i should have it in the ______.hours format, hours being the instance variable used to test and produce the true or false, but i don't know what should go before the period as the formal parameter. Any suggestions/explanations would be appreciated greatly.
public class Clock
{
private static final byte DEFAULT_HOUR = 0,
DEFAULT_MIN = 0,
DEFAULT_SEC = 0,
MAX_HOURS = 24,
MAX_MINUTES = 60,
MAX_SECONDS = 60;
// ------------------
// Instance variables
// ------------------
private byte seconds,
minutes,
hours;
public Clock (byte hours , byte minutes , byte seconds )
{
setTime(hours, minutes, seconds);
}
public Clock ( )
{
setTime(DEFAULT_HOUR, DEFAULT_MIN, DEFAULT_SEC);
}
public void setTime ( byte hours, byte minutes, byte seconds )
{
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
// hours
if (DEFAULT_HOUR >= 0 && DEFAULT_HOUR <= 29)
{
}
else
{
hours = DEFAULT_HOUR;
}
// minutes
if (DEFAULT_MIN >= 0 && DEFAULT_MIN <= 59)
{
}
else
{
minutes = DEFAULT_MIN;
}
// seconds
if (DEFAULT_SEC >= 0 && DEFAULT_SEC <= 59)
{
}
else
{
seconds = DEFAULT_SEC;
}
}
//--------------------------
// Version 3 mutator methods
//--------------------------
public void incrementSeconds()
{
seconds += 1;
if (seconds >= 59)
{
seconds = DEFAULT_SEC;
incrementMinutes();
}
}
public void incrementMinutes()
{
minutes += 1;
if (minutes >= 59)
{
minutes = DEFAULT_MIN;
incrementHours();
}
}
public void incrementHours()
{
hours += 1;
if (hours >= 23)
{
hours = DEFAULT_HOUR;
}
}
//----------
// Version 4
//----------
public boolean equals(Clock your_clock)
{
return boolean your_clock.hours;
}
//----------
// Version 2
//----------
public String toString()
{
final byte MIN_2DIGITS = 10;
String str = "";
// my input
if (hours < MIN_2DIGITS)
{
str += "0" + hours + ":" ;
}
else
str += hours + ":";
if (minutes < MIN_2DIGITS)
{
str += "0" + minutes + ":" ;
}
else
str += minutes + ":";
if (seconds < MIN_2DIGITS)
{
str += "0" + seconds;
}
else
str += seconds;
//end of my input
return str;
}
} // End of class definition
If you're trying to find equality between the parameter Clock and the caller Clock, I would do the following
public boolean equals(Clock another_clock) {
// Check if 'this' is equal to 'another_clock'
// 1. If you're checking if the pointer is the same
return another_clock.equals(this);
// 2. If you're checking if time is the same (You probably need to create getter/setter methods or change privacy for these fields)
return another_clock.hours == this.hours &&
another_clock.minutes == this.minutes &&
another_clock.seconds == this.seconds;
}
Or something along those lines.
I'm not sure why this occurs, but the 60th minute gets printed in the results when it should change to hour after minute 59.
Here is my class and main program:
public class Main {
public static void main(String[] args) {
BoundedCounter minutes = new BoundedCounter(59);
BoundedCounter hours = new BoundedCounter(23);
int i = 0;
while (i < 121) {
System.out.println(hours + ":" + minutes); // the current time printed
if (minutes.getValue() >= 59) {
hours.getValue();
i++;
}
}
}
//BoundedCounter
public class BoundedCounter {
private int value;
private int upperLimit;
public BoundedCounter(int upperLimit) {
this.upperLimit = upperLimit;
}
public void next() {
if (this.value >= this.upperLimit) {
this.value = 0;
} else {
this.value++;
}
}
public String toString() {
if (this.value < 10) {
return "" + 0 + value;
} else {
return "" + value;
}
}
public int getValue() {
if (this.value <= this.upperLimit) {
return this.value++;
} else {
return this.value = 0;
}
}
}
}
Some results:
01:56
01:57
01:58
01:59
02:60
02:00
02:01
02:02
02:03
02:04
02:05
02:06
02:07
02:08
02:09
02:10
02:11
02:12
02:13
02:14
02:15
02:16
02:17
02:18
02:19
02:20
02:21
02:22
02:23
02:24
02:25
02:26
02:27
02:28
02:29
02:30
02:31
02:32
02:33
02:34
02:35
02:36
02:37
02:38
02:39
02:40
02:41
02:42
02:43
02:44
02:45
02:46
02:47
02:48
02:49
02:50
02:51
02:52
02:53
02:54
02:55
02:56
02:57
02:58
02:59
03:60
03:00
03:01
03:02
03:03
03:04
03:05
i.e. 2:60 and 3:60 are unexpected output
First, your methods are doing what they are not supposed to do. For example, getValue() should only return the value, not increment it. Here is how this can be done:
Main.java
public class Main {
public static void main(String[] args) {
BoundedCounter minutes = new BoundedCounter(59);
BoundedCounter hours = new BoundedCounter(23);
int i = 0;
while (i < 121) {
System.out.println(hours + ":" + minutes);
minutes.next(); // counting minutes
if (minutes.getValue() == 0) { // when minutes==0, count hours
hours.next(); // counting hours
i++;
}
}
}
}
BoundedCounter.java
class BoundedCounter {
private int value;
private int upperLimit;
public BoundedCounter(int upperLimit) {
this.upperLimit = upperLimit;
}
public void next() {
// when reach the upperLimit, the next value should be 0
// so >= is not needed, just == will do
this.value = this.value == this.upperLimit ? 0 : this.value+1;
}
public String toString() {
// using smarter approach to pad with zeros :)
return String.format("%02d", value);
}
public int getValue() {
// this method should only return the value, not change it in any way
return this.value;
}
}
Some outputs:
00:00 00:01 00:02 00:03 00:04 00:05 00:06 00:07 00:08 00:09 00:10
00:11 00:12 00:13 00:14 00:15 00:16 00:17 00:18 00:19 00:20 00:21
00:22 00:23 00:24 00:25 00:26 00:27 00:28 00:29 00:30 00:31 00:32
00:33 00:34 00:35 00:36 00:37 00:38 00:39 00:40 00:41 00:42 00:43
00:44 00:45 00:46 00:47 00:48 00:49 00:50 00:51 00:52 00:53 00:54
00:55 00:56 00:57 00:58 00:59 01:00 01:01 01:02 01:03 01:04 01:05
01:06 01:07 01:08 01:09 01:10 01:11 01:12 01:13 01:14 01:15 01:16
01:17 01:18 01:19 01:20 01:21 01:22 01:23 01:24 01:25 01:26 01:27
01:28 01:29 01:30 01:31 01:32 01:33 01:34 01:35 01:36 01:37 01:38
01:39 01:40 01:41 01:42 01:43 01:44 01:45 01:46 01:47 01:48 01:49
01:50 01:51 01:52 01:53 01:54 01:55 01:56 01:57 01:58 01:59 02:00
02:01
You should change the lines :
if (this.value <= this.upperLimit) {
return this.value++;
to
if (++this.value <= this.upperLimit) {
return this.value;
AND
if (minutes.getValue() >= 59) {
to
if (minutes.getValue() == 0) {
The problem is that when minutes value is 59 in your code, it is still <= 59, so it gets inside the if block. Then you return 59 to the user and the minutes are now 60.
So, at the next iteration, you first print 60 and then you call the getValue() method, which turns your minutes to 0.
I guess you are having the same problem when the hour turns 24...
Other than that, consider following a more "expected" functionality of your methods, based on their names, which makes it clear what they do and it becomes easier, even for you to debug. See Shadowfax's answer for example.
You aren't showing any part of what actually increments your hours or minutes, but it appears to be a problem in your getValue call.
public int getValue() {
if (this.value <= this.upperLimit) {
return this.value++;
} else {
return this.value = 0;
}
}
That this.value++; part increments value to 60, and does not roll it over.
there is a slight problem in your approach to create a BoundedCounter
you should not change your counter value when your method name says you just want to read it.Second all your condition check to resets should happen inside your BoundedCounter class only.If you want to reset your counter just expose another method to do that.
Here is the working solution for your reference
import java.util.*;
public class Test{
public static void main(String[] args){
BoundedCounter minutes = new BoundedCounter(59);
BoundedCounter hours = new BoundedCounter(23);
int i = 0;
while (i < 121) {
if (minutes.hasNext()) {
minutes.next();
} else {
hours.next();
minutes.resetTo(0);
}
System.out.println(hours + ":" + minutes);
i++;
}
}
}
class BoundedCounter {
private int value;
private int upperLimit;
public BoundedCounter(int upperLimit) {
this.upperLimit = upperLimit;
}
public void next() {
++this.value;
}
public boolean hasNext() {
return this.value < this.upperLimit;
}
public void resetTo(int resetValue) {
this.value = resetValue;
}
public int getValue() {
if (this.hasNext()) {
return this.value;
}
return 0;
}
public String toString() {
if (this.value < 10) {
return "" + 0 + value;
} else {
return "" + value;
}
}
}
I have an Event class which uses the PriorityQueue and a Time class that I defined myself to use with the Event class.
static class Event implements Comparable<Event>{
Customer customer;
Time eventTime;
char eventType;
public int compareTo(Event e){
int n = e.eventTime.compareTo(eventTime);
int compare = 0;
if(n < 0){
compare = -1;
}
else if(n == 0){
compare = 0;
}
else if(n > 0){
compare = 1;
}
return compare;
}
}
class Time{
private int hour;
private int minute;
private boolean isMorning;
public Time(){
hour = 0;
minute = 0;
isMorning = true;
}
public Time(int h, int m, boolean morning){
hour = h;
minute = m;
isMorning = morning;
}
public void setTime(int h, int m, boolean morning){
hour = h;
minute = m;
isMorning = morning;
}
public int getHour(){
return hour;
}
public int getMinute(){
return minute;
}
public boolean isAM(){
return isMorning;
}
public String toString(){
String AM = "";
String min = "";
if(minute < 10){
min = ("0" + minute);
}
else{
min = ("" + minute);
}
if(isMorning){
AM = "AM";
}
else{
AM = "PM";
}
return ("" + hour + ":" + min + " " + AM);
}
public Time plus(int n){
Time newTime = new Time();
boolean newMorning = false;
int minutes = minute + n;
int newHour = minutes/60;
int newMinutes = minutes%60;
hour = hour + newHour;
if(hour > 12){
hour = hour - 12;
if(isMorning){
newMorning = false;
}
else{
newMorning = true;
}
}
newTime.setTime(hour, newMinutes, newMorning);
return newTime;
}
public int timeDifference(Time t){
int n = totalMinutes();
int m = t.totalMinutes();
return m - n;
}
public int compareTo(Time t){
int n = totalMinutes();
int m = t.totalMinutes();
int compare = 0;
if(n < m){
compare = -1;
}
else if(n == m){
compare = 0;
}
else if(n > m){
compare = 1;
}
return compare;
}
private int totalMinutes(){
int tempMinute = 0;
if(!isMorning){
if(hour == 12){
}
else{
hour = hour + 12;
tempMinute = (hour*60) + minute;
}
}
else{
if(hour == 12){
tempMinute = minute;
}
else{
tempMinute = (hour*60) + minute;
}
}
return tempMinute;
}
}
This isn't all of my code as I have others just holding the values that will later be inserted into the Event queue. When I check the time outside of the Event queue it matches the time that it should be, say I have a Time object as 11:22 AM, but when I insert it into the Event queue my time changes to 23:22 AM. For some reason it is adding 12 hours within the Event queue and I don't understand why.
Figured it out the totalMinutes() method was messing with the hours because it was being called when using compareTo() or timeDifference() implicitly. Thank you for the help!
First, totalMinutes() messes with the hour field when it shouldn't. It should use a local variable.
Second, your entire Event.compareTo(Event e) method can be reduced to
return e.eventTime.compareTo(eventTime);
which is back to front, unless you want the most recent times first. Try
return eventTime.compareTo(e.eventTime);