Teach yourself Java module COL106 (Sem I, 2021-22)

Teach yourself Java module

COL106 (Sem I, 2021-22)?

Department of Computer Science and Engineering, IIT Delhi

August 11, 2021

This module is targeted to kick-start students in programming Java by introducing them to the basic syntax and useful/essential features of Java. This

module is by no means complete reference of Java as it is nearly impossible to

squeeze a book¡¯s worth of material into 2-3 lab session modules. This module

is divided in three sessions, the first of which is to be completed by students on

their own:

? Session 0 (self study)- Introduction to Java, compiling and executing a

java program, basic control structures, string, array

? Lab Session 1 (Aug 16-21)- Container classes, exception handling, file

handling

? Lab Session 2 (Aug 23-27)- Object oriented programming concepts, Multithreading

Each session covers a set of Java features with the help of example programs.

Students are advised to compile and execute each of them. A set of programming

exercises have been created in every section.

Following points must be noted:

? These exercises are hosted on the course website in a compressed file titled

¡°JavaModuleExercises¡±. The tar ball contains 33 directories, one directory for each exercise in the manual.

? There are two type of exercises: (a) self-help, and (b) programming.

The self-help exercises are marked yellow in the manual. For each selfhelp exercise, one file is given in the exercise directory: intro.java. The

intro.java file contains the code required to understand the exercise.

The programming exercises are marked blue in the manual. For each programming exercise, three files are given in the exercise directory: checker.java,

program.java, Makefile. You are supposed to modify the test() function in program.java. checker.java will call the test function of program.java,

? First version by Chinmay Narayan with suggestions by Amitabha Bagchi (approx 2013).

Subsequently revised by Prathmesh Kallurkar. Multithreading added by Ismi Abdi. Reorganized by Rahul Yadav (2021)

1

and it should return the expected output. You should not modify the

signature of the test function (a function signature includes the function¡¯s return type, the number of arguments, and the types of arguments).

Type make to compile and execute the exercise. DO NOT MODIFY

checker.java. This file contains the test cases which should pass with

your program. At the time of demo, we will change the test cases.

? Completing an exercise means your program has passed all test cases for

that exercise. This must be shown to your TA.

? Completing a lab session means completing all exercises of that session.

Example for self-help exercise:

uncompress/extract JavaModuleExercises

$ cd JavaLabModule/exercise1

$ javac intro.java

$ java intro

Example for programming exercise:

uncompress/extract JavaModuleExercises

$ cd JavaLabModule/exercise8

..... Open program.java, and un-comment line 12 .....

$ make

If the make command is not working, replace it with following commands:

$ javac program.java

$ javac checker.java

$ java checker

2

Session 0

(To be completed as self-study before the first lab)

In imperative programming languages like C, a program is made of a set of

functions which are invoked by a main function in some order to perform a

task. In some sense, the most basic unit of computation in these languages

is function and data. In Java, or in any other Object oriented programming

language (OOP), the most basic unit of computation is an object or a class.

Class For any variable x in a programming language, the type of x defines

what kind of operations are valid on that variable. Java, like every programming

language, has a set of primitive types to represent integer, character, real

etc. Java also allows user to create new types by means of class declaration.

A class is an encapsulation of data and the methods operating on this data in

a single unit of type. The notion of Class in Java is somewhat similar to Type

in SML programming language and structure in C. A class in Java is written as

below.

Listing 1: An empty java class.

1 class intro

2 {

3

4 }

A class in Java is called a public class if the keyword public is added before the

keyword class in a class definition. For example the code of listing 2 creates a

public class newintro.,

Listing 2: An empty public java class.

1 public class newintro

2 {

3

4 }

Object Object is an instantiation of a class type. For the example class of

listing 1, to declare an object of type intro, we write

intro var;

where var is any string. Above syntax declares that var is a reference/handle

of type intro. It is to be noted that the above syntax only creates a

handle/reference to an intro object. Actual object is not created by

this syntax.. Java provides a keyword new to create a new object of a given

type and assign it to a reference of that type.

intro var = new intro;

Above syntax declares a reference variable var of type intro as well as initialize

it with a newly created intro object. In absence of new intro() part, reference variable is only initialized to null and any operation on such uninitialized

3

reference will generate run-time error. Therefore we must make sure that

every reference is properly initialized before using it.

Fields and Methods A class consists of a set of variables, also called fields,

and a set of functions, also called methods. For an object of a given class, the

fields and methods of that class are accessed using . operator. A field of a class

is of the following form [Qualifier] type variablename where Qualifier is

optional and can take following values,

? public: It means that this field can be accessed from outside the class by

just using objectname.fieldname.

? static: It means that to access this field you do not need to create an

object of this class. Therefore this field is also sometimes called as class

variable/field.

For the example program in listing 3, credits is a public static variable

and hence can be accessed from outside this class as intro.credits. On the

other hand, age is only a pubic variable and hence can only be accessed after

creating an object of intro type as below.

intro var = new intro;

var.age = 10;

Above code first creates an object of intro class and then assign value 10 to the

field age of this object. It is to be noted that every object of a class, contains

different copies of non-static variable (e.g. age in listing 3) and the same copy

of static variable (credits in listing 3). You do not require to create an object

to access the static members of a class.

Listing 3: field qualifiers

1 class intro

2 {

3

public int age ;

4

public static int credits ;

5 }

methods A method/function is defined inside a class as [Qualifier] returntype function-name (arguments). A method in Java also specifies exception

signature which we will discuss in detail in error handling. Two important

qualifiers for methods are same as for fields,

? public: A public method can be invoked from outside the class using the

object of that class.

? static: A static method can be called without creating an object of that

class.

Following restrictions are applied on invocation of static methods.

? static function can not call non-static functions of that class.

4

? static function can not access non-static variables/fields of that class.

Every class in Java has a public method by default, it is called

Constructor of that class. The name of the constructor function is same as

the name of the class without any return type, but it can take a set of arguments

like any method. This method is called when an object of this class is created

using new operator as shown earlier.

Listing 4: Filling up methods and fields

1 class intro

2 {

3

public int age ;

4

public static int credits ;

5

public intro ( int a )

6

{

7

age = a ;

8

}

9

public void setage ( int newage )

10

{

11

age = newage ;

12

}

13

public static int getcredit ()

14

{

15

return credits ;

16

}

17 }

In the example program of listing 4, static function getcredit returns the value

of static field credits. Function setage takes an integer argument and set the

value of the field age to this value. Return type of setage is void as it does

not return any value, while the return value of getcredit is int. We also have

a constructor in this class which takes an integer and assign it to field age of

that object. Constructors are mainly used to make sure that all fields of an

object are initialized properly at the time of creation. To create an object of

class intro, we use following syntax,

1 intro handler = new intro (4);

Notice that we pass an integer (4 in this case) while creating an object with

new operator. This is because the constructor of class intro takes an integer

argument. Therefore the constructor of a class, when defined, also tells us

what all arguments must be passed while creating an object of that class. If no

constructor is defined explicitly, java itself puts a zero argument constructor

in that class.

Having covered the most essential aspect of a class definition in Java, now

let us look at the process of compiling and executing a Java program.

File naming convention for java programs

? Any java program must be saved in a file with extension ¡°.java¡±.

? A java file can contain at most one public class.

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download