Results 1 to 5 of 5
  1. #1
    miraclefruit is offline Member
    Join Date
    Nov 2016
    Posts
    10
    Rep Power
    0

    Question Java Program Date Class

    (Date Class ) Create a class called Date that includes three instance variables -a month (type int ), a day (type int ) and a year (type int ). Provide a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable . Provide a method displayDate that displays the month, day and year separated by forward slashes (/). Add a main method to the class that demonstrates class Date's capabilities.

    Here is what I have so far.
    Java Code:
    import java.util.Scanner;
    
    public class Date {
    	 
        private int monthdate;
        private int daydate;
        private int yeardate;
         
        public Date (int month, int day, int year) {
            monthdate = month;
            daydate = day;
            yeardate = year;
        }
         
        public void setMonth (int month) {
            monthdate = month;
        }
         
        public int getMonth() {
            return monthdate;
        }
         
        public void setday (int day) {
            daydate = day;
        }
         
        public int getDay() {
            return daydate;
        }
         
        public void setYear (int year ) {
            yeardate = year;
        }
         
        public int getYear() {
            return yeardate;
        }
         
        public void displayDate () {
            System.out.printf ("%d/%d/%d\n", getMonth(), getDay(), getYear());
        }
     
    
    
        public static void main (String [] args) {
             
            Date display = new Date (0,0,0);
     
            Scanner input = new Scanner (System.in);
             
            int month;
            int day;
            int year;
             
            System.out.print ("Enter Month: ");
            month = input.nextInt();
            display.setMonth(month);
             
            System.out.println ();
             
            System.out.print ("Enter Day: ");
            day = input.nextInt();
            display.setday(day);
             
            System.out.println ();
             
            System.out.print ("Enter Year: ");
            year = input.nextInt();
            display.setYear(year);
             
            System.out.println ();
             
            display.displayDate();
     
        }
    }
    I attached the output as a picture. Im not sure what Im missing in my code :( I would appreciate the help Thank you!
    Attached Thumbnails Attached Thumbnails Java Program Date Class-java.png  

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java Program Date Class

    Im not sure what Im missing
    Please explain why you think you are missing anything.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    miraclefruit is offline Member
    Join Date
    Nov 2016
    Posts
    10
    Rep Power
    0

    Default Re: Java Program Date Class

    I dont know how to get implement the new year, new day and new month into my code

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java Program Date Class

    how to get implement the new year, new day and new month
    What does that mean? What is the new year etc?
    Please explain what problems you are having with that.
    I don't see any comments in the code saying where the code for that is supposed to go or what it is supposed to do.
    In fact there are not any comments in the code at all.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    miraclefruit is offline Member
    Join Date
    Nov 2016
    Posts
    10
    Rep Power
    0

    Default Re: Java Program Date Class

    Nevermind, just fixed it. If anyone needs it in the future, here you go.
    Java Code:
    import java.util.Scanner;
    
    public class Date {
    	 
        private int monthdate;
        private int daydate;
        private int yeardate;
         
        public Date (int month, int day, int year) {
            monthdate = month;
            daydate = day;
            yeardate = year;
        }
         
        public void setMonth (int month) {
            monthdate = month;
        }
         
        public int getMonth() {
            return monthdate;
        }
         
        public void setday (int day) {
            daydate = day;
        }
         
        public int getDay() {
            return daydate;
        }
         
        public void setYear (int year ) {
            yeardate = year;
        }
         
        public int getYear() {
            return yeardate;
        }
         
        public void displayDate () {
            System.out.printf ("%d/%d/%d\n", getMonth(), getDay(), getYear());
        }
     
    
    
        public static void main (String [] args) {
             
            Date display = new Date (0,0,0);
     
            Scanner input = new Scanner (System.in);
             
            int month;
            int day;
            int year;
             
    		
            System.out.print("Enter the month, day and year in the form XX XX XXXX and hit enter:");
            month = input.nextInt();
            display.setMonth(month);
    		day = input.nextInt();
    		display.setday(day);
    		year = input.nextInt();
            display.setYear(year); 
    		System.out.print("The Date object's state is: ");
            display.displayDate();
    						 
    		System.out.print("Enter a new year:");
    		year = input.nextInt();
            display.setYear(year);
    		System.out.print("The Date object's state is: ");
            display.displayDate();	
    		System.out.print("Enter a new day:"); 
    		day = input.nextInt();
    		display.setday(day);	
    		System.out.print("The Date object's state is: ");
    		display.displayDate();				 
    		System.out.print("Enter a new month:");
    		month = input.nextInt();
            display.setMonth(month);
    		System.out.print("The Date object's state is: ");
    		display.displayDate();		 
    						 
     
        }
    }

Similar Threads

  1. Dispaly dates in Java without using Date class
    By dokumar in forum New To Java
    Replies: 3
    Last Post: 11-16-2013, 01:08 AM
  2. Replies: 4
    Last Post: 07-27-2011, 11:57 AM
  3. Date() class to get current date
    By bigmac15 in forum New To Java
    Replies: 1
    Last Post: 06-28-2011, 09:17 PM
  4. java program to display BIOS date
    By synclenovo in forum Advanced Java
    Replies: 4
    Last Post: 03-28-2009, 05:21 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •