Unified modeling Language is a standard that is used to model certain Object Oriented principles. In the short example i will display what Composition is and how its used.
Lets get started with the main App Concert, this the main program where
A type of Time is created and italicized by the passed parameter. giving us
a start and end time. then a concert type is created and then they are printed
out the console.
Main Class
public class ConcertApp { public static void main(String[] args) { Time start = new Time(3,12,1); Time end = new Time(1,0,0); Concert myConcert = new Concert("one long concert", start , end); System.out.printf("%s", myConcert); } }
Concert Class
Lets take a closer look at the concert class, it has a couple of private members
which are type of Time, then the concert main method is overloaded, then at last we have
a toString to return the specific format.
public class Concert { private String name; private Time startTime; private Time endTime; public Concert(String n, Time start, Time end) { name = n; startTime = start; endTime = end; } @Override public String toString() { return String.format("%s %s %s", name, startTime, endTime); } }
Class Time
The Class Time has some private integers hour, minute, and seconds. Then we have
our setters and finish up with the toString.
public class Time { private int hours; private int minutes; private int seconds; public Time(int h, int m, int s) { setTime(h, m, s); } public Time() { } public void setHours(int h) { hours = ((h >= 0 && h < 24) ? h : 0); } public void setMinutes(int m) { minutes = ((m >= 0 && m < 60) ? m : 0); } public void setSeconds(int s) { seconds = ((s >= 0 && s < 60) ? s : 0); } public void setTime(int h, int m, int s) { setHours(h); setMinutes(m); setSeconds(s); } public String toString() { return String.format("%02d:%02d:%02d", hours, minutes, seconds); } }
I always had a passion for the field of STEM (Science, Technology, Engineering, and Math) and I knew I wanted to do something to make a difference in the world. I just didn’t know where to start. I was an immigrant in a new country, grew up in a tough environment, and wasn’t sure how… Read More