In my Java class, I have to build a calendar application. I've got it mostly completed, however I need help with a couple of methods. I have commented the parts that I need help with. The code includes three classes and a main called TestCalendar. The functions I need help with are located in the Calendar class, named removeEvent(two of them, taking two different arguments), printEvents, and findEvents. Thanks in advance!
Here is the Date class.
public class Date {
int year, month, day;
//constructor
public Date(int yr, int mth, int dy){
year = yr;
if (yr < 2000 || yr > 2100)
{
System.out.println("Wrong Calander Year");
System.exit(1);
}
month = mth;
if (mth < 1 || mth > 12)
{
System.out.println("Wrong Month");
System.exit(1);
}
day = dy;
if (dy < 1 || dy > 31)
{
System.out.println("Wrong Day");
System.exit(1);
}
}
//accessor methods
public int getYear()
{
return year;
}
public int getMonth()
{
return month;
}
public int getDay()
{
return day;
}
//returns date in correct format
public String toString()
{
return "" + month + "/" + day + "/" + year;
}
}
Here is the Event class
public class Event {
Date date;
int hour;
String activity;
Event(int year, int month, int day, int hour, String activity)
{
if (year < 2000 || year > 2100)
{
System.out.println("Wrong Calander Year");
System.exit(1);
}
if (month < 1 || month > 12)
{
System.out.println("Wrong Month");
System.exit(1);
}
if (day < 1 || day > 31)
{
System.out.println("Wrong Day");
System.exit(1);
}
this.date = new Date(year, month, day);
this.hour = hour;
this.activity = activity;
}
public Date getDate()
{
return date;
}
public int getHour()
{
return hour;
}
public String getActivity()
{
return activity;
}
void setActivity(String newActivity)
{
this.activity = newActivity;
}
public String toString()
{
return "" + date +" " + "#" + hour +":" + " " + activity;
}
public boolean equals(Object obj)
{
if (obj instanceof Event)
{
return true;
}
else return false;
}
}
The Calendar class
public class Calander {
static final int MAXEVENTS = 10;
Event[] events;
int numEvents;
// constructor
public Calander() {
numEvents = 0;
events = new Event[MAXEVENTS];
}
void addEvent(int year, int month, int day, int hour, String activity) {
Event newEvent = new Event(year, month, day, hour, activity);
events[numEvents] = newEvent;
numEvents++;
}
void removeEvent(int year, int month, int day, int hour, String activity) {
{
if (events[numEvents] == null);
numEvents--;
}
}
// instructions say to remove (all) Event objects in the Calendar that are equals to the event argument. Use the equals method from the event class
void removeEvent(Event event) {
//what to put here?
}
// this method needs to print every Event in the associated Calendar that matches the date arguments. Print each on a separate line, using the toString method from the Event class.
void printEvents(int year, int month, int day) { // how to set equality
if (this.events[numEvents] == )
{
// what to put here?
}
}
// same as above but matches the (Date date) arguments
void printEvents(Date date) {
toString();
}
// Return the first Event in the Calendar that has a matching (equals) activity field. If no match is found, you must return a reference type, so return null.
Event findEvent(String activity) {
//what to put here?
return null;
}
void dump() {
for (int i = 0; i < MAXEVENTS; i++)
{
if (events[i] != null)
System.out.println(events[i]);
}
}
}
well, your event class has a method:
public boolean equals(Object obj)
Which, presumably, should return whether or not the passed event is equal to the instance.
So your void removeEvent(Event event) method should look similar to the following:
take note that this is psudo-code and not valid java. you're going to have to flesh out the details on your own.
void removeEvent(Event event)
{
foreach(event e in this.events)
{
if(event.equals(e))
{
// remove e from the events array
}
}
}
The rest of the methods are going to more or less be similar in concept to the first one with 2 varying factorrs:
how you identify a match
what you do with the match
Since this is homework, I don't actually want to do your homework. So as a hint, you want to use (your event).equals(comparing to other event), not "==".
Related
How does the compareTo() method for Dates work here in java? I know that when you compare two dates the result will always be 0 if equal, 1 if the date being
compared inside the compareTo() parameter is older, and -1 if the date inside the parameter is more recent.
//Just an example
String[] da = {"01/14/1975", "08/20/1975", "08/20/1975"};
SimpleDateFormat f = new SimpleDateFormat("MM/dd/yyyy");
Date d1 = new Date();
Date d2 = new Date();
//this outputs 1 because d2 is older than d1
d1 = f.parse(da[1]);
d2 = f.parse(da[0]);
System.out.println(d1.compareTo(d2));
//this outputs 0 because dates are the same
d1 = f.parse(da[1]);
d2 = f.parse(da[2]);
System.out.println(d1.compareTo(d2));
//this outputs -1 because d2 is more recent than d1
d1 = f.parse(da[0]);
d2 = f.parse(da[1]);
System.out.println(d1.compareTo(d2));
Now I want to compare dates without using compareTo() method or any built-in method in java. As much as possible I want to use just the basic operators in java.
What is the computation or the algorithm of the compareTo() method in comparing dates that enable it to return -1, 0, and 1?
Edit:
In the case at the sample problem at my book, using java.util.Date is forbidden, what is supposed to be done is to create your own date object like this:
public class DatesObj
{
protected int day, month, year;
public DatesObj (int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
public int getMonth() { return month; }
public int getDay() { return day; }
public int getYear() { return year; }
}
Now how do I compare this as if like they're int and determine which is old and which is newer??
If you want to compare two dates just as if they were just plain-old integers, you must first turn each date into a plain-old integer. The easiest way to turn a year/month/day representation of a date into a plain-old integer, that can be effectively compared with plain-old integers from other dates, is to line the pieces up in exactly that order: year first, month next, day last:
// in DateObj class....
public int getDateInt() {
return (yyyy * 10000) + (mm * 100) + dd;
}
So for March 19, 2019, you get 20190319, and for December 7, 1941 you get 19411207; by comparing the "integerized" versions of the dates you can see that:
19411207 < 20190319, just as December 7, 1941 is earlier than March 19, 2019;
20190319 > 19411207, just as March 19, 2019 is later than December 7, 1941;
19411207 != 20190319, just as December 7, 1941 and March 19, 2019 are different dates
You're limited to dates within the Common Era and no more than about 200,000 years into the future with this particular implementation. But with a little tweaking, you could easily easily handle dates outside these ranges, an exercise that I will, as the textbooks so often say, leave as an exercise for the reader.
Implement Comparable and override compareTo().
class DatesObj implements Comparable<DatesObj>{
protected int day, month, year;
public DatesObj(int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() { return year; }
#Override
public int compareTo(DatesObj o) {
int diff = this.year - o.year;
if(diff != 0) {
return diff;
}
diff = this.month - o.month;
if(diff != 0) {
return diff;
}
return this.day - o.day;
}
}
Compare the years. If the years of both the dates are same, compare the months.
If the months are same, compare the dates.
public int compareDate(DatesObj d) {
if (this.year != d.year) {
if (this.year > d.year)
return 1;
else
return -1;
}
if (this.month != d.month) {
if (this.month > d.month)
return 1;
else
return -1;
}
if (this.day != d.day) {
if (this.day > d.day)
return 1;
else
return -1;
}
return 0;
}
Ref : https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
Ref : https://developer.android.com/reference/java/util/Calendar
create own class with Interface Comparable
class DateCompare implements Comparable<Date>
{
protected int day, month, year;
public DateCompare(int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
#Override
public int compareTo(Date o) {
Calendar cal = Calendar.getInstance();
cal.setTime(o);
int diff = this.year - cal.get(Calendar.YEAR);
if(diff != 0) {
return diff;
}
diff = this.month - cal.get(Calendar.MONTH);
if(diff != 0) {
return diff;
}
return this.day - cal.get(Calendar.DAY_OF_MONTH);
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() { return year; }
}
And Other More Helpful
https://gist.github.com/Ashusolanki/fed3b6a680092985ac0ab93ed70fcd7c
private String postTime(Date date)
{
long postTime = date.getTime();
long atTime = System.currentTimeMillis();
long diff = atTime - postTime;
long sec = TimeUnit.SECONDS.convert(diff, TimeUnit.MILLISECONDS);
if (sec >= 60) {
long minit = TimeUnit.MINUTES.convert(diff, TimeUnit.MILLISECONDS);
if (minit >= 60) {
long hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
if (hours >= 24) {
long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
return days + " Days Ago";
} else {
return hours + " Hours Ago";
}
} else {
return minit + " Minutes Ago";
}
} else {
return sec + " Secounds Ago";
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following code that I am trying to get working for a project in a class that I am in. I will also include the requirements
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:
A. Set the day.
B. Print the day.
C. Return the day.
D. Return the next day.
E. Return the previous day.
F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
G. Add the appropriate constructors.
H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.
I. Write a program to test various operations on the class Day.
The code is as follows:
`import java.util.*;
class Day {
private int dayValue;
private static final int INVALID = -1;
public Day() { this.dayValue = INVALID; }
public Day(String day) { setDay(day); }
public Day(int day) { this.dayValue = (day<0 || day>6) ? INVALID : day; }
public void setDay(String day) {
if(day.equals("sunday") || day.equals("Sun")) {
this.dayValue = 0;
} else if(day.equal("monday") || day.equals("Mon")) {
this.dayValue = 1;
} else if(day.equals("tuesday") || day.equals("Tues")) {
this.dayValue = 2;
} else if(day.equal("wednesday") || day.equals("Wed")) {
this.dayValue = 3;
} else if(day.equals("thursday") || day.equals("Thurs")) {
this.dayValue = 4;
} else if(day.equal("friday") || day.equals("Fri")) {
this.dayValue = 5;
} else if(day.equal("saturday") || day.equals("Sat")) {
this.dayValue = 6;
} else {
this.dayValue = INVALID;
}
}
public String getDay() {
if (dayValue==0) { return "Sunday"; }
if (dayValue==1) { return "Monday"; }
if (dayValue==2) { return "Tuesday"; }
if (dayValue==3) { return "Wednesday"; }
if (dayValue==4) { return "Thursday"; }
if (dayValue==5) { return "Friday"; }
if (dayValue==6) { return "Saturday"; }
return "\"I don't know what day it is!\"";
}
public void printDay() {
System.out.println("When printing, your day is " + getDay()); //displays the day at the time of printing.
}
// Next Day
public String getNextDay()
{
// the compareTo() method allows us to set saturday as Sat, Sunday to Sun, etc
if((day.compareTo("sunday") == 0) || (day.compareTo("Sun") == 0))
return ("Monday");
else if((day.compareTo("monday") == 0) || (day.compareTo("Mon") == 0))
return ("Tuesday");
else if((day.compareTo("tuesday") == 0) || (day.compareTo("Tue") == 0))
return ("Wednesday");
else if((day.compareTo("wednesday") == 0) || (day.compareTo("Wed") == 0))
return ("Thursday");
else if((day.compareTo("thursday") == 0) || (day.compareTo("Thu") == 0))
return ("Friday");
else if((day.compareTo("friday") == 0) || (day.compareTo("Fri") == 0))
return ("Saturday");
else if((day.compareTo("saturday") == 0) || (day.compareTo("Sat") == 0))
return ("Sunday");
else
return ("\"I don't know what day it is!\"");
}
// Previous day
public String getPreDay()
{
if((day.compareTo("sunday") == 0) || (day.compareTo("Sun") == 0))
return ("Saturday");
else if((day.compareTo("saturday") == 0) || (day.compareTo("Sat") == 0))
return ("Friday");
else if((day.compareTo("friday") == 0) || (day.compareTo("Fri") == 0))
return ("Thursday");
else if((day.compareTo("thursday") == 0) || (day.compareTo("Thu") == 0))
return ("Wednesday");
else if((day.compareTo("wednesday") == 0) || (day.compareTo("Wed") == 0))
return ("Tuesday");
else if((day.compareTo("tuesday") == 0) || (day.compareTo("Tue") == 0))
return ("Monday");
else if((day.compareTo("monday") == 0) || (day.compareTo("Mon") == 0))
return ("Sunday");
return ("\"I don't know what day it is!\"");
}
public Day calcDay(int offset) { /* your code here */ }
// extra good for printin
public String toString() { return getDay(); }
}
// main execution point
public static void main (String args[]) {
{
// One of its weakness is the case sensitive: sun, Sunday, sunday, SuNdAy...
// need more codes to avoid this case sensitiveness...
// instantiate testday object of type MyDay class
// with "Sun" as initial value....
Day testday = new Day("Sun");
// prompt user to set his/her day
System.out.print("Enter day to set your day: ");
// read and store user's day
String storeday = readinput.nextLine().toLowerCase(); //Changes input into all lowercase to deal with variations
// invoke setDay() method to set a day defined by user
testday.setDay(storeday);
// invoke getDay() method to get a day
System.out.println("Your day is " + testday.getDay());
// test printing by invoking printDay() method
testday.printDay();
// invoke getPreDay() method to get the previous day
System.out.println("Your previous day is " + testday.getPreDay());
// invoke getNextDay() method to get the next day
System.out.println("Your next day is " + testday.getNextDay());
System.out.println("How many Days would you like to add? " + testday.calcNextDay());
}
}`
I am receiving the following error:
Day.java:92: error: class, interface, or enum expected
public static void main () {
^
Day.java:101: error: class, interface, or enum expected
System.out.print("Enter day to set your day: ");
^
Day.java:103: error: class, interface, or enum expected
String storeday = readinput.nextLine().toLowerCase(); //Changes input into all lowercase to deal with variations
^
Day.java:105: error: class, interface, or enum expected
testday.setDay(storeday);
^
Day.java:107: error: class, interface, or enum expected
System.out.println("Your day is " + testday.getDay());
^
Day.java:109: error: class, interface, or enum expected
testday.printDay();
^
Day.java:111: error: class, interface, or enum expected
System.out.println("Your previous day is " + testday.getPreDay());
^
Day.java:113: error: class, interface, or enum expected
System.out.println("Your next day is " + testday.getNextDay());
^
Day.java:115: error: class, interface, or enum expected
System.out.println("How many Days would you like to add? " + testday.calcNextDay());
^
Day.java:116: error: class, interface, or enum expected
}
^
10 errors
Originally my code looked like this
public class Day
{
static Scanner readinput = new Scanner(System.in);
String day;
public Day(String day)
{
day = "Sunday";
}
// set the day
public void setDay(String theDay)
{
day = theDay;
}
public String getDay()
{
return day;
}
public void printDay()
{
System.out.println("When printing, your day is " + day);
}
Why not use arrays?
With an array, you could handle things in a more concise and simple way.
String[] dayOfWeekShortNames = new String[] {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
String[] dayOfWeekLongNames = new String[] {
"Sunday", "Monday", "Wednesday", "Thursday", "Friday", "Saturday"
};
Then, simply validate whether the value passed in parameter is correct.
public void setDay(String dayOfWeek) {
if (dayOfWeek == null || (0 < dayOfWeek.length() && dayOfWeek.trim().length() == 0))
throw new IllegalArgumentException("dayOfWeek cannot be null or white space.");
for (int i = 0; i < dayOfWeek.length(); i++)
if (dayOfWeek.charAt(i).isDigit())
throw new IllegalArgumentException("dayOfWeek cannot be numeric.");
if (dayOfWeek.length() < 3) // for short names
throw new IllegalArgumentException("dayOfWeek must be at least 3 characters long.");
for (int i = 0; i < dayOfWeekShortNames.length && i < dayOfWeekLongNames.length; i++)
if (dayOfWeekShortNames[i].toLowerCase() == dayOfWeek.ToLowerCase()
|| dayOfWeekLongNames[i].toLowerCase() == dayOfWeek.ToLowerCase()) {
dayValue = i;
return;
}
throw new IllegalArgumentException(
"Day of week: " + dayOfWeek + " could not be found.");
}
This is just a simple example off the top of my head.
Why not use a Program class?
Instead of just writing the main() method out of nowhere which can lead to multiple compile-time errors, perhaps one great deal would be to locate your main method inside a Program class so that it is a member of a class, and the compiler shall no longer complaint. Furthermore, the advantage of doing so shall make it obvious, at least at some extent, that your program entry point is there. This Program class shall contain nothing more than the main() method.
public class Program {
public static void main(String args[]) {
// You code here...
}
}
As for the user inputs, perhaps using the Console class... Here's an example on how to use it.
I/O from the Command Line
Disclaimer
I have not tested this code and is provided as-is for example purposes only. I am not a Java expert, and I did my best off the top of my head to help.
EDIT
I did not use an array because I wanted to store the value as a string and not the index.
In fact, the code sample in your question stores the day of week in an integer value as per this line:
private int dayValue;
Hence the assignments when you set the day as follows:
public void setDay(String day) {
if (day == "sunday" || day == "Sun")
dayValue = 0;
else if (day == "monday" || day == "Mon")
dayValue = 1;
...
}
The above actually stores the day of week in an integer format which shall satisfy most of any systems which is going to use days of week. Plus, you cannot guarantee the exact syntax of a string input by the user so that even if you either compare day == "sunday" or day == "Sun", you would have to adjust your casing according to the string you're expecting in your setDay() function, which in my opinion doesn't make sense.
The most common practice for such behaviour playing around with days of week, etc. is to use arrays with the proper expected casing, then compare the input values with either uppercase or lowercase values for both, the input and the value from the array (only this can guarantee a perfect match), then you can store the index in you dayValue private member. Then, when retrieving the day of week through the getDay() function, you can simply write a single line of code which can assure you to work flawlessly, since you captured any potential errors while setting the input through the setDay() method.
public String getDay() { return dayOfWeekLongNames[dayValue]; }
And you shall get the name of the day of week previously set, instead of having to write if statements over and over again.
Another way to approach the problem is to use an enum for the days of the week. Here's a working example provided to show the general idea:
public class Day
{
enum DAY {
MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY(
"Saturday"), SUNDAY("Sunday");
public static DAY parse(final String value)
{
for (final DAY day : values())
{
if (day.description.equalsIgnoreCase(value) || day.description.substring(0, 3).equalsIgnoreCase(value))
{
return day;
}
}
return null;
}
private String description;
private DAY(final String description)
{
this.description = description;
}
#Override
public String toString()
{
return description;
}
}
public static void main(final String args[])
{
Day myDay;
myDay = new Day(DAY.SUNDAY);
System.out.println(myDay);
System.out.println(myDay.getPreviousDay());
System.out.println(myDay.getNextDay());
myDay = new Day("monday");
System.out.println(myDay);
System.out.println(myDay.getPreviousDay());
System.out.println(myDay.getNextDay());
myDay = new Day("wed");
System.out.println(myDay);
System.out.println(myDay.getPreviousDay());
System.out.println(myDay.getNextDay());
}
private final DAY day;
public Day(final DAY day)
{
this.day = day;
}
public Day(final String day)
{
this.day = DAY.parse(day);
}
public DAY getNextDay()
{
final DAY[] days = DAY.values();
return days[(day.ordinal() + 1) % days.length];
}
public DAY getPreviousDay()
{
final DAY[] days = DAY.values();
return days[((day.ordinal() - 1) + days.length) % days.length];
}
#Override
public String toString()
{
return day.toString();
}
}
When I try to run this program, the result is always null, 0 0. Why do the values of monthName, day, and year not show up when the getDay() method is invoked and printed on the screen.
public class Assignment1 {
public static void main(String[] args) {
//creates an array of type Date filled with two LongDate objects
Date [] collectionOfDates = { new LongDate("February",2,1996), new LongDate("February",13,1999) };
// loops through the array and displays output of getDate() for each object
for( int i = 0; i < collectionOfDates.length; i++ ) {
System.out.println( collectionOfDates[i].getDate() );
}
}
}
For your information, the LongDate class is a subclass of the Date class which contains methods editDay() and editYear() along with several others. The LongDate method is listed below.
Any help greatly appreciated, Thanks. Also, feel free to comment if you want more information.
public class LongDate extends Date {
private String monthName;
private int day;
private int year;
public LongDate() {
}
public LongDate(String m, int d, int y) {
super.editday(d);
super.edityear(y);
editMonth(m);
}
public void setDate(String m, int d, int y) {
monthName = m;
day = d;
year = y;
}
public String getDate() {
StringBuilder fullDate = new StringBuilder();
fullDate.append(monthName);
fullDate.append(" ");
fullDate.append(day);
fullDate.append(", ");
fullDate.append(year);
return fullDate.toString();
}
public String getShortDate() {
int month = 0;
if (monthName == "January") {
month = 1;
} else if (monthName == "February") {
month = 2;
} else if (monthName == "March") {
month = 3;
} else if (monthName == "April") {
month = 4;
} else if (monthName == "May") {
month = 5;
} else if (monthName == "June") {
month = 6;
} else if (monthName == "July") {
month = 7;
} else if (monthName == "August") {
month = 8;
} else if (monthName == "September") {
month = 9;
} else if (monthName == "October") {
month = 10;
} else if (monthName == "November") {
month = 11;
} else if (monthName == "December") {
month = 12;
}
StringBuilder shortDate = new StringBuilder();
shortDate.append(month);
shortDate.append("/");
shortDate.append(day);
shortDate.append("/");
shortDate.append(year);
return shortDate.toString();
}
protected String editMonth(String m) {
// asks person to try again if month is not capitalized and spelled properly
if (m != "January" && m != "February" && m != "March" && m != "April" && m != "May" && m != "June" && m != "July" && m != "August" && m != "September" && m != "October" && m != "November" && m != "December") {
m = Input.getString( "Invalid month. Please type the month again." );
return m;
} else
return m;
}
}
There's nothing in the constructor of LongDate which sets the fields (monthName, day, and year) that getDate() reads.
I assume that the Date#editDay() and Date#editYear() functions look similar to LongDate#editMonth(). Note that editMonth() does not assign a value to the monthName field!
You should compare your strings with equals() and not ==. The equals() method compares string values, whereas == compares object references, which is not what you want here. So change:
if (monthName == "January") {
to:
if (monthName.equals("January")) {
and similarly for the other comparisons.
Couple of issues. First:
public LongDate(String m, int d, int y) {
super.day(d);
super.year(y);
editMonth(m);
}
You don't show Date so it is unclear to us what day() and year() are supposed to do, but regardless:
public class LongDate extends Date {
private String monthName;
private int day;
private int year;
...
}
Your declarations of these fields are hiding any similar fields that the base presumably has. In any case, at no point in your constructor are you setting this.day or this.year to anything, and so, of course, they remain at their initial value of 0.
You need to clean up your code a bit. Either refer to the correct day and year, or make sure you are setting and getting the base class' version of those fields instead of redeclaring them in the subclass (again, not sure what your base implementation does).
You may want to have a look at the official tutorial on Inheritance. It's concise and well-written and covers topics like overriding methods, hiding fields, etc. I think it will give you a good starting point for solving your issues here.
And, of course, comparing strings with == here will lead to other issues in the future: How do I compare strings in Java?
Your editMonth method returns a string, instead it should set the month:
monthName = m;
Another option is to keep the editMonth method the same, but in your constructor put:
monthName = editName(m);
What I'm trying to do is access an object, in this case date1 which has 3 attributes day, month and year. I'm attempting to make a method called showTomorrow() which will display the objects information 1 day infront in String format. This means I cannot alter the attributes of the original object.
I've written the Data.java program and it's shown below, if someone could point me in the right direction or show me what it would be really helpfull.
This is what I'd essentially be running on my main method I believe.
**Date date1 = new Date(30, 12, 2013)** // instantiate a new object with those paramaters
**date1.showDate();** // display the original date
**date1.tomorrow();** // shows what that date would be 1 day infront
The problem is right now it's not displaying anything. I thought that by saying dayTomorrow = this.day++; I was adding it's default value + 1 day to the variable dayTomorrow.
public class Date
{
private int day;
private int month;
private int year;
private int dayTomorrow;
private int monthTomorrow;
private int yearTomorrow;
public Date()
{
day = 1;
month = 1;
year = 1970;
}
public Date(int inDay, int inMonth, int inYear)
{
day = inDay;
month = inMonth;
year = inYear;
}
public void setDate(int inDay, int inMonth, int inYear)
{
day = inDay;
month = inMonth;
year = inYear;
}
public String getDate()
{
String strDate;
strDate = day + "/" + month + "/" + year;
return strDate;
}
public String getTomorrow()
{
String strTomorrow;
strTomorrow = dayTomorrow + "/" + monthTomorrow + "/" + yearTomorrow;
return strTomorrow;
}
public String tomorrow()
{
dayTomorrow = this.day++;
monthTomorrow = this.month;
yearTomorrow = this.year;
if(dayTomorrow > 30)
{
dayTomorrow = 1;
monthTomorrow = this.month++;
}
if(monthTomorrow > 12)
{
monthTomorrow = 1;
yearTomorrow = this.year++;
}
return getTomorrow();
}
public void showDate()
{
System.out.print("\n\n THIS OBJECT IS STORING ");
System.out.print(getDate());
System.out.print("\n\n");
}
public void showTomorrow()
{
System.out.print("\n\n THE DATE TOMORROW IS ");
System.out.print(getTomorrow());
System.out.print("\n\n");
}
public boolean equals(Date inDate)
{
if(this.day == inDate.day && this.month == inDate.month && this.year == inDate.year)
{
return true;
}
else
{
return false;
}
}
}
You just need to use ++this.day, ++this.month and ++this.year. When you use this.day++ it returns the previous date value, not the new. Putting the ++ in the front solves the problem. Also, it changes the day value... you might want to change that to this.day + 1.
Are You calling showDate() after date1.tomorrow() to show your output?
or instead of date1.tomorrow(); call date1.showTomorrow();
Have a look at this : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
post incremention ...
You could use the native date support in java but I figured you are just practicing right?
This should do the trick:
public class Date {
private int day = 1;
private int month = 1;
private int year = 1970;
private int dayTomorrow = day+1;
private int monthTomorrow;
private int yearTomorrow;
public Date()
{
tomorrow();
}
public Date(int inDay, int inMonth, int inYear)
{
day = inDay;
month = inMonth;
year = inYear;
tomorrow();
}
public void setDate(int inDay, int inMonth, int inYear)
{
day = inDay;
month = inMonth;
year = inYear;
}
public String getDate()
{
String strDate;
strDate = day + "/" + month + "/" + year;
return strDate;
}
public String getTomorrow()
{
String strTomorrow;
strTomorrow = dayTomorrow + "/" + monthTomorrow + "/" + yearTomorrow;
return strTomorrow;
}
public void tomorrow()
{
monthTomorrow = this.month;
yearTomorrow = this.year;
if(dayTomorrow > 30)
{
dayTomorrow = 1;
monthTomorrow = this.month++;
}
if(monthTomorrow > 12)
{
monthTomorrow = 1;
yearTomorrow = this.year++;
}
}
public void showDate()
{
System.out.print("\n\n THIS OBJECT IS STORING ");
System.out.print(getDate());
System.out.print("\n\n");
}
public void showTomorrow()
{
System.out.print("\n\n THE DATE TOMORROW IS ");
System.out.print(getTomorrow());
System.out.print("\n\n");
}
public boolean equals(Date inDate)
{
if(this.day == inDate.day && this.month == inDate.month && this.year == inDate.year)
{
return true;
}
else
{
return false;
}
}
}
Look carefully for any changes i've made ;)
Here's the main:
public static void main(String[] args) {
Date d = new Date();
d.showDate();
d.showTomorrow();
}
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());
}
}