Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1 1. In the src edu.neiu.p2 problem1 directory, create a Java class called Fridge and add the following: Two properly encapsulated instance variables: -

Problem 1

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

Two properly encapsulated instance variables: - items: String[] - warranty: String

A default, no-arg constructor that explicitly sets the warranty instance variable to a value of "01/01/2022" and the items instance variable to have a length of zero (yes, you can have a zero-length array!).

A constructor that takes 2 parameters, a String and a 1D String array, and sets the instance variables. Note: You should not directly assign the String array parameter to the String array instance variable, but instead create a new array and copy every element over. Why??

Getters for both instance variables. Go to the FridgeTest class in the tests directory and uncomment the import at the top and the first block of tests (find the comment FIRST TESTS). Run the tests to see if you created your code correctly thus far.

A public method named isEmpty that does not take any parameters and returns true if items does not have any elements and false otherwise. Uncomment the second block of tests (find the comment SECOND TESTS). Run the tests to see if the method has been created correctly.

A public method named addItem that takes one parameter, a String, and modifies the items instance variable to add an additional item as the last element of the array. Hint: You will need to create a new array. Uncomment the third block of tests (find the comment THIRD TESTS). Run the tests to see if the method has been created correctly.

A public method named printItems that prints each element of the items instance variable on its own line. If there are no elements (Hint: use your isEmpty method!!), print out "No items in fridge". Uncomment the fourth block of tests (find the comment FOURTH TESTS). Run the tests to see if the method has been created correctly.

2. You have been provided with a class named DemoProblems that has the main method (which is empty). In the main method, create at least two objects, one with the default constructor and one with the overloaded constructor. Write code to call the isEmpty method, add items to your objects, and print out the results. Each method should be called at least once on each of your objects. You should run this class to see your output. You can also use this class to help you debug any problems with tests.

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 edu.neiu.p2 test directory. To run an individual test, put your cursor on the test method name, right-click and choose the green play button that is followed by "Run" and the test name. To run all the uncommented tests in a file, right-click the file in the Project view and choose Run followed by the class name. The output for the tests will appear in the console. If a test fails, you will see a stack trace and right above the stack trace will be a statement that shows the expected value vs the actual value (that your code provided). Do not modify the tests - only comment or uncomment. Remember that red squiggly lines means theres an error in your code - do not run any tests if your code has errors - the tests wont run! Note - you get better error messages when you run an individual test as opposed to running all of them, but all of them have to pass!

Note: These problems are done through intelliJ. The already given classes were sent through a zip file.

DemoProblems class

package edu.neiu.p2; public class DemoProblems { public static void main(String[] args) { } }

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

 For Problem 1: FridgeTest class package problem1; // UNCOMMENT THIS IMPORT!! // import edu.neiu.p2.problem1.Fridge; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import static org.junit.jupiter.api.Assertions.*; public class FridgeTest { private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; @BeforeEach public void setUpStream() { System.setOut(new PrintStream(outContent)); } @AfterEach public void restoreStream() { System.setOut(originalOut); } // FIRST TESTS /*@Test void shouldReturnDefaultWarrantyValueForDefaultConstructor() { Fridge f = new Fridge(); assertEquals("01/01/2022", f.getWarranty()); } @Test void shouldReturnEmptyArrayForDefaultConstructor() { Fridge f = new Fridge(); String[] empty = new String[0]; assertArrayEquals(empty, f.getItems()); } @Test void shouldReturnWarrantyForOtherConstructor() { Fridge f1 = new Fridge("02/05/2021", new String[0]); Fridge f2 = new Fridge("03/12/2024", new String[0]); assertEquals("02/05/2021", f1.getWarranty()); assertEquals("03/12/2024", f2.getWarranty()); } @Test void shouldReturnItemsForOtherConstructor() { String[] items = {"apples", "bananas", "limes", "steak"}; Fridge f = new Fridge("02/05/2021", items); assertArrayEquals(items, f.getItems()); } @Test void shouldReturnFalseWhenComparingConstructorParameterAndItemsVariable() { String[] items = {"apples", "bananas", "limes", "steak"}; Fridge f = new Fridge("02/05/2021", items); assertEquals(false, f.getItems() == items); }*/ // SECOND TESTS /*@Test void shouldReturnTrueIfItemsIsEmpty() { Fridge f = new Fridge(); assertEquals(true, f.isEmpty()); } @Test void shouldReturnFalseIfItemsIsNotEmpty() { String[] items = {"apples", "bananas", "limes", "steak"}; Fridge f = new Fridge("02/05/2021", items); assertEquals(false, f.isEmpty()); }*/ // THIRD TESTS /*@Test void shouldAddItemToItems() { String[] items = {"apples", "bananas", "limes", "steak"}; Fridge f = new Fridge("02/05/2021", items); f.addItem("chicken"); String[] items2 = {"apples", "bananas", "limes", "steak", "chicken"}; assertArrayEquals(items2, f.getItems()); }*/ // FOURTH TESTS /*@Test void shouldPrintAllItemsOnOneLine() { String[] items = {"apples", "bananas"}; Fridge f = new Fridge("02/05/2021", items); f.printItems(); assertEquals("apples bananas ", outContent.toString()); } @Test void shouldPrintMessageIfNoItemsInFridge() { Fridge f = new Fridge(); f.printItems(); assertEquals("No items in fridge ", outContent.toString()); }*/ } 

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

For problem 2: CoordinateSystemTest class

package problem2; // UNCOMMENT THIS IMPORT!! // import edu.neiu.p2.problem2.CoordinateSystem; import org.junit.jupiter.api.Test; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import static org.junit.jupiter.api.Assertions.*; class CoordinateSystemTest { // FIRST TESTS /*@Test void shouldVerifyConstructorIsPrivate() throws NoSuchMethodException { Constructor constructor = CoordinateSystem.class.getDeclaredConstructor(); assertEquals(true, Modifier.isPrivate(constructor.getModifiers())); }*/ // SECOND TESTS /*@Test void shouldTestTopRightQuadrant() { assertEquals("Top right", CoordinateSystem.quadrant(2, 5)); } @Test void shouldTestTopLeftQuadrant() { assertEquals("Top left", CoordinateSystem.quadrant(-2, 3)); } @Test void shouldTestBottomLeftQuadrant() { assertEquals("Bottom left", CoordinateSystem.quadrant(-3, -4)); } @Test void shouldTestBottomRightQuadrant() { assertEquals("Bottom right", CoordinateSystem.quadrant(7, -12)); } @Test void shouldTestAxisOfQuadrant() { assertEquals("On an axis", CoordinateSystem.quadrant(0, 22)); assertEquals("On an axis", CoordinateSystem.quadrant(-4, 0)); }*/ // THIRD TESTS /*@Test void shouldTestDistance() { assertEquals(5.0, CoordinateSystem.distance(5, 7, 9, 4)); }*/ }

Problem 2 1. In the src edu.neiu.p2problem2 directory, create a Java class called CoordinateSystem and add the following: A private constructor that does nothing. Go to the CoordinateSystemTest class in the tests directory and uncomment the import at the top and the first block of tests (find the comment FIRST TESTS). Run the tests to see if you created your code correctly thus far. A static method named quadrant that takes two double values as parameters (that represent the x and y values of a point) and returns a String. The method should return "Top right" if both x and y are greater than zero, "Top left" if x is less than zero and y is greater than zero, "Bottom left" if both x and y are less than zero,"Bottom right" if x is greater than zero and y is less than zero, and "On an axis" otherwise. Why is this method static? Uncomment the second block of tests (find the comment SECOND TESTS). Run the tests to see if you created the method correctly. A static method named distance that takes four double values as parameters that rep- resent two points: (x,y) and (x1, yl) and returns a double. The parameters should be in the following order: x, y, xl, yl. The method should calculate and return the distance between the two points using the formula below: distance = V(x - x1)2 + (y - y1)2 Uncomment the third block of tests (find the comment THIRD TESTS). Run the tests to see if you created the method correctly. 2. In the main method of the Demo Problems class, write code to call each of the methods from the CoordinateSystem 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

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

Step: 3

blur-text-image

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

DB2 11 The Database For Big Data And Analytics

Authors: Cristian Molaro, Surekha Parekh, Terry Purcell, Julian Stuhler

1st Edition

1583473858, 978-1583473856

More Books

Students also viewed these Databases questions

Question

What is meant by 'Wealth Maximization ' ?

Answered: 1 week ago