Sign in with
Sign up | Sign in
Your question
Solved

Java MyDate code

Last response: in Applications
Share
February 21, 2011 2:17:32 AM

Hello all,

Pretty much got all the code done just confused on one part of the assignment, part A. How would I pass in an instance?


Here's the assignment, there were more that I deleted, was just get/set methods and all those work:

a. Write a method called Compare. This method should take a MyDate object as a parameter. If the passed in object contains a date that is before the date stored in the current class instance then return -1. If the date is the same then return 0. If the passed in object contains a date that is after the one stored in the current instance then return 1.
The method signature should be:
int Compare (MyDate other)

b. Write a method called Show. This method should print the date on the screen.
The method signature should be:
void Show()

c. Inside of main() create a few instances of MyDate.

d. Inside of main() you should write code that demonstrates that the MyDate class works properly for ALL of its methods.


  1. /**
  2. * @(#)MyDate.java
  3. *
  4. * MyDate application
  5. *
  6. * @author
  7. * @version 1.00 2011/2/20
  8. */
  9.  
  10. public class MyDate {
  11. private int year;
  12. private int month;
  13. private int day;
  14.  
  15. public MyDate(){
  16. year = 2011;
  17. month = 2;
  18. day = 15;
  19. }
  20.  
  21. public int GetYear(){
  22. return year;
  23. }
  24.  
  25. public int GetMonth(){
  26. return month;
  27. }
  28.  
  29. public int GetDay(){
  30. return day;
  31. }
  32.  
  33. public void SetYear(int newYear){
  34. year = newYear;
  35. }
  36.  
  37. public void SetMonth(int newMonth){
  38. if(newMonth > 12 || newMonth < 0){
  39.  
  40. }
  41. else{
  42. month = newMonth;
  43. }
  44. }
  45.  
  46. public void SetDay(int newDay){
  47. day = newDay;
  48. }
  49.  
  50. /*public int Compare(MyDate){
  51.  
  52. }*/
  53.  
  54. public void Show(){
  55. System.out.printf("%d/%d/%d\n",month,day,year);
  56.  
  57. }
  58.  
  59.  
  60. public static void main(String[] args) {
  61.  
  62. MyDate x1 = new MyDate();
  63. MyDate x2 = new MyDate();
  64. MyDate x3 = new MyDate();
  65.  
  66. x1.SetYear(2011);
  67. x1.SetMonth(5);
  68. x1.SetDay(20);
  69. int a1 = x1.GetYear();
  70. int b1 = x1.GetMonth();
  71. int c1 = x1.GetDay();
  72.  
  73.  
  74. x2.SetYear(2010);
  75. x2.SetMonth(8);
  76. x2.SetDay(2);
  77. int a2 = x2.GetYear();
  78. int b2 = x2.GetMonth();
  79. int c2 = x2.GetDay();
  80.  
  81.  
  82. x3.SetYear(2011);
  83. x3.SetMonth(3);
  84. x3.SetDay(15);
  85. int a3 = x3.GetYear();
  86. int b3 = x3.GetMonth();
  87. int c3 = x3.GetDay();
  88.  
  89.  
  90. x1.Show();
  91. x2.Show();
  92. x3.Show();
  93.  
  94. }
  95. }

More about : java mydate code

a b L Programming
February 21, 2011 2:16:59 PM

Your compare function needs to have a name for MyDate, like:

  1. public int Compare(MyDate date){
  2. // 'date' is now a MyDate object, which you can compare to this current
  3. // object
  4. }

Best solution

March 4, 2011 12:31:28 PM
Share

i revise and add imports and method on your code...hope it helps, i am a newbie too ☺


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ADMIN
*/

public class MyDate {
private int year;
private int month;
private int day;

public MyDate(){
year = 2011;
month = 2;
day = 15;
}

public int GetYear(){
return year;
}

public int GetMonth(){
return month;
}

public int GetDay(){
return day;
}

public void SetYear(int newYear){
year = newYear;
}

public void SetMonth(int newMonth){
if(newMonth > 12 || newMonth < 0){

}
else{
month = newMonth;
}
}

public void SetDay(int newDay){
day = newDay;
}

//string to date converter...
private Date convertStringToDate(String stringDate) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

Date theDate = dateFormat.parse(stringDate);

return theDate;
}

//this is compare...it should have two object arguments...
public int Compare(MyDate d1, MyDate d2) throws ParseException{
return (convertStringToDate(String.valueOf(d1.GetMonth()).concat("/"+String.valueOf(d1.GetDay())).concat("/"+String.valueOf(d1.GetYear()))).
compareTo(convertStringToDate(String.valueOf(d2.GetMonth()).concat("/"+String.valueOf(d2.GetDay())).concat("/"+String.valueOf(d2.GetYear())))));
}

public void Show(MyDate d1, MyDate d2) throws ParseException{
System.out.printf("%d/%d/%d",month,day,year);
System.out.println("\t"+Compare(d1, d2));
}

public static void main(String[] args) throws ParseException {
MyDate x0 = new MyDate(); //default...
MyDate x1 = new MyDate();
MyDate x2 = new MyDate();
MyDate x3 = new MyDate();

x1.SetYear(2011);
x1.SetMonth(5);
x1.SetDay(20);
//... lines deleted


x2.SetYear(2010);
x2.SetMonth(8);
x2.SetDay(2);
//... lines deleted


x3.SetYear(2011);
x3.SetMonth(3);
x3.SetDay(15);
//... lines deleted

System.out.println("LEGEND:\n0 - [ = ]\n1 - [ > ]\n-1 - [ < ]\n"); //line temporary...to show comparison legend

x0.Show(x0, x0); //added to show default date...[ temporary ]
x1.Show(x1, x0);
x2.Show(x2, x1);
x3.Show(x3, x2);
}
}
Related resources
March 5, 2011 2:43:26 PM

Hehehe thanks, forgot to delete this, got help from professor. Was fairly simple once he explained it.
March 5, 2011 2:43:37 PM

Best answer selected by laserpp.
!