To Lecture Notes

IT 313 -- Jan 21, 2020

Review Exercises

  1. For a class, what are getters and setters?
    Ans: a getter is a public method that returns the value of the corresponding private instance variable.
    A setter is a public method that changes the value of the corresponding private instance variable.
  2. Use IntelliJ to write the constructor and the toString methods of the Person class with fields name, gender, and age. We saw last time how to have IntelliJ write the getters and setters.
  3. Write a static method numItems that inputs an array of int and an int value. Return the number of items in the array that are greater than or equal to the value. Ans:
    Here is the Method class containing the numItems method:
    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);
        }
    }
    
  4. Write a unit test project that tests the String methods
    equals  lastIndexOf  repeat  strip
    
    Because 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");
        }
    }
    
  5. What is the difference between a static method and an instance method?
  6. Write a Counter class that contains the instance variable count and the methods
    incrementCount  resetCount  getCount  toString   constructor
    
    Ans:
    // 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);
        }
    }
    
  7. Create a traditional test file to test your class. Ans:
    // 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( ));
        }
    }
    
  8. Create a unit test file to test your class. Ans:
    // 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");
        }
    }
    
  9. Add the static variable numObjects to your Counter class in the previous problem.
  10. Look at the unicode1 and unicode2 modules in the Unicode Example project.

UML Diagrams

Project 3

The StringBuilder Class

Reading Data from the Web

File I/O

ArrayLists and Wrapper Classes

  1. Look at the IntegerTest class in the InstanceMethods Example project.
  2. Look up the ArrayList collection in the Java Class Library and find some useful methods to test.
  3. Declare and instantiate an ArrayList object a. Add these items to a:
    37  3.452  "dog"  'c'  true.
    

Catching Exceptions