Introduction to Programming

tamerunΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 10 μήνες)

621 εμφανίσεις

Computer

Programming

I







Instructor: Greg Shaw

COP 2210













Programming Assignment #
7


"
Repetition and Decision
-
Making
"



I.

Before Beginning


1.

Download
Date.java

and
SpeedDating.java

from the class web
page and store them in t
he
src

folder of your NetBeans
project


2.

Read the online documentation for both classes to get an
idea of how to use them


3.

For this assignment, you will
not

modify the Date class in
any way.




You
will

modify the
SpeedDating

class, but only by
completing the

method bodies of the three methods already
declared. Do
not

add any other methods or instance
variables to the
SpeedDating

class, do not modify the
constructor
, and do no
t modify the method declarations
(“headings”) provided!

(Not necessary!
)



II.


Complete
t
he
SpeedDating

Class



Write
the method bodies for
each of
the 3 methods declared in
SpeedDating
. Study the method declarations and documentation so
that you understand what each method do
es
.



To receive credit for a method, it must use one of the Java
loops (
your choice
). Nested loops are not necessary.




Do
not

modify the method declarations (“headings”) in any
way
!




III.


Write a Test Class for
Your

SpeedDating

Class


Your
test class
will have a
main

method
that
does the
following:


1.

Create a
SpeedDating

o
bject


2.

Have the user enter a year
,

and call the
auldLangSync

m
ethod
to
get the next year in which New Year’s Day will fall on the
same day of the week. Print the year and the day

of the
week, properly
labeled

in your
main

method and not in
auldLangSync
.

3.

In the USA,
Election
Day

is
fixed by law as the
first Tuesday
after the first
Monday in
November.

Have the user enter
another year, call the
pollDancer

method, and print the Date

object
returned, properly
labeled
.

Print the Date in
your

main

method, not in

SpeedDating
.


4.

Microsoft Excel
®

numbers dates as 1, 2, 3, etc, beginning
with January 1st, 1900. E.g., February 6th, 1900 is Excel
Date 37.

Have the user enter a date (month, day, and year)
and call the
iExcel

method to ge
t the Excel Date for the
input d
ate. Print it in your
main

method, not in
SpeedDating.




Excel mistakenly thought that 1900 was a leap year!

You

know better and so does
the

Date class
!

Therefore, all
Excel Dates after 2/28/1900 are “off by one
.” Not a
problem! Do not attempt to compensa
te for this error!
Just use the value computed using
the

Date class.


In other words, what we are really computing is what
the Excel Date
would

be if Excel
knew as much about
Leap Year as you
!



IV.


Data to be Used



Although your
SpeedDating

methods must w
ork for all valid
inputs, use this data in the run you hand in:


1.

First input year (for
auldLangSync
): 2012


2.

Second input year

(for
pollDancer
): 20
1
2


3.

Date for
iExcel
:
December 21st, 2012

(Note that the Date
constructor takes 3 int parameters


the month, d
ay, and
year)



V.


Due Date
:
T
hurs
day
,
April

5th



VI.


Upload a Zip File Containing Your SpeedDating Class,
Your Tester Class, and the Output
. Do
not

zip the
Date class


(Make sure your implementations of the
SpeedDating

methods
have
proper

“internal” documen
tation)



VII.


Using
The
Date

Class
-

Examples


The

Date

class is a

class for manipulating dates.
A date object
represents a d
ate in the Gregorian calendar (i.e.,
dates after
October

15,
1582
). Any attempt

to c
reate an invalid date will throw
an exception.


1.

Cr
eating Objects


Date d1

= new Date
(
month
,
day
,
year
) ;



// initi
alizes Date object d1 to int

arguments
month
,
day
, and
year.

// e.g.,


Date d
1 = new Date
(
3
,
2
3,2011
);
//
initializes d
1

to
March

2
3rd, 2011



d1 =
new
Date(
1,1,201
2
) ;


// d1 is now
Januar
y 1, 201
2




2.

Accessor
Methods


d1.
get
Month()


// returns the month as int

1..12

d1.
get
Day()

/
/ returns the day of the month
, as an int

d
1
.
get
Year()

// returns the year


d1.
getMo
nthName()
//
r
eturns the month as a String (e.g.

March")

d1.getDa
yOfWeek()
/
/
returns the day
of the week
as a string (e.g.,






//

“Friday”)


d1.equals(d2)

// returns true if d1 is the same date as d2

//
otherwise, returns false


d1.getShortDate()


// returns the date as mm/dd/yyyy

(e.g. 3/23/2011)

d1.getMediumDat
e()

// returns the date in the form of


March

2
3, 2011


d1.getLongDate()


// returns the date in the form of





//

Thurs
day,
March
2
3rd, 2011



3.

Mutator Methods


Date d

= new Date
(1,1,
2012) ;
// d is January 1,
2012

(a leap year)


d
.next() ;

// d
is
January 2,
2012

d
.previous() ; // d is January 1, 2012

d
.add(3
1
)

;


// d is February 1,
2012

d
.subtract(
32
) ;

// d is December 31,
2011

d
.next()

;




// d is January 1, 2012


d

= new
Date
(
3
,1,
2012) ;


// d is
March

1,
2012

d
.previous() ; // d is now February 29, 2012