Computer Science 1: Enumerations

Lesson 17

This is a simple one. Java will let you define an enumerated type- or limit all the possible values a variable can have. For example:

enum Entrees {chicken, beef, fish, pork, tofu}

Entrees dinnerMenu;

dinnerMenu = Entrees.beef;

The variable dinnerMenu is now restricted to the 5 options given by the type Entrees. It is considered type-safe, which means attempted use of a value that doesn’t exist will result in a compile-time error. The value is accessed using dot notation.

Computer Science 1: Class Relationships

Lesson 16

The classes in a piece of software relate to each other in specific ways. 3 of these ways are dependency, aggregation, and inheritance.

Dependency

The dependency relationship is a “uses” relationship; where one class uses the other. This often happens when once class uses the methods of another.In general it’s best to have less dependancy- that way errors have less of a chance to move down the chain like dominos. An object can also depend on itself, or it’s objects can be dependant on one another.

Aggregation

Aggregation is a has-a relationship. An aggregate object id an object that contains references to another object as instance data. The more complex an object, the more likely it will need to be broken down into aggregate objects- smaller, more workable pieces.

“this”

“this” is a reserved word in java that allows an object to reference itself.This is often used to distinguish the parameters of the constructor from the instance variables of the object.

int variable1, variable2;

public Sum(variable1, variable2) {

this.variable1 = variable1;
this.variable2 = variable2;

}

Method Design

Every method has an algorithm that determines how it will work. An algorithm is a step-by-step process for solving a problem- like directions or a recipe. Algorithms often use psudocode- a mix of english and simple code statements to plan and show how the code will operate. When a method is too complex the process of method decomposition can be used to break the method into smaller, more manageable private methods to handle certain tasks.

Computer Science 1: File Parsing and Exceptions

In this lesson we are going to take advantage of the fact that the scanner object is an iterator to process input from a source. We have a file in .inp format that contains a list of URLs for the program to dissect. The example program uses two scanners located within a nested while loop to read the information. The first loop reads each line of the file or each URL, and the second part uses a delimiter “/” to print each part of the URL.

If there is an issue opening the file or finding the file the program will throw an IO Exception. This is written into the code by putting the phrase “throws IOException directly after calling the main method.

public static void main(String[] args) throws IOException {
//write code here
}

Questions

  1. Why did 2 different scanners have to be used?

Words to know

  • iterator: an object that allows you to process a collection of items one at a time.
  • Uniform Resource Locators : The long name of URLs or web addresses
  • delimiter: The character used to detect how to break up a string or break up tokens using the scanner object. Ex.- to break up parts of a URL the example program used /
Design a site like this with WordPress.com
Get started