























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























public static void main(…){^
// October 1, 1990 DateTime lukesBD =
new DateTime(1990, 10, 1);
// The data and time given by the computer’s clock. DateTime today = new DateTime();int daysOld = lukesBD.daysUntil(today);System.out.println(
"Luke is “
" days old."
DateTime
-int year-int month-int day+DateTime( )+DateTime(int yr, int mth, int day)+DateTime(DateTime dateToCopy)+void addYears(int howM any)+void addM onths(int howM any)+void addDays(int howM any)+int daysUntil(DateTime d)+boolean equals(Object obj)+String format( )+int getYear( )+int getM onth( )+int getDay( )+boolean isAfter(DateTime d)+boolean isBefore(DateTime d)+void setFormatInclude(int what)+void setFormatLength(int len)+String toString( )
import becker.util.Test;import becker.util.DateTime; public class Person extends Object{ private String name;
// person's name
private String mother;
// person's mother's name
private String father;
// person's father's name
private DateTime birth;
// birth date
private DateTime death;
// death date (null if still alive)
/** Construct a new person. */ public Person(String aName, String mom, String dad,
DateTime birthDate, DateTime deathDate)
{ super();
this.name = aName;this.mother = mom;this.father = dad; this.birth = birthDate;this.death = deathDate; }
if (this.death == null){ …
if (this.death != null){ …
public class Person extends Object{ private DateTime death = null;
…public int daysSinceDeath(){ DateTime today = new DateTime();
return this.death.daysUntil(today); } } Exception in thread “main” java.lang.NullPointerException
at Person.daysSinceDeath(Person.java:53)at Person.main(Person.java:75)
Person
-String name-String mother-String father-DateTime birth-DateTime death+Person(String aName,
String dad, String mom,int bYear, int bM onth,int bDay)
+int getAge( )+String getFather( )+String getM other( )+String getName( )+void setDeathDate(int dYear,
int dM onth, int dDay)
DateTime
-int year-int month-int day+DateTime( )+DateTime(int yr, int mth, int day)+void addYears(int howM any)+void addM onths(int howM any)+void addDays(int howM any)+long daysUntil(DateTime d)+boolean equals(Object obj)+int getYear( )+int getM onth( )+int getDay( )+boolean isAfter(DateTime d)+boolean isBefore(DateTime d)+String toString( )
1..
Superclass
attributesmethods
Subclass
attributesmethods
import becker.util.DateTime;public class Main extends Object{ public static void main(String[ ] args)
DateTime lukesBD = new DateTime(1990, 10, 1);DateTime today = new DateTime();int daysOld = lukesBD.daysUntil(today);System.out.println(
"Luke is "
" days old."
DateTime
year: 1990 month: 10
day: 1
lukesBD
51025103
(daysOld) 5104
510551065107510851095110
12 101605009
127 -
0 0 0 0
5102
(lukesBD) 5103
5104 10159
(year) 10160 (month) 10161
(day) 10162
10163
12 101605009
-19 199010 (^120)
DateTime lukesBD = new DateTime(1990, 10, 1);DateTime annasBD = lukesBD;
DateTime
year 1990 month 10
day 1
lukesBD
annasBD
DateTime
year 1990 month 10
day 1
lukesBD
d
DateTime lukesBD = new DateTime(1990, 10, 1);DateTime annasBD = new DateTime(1990, 10, 1);DateTime today = new DateTime();int lukesAge = lukesBD.daysUntil(today);int annasAge = annasBD.daysUntil(today);if (lukesAge == annasAge){^
// what to do if Luke and Anna are the same age … if (lukesBD == annasBD){^
// what to do if Luke and Ann have the same birth date …
DateTime year: 1990 month: 10
date: 1
DateTime year: 1990 month: 10
date: 1
annasBDlukesBD
DateTime year: 1990 month: 10
date: 1
annasBDlukesBD
public class DateTime extends Object{ private int year;
private int month;private int day;… /** Test if this date/time object means the same date/time as the other DateTime object. */ public boolean isEquivalent(DateTime other){ return other != null &&
this.year == other.getYear() &&this.month == other.getMonth() &&this.day == other.getDay();
} /** Another way to do it. */ public boolean isEquivalent(DateTime other){ return other != null &&
this.year == other.year &&this.month == other.month &&this.day == other.day;
import becker.util.IView; /** Objects implementing this interface can count the votes in an election.* * @author Byron Weber Becker */ public interface IVoteCounter{^
/** Add a candidate for the election.* @param name The candidate's name* @param pixFile A gif or jpg of the candidate, if available / public void addCandidate(String name, String pixFile); /* Get the number of candidates registered with this vote counter. / public int getNumCandidates(); /* Get the name of the give candidate.* @param candidateNum The number (0..getNumCandidates()-1) of the candidate whose*^
name is desired. */
public String getName(int candidateNum); /** Get the picture file name of the give candidate.* @param candidateNum The number (0..getNumCandidates()-1) of the candidate whose*^
pixfile is desired. */
public String getPixFile(int candidateNum);
/** Add one vote for the named candidate, provided the vote has not yet closed..* @param candidateNum The number (0..getNumCandidates()-1) of the candidate that*^
received the vote. */
public void addVote(int candidateNum); /** Get the number of votes for the named candidate, but only if the election has closed.* @param candidateName The name of a candidate* @return The number of votes received by the candidate; -1 if voting has not yet closed. / public int getVotes(int candidateNum); /* Stop accepting votes for this election. / public void closeElection(); /* Is the election closed? / public boolean isClosed(); /* Add a view to this model.* @param aView The view to add. */ public void addView(IView aView); }