Using Composition and Polymorphism in Java 1. Complete the classes where indicat
ID: 3595752 • Letter: U
Question
Using Composition and Polymorphism in Java
1. Complete the classes where indicated below, to be used in conjunction with the provided
TimeStamp class.
Date Formats
mm/dd/yyyy American form
dd/mm/yyyy European form
yyyy-mm-dd Asian
Time Formats
hh:mm am/pm (e.g., 4:30pm)
mmmm (military time, hh = 0-23) e.g., 0400 (4:00am) 1200 (noon) 2230 (9:30pm)
Time Stamps Examples
10/25/2017 5:30pm 25/10/2017 5:30pm 2017-10-25 1750
The TimeStamp class constructor is passed specific Date and Time objects:
public class TimeStamp {
private Date date;
private Time time;
public TimeStamp(Date d, Time t) {
date = d;
time = t;
}
public String getStamp(){
return date.getFormattedDate() + “ “ + time.getFormattedTime();
}
}
Date Interface
public interface Date {
public abstract String getFormattedDate();
}
Classes Implementing the Data Interface (and therefore of type Date)
public class AmericanDate implements Date {
// to complete
}
public class EuropeanDate implements Date {
// to complete
}
public class AsianDate implements Date {
// to complete
}
Time Interface
public interface Time {
public abstract String getFormattedTime();
}
Classes Implementing the Time Interface (and therefore of type Time)
public class AMPM implements Time {
// to complete
}
public class MilitaryTime implements Time {
// to complete
}
EXAMPLE USE
Date d = new AmericanDate(11,30,2017); Date d2 = new AsianDate(11,30,2017)
Time t = new AMPM(5, 15, “am”); Time t2 = new MilitaryTime(5, 15, “pm”);
TimeStamp t_stamp = new TimeStamp(d, t); TimeStamp t_stamp2 = new TimeStamp(d2, t2);
System.out.println(t_stamp.getStamp()); System.out.println(t_stamp2.getStamp());
>> 11/30/2017 5:15am >> 2017/11/30 1715
Explanation / Answer
Hi
I have filled all classes as requested,
The concept being tested is simple, an interface can be used as a template so that all classes implementing it can have the same behaviour and we just have to implement abstract methods if any declared in the interface
public class AmericanDate implements Date {
// to complete
private int year,month,date;
AmericanDate(int month,int date,int year)
{
this.month=month;
this.date=this.date;
this.year=year;
}
public String getFormattedDate(){
return month+"/"+date+"/"+year;
}
}
public class EuropeanDate implements Date {
// to complete
private int year,month,date;
EuropeanDate(int date,int month,int year)
{
this.month=month;
this.date=this.date;
this.year=year;
}
public String getFormattedDate(){
return date+"/"+month+"/"+year;
}
}
public class AsianDate implements Date {
// to complete
private int year,month,date;
AsianDate(int year,int month,int date)
{
this.month=month;
this.date=this.date;
this.year=year;
}
public String getFormattedDate(){
return year+"/"+month+"/"+date;
}
}
Time Interface
public interface Time {
public abstract String getFormattedTime();
}
Classes Implementing the Time Interface (and therefore of type Time)
public class AMPM implements Time {
// to complete
private int hours,minutes;
private String meridian;
AMPM(int hours,int minutes,String meridian)
{
this.hours=hours;
this.minutes=minutes;
this.meridian=meridian;
}
public String getFormattedDate(){
return hours+"/"+minutes+"/"+meridian;
}
}
public class MilitaryTime implements Time {
// to complete
private int hours,minutes;
private String meridian;
AMPM(int hours,int minutes,String meridian)
{
this.hours=hours;
this.minutes=minutes;
this.meridian=meridian;
}
public String getFormattedDate(){
if(hours>12)
hours-=hours-12;
return hours+"/"+minutes+"/"+meridian;
}
}
}
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.