logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

Make sure you have studied ECK Chapter 5 and the related topics on Canvas.

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

Figure 2.5: Creating/editing an application for university admission (Exercise D-2.8)

 

2.3 Programming

2.3.1 Laboratory exercises

Import the contents of this week’s lab files from Canvas to your INTELLIJ project by following the instruc- tions mentioned on page 34.

Important: INTELLIJ will show compilation errors in the classes that you have just imported. This is because some imported classes make references to classes that you will develop during the lab sessions this week. After completing the assignment for class DollarsAndCentsCounter in Exercise P-2.1, for instance, the compilation errors in DollarsAndCentsCounterTest should be gone.

 

Objects and Classes

Make sure you have studied ECK Chapter 5 and the related topics on Canvas.

 

Javadoc

This week, you will be required to document your classes with Javadoc. Javadoc for a class is placed immediately before the class declaration, similar to how Javadoc for a method is placed immediately before the method. Typical conventions for Javadoc in Java projects include rules like:

All classes and public constants and methods must have Javadoc

Any side-effects (changes to the object) should be clear from the Javadoc

Use @param to describe every parameter, even if trivial

Use @returns to describe the return value, even if trivial

Do not use @author and @version

Consider whether parameters can be null

See also https://www.oracle.com/technical-resources/articles/java/javadoc-tool. html and https://www.oracle.com/java/technologies/javase/api-specifications. html.

 

Preconditions and postconditions

This week, you will be required to specify methods of classes, that is, to write preconditions and postcondi- tions. See Appendix B (page 129) and the related topics on Canvas. Preconditions and postconditions are placed between the Javadoc and the method declaration. Invariants are typically placed after the fields and before the methods.

Whenever we ask you to specify in lab exercises, this means adding specifications in the form of invari- ants, preconditions and postconditions in JML.

 

☞ P-2.1 In this exercise, you implement and test a class that counts an amount of money in dollars and cents.

The counter should provide the following functionality:

The method public int getDollars() returns the amount of dollars.

The method public int getCents() returns the amount of cents.

The method public void add(int dollars, int cents) adds the specified amount of dollars and cents to the counter.

The method public void reset() sets the amount of dollars and cents to 0.

Pay special attention to the Javadoc and JML specification of the ss.week2.DollarsAndCentsCounter class. In the imported lab files you will find the test class ss.week2.DollarsAndCentsCounterTest. Improve the implementation until it passes all test cases.

Hints:

Consider the internal representation: what field(s) do you need, what type of field(s)?

Do you need a constructor? If so, add an appropriate one.

Make sure your class fulfills the postconditions (the ensures expressions).

 

Three-Way Lamp

In the following exercises, you will implement a three-way lamp. This is a light switch with four different settings: off, low, medium, high. The ideal way to represent these settings is with an enumerated type. In case you have forgotten about enum, consult Section 2.3.5 of ECK. The lamp will be accompanied by a textual user interface, which should give the user several options:

Change the current setting to a specified setting

Print the current setting of the lamp to the screen

Switch to the next setting, observing the order off → low → medium → high → off

All in all, you will design and implement:

The class ThreeWayLamp

The enumerated type ThreeWayLamp.LampSetting

The class ThreeWayLampTUI

P-2.2 You will define a JAVA class to model a three-way lamp. What query (or queries) should the class support? Which command or commands?

 

P-2.3 Create a new JAVA class ss.week2.ThreeWayLamp and write a stub implementation for this class. A stub implementation contains all methods of the class, but with minimal bodies, only to make sure the code compiles without errors.

Write Javadoc documentation before the class and before every method.

Work on the code until there are no compilation errors in the ThreeWayLamp class. This means that any method that has a return type that is not void should contain a return instruction with an appropriate value. Typically:

if the return type is int, use return 0;

if the return type is boolean, use return false; and

if the return type is the name of a class or an enumerated type, use return null.

The method bodies in these cases should only contain these return statements, while a method with a

void return type should be empty.

 

P-2.4 Complete the specification of the ThreeWayLamp class by JML for it. This means defining invariants at the class level, and preconditions and postconditions for all methods. See also Appendix B.

In this specification you have to explicitly define that after OFF the lamp goes to LOW, after LOW it goes to MEDIUM, etc. Every method with a return value and/or side-effects should have an ensures post-condition specifying the return value and/or the side-effect. Also consider statements that are always true of the fields, which you can specify as invariants.

Hint: make use of \old to reason how values changed after method calls.

 

P-2.5 Write unit tests for the ThreeWayLamp class based on the conditions you defined in Exercise P-

2.4. Create a class ThreeWayLampTest in package ss.week2 that implements your tests. See also Appendix A. The test class should test the following cases:

If after being created the lamp is OFF;

If the sequence OFF LOW MEDIUM HIGH OFF is properly implemented.

If setting the lamp to a specific setting works properly.

The ThreeWayLampTest class should have the following elements:

A (private) field of type ThreeWayLamp to hold the object to be tested.

A setUp method that creates the ThreeWayLamp object to be tested. This method should get the

@BeforeEach annotation, so that every unit test starts with a fresh object.

A method for each of the test cases above; these methods should get the @Test annotation.

Use assertions such as assertEquals to test your methods. After creating the test, run it to see that all tests fail.

 

☞ P-2.6 Implement the methods that you specified in Exercise P-2.4 by replacing the method body of the methods in the stub implementation with the actual intended functionality of the class. The result should

compile without errors and it should pass the ThreeWayLampTest tests you wrote in Exercise P-2.5.

Now that you have a correct implementation of a three-way lamp, it is time to make a textual user interface (TUI) for the lamp. The idea of a TUI is to repeatedly ask the user for input, and then take the appropriate action. This input takes the form of commands. The TUI can also ask the user for additional input.

The following input options should be offered to the user (as String values):

OFF: Set the lamp to OFF

LOW: Set the lamp to LOW

MEDIUM: Set the lamp to MEDIUM

HIGH: Set the lamp to HIGH

STATE: Print the current setting of the lamp

NEXT: Change to the next setting, observing the order OFF → LOW → MEDIUM → HIGH →

HELP: Show a help menu, explaining how the user should interact with the program

EXIT: Quit the program

In the previous exercises, the main method implemented dealing with user input. In the object-oriented paradigm, the main method should be minimal. It only creates the objects necessary to start the program, and then calls methods of these objects that implement the desired functionality. The code that runs the TUI should be in a separate method that will be invoked by main after constructing the ThreeWayLampTUI object. This method could be named run.

Your ThreeWayLampTUI also needs a ThreeWayLamp to be a user interface for. You should add this

ThreeWayLamp as a field of ThreeWayLampTUI.

P-2.7 Create a new class ss.week2.ThreeWayLampTUI with a main function. Tip: if you type main in IntelliJ, it suggests to automatically create a main prototype. Create a field for the ThreeWayLamp object, a run() command, and a constructor.

The field needs to be initialized with an actual ThreeWayLamp object in the constructor. There are two ways to do this:

Let the constructor of ThreeWayLampTUI create a ThreeWayLamp object.

Create the ThreeWayLamp object first, then give it as a parameter to the constructor of the

ThreeWayLampTUI.

This is a program design decision. Make a decision and explain why. Then implement the constructor and main accordingly, while keeping run a stub.

 

 

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Atharva PatilComputer science

816 Answers

Hire Me
expert
Chrisantus MakokhaComputer science

848 Answers

Hire Me
expert
AyooluwaEducation

821 Answers

Hire Me
expert
RIZWANAMathematics

978 Answers

Hire Me

Get Free Quote!

388 Experts Online