Lecture 30, CS 302-6, November 11

  1. Various things
    1. Program 3

                                                               i.     Using P - pause

                                                             ii.     Using T – tick

                                                            iii.     Using S - to string

  1. Review:
    1. References, ==,  .equal
    2. Class members

                                                               i.     Class variables

                                                             ii.     Class constants

                                                            iii.     Class methods

    1. Combining tester and instantiable classes
  1. Command Line Arguments
    1. Can pass information to our programs from the command line

                                                               i.     (From run configuration in Eclipse)

    1. Reminder how we declare our main methods:

public static void main(String[] args)

                                                               i.     The argument for the main method is an array of Strings

    1. What are these used for?

                                                               i.     A lot of the time, file names

                                                             ii.     Or, values that you would otherwise have to specify in your program

    1. Example – CommandLineArgs.java

                                                               i.     If we put two words in “”s, they are treated as one String

    1. These are used in Program 3 – just follow the instructions on the program website.
  1. Refactoring
    1. Definition

                                                               i.     Automatically restructuring existing code

    1. Can refer to lots of different things
    2. In Eclipse – one thing we can do is change a variable name everywhere it is used in our code

                                                               i.     Refactor - > Rename

    1. We can also move methods, etc
  1. Importing packages
    1. What is a package?

                                                               i.     A set of related classes

                                                             ii.     Java standard library contains many

                                                            iii.     Examples we’ve seen:

1.    java.util

2.    java.lang

                                                           iv.     Most of the time, we need to import packages before using classes from them.

1.    Why?  Prevent class name collision – different packages can contain classes with the same name

                                                             v.     Don’t need to import java.lang – it is so common that it’s included by default

    1. How to create/add to a package

                                                               i.     Package packageName; at top of file

                                                             ii.     Store in folder with name/name/name

1.    corresponds to the dots in the package name.

2.    So, if we put our class in the package rob.atlas, we would need to store it in the directory /rob/atlas/

                                                            iii.     FYI: Why can we import the java dot packages, even though they are not stored in the same directory as our code?

1.    Based on classpath

                                                           iv.     In Eclipse – just create a package (via menu), and it takes care of the rest.

1.    You can then drag and drop this package into other projects

  1. Think about:
    1. P7.13 from the book
    2. Create a Country class

                                                               i.     This class takes – country name, population, and area as command line args, in sets of three

1.    So, to create two countries, you give it 6 arguments, etc

                                                             ii.     We will assume good formatting, right # of arguments, etc

                                                            iii.     Integer.parseInt(); to change a string argument to an integer in our program

    1. For each set of three input args, create a new Country object
    2. Tell the user:

                                                               i.     The largest country

                                                             ii.     The country with the highest Population

                                                            iii.     The country with the highest population density

    1. Getters/setters for each of the above
    2. Also, keep track of a unique ID number for each country (in case they put in two with the same name) using class members
  1. Homework – For Monday – Code up the Country class, work on Program 3, study for Exam 2