I have no idea how to do this:

Create a class called Date that contains three pieces of information as instance variables – a month, day and year e.g. 4, 14, 2007. All three of type int. Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. Provide a displayDate method that displays the month, day and year separated by forward slashes (/). Provide a calcAge method to calculate and display the person’s age in years. The method should receive the current year as a parameter (type int). A get method must return the age.

Write a test application called testDate that will test Date class. Assume the initial values are 10, 23, 2005

Recommended Answers

All 21 Replies

Okay? Sorry to hear that, but what is your question again?

The instructions are very clear and detailed. Just start with the first one "Create a class called Date", then carry on, doing one step at a time. When you get stuck, come back here, post what you have done so far, and explain exactly what's confusing you about the next step.

Would this be right for the Date class?

    public class Date

        private int today_month; 

       public void setTodayMonth( int month )
       {
          today_month = month; 
       } 

       public int getTodayMonth()
       {
          return today_month;
       } 


       private int today_day ; 

       public void setTodayDay( int day )
       {
          today_day = day; 
       } 

       public int getTodayDay()
       {
          return today_day;
       } 


       private int today_year; 

       public void setTodayYear( int year )
       {
          today_year = year; 
       } 

       public int getTodayYear()
       {
          return today_year;
       } 

       public void displayMessage1()
       {
          System.out.printf("\nThe month is: " + getTodayMonth());
       } 

       public void displayMessage2()
       {
          System.out.printf("\nThe day is: " + getTodayDay());
       } 

       public void displayMessage3()
       {
          System.out.printf("\nThe year is: " + getTodayYear());
       } 

       public void displayDate()
       {
           System.out.println("\nThe Date is: " + getTodayDay() + "/" + getTodayMonth() + "/" + getTodayYear());
        }


   public void calcAge()
   {
       int age = 2013 - getTodayYear();
       System.out.println("The age is: " + age);
    }
}

And for the TestDate class??

public class DateTest
{
    public static void main( String args[] )
   { 
      Scanner input = new Scanner(System.in);

      Date date1 = new Date(); 

      System.out.print("Enter the Month:");
      int month = input.nextInt();
      System.out.println();
      date1.setTodayMonth(month);

      System.out.print("Enter the Day:");
      int day = input.nextInt();
      System.out.println();
      date1.setTodayDay(day);   

      System.out.print("Enter the Year:");
      int year = input.nextInt();
      System.out.println();
      date1.setTodayYear(year); 

      date1.displayMessage1(); 
      date1.displayMessage2(); 
      date1.displayMessage3(); 
      date1.displayDate();
      date1.calcAge();
}
}

For someone who "had no idea how to do this" less than an hour ago, that's a pretty good effort!
When you compile and run that code does it give any error messages? Does it display the correct answers?

Thank You! Yes it runs without any errors. I'm not sure though whether it is doing what it is supposed to???

Now that you have seen that example, give it back to whoever you got it from and write your own version, following the instructions you were given.

the weird thing is that with me it doesnt want to compile the problem is the Date date1 = new Date();

most likely you've imported a wrong Date class. also, don't know whether it is an error in copy paste, but there seems to be no opening bracket in your Date class.

After I put an integer 2013 in that bracket it worked

I tried that hey but I still don't know where am I going wrong this is all so confusing...

where exactly must I put in int 2013?

not at all. you don't have a constructor that takes a parameter, so if it works with a parameter it is a clear sign you're using another Date class

ok so how do I create this parameter

the code works just fine, if you have your testClass point to the right Date class.
I would, however, consider renaming the variables of your Date class: today_day ... the date isn't 'today', it's a date you random enter.

also: a few still missing parts in your Date class:

Your class should have a constructor that initializes the three instance variables.

A get method must return the age.

also: the calculate age logic is a bit dodgy, since by a year alone, you can't 100% surely calculate the age (someone being born 1/1/2005 won't have the same age as someone born on 31/12/2005), but that's a flaw in the assignment, not in your code per se.

omg u just talking greek now can we please slow down im jus a beginner... ok when I inserted 2013 in the brackets it said it canoot find the symbol class date

where did I go wrong
import java.util.Scanner;
public class testdate
{
    public static void main( String args[] )
   { 
      Scanner input = new Scanner(System.in);

      Date date1 = new Date(2013); 

      System.out.print("Enter the Month:");
      int month = input.nextInt();
      System.out.println();
      date1.setTodayMonth(month);

      System.out.print("Enter the Day:");
      int day = input.nextInt();
      System.out.println();
      date1.setTodayDay(day);   

      System.out.print("Enter the Year:");
      int year = input.nextInt();
      System.out.println();
      date1.setTodayYear(year); 

      date1.displayMessage1(); 
      date1.displayMessage2(); 
      date1.displayMessage3(); 
      date1.displayDate();
      date1.calcAge();
   }
}

faithie: do not, I repeat: do NOT add a parameter to that
Date date = new Date();

it shouldn't be there.
check your import statements at the beginning of your testclass. I think you'll find an
'import x.x.Date;'
line, that does not correspond with the Date class that was written for this exercise, that 's that problem.

what I said about the variable names: forget that. it won't make your code any more correct, it's just a bit unlogical to call it today (imho)

as for the constructor:

a constructor is built like this:

<accessModifier><className>(<Parameters>){

}

in your code this should be

public Date(int day, int month, int year){
  today_day = day;
  today_month = month;
  today_year = year;
}

OR

public Date(){
  today_day = 0;
  today_month = 0;
  today_year = 0;
}

if you don't specify a constructor yourself, the compiler will automatically add one without parameters. which is why you still can instantiate the class, but it doesn't initialize the members (which is what is asked for in your assignment)

as for the getter: at this point you call a method that just prints the age. it isn't supposed to do that. it is supposed to do that. it is supposed to return the age as an int to the method/class that called that get-method. there it should be printed.

thank u so much

does it work now? did you manage to change the constructor/getter too?

Please people, this is not going well.
You can't learn to program Java by copying someone else's similar-but-different program then try making random changes until it works. You are wasting your own time, and stultuske's time, and heading for a "fail" when your teacher sees what you have done/not done.

This is a carefully constructed exercise, with each step clearly described. If you both just try to write this yourselves, following the steps exactly as laid out in the instructions, you will find it's not so difficult, and you will start to learn some Java.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.