Java PriorityQueue not working? - java

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);

Related

Is there something wrong with the setZone method?

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");

How to eliminate element from an arraylist?

I am currently working on my homework which is simulation to an alarm, I have a method called removeAlarm() , I wanna force remove the alarm which I added, I tried alarm.remove(0) and tried to iterator but in the test program the alarm is still there.
Does anyone observe the issue?
private ArrayList<String> alarm;
public AlarmClock() throws IllegalValueException {
super();
alarm = new ArrayList<String>();
}
public AlarmClock(int hour, int minute) throws IllegalValueException {
super(hour, minute);
alarm = new ArrayList<String>();
}
public void addAlarm(int hour, int minute) throws IllegalValueException {
int limit = 2;
int length = String.valueOf(hour).length();
String displayValue = "";
for (int i = 0; i < limit - length; i++) {
displayValue = displayValue + "0";
}
displayValue = displayValue + hour + ":";
limit = 2;
length = String.valueOf(minute).length();
for (int i = 0; i < limit - length; i++) {
displayValue = displayValue + "0";
}
displayValue = displayValue + minute;
alarm.add(displayValue);
}
public void removeAlarm() {
if (alarm.equals(getClass())) {
alarm.remove(alarm.equals(getTime()));
}
}
super.timeTick();
for (int x = 0; x < alarm.size(); x++) {
if (alarm.get(x).equals(getTime())) {
System.out.println("LARMET ÄR KLARR, VAKNAAAAA");
alarm.remove(x);
} else {
return;
}
}
My test program
try {
AlarmClock alarm = new AlarmClock(12, 3);
alarm.timeTick();
System.out.println(alarm.getTime());
alarm.timeTick();
System.out.println(alarm.getTime());
alarm.removeAlarm();
alarm.addAlarm(12, 6);
alarm.timeTick();
System.out.println(alarm.getTime());
} catch (IllegalValueException a) {
System.out.println(a.getMessage());
}
System.out.println("Expected: alarm removed at 12:06 ");
System.out.println(" ");
Output
12:04
12:05
LARMET ÄR KLARR, VAKNAAAAA
12:06
Expected: alarm removed at 12:06
alarm.remove(0) will remove the first item in the list. To remove an item from anywhere in the list the function would have to be
void removeAlarm(int target) {
alarm.remove(target);
}
or
void removeAlarm(int hour, int min) {
alarm.remove{hour+":"+min);
}
for just removing the last alarm:
void removeLastAlarm() {
alarm.remove(alarm.size()-1);
}

How to make my clock like this 03:05:01

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.

Loop will not execute properly

I am writing a program for class and the loop seems to not execute correctly. It always returns the value for i as 0. The rest of the code seems to work as advertised, i is just not increasing in index value.
public class Day {
String strDay;
private int d = 0;
private String[] Days = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
String day;
public Day() {
return;
}
public Day(String strDay) {// this is my issue. I think I am going about this constructor all wrong
for (int i = 0; i < Days.length; i++) {
if (strDay.equalsIgnoreCase(Days[i]))
d = i;
return;
}
}
public int getDay() {
return d;
}
public void nexDay() {
int next;
if (d < 6) {
next = (this.d) + 1;
System.out.println("Next Day is :" + Days[next]);
} else {
next = 0;
System.out.println("Next Day is :" + Days[next]);
}
}
public void prevDay() {
int prev = 0;
if ((d > 0) && (d < 6)) {
prev = (this.d) - 1;
System.out.println("previous day is " + Days[prev]);
} else
System.out.println("previous day id " + Days[6]);
}
public int calcDay(int num) {
int newDay;
this.d = d + num;
newDay = this.d % 7;
System.out.println("calc day is" + Days[d]);
return newDay;
}
public void print() {
System.out.println("day is " + Days[d]);
}
public static void main(String[] args) {
String day;
Day Callmethod = new Day();
Scanner console = new Scanner(System.in);
System.out.println("enter a day of the week");
day = console.nextLine();
Callmethod.print();
Callmethod.nexDay();
Callmethod.prevDay();
Callmethod.getDay();
}
}
Well, this
Day Callmethod = new Day();
is calling your empty constructor. Not your constructor with a loop (which takes a String). Also, Java variables start with a lower case letter (Callmethod looks like a class). I think you were looking for something like
Day day = new Day("SUNDAY");
Also, your if needs braces or the return will be invoked without doing anything (unless it matches on the first entry) like
for(int i=0;i<Days.length;i++){
if (strDay.equalsIgnoreCase(Days[i])) {
d = i;
return;
}
}
try change this:
public Day(String strDay)/// this is my issue. I think I am going about this constructor all wrong
{
for(int i=0;i<Days.length;i++){
if (strDay.equalsIgnoreCase(Days[i]))
d = i;
return;
}
}
for this:
public Day(String strDay)/// this is my issue. I think I am going about this constructor all wrong
{
for(int i=0;i<Days.length;i++){
if (strDay.equalsIgnoreCase(Days[i]))
{
d = i;
return;
}
}
}
Without the curly braces your conditional block will be only the next line of code. So your loop was only running once
public Day(String strDay) {
for (int i = 0; i < Days.length; i++) {
if (strDay == Days[i])
d = i;
return;
}
}
If I understood it clearly this will work. If not, just explain me what the goal is on that for loop. And place the return statement outisde of the next bracket.

Generics and Performance question

I was wondering if anyone could look over a class I wrote, I am receiving generic warnings in Eclipse and I am just wondering if it could be cleaned up at all. All of the warnings I received are surrounded in ** in my code below.
The class takes a list of strings in the form of (hh:mm AM/PM) and converts them into HourMinute objects in order to find the first time in the list that comes after the current time.
I am also curious about if there are more efficient ways to do this. This works fine but the student in me just wants to find out how I could do this better.
public class FindTime {
private String[] hourMinuteStringArray;
public FindTime(String[] hourMinuteStringArray){
this.hourMinuteStringArray = hourMinuteStringArray;
}
public int findTime(){
HourMinuteList hourMinuteList = convertHMStringArrayToHMArray(hourMinuteStringArray);
Calendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
HourMinute now = new HourMinute(hour,minute);
int nearestTimeIndex = findNearestTimeIndex(hourMinuteList, now);
return nearestTimeIndex;
}
private int findNearestTimeIndex(HourMinuteList hourMinuteList, HourMinute now){
HourMinute current;
int position = 0;
Iterator<HourMinute> iterator = **hourMinuteList.iterator()**;
while(iterator.hasNext()){
current = (HourMinute) iterator.next();
if(now.compareTo(current) == -1){
return position;
}
position++;
}
return position;
}
private static HourMinuteList convertHMStringArrayToHMArray(String[] times){
FindTime s = new FindTime(new String[1]);
HourMinuteList list = s.new HourMinuteList();
String[] splitTime = new String[3];
for(String time : times ){
String[] tempFirst = time.split(":");
String[] tempSecond = tempFirst[1].split(" ");
splitTime[0] = tempFirst[0];
splitTime[1] = tempSecond[0];
splitTime[2] = tempSecond[1];
int hour = Integer.parseInt(splitTime[0]);
int minute = Integer.parseInt(splitTime[1]);
HourMinute hm;
if(splitTime[2] == "AM"){
hm = s.new HourMinute(hour,minute);
}
else if((splitTime[2].equals("PM")) && (hour < 12)){
hm = s.new HourMinute(hour + 12,minute);
}
else{
hm = s.new HourMinute(hour,minute);
}
**list.add(hm);**
}
return list;
}
class **HourMinuteList** extends **ArrayList** implements RandomAccess{
}
class HourMinute implements **Comparable** {
int hour;
int minute;
public HourMinute(int hour, int minute) {
setHour(hour);
setMinute(minute);
}
int getMinute() {
return this.minute;
}
String getMinuteString(){
if(this.minute < 10){
return "0" + this.minute;
}else{
return "" + this.minute;
}
}
int getHour() {
return this.hour;
}
void setHour(int hour) {
this.hour = hour;
}
void setMinute(int minute) {
this.minute = minute;
}
#Override
public int compareTo(Object aThat) {
if (aThat instanceof HourMinute) {
HourMinute that = (HourMinute) aThat;
if (this.getHour() == that.getHour()) {
if (this.getMinute() > that.getMinute()) {
return 1;
} else if (this.getMinute() < that.getMinute()) {
return -1;
} else {
return 0;
}
} else if (this.getHour() > that.getHour()) {
return 1;
} else if (this.getHour() < that.getHour()) {
return -1;
} else {
return 0;
}
}
return 0;
}
}
If you have any questions let me know.
Thanks,
Rob
It's because you a not specify generics for your List and Comparable instances, that can support generics. You can rewrite your code with:
class HourMinuteList extends ArrayList<HourMinute> implements RandomAccess{
}
class HourMinute implements Comparable<HourMinute> {
public int compareTo(HourMinute aThat) {
....
}
}
Note: generics is not required, and not used at runtime, but it's better to use them because it helps you to avoid some bugs at your code.
I wouldn't use the HourMinute class, unless it has some other added value. If you only need to find the closest event time after a given point in time, convert your strings to Date (or to long values representing time), and store them in some sorted collection.
The conversion can be done with SimpleDateFormat.
If items are added dynamically, use TreeSet<Date>, together with ceiling(t) / higher(t) methods.
If the set of items is not dynamic, use an array Date[], together with Arrays.binarySearch(..).
Here is a (working) draft of the first approach:
public class TimedEventsMgr {
private TreeSet<Date> pointsInTime = new TreeSet<Date>();
private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm a");
//timeStr: hh:mm AM/PM
public void add(String timeStr) throws ParseException{
Date time = sdf.parse("20000101 "+timeStr);
pointsInTime.add(time);
}
public Date closestFutureTime(Date time){
Calendar c = Calendar.getInstance();
c.setTime(time);
c.set(Calendar.YEAR, 2000);
c.set(Calendar.MONTH, 0); //January
c.set(Calendar.DATE, 1);
return pointsInTime.higher(c.getTime());
}
}

Categories