Archive

Archive for the ‘WJ1102A – Beginning to Program with the Java Programming Language’ Category

Exercise 2 – String class in Java

November 12, 2009 Leave a comment

 

Exercise – 2 for module 2 of Beginning to Program with the Java Programming Language:

 

This post has the exercise – 2 for module 2 of Beginning to Program with the Java Programming Language. Let’s practice what we have learnt. Before practicing the exercise, follow the instructions below which will give you a step-by-step instruction:

 

 

 

 

You can download the source code (WJ-1102A-module2-exercise2.zip) for this exercise from the Box widget in the sidebar. Follow the video tutorial below if you ever get stuck while executing the programs. Read more…

Advertisements

Exercise 1 – Creating Test class in Java

November 11, 2009 Leave a comment

 

Exercise – 1 for module 2 of Beginning to Program with the Java Programming Language:

  

This post has the exercise – 1 for module 2 of  Beginning to Program with the Java Programming Language . Let’s practice what we have learnt. Before practicing the exercise, follow the instructions below which will give you a step-by-step instruction:

 

 

 

 

You can download the source code (WJ-1102A-module2-exercise1.zip) for this exercise from the Box widget in the sidebar. Follow the video tutorial below if you ever get stuck while executing the programs. Read more…

Creating and using objects in Java

November 11, 2009 Leave a comment

 

Module 2: Creating and using objects.

 

Objectives:

  • Declaring object references, instantiating objects, and intializing object reference variables.
  • Compare how object reference variables are stored in relation to primitive variables.
  • Use a class (the String class) included in the Java SDK.
  • Use the J2SE class library specification to learn about other classes in this API.

 

In the previous module we discussed about primitive data types and how to use them with variables. In this module we will be focussing about objects and object reference variables. We will learn how object reference variables are stored in memory with reference to local variables. We will also take a look into the String class, as well as the class library specification to know about the other classes that we have access to.

 

1. Declaring object references, instantiating objects, and intializing object reference variables:

In the previous module we were dealing with primitive data types. And when we declared a variable of  a primitive datatype, we stored the primitive in the variable itself. But Java is object oriented language, so we really want to be dealing with objects. While dealing with objects you will also need a variable associated with it. But it works different with the objects. You dont store an object inside of a variable like you do with the primitives. What you do is, you store a reference to the object inside the variable.

Now a reference is like an address to a house. It allows you to find that object. So using that reference we can call up that object and ask it to do something. We can either access its attributes, or we can access its methods. Just like we can send a letter to a house as we have its address, we can access an object using its reference.

 

object reference

 

 

 

Example: Declaring object references:

Here we have the ShirtTest class that we used in the previous module to create a Shirt object and then access it. In the following sections we will take a detailed look at the creation of myShirt object refernce variable and the creation of the Shirt object and how these two are tied together and how the myShirt object reference variable is used to access the Shirt object.  

 

 ShirtTest class

 

 

Declaring object reference variables: Read more…

Exercise 2 – Using operators and type casting in Java

November 10, 2009 Leave a comment

 

Exercise – 2 for module 1 of Beginning to Program with the Java Programming Language:

 

This post has the exercise – 2 for module 1 of  Beginning to Program with the Java Programming Language. Let’s practice what we have learnt. Before practicing the exercise, follow the instructions below which will give you a step-by-step instruction:

 

 

 

 

You can download the source code (WJ-1102A-module1-exercise2.zip) for this exercise from the Box widget in the sidebar. Follow the video tutorial below if you ever get stuck while executing the programs. Read more…

Exercise 1 – Primitive type variables in Java

November 10, 2009 Leave a comment

 

Exercise – 1 for module 1 of Beginning to Program with the Java Programming Language:

 

This post has the exercise – 1 for module 1 of  Beginning to Program with the Java Programming Language . Let’s practice what we have learnt. Before practicing the exercise, follow the instructions below which will give you a step-by-step instruction:

 

 

 

You can download the source code (WJ-1102A-module1-exercise1.zip) for this exercise in the Box widget in the site sidebar. Follow the video tutorial below if you ever get stuck while executing the programs. Read more…

Declaring, initializing and using variables in Java

November 7, 2009 Leave a comment

 

Module 1: Declaring, initializing and using variables.

  

Objectives

  • Identify the uses for variables and define the syntax for variables.
  • 8 different types of java programming language primitive data types.
  • Declare, initialize, and use variables and constants according to java programming language guidelines and coding standards.
  • Modify variable values using operators.
  • Using promotion and type casting.

 

 

1. Identify the uses for variables and define the syntax for variables.

Variable can be declared in java in 2 different places:

  • In body of a method – in this case, the variable will be local to that method & exist only during the execution of that method.
  • Class definition – in this case, the variable will be declared outside of any method body and such variable are called instance / attribute variables.

 

1. attr var

 

In the above example, we hve 5 attribute / instance variables, since they are declared outside of any method body, but inside the class Shirt. These variables come into existence when the object of the class are created and each object of the class will have its own set of these variables. On the right side of the above figure, there are 3 shirt objects. Each shirt object has its own shirt ID, price, color, description, quantity in stock variables.

These variable declarations can exist anywhere within the class body as long as they are not in any method body. Typically they are put first in the class, before any method declaration. But they can actually come after method declaration, between method declaration or anywhere in class definition as long as they are not in a method body.

  

Variable declaration and intialization:

Attribute / instance variable syntax:

[modifiers] type identifier [=value];

  • [modifiers] -> optional – These are special Java keywords that modify the way the variable functions. ex: public, static etc.
  • type -> required – This specifies the type of information or data held by the variable. ex: int, float etc can be either primitive or can be object reference type.
  • identifier -> required – This is the name that you assign to the variable.
  • [=value] -> optional – This is an initial value that u may want to assign to the variable. It should match with the type of the data that we have specified.

The variable declaration / initialization should end with ‘;’ semi-colon.

  

Describing primitive data data types: Read more…