This lesson is being piloted (Beta version)

Classes in Java

Overview

Teaching: 30 min
Exercises: 30 min
Questions
  • What are the principles of Object-Oriented Programming?

  • How do we create classes in Java?

Objectives
  • Explain the four principles of OOP.

  • Build an example Rectangle class.

Pi j2.2 classes from mcollison

JiP2.1-OOP-intro.pptx - slides download JiP2.2-OOP-classes.pptx - slides download

  • Recall that you implemented a program to detect odd numbers. Create a class called OddEven and refactor the 'odd' code as a method.
      [access privacy] [return type] [identifier] (type arguments) { ... }
      public boolean odd(int input){ ... }
      
  • Define a new method called oddSum that, given an array of ints, e.g. arr, as an argument returns the sum of the odd numbers using the odd method. Implement a third method in OddEven that returns the sum of the even numbers, invoking the oddSum method in the process. Test your function from a Main class extracting and printing the odd and even numbers from arr array. Remember a for loop iterates through each element in a list and variables for cumulative updates must be created before the loop. 

    
    int[] arr = [1, 42, -3, 2, 39]
    for (int num : arr ) {
      //this for each loop iterates through each item in an array 
      //do something with each_number
    }
    

    Solution OddEven.java Solution Main.java - see exercise 5


  • Create a class, DigitExtraction, that contains a method lastDigit that returns the last digit of its argument. Thus DigitExtraction.lastDigit(657) should return 7. Make sure that your function works for negative numbers too. Hint, This can be a very short function.
    Write another method, firstDigit, that returns the first digit of its integer argument. Thus, DigitExtraction.FirstDigit(657) should return 6. Make sure that your program works for negative numbers too.
    You may find the remainder operator or modulo operator useful. This gives the remainder when the left hand operand is divided by the right hand operand as below. Create a DigitExtract object in Main and test your functions there as well.
    
    >>> 10 % 2
    0
    >>>11 % 2
    1
    >>> -10 % 3
    -1
    
  • Solution DigitsExtraction.java

    Solution Main.java see exercise 2


  • Following the example (CircleComputation.java and Rectangle.java) in the slides, write your own class called Rectangle.java. Use the Rectangle class to create a Rectangle object in Main and call the methods to return and print out the area and perimeter of a rectangle, given that you pass a height of 10.0 and width of 20.0 (in doubles) to the constructor. Extend your class to declare a method isSquare() that returns a boolean. If the ratio of width and height equals to 1, it is a square, otherwise it is not.
  • Key Points

    • Object-oriented programming is a programming paradigm that uses abstraction (in the form of classes and objects) to create models based on the real world environment