Home‎ > ‎ICT Programming‎ > ‎

Java

What is Object Oriented Programming? 

OOP is method of computer programming that involves using 'objects'. An object is essentially a piece of code that lets you create many similar pieces of code without actually re-writing the code each time. This is done by first writing a 'class', which is essentially a blueprint of the object, then you can easily create an 'instance' of the object, which is basically just one particular version of the object, built from the blueprint.

A class can have both methods (or 'functions' - basically, things that every object of that class can do) and properties (see below for examples of properties).

For example, a program involving animals might have a 'dog' class. The 'dog' class could contain methods for walking, barking, tail-wagging, begging, and sitting. These are things that all dogs can do. The properties of the dog class could be fur color (all dogs have fur, but each dog can have a unique fur color), size (all dogs have a size, but each dog can have a different size), and 'breed' (all dogs have a breed, but each dog can be a different breed, such as a lab, pit bull, etc).

Rather than write code for each dog to be able to run and bark, you can just create new dogs from your 'dog' class. Your 'run' and 'bark' code is only written once, but every 'dog' object you create from the dog class can use those 'run' and 'bark' functions. (source)


Java Unit Projects

First - Read Java Chapter 1

(download Shapes Lab here if you want to try it)

Main concepts discussed in this chapter:

■ objects ■ methods

■ classes ■ parameters

This chapter is the start of our journey into the world of object-oriented programming.

Here we introduce the most important concepts you will learn about: objects and classes.

By the end of the chapter you should have an understanding of what objects and classes

are, what they are used for, and how to interact with them. This chapter forms the basis of

all other explorations in the book.

All about Data Type (Variables)

variablewhat it storesexample
integernumberint i = 0;
doubledecimal numberdouble d = 3.14159;
character (char)one letterchar c = ‘j’;
stringtextString str = “hello”;
booleantrue/falseboolean b = true;

Making new variables:  Making a new variable is called initializing it.  To be able to use a variable later in the program you need to initialize it, and then you can use it within the set of brackets you initialized it in, and all ones further in.  All of the examples above are examples of initializing a variable.  you need the name of the variable (all lowercase except for String, and none abbreviated, except for int and char), followed by the name of the variable.  If you want to set a value, you add an equals sign, then the value. (char values are enclosed by single quotes (‘) and Strings are enclosed by double quotes (“).  And then you end with a semicolon.

Naming Variables: Believe it or not, this is sort of important.  Variable names can’t have spaces, or any characters that aren’t letters or numbers, or start the names with numbers.  So, what you do, is have the first word of the variable be lower case, and have each additional word start with a capitol letter.  (helloWorldHowAreYou). The one other thing you must always do when naming variables is use a name that describes the variable. It helps others understand your code.

Watch: Introduction to Basic Concepts



Project Title
 
Value


        Hello World

 
20
 2
  1. The Shapes Project

 10
 3
  1. The DOG Class

 5
 4
  1. Student Class

 10
 5
  1. Cellphone Class

 10
 6
  1. Ticket Machine

 15
 7
  1. Gym Membership
 30
 
Unit Total
 
100

Java units - Part 2

 8The Club

 20
 9The Product

 30
10 The Auction

 50

Java - Final Project - 50 pts 

Choose an appropriate project based on your level of comfort and understanding with Java.


Novice - Quiz Grading

Intermediate  - Leap Year

Advanced - The Game



Bonus Projects 


 
Calculator

Create a simple calculator in BlueJ that can add, subtract, multiply, divide, and round. Use this video as your example.    
 

+20 Bonus

 


Turtles 

Download this TURTLES package and see if you can create Turtle objects and get them to do things.

 
+5 Bonus




What Is an Object?

Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.

Real-world objects share two characteristics: They all have state and behaviour. Dogs have state (name, colour, breed, hungry) and behaviour (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behaviour (changing gear, changing pedal cadence, applying brakes). Identifying the state and behaviour for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: "What possible states can this object be in?" and "What possible behaviour can this object perform?". Make sure to write down your observations. As you do, you'll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviours (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behaviour (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.

Software objects are conceptually similar to real-world objects: they too consist of state and related behaviour. An object stores its state in fields (variables in some programming languages) and exposes its behaviour through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

What Is a Class?

A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behaviour of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behaviour.

Review this material: Java Tutorials - Classes and Objects

Defining objects and classes


A class declaration typically contains a set of attributes (sometimes called instance vari- ables) and functions (called methods in Java). So far a Java class declaration is very simi- lar to one in Simula. Attributes are declared almost as usual:

class Turtle {
private boolean penDown; protected int x, y;

          // Declare some more stuff
      }

The private and protected keywords require some explanation. The private declara- tion means that those attributes cannot be accessed outside of the class. In general, attributes should be kept private to prevent other classes from accessing them directly.

There are two other related keywords: public and protected. The public keyword is used to declare that something can be accessed from other classes. The protected key- word specifies that something can be accessed from within the class and all its subclasses, but not from the outside. 



Simple Declarations

Java supports the usual set of simple types, such as integer, boolean, and real variables. Here are a few of the most common ones:

int m, n; double x, y; boolean b; char ch;

// Two integer variables
// Two real coordinates
// Either ‘true’ or ‘false’
// A character, such as ‘P’ or ‘@’


Methods

In Java, functions and procedures are called methods. Methods are declared as follows: class Turtle {

          // Attribute declarations, as above

public void jumpTo(int newX, int newY) { x = newX;

y = newY; }

public int getX() { return x;

} }

This example contains two methods. The first is called jumpTo and has two integer param- eters, newX and newY.

The second method is called getX, has no parameters, and returns an integer. Note that the empty pair of parentheses must be present.

Both method declarations begin with the keyword public, to make sure they can be accessed from other classes. (It is however possible to declare methods private or pro- tected, which can be useful for internal methods which should not be used from other classes.)

Teach Yourself Java in 21 Minutes 7

Before the method’s name, a type is written to indicate the method’s return type. The jumpTo method does not return a value (i.e., it is a procedure, not a function). For this rea- son, it is declared as void (meaning ‘nothing’). The getX method returns an integer, so it is declared as int



Constructors:

Constructor, as the name suggests is a special function in the program which is invoked before the object is created.

A constructor doesn't allow any arithmetic functions i.e its not allowed to perform addition, subtraction or any type of function inside a constructor except declaration of variables. So a constructor is used to declare variables before the object of the class/program is created.




IF / ELSE Statements

Read more about if/else statements here

Another very useful tool in java is the "if & else statement."  the code for one would look like this:

{
if(number <= 0) {
System.out.println(“number is negative or zero”);
}
else { // now we can do another test
if((number > 0) && (number < 10)){
System.out.println(“between 0 and 10”);
}
}
else {
System.out.println(“number is 10 or higher”);
}
}

Things to notice:
  • There are no semicolons after the if and else. This is because the if and else aren't a statement by themselves, they need an action to be done to qualify them as a statement. That action is done within the brackets.
  • The brackets determine what actions are triggered if the if is true, or if the else is true. Be sure to close your brackets.
If / Else Statements - Example 1

If / Else Statements - Example 2



History of Java


What is object oriented programming?