Question

We have been learning about the Logical Expression based method for developing test coverage. In this...

We have been learning about the Logical Expression based method for developing test coverage. In this assignment, we will use these techniques to discover and define test case information for the provided Thermostat class (and supplemental ProgrammedSettings, Period, and Daytime class), and convert them into JUnit/NUnit Test Cases.

  1. Develop a suite of test cases to satisfy the Predicate Coverage for the Thermostat class
    Example of a Test Case:

P = (((A < B) ∨ (C ∧ A < D)) ∧ (E > F))

Predicate = TRUE:

A = 5, B = 10, C = true, D = 10, E = 10, F = 5

(((5 < 10) ∨ (true ∧ 5 < 10)) ∧ (10 > 5))

(((True) ∨ (True ∧ True)) ∧ (True)) = True

  1. Implement them in JUnit.

Thermostat.java

import java.io.*;
import java.time.Period;
import java.util.*;

// Programmable Thermostat
public class Thermostat
{
private int curTemp; // current temperature reading
private int thresholdDiff; // temp difference until we turn heater on
private int timeSinceLastRun; // time since heater stopped
private int minLag; // how long I need to wait
private boolean override; // has user overridden the program
private int overTemp; // overriding temperature
private int runTime; // output of isEnergyEfficient - how long to run
private boolean energyModeEnabled; // output of isEnergyEfficient - whether to run
private Period period; // morning, day, evening, or night
private int day; // week day or weekend day
private boolean isHeating; // Whether the unit is currently heating or not


public boolean isEnergyEfficient (ProgrammedSettings pSet)
{
   int dTemp = pSet.getSetting (period, day);

if ( ( (!override && curTemp < overTemp - thresholdDiff) ||
   (curTemp < dTemp - thresholdDiff) ) &&
((timeSinceLastRun > minLag) ^ override) )
{ // Disable low energy mode to heat up the place
// How long? Assume 1 minute per degree (Fahrenheit)
int timeNeeded = curTemp - dTemp;
if (override)
{
timeNeeded = curTemp - overTemp;
}
setRunTime (timeNeeded);
enableEnergyMode (false);

return (true);
}
else
{
enableEnergyMode (true);

return (false);
}
}

public void setCurrentTemp (int temperature) { curTemp = temperature; }
public void setThresholdDiff (int delta) { thresholdDiff = delta; }
public void setTimeSinceLastRun (int minutes) { timeSinceLastRun = minutes; }
public void setMinLag (int minutes) { minLag = minutes; }
public void setOverride (boolean value) { override = value; }
public void setOverTemp (int temperature) { overTemp = temperature; }
public void setIsHeating (boolean value) { isHeating = value; }

// for the ProgrammedSettings
public void setDay (int curDay) { day = curDay; }
public void setPeriod (Period curPeriod) { period = curPeriod; }

// outputs from isEnergyEfficient - need corresponding getters to activate heater
void setRunTime (int minutes) { runTime = minutes; }
void enableEnergyMode (boolean value) { energyModeEnabled = value; }


} // End Thermostat class

ProgrammedSettings.java

import java.time.Period;

//NOTE: Do not make predicates based on this class for Homework 4
public class ProgrammedSettings {

int temperature;

public ProgrammedSettings()
{
temperature = 76;
}

public ProgrammedSettings(int t)
{
temperature = t;
}

public int getSetting(Period p, int day)
{
return temperature;
}


}

Period.java


public enum Period
{
MORNING, DAY, EVENING, NIGHT
}

DayType.java

public enum DayType
{
WEEKDAY, WEEKEND
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

CODE:

ThermostatPredicateCoverageTest.java

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class ThermostatPredicateCoverageTest {

private Thermostat thermostat;
private ProgrammedSettings programmedSettings;

@Before
public void Setup() {
thermostat = new Thermostat();
}

@Test
public void isEnergyEfficientTest1() {
programmedSettings = new ProgrammedSettings(35);
thermostat.setIsHeating(true);
thermostat.setOverride(false);
thermostat.setThresholdDiff(6);
assertTrue(thermostat.isEnergyEfficient(programmedSettings));
}

@Test
public void isEnergyEfficientTest2() {
programmedSettings = new ProgrammedSettings(70);
thermostat.setIsHeating(true);
thermostat.setOverride(false);
thermostat.setThresholdDiff(6);
assertTrue(thermostat.isEnergyEfficient(programmedSettings));
}

@Test
public void isEnergyEfficientTest3() {
programmedSettings = new ProgrammedSettings(90);
thermostat.setIsHeating(true);
thermostat.setOverride(false);
thermostat.setThresholdDiff(6);
assertFalse(thermostat.isEnergyEfficient(programmedSettings));
}

@Test
public void isEnergyEfficientTest4() {
programmedSettings = new ProgrammedSettings(70);
thermostat.setIsHeating(true);
thermostat.setOverride(true);
thermostat.setThresholdDiff(6);
assertFalse(thermostat.isEnergyEfficient(programmedSettings));
}

@Test
public void isEnergyEfficientTest5() {
programmedSettings = new ProgrammedSettings(70);
thermostat.setIsHeating(false);
thermostat.setOverride(false);
thermostat.setThresholdDiff(6);
assertTrue(thermostat.isEnergyEfficient(programmedSettings));
}

@Test
public void isEnergyEfficientTest6() {
programmedSettings = new ProgrammedSettings(40);
thermostat.setIsHeating(false);
thermostat.setOverride(true);
thermostat.setThresholdDiff(6);
assertTrue(thermostat.isEnergyEfficient(programmedSettings));
}

@Test
public void isEnergyEfficientTest7() {
programmedSettings = new ProgrammedSettings(40);
thermostat.setIsHeating(false);
thermostat.setOverride(false);
thermostat.setThresholdDiff(4);
assertTrue(thermostat.isEnergyEfficient(programmedSettings));
}

@Test
public void isEnergyEfficientTest8() {
programmedSettings = new ProgrammedSettings(40);
thermostat.setIsHeating(false);
thermostat.setOverride(true);
thermostat.setThresholdDiff(4);
assertFalse(thermostat.isEnergyEfficient(programmedSettings));
}
@Test
public void isEnergyEfficientTest9() {
programmedSettings = new ProgrammedSettings(40);
thermostat.setIsHeating(false);
thermostat.setOverride(false);
thermostat.setThresholdDiff(6);
assertFalse(thermostat.isEnergyEfficient(programmedSettings));
}
}

SCREENSHOTS OF THE CODE SHOWN BELOW:

ThermostatClauseCoverageTest.java

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class ThermostatClauseCoverageTest {

private Thermostat thermostat;
private ProgrammedSettings programmedSettings;

@Before
public void setUp() throws Exception {
thermostat = new Thermostat();
}

@Test
public void isEnergyEfficientTest1() {
programmedSettings = new ProgrammedSettings(35);
thermostat.setIsHeating(true);
thermostat.setOverride(false);
thermostat.setThresholdDiff(6);
thermostat.setCurrentTemp(40);
assertTrue(thermostat.isEnergyEfficient(programmedSettings));
}

@Test
public void isEnergyEfficientTest2() {
programmedSettings = new ProgrammedSettings(35);
thermostat.setIsHeating(true);
thermostat.setOverride(true);
thermostat.setThresholdDiff(6);
assertFalse(thermostat.isEnergyEfficient(programmedSettings));
}

@Test
public void isEnergyEfficientTest3() {
programmedSettings = new ProgrammedSettings(70);
thermostat.setIsHeating(false);
thermostat.setOverride(true);
thermostat.setThresholdDiff(6);
thermostat.setCurrentTemp(55);
assertFalse(thermostat.isEnergyEfficient(programmedSettings));
}
}

SCREENSHOT OF THE CODE SHOWN BELOW:

HOPE THIS MAY HELPS YOU.PLEASE GIVE POSITIVE RATINGS.THANKYOU...!!

Add a comment
Know the answer?
Add Answer to:
We have been learning about the Logical Expression based method for developing test coverage. In this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • [Java] We have learned the class Clock, which was designed to implement the time of day...

    [Java] We have learned the class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following: Derive the class ExtClock from the class Clock by adding a data member to store the time zone. Add necessary methods and constructors to make the class functional. Also write the definitions of the methods and constructors. Write a test...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • Java Help, Will definitely give a thumbs up if it works. Declare a class ComboLock that...

    Java Help, Will definitely give a thumbs up if it works. Declare a class ComboLock that works like the combination lock in a gym locker (Consult the API provided to look for the methods needed). The locker is constructed with a combination - three numbers between 0 and 39. The reset method resets the dial so that it points to 0. The turnLeft and turnRight methods turn the dial by a given number of ticks to the left or right....

  • This wont take you more than 15 mins. Comple the two method and besure to test...

    This wont take you more than 15 mins. Comple the two method and besure to test with Junit test that provided below. toArray() -- this method returns a newly allocated array containing the elements in the multiset. The array this method returns must only contain the elements in the multiset and not any nulls or other values that are stored in the backing store, but are not in the multiset. fromArray(E[] arr) -- this method updates the multiset so that...

  • Add another changeColor() method (i.e. you will now have 2 methods called "changeColor"). This one accepts...

    Add another changeColor() method (i.e. you will now have 2 methods called "changeColor"). This one accepts an int parameter and changes the color based on that int. The valid colors are "red", "yellow", "green", "blue", "magenta" and "black". In your code, map each color to an integer (e.g. in my code 3 means green.) If the number passed to the method is not valid, change the color to red. In the bounceTheBall() method, where you test for collisions with top...

  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe {    private char currentPlayer;    private char[][] board;    private char winner;       /**    * Default constructor    * Initializes the board to be 3 by 3 and...

  • NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the...

    NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the current displayed customer PLEASEEEEEEEEEE package bankexample; import java.util.UUID; public class Customer { private UUID id; private String email; private String password; private String firstName; private String lastName;    public Customer(String firstName, String lastName, String email, String password) { this.id = UUID.randomUUID(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; } public String getFirstName() { return firstName; } public void setFirstName(String...

  • Need help to build a fingerprint app that we have sslserversocket to tell a file use finger print...

    need help to build a fingerprint app that we have sslserversocket to tell a file use finger print before open the file what have been done below Here is my mainactivity.java and fingerprintHandler.java below <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/heading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:padding="20dp" android:text="Fingerprint Authentication" android:textAlignment="center" android:textColor="@android:color/black" android:textSize="18sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <TextView android:id="@+id/paraMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="Place your finger on the fingerprint scanner to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT