Lesson 19 Static Constants and Methods

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Bluej
  4. Navigate to laughton.com/obrien/sjsu/cs46a/CRT_lessons/ and open this lesson

Questions

Concept - About Static Variables

 

Concept - Why static constants

Why would someone use a static constant?

Activity - Static constant

Record your participation in Piazza clicker question Lesson19 Q1

Concept - Static methods

A static method:

Activity - Character class

Record your participation in Piazza clicker question Lesson19 Q2

What about the Character class?

  1. Yes
  2. No
  3. I did not check

Activity - Static vs instance method

Record your participation in Piazza clicker question Lesson19 Q3

Non-static methods are called instance methods.

Pick a library class we have used and name an instance method it contains

  1. I found one
  2. I didn't know how to look
  3. I didn't try

 

Concept - Writing static methods

 

Activity - Writing a static method

Record your participation in Piazza clicker question Lesson19 Q4

Here is a project containing a Circle class which represents a circle in 2d space.

 

Activity - More practice writing static methods

Record your participation in Piazza clicker question Lesson19 Q5

Now in the same project, add a class called Util

Activity - More practice writing static methods

Record your participation in Piazza clicker question Lesson19 Q6

Add a static method to Util that takes an ArrayList of Strings as a parameter and returns the longest word or the empty String if the ArrayList is empty. Call the method longest()

Activity - More practice writing static methods

Record your participation in Piazza clicker question Lesson19 Q7

Add a tester class to the project which tests your methods. Here is a starter.

import java.util.ArrayList;
public class UtilTester
{
    public static void main(String[] args)
    {
        int[] numbers = {2, 7, 4, 9, 11};
        
        ArrayList<String> strings =new ArrayList<>();
        strings.add("int");
        strings.add("final");
        strings.add("static");
        strings.add("interface");
        strings.add("final");
        
         ArrayList list = new ArrayList();      
         
        //put your test code below

        //test sum
               
        //test longest
        
        //test empty ArrayList        
    }
}