To Lecture Notes

IT 313 -- Jan 23, 2020

Review Exercises

  1. What is the output?
    System.out.println('R' + 91);
    System.out.println("" + 'R' + 91);
    
    Ans:
    173
    R91
    
    For char + int, both values are treated as numeric, so the char is changed to its ASCII(or Unicode) code.
  2. For String + char + int, String + char is performed first, which is a String, then String + remaining_int is also String.
  3. Add the static variable numObjects to your Counter class from Monday.
    Ans: add this code to your Counter class:
    private static numObjects = 0;
    
    public int getNumObjects( ) {
        return numObjects;
    }
    
    Add this line to your Counter constructor:
    numObjects++;
    
  4. Look at the unicode1 and unicode2 modules in the Unicode Example project.
  5. For Project 3 test these instance variables:
          getAmtForPurchase   depositDime   purchaseCandyBar
    Provide traditional and unit tests. Ans:
    // Traditional Test File: Test1.java
    // Stan Smith
    // Project 3
    // Jan 23, 2020
    
    public class Test1 {
        public static void main(String[] args) {
    
            // Write and test these two lines before
            // typing anything else into your test file.
            VendingMachine vm = new VendingMachine();
            System.out.println(vm);
    
            // Test depositDime, getAmtForPurchase, and
            // purchaseCandyBar
            vm.depositDime( );
            vm.depositDime( );
            vm.depositDime( );
            vm.depositNickel( );
            vm.depositQuarter( );
            vm.depositQuarter( );
            System.out.println(vm.getAmtForPurchase( ));
            vm.loadCandyBars(5);
            vm.purchaseCandyBar( );
            System.out.println(vm);
        }
    }
            
    
    
    / Unit test file: Test2.java
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import static org.junit.jupiter.api.Assertions.*;
    
    class Test2 {
        private VendingMachine vm;
    
        @BeforeEach
        void setUp() {
            vm = new VendingMachine( );
            vm.loadCandyBars(5);
        }
    
        @Test
        void testAmtForPurchase( ) {
            assertEquals(0, vm.getAmtForPurchase( ));
        }
    
        @Test
        void testDepositDime() {
            vm.depositDime( );
            assertEquals(10, vm.getAmtForPurchase( ));
        }
    
        @Test
        public void testPurchaseCandyBar() {
            for(int i = 1; i <= 3; i++) {
                vm.depositQuarter( );
            }
            vm.purchaseCandyBar();
            assertEquals(vm.toString( ),"4 0 Candy bar dispensed");
        }
    }
    

The StringBuilder Class

File I/O