Question

JAVA LANGUAGE create an empty Java file called Date build it to find any syntax errors...

JAVA LANGUAGE

  1. create an empty Java file called Date
  2. build it to find any syntax errors and eliminate them
  3. identify each member of this class and write your answer in a comment
    1. There are four different types of class member: constant, instance variable, constructor, and method
    2. identify a method as class method if it is static

/*
* Java program: Date.java
*
* Define the class Date
*/
import java.time.LocalDateTime;
public class Date
{
private static final int daysEachYear = 365;
private int year;
private int month;
private int date;
public Date(int y, int m, int d)
{
year = y;
month = m;
date = d;
}
public Date(int y)
{
year = y;
month = 1;
date = 1;
}
public void setYear(int y)
{
year = y;
}
public void setMonth(int m)
{
month = m;
}
public void setDate(int d)
{
date = d;
}
public int getYear()
{
return year;
}
public int getMonth()
{
return month;
}
public int getDate()
{
return date;
}
public void displayDate()
{
System.out.printf("\nThe date is %d/%d/%d\n", month, date, year);
}
public static void displayCurrentTime()
{
System.out.println("\nThe current date & time is: " +
LocalDateTime.now());
}
} //end class Date

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java code with comments

/*
 * Java program: Date.java
 *
 * Define the class Date
 */
import java.time.LocalDateTime;
public class Date
{
    //constant
    private static final int daysEachYear = 365;
    //instance variable
    private int year; //year
    private int month;//month
    private int day;//day
    //Overload constructor

    /**
     * set all the instance variable
     * @param y
     * @param m
     * @param d
     */
    public Date(int y, int m, int d)
    {
        year = y;
        month = m;
        day = d;
    }
    //Constructor with one Parameter

    /**
     * set year to passed argument variable
     * and set month to 1 and day to 1
     * @param y
     */
    public Date(int y)
    {
        year = y;
        month = 1;
        day = 1;
    }
    //Note:- all getters and setters are member methods
    //to set and get instance variables
// SETTERS
    /**
     * set year
     * @param y
     */
    public void setYear(int y)
    {
        year = y;
    }

    /**
     * set month
     * @param m
     */
    public void setMonth(int m)
    {
        month = m;
    }

    /**
     * set day
     * @param d
     */
    public void setDay(int d)
    {
        day = d;
    }
    //getters

    /**
     *
     * @return year
     */
    public int getYear()
    {
        return year;
    }

    /**
     *
     * @return month
     */
    public int getMonth()
    {
        return month;
    }

    /**
     *
     * @return day
     */
    public int getDay()
    {
        return day;
    }
    // member method
    /**
     * print date on console
     */
    public void displayDate()
    {
        System.out.printf("\nThe day is %d/%d/%d\n", month, day, year);
    }
    // class method
    /**
     * display current time
     */
    public static void displayCurrentTime()
    {
        System.out.println("\nThe current day & time is: " +
                LocalDateTime.now());
    }
} //end class Date

//================ test class=====================

public class Main {
    public static void main(String[] args)
    {
        Date date = new Date(2019);
        date.displayDate();
        Date.displayCurrentTime();
        Date date1 = new Date(2019,10,20);
        date1.displayDate();
    }
}

//============= output ===================

//If you need any help regarding this solution ........... please leave a comment ........ thanks...

Add a comment
Know the answer?
Add Answer to:
JAVA LANGUAGE create an empty Java file called Date build it to find any syntax errors...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT