Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Note: These problems are done through IntelliJ. Problem 1 1. In the src edu.neiu.p2 problem1 directory, create a Java class called CheckingAccount and add the

Note: These problems are done through IntelliJ.

Problem 1

1. In the src edu.neiu.p2 problem1 directory, create a Java class called CheckingAccount and add the following:

A properly encapsulated instance variable: - balance: BigDecimal

A constructor that takes 1 parameter, a String and sets the balance instance variable.

A getter for the instance variable. Go to the CheckingAccountTest class in the tests directory and uncomment the import at the top, the first block of tests (find the comment FIRST TEST), and the corresponding method. Run the test to see if you created your code correctly thus far. Make sure all 1 tests pass.

A public method named withdraw that takes a String parameter and does not return anything. The method should subtract that value from the balance. Uncomment the second block of tests (find the comment SECOND TEST) and the corresponding method. Make sure all 1 tests pass.

A public method named deposit that takes one parameter, a String, does not return anything. The method should add that value to the balance. Uncomment the third block of tests (find the comment THIRD TEST) and the corresponding method. Make sure all 1 tests pass.

2. You have been provided with a class named HomeworkDemo that has the main method (which is empty). In the main method, create a CheckingAccount object with an initial balance of $100.00. Withdraw 1.37 and print out the balance. Deposit 2.55 and print out the result. Then, print out the result of doing the following calculation using doubles: 100.00 - 1.37 + 2.55. You can also use this class to help you debug any problems with tests.

Problem 2

1. In the src edu.neiu.p2 problem2 directory, create a Java class called FirstDivisible and add the following:

A private constructor that does nothing. Go to the FirstDivisibleTest class in the tests directory and uncomment the import at the top, the first block of tests (find the comment FIRST TEST), and the corresponding method. Make sure all 1 tests pass.

A static method named firstNumberDivisibleBy that takes three integer values named a, b, and n and returns an integer. The method should return the first value that is divisible by both a and b and that is larger than n. Do not exceed the maximum possible value for ints. If you cannot find a number that is larger than n but less than or equal to the largest possible value for ints, return -1. Hint: You may find the Integer wrapper class helpful! Uncomment the second block of tests (find the comment SECOND TESTS) and the corresponding methods. Make sure all 2 tests pass.

A static overloaded method named firstNumberDivisibleBy that takes two integer values named a and b and returns a BigInteger. The method should return the first value that is divisible by both a and b and that is larger than the largest possible long value (Hint: The Long wrapper class may be helpful!). Note: To compare two BigInteger objects, you cannot use ==. Instead, use the equals method. An example is provided below. Uncomment the third block of tests (find the comment THIRD TEST) and the corresponding method. Make sure all 1 tests pass.

image text in transcribed

Running Tests

At various steps of the assignment, you will be told to run tests to check your code. All tests can be found in src test directory. DO NOT MODIFY THE TEST PACKAGE LOCATION OR THE TEST CODE OTHER THAN TO UNCOMMENT.

All test classes have a main method.

Uncomment the method calls in the main method and the methods themselves as directed.

Each method call in the main method is a test.

To run the tests, right-click on the test file and choose Run (followed by the class name).

Check to make sure that all the tests display "PASSED". If not, look at the Expected vs the Actual values or the stack trace (i.e. error) if applicable.

For Problem 1: CheckingAccountTest class

package edu.neiu.p2.test.problem1;
// UNCOMMENT THIS IMPORT //import edu.neiu.p2.problem1.CheckingAccount; import java.math.BigDecimal; public class CheckingAccountTest { public static void main(String[] args) { // FIRST TEST // constructorShouldSetInstanceVariable(); // SECOND TEST // shouldWithdrawAmountFromBalance(); // THIRD TEST // shouldDepositAmountIntoBalance(); } // UNCOMMENT THIS METHOD FOR THE FIRST TEST /*private static void constructorShouldSetInstanceVariable() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { BigDecimal actual = new CheckingAccount("1480.29").getBalance(); System.out.println(actual.equals(new BigDecimal("1480.29")) ? "PASSED" : "FAILED"); System.out.println("Expected: 1480.29"); System.out.println("Actual: " + actual); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ // UNCOMMENT THIS METHOD FOR THE SECOND TEST /*private static void shouldWithdrawAmountFromBalance() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { CheckingAccount account = new CheckingAccount("500.24"); account.withdraw("223.22"); System.out.println(account.getBalance().equals(new BigDecimal("277.02")) ? "PASSED" : "FAILED"); System.out.println("Expected: 277.02"); System.out.println("Actual: " + account.getBalance()); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ // UNCOMMENT THIS METHOD FOR THE THIRD TEST /*private static void shouldDepositAmountIntoBalance() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { CheckingAccount account = new CheckingAccount("645.08"); account.deposit("33.87"); System.out.println(account.getBalance().equals(new BigDecimal("678.95")) ? "PASSED" : "FAILED"); System.out.println("Expected: 678.95"); System.out.println("Actual: " + account.getBalance()); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ } 

------------------------------------------------------------------------------------

For Problem 2: FirstDivisibleTest class

package edu.neiu.p2.test.problem2; //UNCOMMENT THIS IMPORT!! //import edu.neiu.p2.problem2.FirstDivisible; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import java.math.BigInteger; public class FirstDivisibleTest { public static void main(String[] args) { // FIRST TEST // shouldReturnTrueIfConstructorIsPrivate(); // SECOND TESTS /*shouldReturnFirstDivisibleNumberWhenSpecifyingN(); shouldReturnNegativeOneIfNumberExceedsMaxIntegerValue()*/; // THIRD TESTS // shouldReturnFirstDivisibleNumberWhenNotSpecifyingN(); } // UNCOMMENT THIS METHOD FOR THE FIRST TEST /*private static void shouldReturnTrueIfConstructorIsPrivate() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { Constructor constructor = FirstDivisible.class.getDeclaredConstructor(); System.out.println(Modifier.isPrivate(constructor.getModifiers()) ? "PASSED" : "FAILED"); System.out.println("Expected: true"); System.out.println("Actual: " + Modifier.isPrivate(constructor.getModifiers())); } catch (NoSuchMethodException | RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ // UNCOMMENT THE NEXT TWO METHODS FOR THE SECOND TESTS /*private static void shouldReturnFirstDivisibleNumberWhenSpecifyingN() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { System.out.println(150 == FirstDivisible.firstNumberDivisibleBy(25, 50, 100) ? "PASSED" : "FAILED"); System.out.println("Expected: 150"); System.out.println("Actual: " + FirstDivisible.firstNumberDivisibleBy(25, 50, 100)); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldReturnNegativeOneIfNumberExceedsMaxIntegerValue() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { System.out.println(-1 == FirstDivisible.firstNumberDivisibleBy(10, 50, 2147483645) ? "PASSED" : "FAILED"); System.out.println("Expected: -1"); System.out.println("Actual: " + FirstDivisible.firstNumberDivisibleBy(10, 50, 2147483645)); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ // UNCOMMENT THIS METHOD FOR THE THIRD TEST /*private static void shouldReturnFirstDivisibleNumberWhenNotSpecifyingN() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { System.out.println(new BigInteger("9223372036854775856").equals(FirstDivisible.firstNumberDivisibleBy(7, 49)) ? "PASSED" : "FAILED"); System.out.println("Expected: 9223372036854775856"); System.out.println("Actual: " + FirstDivisible.firstNumberDivisibleBy(7, 49)); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ } 

------------------------------------------------------------------------------------

HomeworkDemo class

package edu.neiu.p2; public class HomeworkDemo { public static void main(String[] args) { } }
2. In the main method of the HomeworkDemo class, write code to call each of the methods from the FirstDivisible class. You should run this class to see your output. You can also use this class to help you debug any problems with tests. Make sure to clearly separate the code for Problem 1 from the code for Problem 2 with comments. | BigInteger el = new BigInteger("5"); BigInteger e2 = BigInteger.ZERO; boolean b = el.equals(e2)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

Students also viewed these Databases questions

Question

Explain the employee benefits that are required by law.

Answered: 1 week ago

Question

List the types of incentive plans.

Answered: 1 week ago