Question
Create a JUnit Test Case for the following provided java program. Provided below are the java program (ShannonsTheorem.java) for which the Junit will be tested
Create a JUnit Test Case for the following provided java program. Provided below are the java program (ShannonsTheorem.java) for which the Junit will be tested against. Write a working Junit test case for Test_ShannonsTheorem.java and AllTests.java program. A starter code will be provided. Testing (Test_ShannonsTheorem.java), tests for negative values and positive values for both bandwidth and signal-to-noise ratio plus testing for max data rate results. Test_ShannonsTheorem.java should be tested for: Constructors, Accessors, Mutators, Behavior
ShannonsTheorem.java class (This class is complete, please work on the Junit)
package network;
import java.lang.Math;
import javax.swing.JOptionPane;
/**
* This class is intended to calculate the application of
* Shannon's Theorem for capacity, prompting users to
* enter a bandwidth and signal-to-noise ratio values
*/
public class ShannonsTheorem {
/**
* Bandwidth in hertz
*/
private double bandwidth;
/**
* Signal-to-noise ratio in decibels
*/
private double signalToNoise;
/**
* Default constructor
*/
public ShannonsTheorem() {
super();
}
/**
* Accessor for bandwidth field
* @return value for bandwidth in hertz
*/
public double getBandwidth() {
return bandwidth;
}
/**
* Mutator for bandwidth field
* @param bandwidth in hertz
*/
public void setBandwidth(double bandwidth) {
this.bandwidth = bandwidth;
}
/**
* Accessor for signalToNoise field
* @return value for signal to noise in decibels
*/
public double getSignalToNoise() {
return signalToNoise;
}
/**
* Mutator for signalToNoise field
* @param signalToNoise in decibels
*/
public void setSignalToNoise(double signalToNoise) {
this.signalToNoise = signalToNoise;
}
/**
* Accessor to get value of maximumDataRate from the results of bandwidth multiplied by signalToNoise ratio
* @return maximumDataRate in bits-per-second (bps)
*/
public double getMaximumDataRate() {
return getMaximumDataRate(bandwidth, signalToNoise);
}
/**
* Converts this class to a meaningfull string.
* @return Converts bandwidth, signalToNoise ratio and maximumDataRate into a readable string program.
*/
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("[");
builder.append("bandwidth = ").append(bandwidth).append(" hz,");
builder.append(" signalToNoise = ").append(signalToNoise).append(" db,");
builder.append(" maximumDataRate = ").append(String.format("%.2f",getMaximumDataRate())).append(" bps]");
return builder.toString();
}
/**
* Private method that calculates the maximum data rate
* @param bandwidth in hertz
* @param signalToNoise in decibels
* @return maximumDataRate in bits-per-second (bps)
*/
private double getMaximumDataRate(double bandwidth, double signalToNoise) {
return bandwidth * (Math.log(1+Math.pow(10, signalToNoise/10))/ Math.log(2));
}
/**
* Main method to run the application of Shannon's Theorem
* @param args
*/
public static void main (String args[]) {
ShannonsTheorem application = new ShannonsTheorem();
try {
JOptionPane.showMessageDialog(null, "Hey there! Welcome to the Shannon's Theorem calculator application.", "Hello and Welcome! Please start here.", JOptionPane.INFORMATION_MESSAGE);
double bandwidth = Double.parseDouble(JOptionPane.showInputDialog("Please enter the bandwidth (Hertz)"));
double signalToNoise = Double.parseDouble(JOptionPane.showInputDialog("Please enter the signal-to-noise ratio (Decibels)"));
application.setBandwidth(bandwidth);
application.setSignalToNoise(signalToNoise);
String message = String.format("The maximum data rate is %.2f %s",application.getMaximumDataRate(), "bits per second (bps)");
JOptionPane.showMessageDialog(null, message, "Shannon's Theorem Calculation Results", JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex) {
JOptionPane.showMessageDialog(null, "Invalid number, try again! Please enter numerical values only.", "Error Detected!", JOptionPane.ERROR_MESSAGE);
}
} //end of method main
} //end of class
Complete the following Junit classes for the programs Test_ShannonsTheorem.java and AllTests.java.
Test_ShannonsTheorem.java
package networktest;
import junit.framework.*;
import network.ShannonsTheorem;
/**
* JUnit tests for the ShannonsTheorem class from the "network" project.
* @author
* @version 1.0.0
*/
public class Test_ShannonsTheorem extends TestCase {
public Test_ShannonsTheorem(String name) { super(name); }
public static Test suite() { return new TestSuite(Test_ShannonsTheorem.class); }
protected void setUp() throws Exception { System.out.println("Test_ShannonsTheorem Begin"); }
protected void tearDown() throws Exception { System.out.println("Test_ShannonsTheorem End"); }
/**
* Test the constructors.
*/
public void testConstructors() {
System.out.println("\tExecuting Test_ShannonsTheorem.testConstructors");
shannonsTheorem = new ShannonsTheorem();
assertNotNull("\t\tTest_ShannonsTheorem.testConstructors: ShannonsTheorem is null", ShannonsTheorem);
ShannonsTheorem = new ShannonsTheorem("Some attribute value");
assertNotNull("\t\tTest_ShannonsTheorem.testConstructors: ShannonsTheorem is null", ShannonsTheorem);
/*
* TODO: Add tests for your parameterized constructors here
*/
}
/**
* Test the accessors.
*/
public void testAccessors() {
System.out.println("\tExecuting Test_ShannonsTheorem.testAccessors");
shannonsTheorem = new ShannonsTheorem();
assertNotNull("\t\tTest_ShannonsTheorem.testAccessors: ShannonsTheorem is null", ShannonsTheorem);
/*
* TODO: Add tests for your accessors
*/
}
/**
* Test the mutators/modifiers.
*/
public void testMutators() {
System.out.println("\tExecuting Test_ShannonsTheorem.testMutators");
shannonsTheorem = new ShannonsTheorem();
assertNotNull("\t\tTest_ShannonsTheorem.testMutators: ShannonsTheorem is null", ShannonsTheorem);
/*
* TODO: Add tests for your mutators here.
*/
}
/**
* Test behaviors.
*/
public void testBehaviors() {
System.out.println("\tExecuting Test_ShannonsTheorem.testBehaviors");
shannonsTheorem = new ShannonsTheorem();
assertNotNull("\t\tTest_ShannonsTheorem.testBehaviors: ShannonsTheorem is null", ShannonsTheorem);
/*
* TODO: Add tests for you behavior here.
*/
}
/* STAND-ALONE ENTRY POINT ----------------------------------------- */
/**
* Main line for standalone operation.
* @param args Standard string command line parameters.
*/
public static void main(String[] args) {
System.out.println("Executing Test_ShannonsTheorem suite");
junit.textui.TestRunner.run(suite());
}
/* ATTRIBUTES ----------------------------------------------- */
private ShannonsTheorem shannonsTheorem = null;
} /* End of CLASS: Test_ShannonsTheorem.java */
AND AllTests.java
package networktest;
import junit.framework.*;
import networktest.*;
/**
* JUnit test class to execute all JUNIT tests for the "Shannons Theorem" project.
*
*/
public class AllTests extends TestCase {
public AllTests(String name) { super(name); }
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(Test_ShannonsTheorem.suite());
return suite;
}
/* STAND-ALONE ENTRY POINT ----------------------------------------- */
/**
* Main line for standalone operation.
* @param args Standard string command line parameters.
*/
public static void main(String[] args) {
System.out.println("Executing AllTests ...");
junit.textui.TestRunner.run(suite());
}
} /* End of CLASS: AllTests.java */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started