Problem 2
1. In the src → edu.neiu.p2 directory, create a package named problem1.
2. Create a Java class named Complement with the following:
• A public static method named onesComplement that takes a String as a parameter and returns a String that is the 1’s complement of the parameter.
• If the String is not a valid binary number (i.e. only has 0s and 1s), you should throw an IllegalArgumentException with the message "Not a valid binary number".
• You may not use any loops.
• You may use one conditional statement, but it cannot include any else if or else blocks.
3. Once you’ve created the method, run the ComplementTest class in the tests package to see if you’ve created the code correctly.
4. To help yourself debug and test your code, create a Java class named ComplementDemo that has the main method. In the main method include at least three examples.
Problem 3
1. In the problem3 package, create a class named PositiveNumberStatistics that has the following:
• A private double 1D array instance variable named numbers.
• A constructor that takes one parameter, a 1D double array. The constructor should throw an IllegalArgumentException with the message "Array length of zero" if the parameter has a length of 0. If the exception is not thrown, the constructor should create the numbers array and copy every element from the parameter into the numbers instance variable. However, if any of the elements are less than or equal to zero, the constructor should throw a NumberFormatException with the message "Non-positive value at index X" where X is the first index at which a non-positive value occurs.
• A method named average that does not take any parameters and returns a double. The method should return the average of the numbers in the instance variable numbers. Note that you do not need to worry about whether the numbers instance variable has been created in this method because this method will only be usable if the constructor does not throw an exception.
2. Once you’ve created the method, run the PositiveNumberStatisticsTest class in the tests package to see if you’ve created the code correctly.
3. Then, in the StatsDemo class in the problem3 folder, write the code to handle creating an object for each of the provided arrays in the main method and call the average method. You should handle any exceptions by printing out the result of calling the toString method on the exception object.
Note: these problems are done through IntelliJ due to the files provided, but can be done using any java program. Any help is appreciated, thank you!
Problem 3: StartDemo class
package neiu.edu.p2.problem3;
public class StatsDemo
{
public static void main(String[] args) {
double[] nums = { 0, 2.2, 9.8, 3 };
double[] nums1 = new double[0];
double[] nums2 = { 8, 3, -2, 0, -9 };
double[] nums3 = { 3, 2, 8, 8, 10, 100 };
}
}
Problem 2: ComplementTest class
package neiu.edu.p2.tests.problem2;
// UNCOMMENT THIS IMPORT
//import neiu.edu.p2.problem2.Complement;
public class ComplementTest {
public static void main(String[] args) {
// TESTS - UNCOMMENT THE TEST METHOD CALLS
/*shouldTestMixed01Complement();
shouldTestInvalidBinaryStringComplement();
shouldTestEmptyStringComplement();
shouldTestAllZerosComplement();
shouldTestAllOnesComplement();*/
}
// UNCOMMENT ALL OF THESE TESTS
/*private static void shouldTestMixed01Complement() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
try {
String actual = Complement.onesComplement("0100010");
String expected = "1011101";
System.out.println(actual.equals(expected) ? "PASSED" : "FAILED");
System.out.println("Expected: " + expected);
System.out.println("Actual: " + actual);
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}
private static void shouldTestAllZerosComplement() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
try {
String actual = Complement.onesComplement("0000");
String expected = "1111";
System.out.println(actual.equals(expected) ? "PASSED" : "FAILED");
System.out.println("Expected: " + expected);
System.out.println("Actual: " + actual);
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}
private static void shouldTestAllOnesComplement() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
try {
String actual = Complement.onesComplement("111111");
String expected = "000000";
System.out.println(actual.equals(expected) ? "PASSED" : "FAILED");
System.out.println("Expected: " + expected);
System.out.println("Actual: " + actual);
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}
private static void shouldTestInvalidBinaryStringComplement() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
try {
Complement.onesComplement("047&3921t");
} catch (IllegalArgumentException e) {
String expected = "java.lang.IllegalArgumentException: Not a valid binary number";
System.out.println(e.toString().equals(expected) ? "PASSED" : "FAILED");
System.out.println("Expected: " + expected);
System.out.println("Actual: " + e.toString());
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}
private static void shouldTestEmptyStringComplement() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
try {
Complement.onesComplement("");
} catch (IllegalArgumentException e) {
String expected = "java.lang.IllegalArgumentException: Not a valid binary number";
System.out.println(e.toString().equals(expected) ? "PASSED" : "FAILED");
System.out.println("Expected: " + expected);
System.out.println("Actual: " + e.toString());
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}*/
}
Problem 3: PositiveNumberStatisticsTest class
package neiu.edu.p2.tests.problem3;
// UNCOMMENT THIS IMPORT
//import neiu.edu.p2.problem3.PositiveNumberStatistics;
public class PositiveNumberStatisticsTest
{
public static void main(String[] args) {
// TESTS
/*shouldTestZeroLengthArray();
shouldTestArrayWithNonPositiveValues();
shouldTestAverage();*/
}
/*private static void shouldTestZeroLengthArray() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
try {
double[] nums = new double[0];
new PositiveNumberStatistics(nums);
} catch (IllegalArgumentException e) {
String expected = "java.lang.IllegalArgumentException: Array length of zero";
System.out.println(e.toString().equals(expected) ? "PASSED" : "FAILED");
System.out.println("Expected: " + expected);
System.out.println("Actual: " + e.toString());
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}
private static void shouldTestArrayWithNonPositiveValues() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
try {
double[] nums = { 8, 3, -2, 0, -9 };
new PositiveNumberStatistics(nums);
} catch (IllegalArgumentException e) {
String expected = "java.lang.NumberFormatException: Non-positive value at index 2";
System.out.println(e.toString().equals(expected) ? "PASSED" : "FAILED");
System.out.println("Expected: " + expected);
System.out.println("Actual: " + e.toString());
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}
private static void shouldTestAverage() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
try {
double[] nums = { 4, 2, 6, 12 };
PositiveNumberStatistics pn = new PositiveNumberStatistics(nums);
double avg = pn.average();
System.out.println(avg == 6.0 ? "PASSED" : "FAILED");
System.out.println("Expected: " + 6.0);
System.out.println("Actual: " + avg);
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}*/
}
Problem 2
Complement class :
package edu.neiu.p2.problem1;
public class Complement {
//constructor
public Complement(){
}
//public static method that
//takes string as input
//and returns string as output
public static String onesComplement(String binaryInput){
//to store ones complement
String onesComp = "";
//check whether string entered
//is binary or not
//check string not contains only 1 and 0
if(!binaryInput.matches("^[01]+$")){
//throw an exception
//with the error message
//"Not a valid binary number
throw new IllegalArgumentException("Not a valid binary
number");
}
//if valid binary replace
//all 0 with 1 and
//all 1 with 0
binaryInput = binaryInput.replaceAll("0","-");
onesComp = binaryInput.replaceAll("1","0");
onesComp = onesComp.replaceAll("-","1");
//return the 1's complement of parameter
return onesComp;
}
}
Testing Class for Complement
package edu.neiu.p2.problem1;
//import the complement class
import edu.neiu.p2.problem1.Complement;
//test class
public class ComplementDemo {
public static void main(String args[]){
//create an object
Complement comp = new Complement();
String binaryInput = "10101111";
String binaryInput1 = "11111010";
String binaryInput2 = "10103451111";
//call method and display ones complement
System.out.println("Ones complement of "+binaryInput+" is :
"+comp.onesComplement(binaryInput));
System.out.println("Ones complement of "+binaryInput1+" is :
"+comp.onesComplement(binaryInput1));
//here will be an exception
System.out.println("Ones complement of "+binaryInput2+" is :
"+comp.onesComplement(binaryInput2));
}
}
Output of problem 2:



Problem 3
package problem3;
public class PositiveNumberStatistics {
//private double 1D array instance variable
private double[] numbers;
//constructor
public PositiveNumberStatistics(double[] num){
if(num.length == 0){
//throw exception
throw new IllegalArgumentException( "Array length of zero");
}
else{
numbers = new double[num.length];
//copying elements of array num to numbers
for(int i=0;i<num.length;i++){
if(num[i]<=0)
throw new NumberFormatException("Non-positive value at index
"+i);
else{
numbers[i] = num[i];
}
}
}
}
//method to calculate average
public double average(){
//to store average value
double avg;
double sum=0.0;
for(int i=0;i<numbers.length;i++){
sum += numbers[i];
}
//calculate average
avg = sum / numbers.length;
//return average value
return avg;
}
}
Testing Class
package problem3;
import problem3.PositiveNumberStatistics;
//test class
public class StatsDemo {
public static void main(String args[]){
//double arrays
double[] dn1,dn2;
dn1 = new double[]{0.7d,0.8d,2.38d,0.34d};
dn2 = new double[]{-0.7d,0.8d,2.38d,0.34d};
//create an object
PositiveNumberStatistics pos = new
PositiveNumberStatistics(dn1);
//display result
System.out.println("Average : "+pos.average());
PositiveNumberStatistics pos1 = new
PositiveNumberStatistics(dn2);
System.out.println("Average : "+pos1.average());
}
}
Output of problem 3



Problem 2 1. In the src → edu.neiu.p2 directory, create a package named problem1. 2. Create...
Problem 1
1. In the src → edu.neiu.p2 directory, create a package named
problem1.
2. Create a Java class named StringParser with the
following:
• A public static method named findInteger that takes a String
and two char variables as parameters (in that order) and does not
return anything.
• The method should find and print the integer value that is
located in between the two characters. You can assume that the
second char parameter will always follow the first...
Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called HW4P1 and add the following: • The main method. Leave the main method empty for now. • Create a method named xyzPeriod that takes a String as a parameter and returns a boolean. • Return true if the String parameter contains the sequential characters xyz, and false otherwise. However, a period is never allowed to immediately precede (i.e. come before) the sequential characters xyz....
JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...
Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...
Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...
Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...
Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change. I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here package threadExamples; import java.util.Scanner; public class InteractiveCounting {...
I keep getting an Error after I ask the user for test scores. What is missing? package testscores; import java.util.Scanner; public class TestScores { /** * @param args the command line arguments */ private double[] scores; public TestScores(double[] score) throws IllegalArgumentException { scores = new double[scores.length]; for (int i = 0; i < scores.length; i++) { if (score[i] < 0 || score[i] > 100){ throw new IllegalArgumentException(); } scores[i]=score[i]; } } public double getAverage() { int sum=0;...
How do I separate the method into different class? I try
separate the testing and method class into 2 different class. And
when I test it, it said that the method is undefined. And it ask me
to create the method in the main class. I don't know what is the
problem.
This is the access to the code
https://repl.it/@Teptaikorn/test
Thank you very much
MazeSolver.java MazeTerster.... TwoDim AutoA... testfile.txt Main.java x > 0 N Binary TreeN... X LinkedBinary... Maze.java 1...
The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket. Socket are auto-closeable and thus can use a try-with-resources instead. Edit the dictionary program to use try-with-resources instead. package MySockets; import java.net.*; import java.io.*; public class DictionaryClient { private static final String SERVER = "dict.org"; private static final int PORT = 2628; private static final int TIMEOUT = 15000; public static void main(String[] args) { Socket ...