Sunday, October 11, 2015

// // Leave a Comment

Java Program To Display Date

The program below is the answer to Deitel's Java How to Program (9th Edition) Chapter 3 Exercise 3.15.

Question: 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 (/). Write a test application named DateTest that demonstrates class Date’s capabilities.

To run the application save both files with the same name as the class (because it is a public class) and with the .java file extension (in this case Date.java and DateTest.java). Then compile both classes and run DateTest. Date will not run because it does not have a main method.

To compile both classes at the same time using the command prompt, use the command
javac Date.java 
javac DateTest.java

To run DateTest, use the command
java DateTest

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 3.15 - Date Class
 * This Program Display Date In The Format mm/dd/yy
 *
 */ 

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());
    }

}

Below is Class DateTest.java to test Class Date.java

//Exercise 3.15 - Date Class Test
//This Program Demonstrates Class Date Capabilities

import java.util.Scanner;

public class DateTest {
    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();

    }
}


Click here to see other answers to Java How to Program.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.