Java

OOP GUIDE / WORK

 

DATE CLASS SOLUTIONS

 

 

TASK – PART 1 – DATE CLASS: INSTANCE VARIABLES


Here is my solution:

public class Date

{

     private int year;

     private int month;

     private int day;

}

 

 

TASK – PART 2 – DATE CLASS: CONSTRUCTOR


Here is my solution:

 

public class Date

{

     private int year;

     private int month;

     private int day;

    

     public Date(int year, int month, int day)

     {

           this.year = year;

           this.month = month;

           this.day = day;

     }

}

 

 

 

TASK – PART 3 – DATE CLASS: GET METHODS

 

Here is my solution:

 

public class Date

{

     private int year;

     private int month;

     private int day;

    

     public Date(int year, int month, int day)

     {

           this.year = year;

           this.month = month;

           this.day = day;

     }

    

     public int getDay()

     {

           return day;

     }

    

     public int getMonth()

     {

           return month;

     }

    

     public int getYear()

     {

           return year;

     }

    

     public String getMonthName()

     {

           if (month == 1)

                return "January";

           else if (month == 2)

                return "February";

           else if (month == 3)

                return "March";

           else if (month == 4)

                return "April";

           else if (month == 5)

                return "May";

           else if (month == 6)

                return "June";

           else if (month == 7)

                return "July";

           else if (month == 8)

                return "August";

           else if (month == 9)

                return "September";

           else if (month == 10)

                return "October";

           else if (month == 11)

                return "November";

           else if (month == 12)

                return "December";

           else

                return "ERROR";     //not necessarily a great solution

     }

 

     public String toString()

     {

           //WE NEED A TWO DIGIT MONTH

           String month2Digit;

           if (month < 10)

                month2Digit = "0" + month;

           else

                month2Digit = "" + month;

          

           //WE NEED A TWO DIGIT DAY

           String day2Digit;

           if (day < 10)

                day2Digit = "0" + day;

           else

                day2Digit = "" + day;

          

           return year + "-" + month2Digit + "-" + day2Digit;

     }

}

 

 

TASK – PART 4 – DATE CLASS: MORE DATE FORMATS

 

Here is my solution:

public class Date

{

     private int year;

     private int month;

     private int day;

    

     public Date(int year, int month, int day)

     {

           this.year = year;

           this.month = month;

           this.day = day;

     }

    

     public int getDay()

     {

           return day;

     }

    

     public int getMonth()

     {

           return month;

     }

    

     public int getYear()

     {

           return year;

     }

    

     public String getMonthName()

     {

           if (month == 1)

                return "January";

           else if (month == 2)

                return "February";

           else if (month == 3)

                return "March";

           else if (month == 4)

                return "April";

           else if (month == 5)

                return "May";

           else if (month == 6)

                return "June";

           else if (month == 7)

                return "July";

           else if (month == 8)

                return "August";

           else if (month == 9)

                return "September";

           else if (month == 10)

                return "October";

           else if (month == 11)

                return "November";

           else if (month == 12)

                return "December";

           else

                return "ERROR";        //not necessarily a great solution

     }

 

     public String toString()

     {

           //WE NEED A TWO DIGIT MONTH

           String month2Digit;

           if (month < 10)

                month2Digit = "0" + month;

           else

                month2Digit = "" + month;

          

           //WE NEED A TWO DIGIT DAY

           String day2Digit;

           if (day < 10)

                day2Digit = "0" + day;

           else

                day2Digit = "" + day;

          

           return year + "-" + month2Digit + "-" + day2Digit;

     }

    

     //Format example: 2022-10-15

     public String getInternationalNumericDate()

     {

           return toString();

     }

    

     //Format example: 2022 October 15

     public String getInternationalWordedDate()

     {

           return year + " " + getMonthName() + " " + day;

     }

    

     //Format example: 15-10-2022

     public String getBritishNumericDate()

     {

           //WE NEED A TWO DIGIT MONTH

           String month2Digit;

           if (month < 10)

                month2Digit = "0" + month;

           else

                month2Digit = "" + month;

          

           //WE NEED A TWO DIGIT DAY

           String day2Digit;

           if (day < 10)

                day2Digit = "0" + day;

           else

                day2Digit = "" + day;

 

           return day2Digit + "-" + month2Digit + "-" + year;

     }

    

     //Format example: 15 October 2022

     public String getBritishWordedDate()

     {

           return day + " " + getMonthName() + " " + year;

     }

    

     //Format: 10-15-2022

     public String getAmericanNumericDate()

     {

           //WE NEED A TWO DIGIT MONTH

           String month2Digit;

           if (month < 10)

                month2Digit = "0" + month;

           else

                month2Digit = "" + month;

          

           //WE NEED A TWO DIGIT DAY

           String day2Digit;

           if (day < 10)

                day2Digit = "0" + day;

           else

                day2Digit = "" + day;

          

           return month2Digit + "-" + day2Digit + "-" + year;

     }

    

     //Format: October 15, 2022

     public String getAmericanWordedDate()

     {

           return getMonthName() + " " + day + ", " + year;

     }

}

 

 

TASK – PART 5 – DATE CLASS: ANOTHER INSTANCE VARIABLE

 

Here is my solution:

 

public class Date

{

     private int year;

     private int month;

     private int day;

     private String separator;

    

     public Date(int year, int month, int day)

     {

           this.year = year;

           this.month = month;

           this.day = day;

           separator = "-";

     }

    

     public int getDay()

     {

           return day;

     }

    

     public int getMonth()

     {

           return month;

     }

    

     public int getYear()

     {

           return year;

     }

    

     public String getMonthName()

     {

           if (month == 1)

                return "January";

           else if (month == 2)

                return "February";

           else if (month == 3)

                return "March";

           else if (month == 4)

                return "April";

           else if (month == 5)

                return "May";

           else if (month == 6)

                return "June";

           else if (month == 7)

                return "July";

           else if (month == 8)

                return "August";

           else if (month == 9)

                return "September";

           else if (month == 10)

                return "October";

           else if (month == 11)

                return "November";

           else if (month == 12)

                return "December";

           else

                return "ERROR";        //not necessarily a great solution

     }

 

     public String toString()

     {

           //WE NEED A TWO DIGIT MONTH

           String month2Digit;

           if (month < 10)

                month2Digit = "0" + month;

           else

                month2Digit = "" + month;

          

           //WE NEED A TWO DIGIT DAY

           String day2Digit;

           if (day < 10)

                day2Digit = "0" + day;

           else

                day2Digit = "" + day;

          

           return year + separator + month2Digit + separator + day2Digit;

     }

    

     //Format example: 2022-10-15

     public String getInternationalNumericDate()

     {

           return toString();

     }

    

     //Format example: 2022 October 15

     public String getInternationalWordedDate()

     {

           return year + " " + getMonthName() + " " + day;

     }

    

     //Format example: 15-10-2022

     public String getBritishNumericDate()

     {

           //WE NEED A TWO DIGIT MONTH

           String month2Digit;

           if (month < 10)

                month2Digit = "0" + month;

           else

                month2Digit = "" + month;

          

           //WE NEED A TWO DIGIT DAY

           String day2Digit;

           if (day < 10)

                day2Digit = "0" + day;

           else

                day2Digit = "" + day;

 

           return day2Digit + separator + month2Digit + separator + year;

     }

    

     //Format example: 15 October 2022

     public String getBritishWordedDate()

     {

           return day + " " + getMonthName() + " " + year;

     }

    

     //Format: 10-15-2022

     public String getAmericanNumericDate()

     {

           //WE NEED A TWO DIGIT MONTH

           String month2Digit;

           if (month < 10)

                month2Digit = "0" + month;

           else

                month2Digit = "" + month;

          

           //WE NEED A TWO DIGIT DAY

           String day2Digit;

           if (day < 10)

                day2Digit = "0" + day;

           else

                day2Digit = "" + day;

          

           return month2Digit + separator + day2Digit + separator + year;

     }

    

     //Format: October 15, 2022

     public String getAmericanWordedDate()

     {

           return getMonthName() + " " + day + ", " + year;

     }

    

     public String getSeparator()

     {

           return separator;

     }

    

     public void setSeparator(String separator2)

     {

           separator = separator2;

     }

}

 

 

 

TASK – PART 6 – TESTING

 

Here is my solution:

 

public class DateTester

{

     public static void main(String[] args)

     {

           Date d1 = new Date(2024,10,7);

           System.out.println(d1);

           System.out.println(d1.getInternationalNumericDate());

           System.out.println(d1.getInternationalWordedDate());

           System.out.println(d1.getBritishNumericDate());

           System.out.println(d1.getBritishWordedDate());

           System.out.println(d1.getAmericanNumericDate());

           System.out.println(d1.getAmericanWordedDate());

          

           d1.setSeparator("/");

           System.out.println(d1.getInternationalNumericDate());

           System.out.println(d1.getBritishNumericDate());

           System.out.println(d1.getAmericanNumericDate());

 

           d1.setSeparator("\\");   //need two backslashes

           System.out.println(d1.getInternationalNumericDate());

           System.out.println(d1.getBritishNumericDate());

           System.out.println(d1.getAmericanNumericDate());

          

           System.out.println(d1.getMonthName());

     }

}