public class Methods { public static int numItems(int[ ] array, int cutoff) { int count = 0; for(int value : array) { if (value >= cutoff) { count++; } } return count; } }Here is the JUnit5 test class that tests the numItems method:
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class MethodsTest { private int[ ] a = {32, 57, 81, 23, 21}; @BeforeEach void setUp( ) { } @Test void testNumItems( ) { assertEquals(Methods.numItems(a, 50), 2); } }
equals lastIndexOf repeat stripBecause the class you are testing is not in your IntelliJ project, create a class named Dummy to which you use IntelliJ to add a unit test. Ans:
// Unit test class for testing String methods. import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class StringTest { private String s, t, u, v, w; @BeforeEach void setUp() { s = "cantaloupe"; t = "canta" + "loupe"; u = "mississippi"; v = "dog"; w = " abc \t \n"; } @Test void testEquals( ) { assertEquals(s.equals(t), true); } @Test void testLastIndexOf( ) { assertEquals(u.lastIndexOf("ssi"), 5); } @Test void testRepeat( ) { assertEquals(v.repeat(3), "dogdogdog"); } @Test void testStrip( ) { assertEquals(w.strip( ), "abc"); } }
incrementCount resetCount getCount toString constructorAns:
// Counter class. public class Counter { private int count; public Counter( ) { this.count = 0; } public void incrementCount( ) { this.count++; } public void resetCount( ) { this.count = 0; } public int getCount( ) { return count; } @Override public String toString( ) { return String.valueOf(count); } }
// Traditional test file public class Test1 { public static void main(String[] args) { Counter c = new Counter( ); c.incrementCount( ); c.incrementCount( ); c.incrementCount( ); System.out.println(c.getCount( )); System.out.println(c); c.resetCount( ); System.out.println(c.toString( )); } }
// Unit test file import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class CounterTest { private Counter c; @BeforeEach void setUp( ) { c = new Counter( ); } @Test void testIncrementCount( ) { c.incrementCount( ); c.incrementCount( ); c.incrementCount( ); assertEquals(c.toString( ), "3"); } @Test void testResetCount( ) { c.incrementCount( ); c.resetCount( ); assertEquals(c.toString( ), "0"); } @Test void testGetCount( ) { c.incrementCount( ); c.incrementCount( ); c.incrementCount( ); assertEquals(c.getCount( ), 3); } @Test void testToString( ) { assertEquals(c.toString( ), "0"); } }
Person |
- name : String - gender : char - age : int |
+ Person(theName : int, theGender : char, theAge : int) + getName( ) : String + getGender( ) : char + getAge( ) : int + setAge(theName : String) + haveBirthday( ) + toString( ) : String |
- name : Stringmeans that name is a private instance variable of datatype String.
+ getAge( ) : Stringmeans that getAge is a public instance method with no parameters and String return value.
+ setAge(theAge : int)means that setAge is a public instance method with one int parameter and no return value.
/* Before class header: */ import java.io.*; /* The main method: */ public static void main(String[ ] args) throws FileNotFoundException { PrintWriter pw = new PrintWriter("greeting.txt"); pw.println("Hello."); pw.close( ); }greeting.txt is created in the project folder, not in the src or bin folders.
Alice PeterThe greeting should be written to the files alice.txt and peter.txt.
37 3.452 "dog" 'c' true.
try { int x = 3 / 0; System.out.println("Quotient == " + x); } catch(ArithmeticException e) { System.out.println("Division by zero."); }
public static void main(String[ ] args) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.println("Sum: " + (a / b)); }