讲解SE2203a留学生、辅导Java编程、Java语言程序辅导留学生、辅导Java设计
- 首页 >> Java编程WESTERN UNIVERSITY - CANADA
FACULTY OF ENGINEERING
DEPARTMENT OF ELECTRICAL & COMPUTER ENGINEERING
SE2203a –Software Design
Assignment 1
Due Date: October 7th , 2018
IF YOU ARE WORKING WITHIN A TEAM, PLEASE MAKE SURE THAT THE NAME OF BOTH TEAM
MEMBERS NEED TO BE ADDED IN THE TOP OF EACH JAVA CODE.
1 Goals
Write specification for methods that include preconditions and postconditions.
Write a Java interface for a class.
Choose appropriate classes and methods during the design of a program, including
classes that might be written already.
2 Assignment works
An odometer records a car's mileage. It contains a number of wheels that turn as the car
travels. Each wheel shows a digit from 0 to 9. The rightmost wheel turns the fastest and
increases by 1 for every mile traveled. Once a wheel reaches 9, it rolls over to 0 on the next
mile and increases by 1 the value on the wheel to its left.
You can generalize the behavior of such wheels by giving them symbols other than the
digits from 0 to 9. Examples of such wheel counters include
A binary odometer whose wheels each show either 0 or 1
A desktop date display with three wheels, one each for year, month, and day
A dice roll’s display whose wheels each show the spots for a single die
2.1 Question 1 – Define generic type Java Interface (assesses the ET1 CEAB indicator)
1. Using IntelliJ IDE create a new Java project and name it
yourUwoIdAssignment1. For example, if yourUWOId is
aouda then name the project as aoudaAssignment1. Note
that, if you are working with a team, the project name
should include the UWOId for both team members
separated by hyphen, e.g., uwoid1-uwoid2Assignment1.
2. Write a generic type Java interface named Rollable to be used
for creating any class that represents a wheel. Use the UML
class diagram shown in Figure 1 to write your Interface.
Figure 1
<< interface>>
Rollable <T>
+reset():void
+increas():void
+lastRolledOver():boolean
+getValue():T
SE2203a: Software Design Assignment 1
Abdelkader Ouda page 2 Fall 2018
3. This interface describe four methods; Reset() to reset the counter to the minimum value,
increase() to increase the value on the counter, lastRolledOver() to test whether the last
increase caused a rollover, and getValue() to return the value of the counter.
4. The interface Rollable will be implemented later by any class that represents a wheel. The
generic data type T represents the type of the wheel's
current value. For example, a value's type might be
String or Integer.
5. Write a generic type Java interface named
WheelCounterInterface for a general wheel counter as
shown in figure 2.
6. Do not forget to write descriptive comments at the top
of each class and method.
Notice If you have some questions or if you need to make sure you are doing the right answers, your TAs
are willing to help, they are available during the designated Labs hours.
2.2 Question 2 – Implement Java Interface (assesses the ET2 CEAB
indicator)
1. Write a new class to keep track of a non-negative count (i.e.,
0,1,2,…) and name it Counter as shown in Figure 3. This class
has increase() method to increase the count by one, decrease()
method to decrease the count by one, getCount() method to
return the value of the count, and isZero() method to check
whether the count is zero.
2. Do not forget to add toString() method to return a string representation of the count.
3. Write a new class named Wheel. This class records a non-negative count value that is
between a minimum and a maximum value. Increasing the count beyond its maximum
will roll it over to the minimum value. Decreasing the count below its minimum will roll
it over to the
maximum value.
4. The Wheel class
needs to extend
the Counter
class and
implement the
Rollable
interface, as
shown in
Figure 4.
Figure 2
Figure 3
Figure 4 Figure 5
<< interface>>
WheelCounterInterface<T>
+reset():void
+increas():void
+getwheelValue(thewheel:int):T
+getwheelsInUse():int
<< interface>>
Rollable <T>
+reset():void
+increas():void
+lastRolledOver():boolean
+getValue():T
Counter
Wheel
<< interface>>
WheelCounterInterface<T>
+reset():void
+increas():void
+getwheelValue(thewheel:int):T
+getwheelsInUse():int
WheelCounter<T>
SE2203a: Software Design Assignment 1
Abdelkader Ouda page 3 Fall 2018
5. Write a new class named WheelCounter that implements the WheelCounterInterface,
see Figure 5 above. This class maintains a collection of wheels of type Rollable. Note
that, the constructors of WheelCounter should be able to instantiate up to 4 wheels, (we
can do this by having 4 different constructors).
6. Do not forget to add toString() method to return a string representation of the value of
all wheels.
7. Now, add the given Java class “DiceProbability.java” to your project, then compile and
run you should have an output like the one shown below.
Welcome to the dice roller program
For 1111 the sum was 4
For 1112 the sum was 5
For 1113 the sum was 6
For 1114 the sum was 7
For 1115 the sum was 8
For 1116 the sum was 9
For 1121 the sum was 5
<<< cut to fit the output in one page >>>
For 6664 the sum was 22
For 6665 the sum was 23
For 6666 the sum was 24
The number of rolls out of 1296 that were greater than 12 was 861
The probability is 0.6643519
2.3 Question 3 – More hand-in experience on classes and objects (assesses the ET2 CEAB
indicator)
1. Using your interface classes Rollable<T> and WheelCounterInterface<T> to develop a
desktop date display program called Calendar that has with four wheels, one for the day
of the week, one for the month, one for the day, and one for the year. Note that the
name and number of the day increment at the same rate, but rollover at different points.
They are not part of the same wheel counter.
2. The class diagram shown in Figure 6, will help you to construct your program quickly.
Figure 6
SE2203a: Software Design Assignment 1
Abdelkader Ouda page 4 Fall 2018
3. The class DateCounter extends WheelCounter<String> and uses Month, Day, Year to
represent a date. DateCounter overrides the method increase, so that when the month
changes (i.e., when a Day object rolls over) it updates the new month's last day.
4. Note that, a Year class is able to tell whether it is a leap year, while a Month class can
report its length. The Day class needs to know the last day of a month so it can rollover
correctly. The day of week class uses Zeller's congruence (see Wikipedia) to determine
the day on which a given date falls.
5. Now, add the given Java class “Calendar.java” to your project, then compile and run you
should have an output like the one shown below.
Sun Jan 1, 2017
Mon Jan 2, 2017
Tue Jan 3, 2017
Wed Jan 4, 2017
Thu Jan 5, 2017
Fri Jan 6, 2017
Sat Jan 7, 2017
Sun Jan 8, 2017
Mon Jan 9, 2017
Tue Jan 10, 2017
Wed Jan 11, 2017
Thu Jan 12, 2017
<<< cut to fit the output in one page >>>
Mon Mar 25, 2019
Tue Mar 26, 2019
Wed Mar 27, 2019
Thu Mar 28, 2019
Fri Mar 29, 2019
Sat Mar 30, 2019
Sun Mar 31, 2019
3 Hand In
You should use IntelliJ IDEA IDE to create, edit, and run the required Java programs.
1. From the IntelliJ main menu, select File ? Export to Zip File…, the Save as popup
window appears, click OK to save your project as yourUwoIdAssignment1.zip
2. Submit this zip file using OWL assignment link at the due date mentioned above.