Need to create JUnit test file for this one. import java.util.Scanner; public cl
ID: 3936064 • Letter: N
Question
Need to create JUnit test file for this one.
import java.util.Scanner; public class Homeworkl{public static void main(String[] args) {//Let the user know what this program is for. JOptionPane.showMessageDialog(null, "This program calls a method to search an array for the specified word");//Array Declaration int t = 0;//Instantiate the Array//Make the array of words dynamic System.out.print("How many words would you like to list? (Please enter positive number): "); Scanner sc = new Scanner (System.in); int numberOfWords = sc.nextlnt(); String[] wordList = new String[numberOfWords]; System.out.println("");//Prompt the user to input the list of the words System.out.println("*Enter the list of words"); for(int i = 0; iExplanation / Answer
Hi there ,
I have made some changes on your code so that it could run without any problem.I have removed the JOptionPane from the for loop and I have placed it outside the loop and I have created a method and the implemntation I have moved it from main() to this newly created method and after that I have created a JUnit Test file named Homework1Test.Please have a look.
Homework1.java:
package com.chegg.programs;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Homework1 {
public static void main(String[] args) {
Homework1 homework1 = new Homework1();
homework1.show();
}
public void show() {
JOptionPane.showMessageDialog(null, "This program calls a method to search an array for a specified word");
// Array declaration
int t = 0;
// make the array of the words dynamic
System.out.println("how many words would you like to list?(Please enter positive number):");
Scanner sc = new Scanner(System.in);
int numberOfWords = sc.nextInt();
String[] wordList = new String[numberOfWords];
System.out.println("");
// Prompt the user to input the list of the words
System.out.println("Enter the list of the words");
for (int i = 0; i < numberOfWords; i++) {
System.out.println("Words" + (i + 1) + ".");
wordList[i] = sc.next();
}
// make the word searched for dynamic,so that a different word can be
// searched for each run
System.out.println("");
System.out.println("Enter the word searching for:");
String userInput = sc.next();
// printout the result with count of the number of times the word is
// found.
for (int i = 0; i < wordList.length; i++) {
if (wordList[i].contains(userInput)) {
t = t + 1;
JOptionPane.showMessageDialog(null, "Array value is True:" + wordList[i]);
break;
}
}
if (t > 0) {
JOptionPane.showMessageDialog(null, userInput + "exists in the Array " + t + "times.");
} else {
JOptionPane.showMessageDialog(null, userInput + "does not exist in the Array");
}
}
}
Homework1Test.java(Your JUnit test file):
package com.chegg.programs;
import static org.junit.Assert.*;
import org.junit.Test;
public class Homework1Test {
@Test
public void testMain() {
Homework1 homework1 = new Homework1();
homework1.show();
}
}
Hope this helps you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.