Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help on this class. Almost done

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help on this class. Almost done

    In my class we each have a class my part is this:

    Game
    -String opponent
    -Date date
    -----------------------------------------------------------------------------------------------------------------
    +Game(String opp, int month, int day, int year, int hours, int min)
    +String getOpponent()
    +void setOpponent(String opp)
    +Date getDate()
    +void setDate(int month, int day, int year, int hours, int min)
    +String toString()
    +boolean equals(Object o) //both field equal
    +int compareTo(Object o) //ordered by date

    -------------------------------------------------------------

    Im not sure how to get the date to work the way that its suppose to in the UML diagram and also need help with the compareTo

    this is what I have:
     
    import java.util.Date;
     
     
    public class Game {
    private String opponent;
    Date date;
     
     
    Game(String opponent, int month, int day, int year, int hours, int min){
    	this(opponent, date);
    }
    //getter & setter opponent
    public String getOpponent() {
    	return opponent;
    }
    public void setOpponent(String opponent) {
    	this.opponent = opponent;
    }
    //get date and set date
    Date getDate(){
    	return date;
    }
     
    public void setDate(int month, int day, int year, int hours, int min) {
    	this.date = date;
    }
     
    public String toString(){
    	return "The date and time of the game is " + date;
    }
    public int compareTo(Game temp){
    	return this.getDate() - temp.getDate();
    	}
    // equals
    public boolean equals (Object o){
    	if( o instanceof Game){
    		Game temp = (Game)o;
    		return this.getOpponent().equals((temp.getOpponent())) && this.getDate() == temp.getDate();}
    	 else {return false;}
     
     
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help on this class. Almost done

    Please read the Announcement topic at the top of each/every sub-forum to learn how to post your code in code/formatting tags.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help on this class. Almost done

    adjusted it thanks

  4. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Help on this class. Almost done

    You can't just use the - operator for things that aren't primitives. (The String class lets you use the + operator but that's beside the point.)


    This isn't C++.

    You can't subtract one Date from another that way.

    For another issue, Date class is mostly deprecated now in the way you're using it. Use the Calendar (Java Platform SE 7 ) class instead.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help on this class. Almost done

    so how do I write this part
    Game(String opponent, int month, int day, int year, int hours, int min){
    	this(opponent, date);
    }
    Im getting the error on date and I understand kind of what you are saying. I just have to do it by following the uml that was provided but im confused by the int parameters or what im suppose to do in the game constructor

    when it comes to the compare to im suppose to use it to order them by date which im not sure how to do that. Obviously getting an error there

  6. #6
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Help on this class. Almost done

    Call your setter methods.

    setOpponent(opponent);

    in your constructor.

    You will need to construct a new date object using your five int params and pass it to your setDate() method.

    Calendar class has a compareTo() method.

    You can't call Calendar constructor directly but you can get a Calendar object by calling one of the static getInstance() methods, preferably the no param one.

    As I don't see a boolean field for A.M./P.M. in your UML, I'm assuming you're using military time (i.e. 0:00 - 23:59) for hours.

    If so, then you can use your new Calendar object and call

    set(int year, int month, int day, int hourOfDay, int minute);

    Note that the you use the value 0 for the month of January and so on up to 11 for December.

    Then you can use your Calendar object to call setDate() with it as a parameter.

Similar Threads

  1. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 02:30 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  4. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  5. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM