lab manual for beginner java developer, Lecture notes of Computer Science

this document's are very important for java developer to start application from scratched

Typology: Lecture notes

2020/2021

Uploaded on 02/19/2023

muluken-kindachew
muluken-kindachew 🇪🇹

6 documents

1 / 84

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Bahir Dar University
Bahir Dar Institute of Technology
Faculty of Computing
Laboratory Manual
for
Object-Oriented Programming (Java)
Prepared By: Desta Berihu (MSc.)
Reviewed By: Workneh Chekol (MSc.)
Sept. 2009 E.C
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54

Partial preview of the text

Download lab manual for beginner java developer and more Lecture notes Computer Science in PDF only on Docsity!

Bahir Dar University

Bahir Dar Institute of Technology

Faculty of Computing

Laboratory Manual

for

Object-Oriented Programming (Java)

Prepared By: Desta Berihu (MSc.)

Reviewed By: Workneh Chekol (MSc.)

Sept. 2009 E.C

2009 E.C

2009 E.C

Prerequisite for all the laboratory lessons:

 Knowing the naming conventions in java

 Knowing the syntax and semantics of the basic programming

elements in Java. These are:

o Identifier, variable, keyword, comment, data type, operator,

constant, literal, expression and statement.

 Knowing the syntax and semantics of the basic programming

contracts in Java. These are:

 Conditional statements [ if, switch],

 Jumping keywords [break , continue , label]

 Loops [for , while , do while and for each]

 Understanding the syntax and semantics of arrays and methods

(functions) in java.

 Understanding Strings in java.

2009 E.C

Background

A fundamental understanding of computing discipline enables students to be not just educated users of technology, but the innovators capable of designing new computers and programs to improve the quality of life for everyone. It is not an exaggeration to say that our lives depend upon computer systems and the people who maintain them to keep us safe on the land and in air.

Without a deep-rooted understanding of computing, we are nobody but users who let their life to be dependent on others and our country will continue containing a society in slavery to those who can develop the new technology. Without understanding of computing principles (particularly, programming principles), computer and software projects go wrong and we are likely to misuse or use inappropriate technology with dangers for the life and liberty of our society.

We are living in a digitized, computerized and highly programmable world. And to make sense of it, we need to give kernel focus to teaching computing. An engineer using a computer to design a bridge must understand the limitations of the numerical methods used, how the maximum capacity estimates were computed and how reliable they are. An educated citizen using governmental e-procurement, e- banking systems (the ATM and the like), etc, should have a basic understanding of the underlying algorithms of such conveniences, as well as the security and privacy issues that arise when information is transmitted and stored digitally. These all are computing issues. Much more than this, computing is becoming one of the leading disciplines helping us understand how the human mind works, one of the great intellectual questions of our era.

To go in- line with this all and to fill the computing demand of our country, our school, the School of Computing (SC), is teaching students the basic computing principles such as logical reasoning, algorithmic thinking, mathematical problem solving and all inherent concepts and skills that are valuable for solving the critical communal problems of our country. They are gaining awareness of the resources required to implement and deploy a solution and how to deal with socioeconomic problems that our society is facing. It is believed that these skills are applicable in many developmental slots of the country, in solving or else minimizing the dangers and sufferings that our society is living with.

Towards achieving the main aim of the school entirely and o f the computing discipline particularly in making students competent system designers and programmers, I have prepared this laboratory manual for the course Object-Oriented Programming - Java - so as to provide a very detailed and

2009 E.C Example:

Fig-1: Content of a computer program

ከሊይ የምናየው ቀመር (program) 15 ትእዛዛት (instructions) ይዟሌ። ኮምፒዩተሩ እነዚህ ትእዛዛትን በቅዯም ተከተሌ በመተግበር አንድ የተሟሊ ተሌእኮ ይፈፅማሌ። በመሆኑም የእንዱህ ዓይነት ትእዛዛት መድበሌ (ስብስብ) የኮምፒዩተር ቀመር ይባሊሌ።

Array / ዝርዝር/፡- Array is a list of data values of the same type, any element of which can be referenced by an expression consisting of the array name followed by an indexing expression. Arrays are part of the fundamentals of data structures, which, in turn, are a major fundamental of computer programming.

የአንድ ዓይነት ምድብ ያሊቸው ነገሮች ዝርዝር ማሇት ነው። የተመሳሳይ ነገሮች ድርድር ማሇት ነው። በዝርዝሩ ሊይ ያለ ነገሮች ቅዯም ተከተሌ የሊቸውም። ሇምሳላ፡ ሀ = {55፣32፣71፣42፣86} ። ይህ ስሙ ሀ የሚባሌ ድርድር/ዝርዝር/ ነው። አራት አባሊትም ይዞዋሌ። እነዚህም ሀ[0]፣ ሀ[1]፣ ሀ[2]፣ ሀ[3] በማሇት ሌንጠራቸውም ሌንጠቀማቸውም እንችሊሇን። የዝርዝሩ አባሊት ጠቋሚ /index/ የሚጀምረው ከ ዜሮ ነው።

Example : int[] arr; // this is an array declaration. In this example arr is a reference variable pointing to the memory where the list exists. The int[] is the data type telling that the type of the content of the array is only integer.

Class/ መዯብ/፡- Class is a named template or descriptor for a set of objects that share the same attributes (fields), behaviors (methods) and relationships. It models /abstracts/ a real world entity.

2009 E.C አንድ ዓይነት መገሇጫዎች (attributes) ፣ ተግባራት (behaviours) እና ግንኝነቶች (relationshiops) ሇሚጋሩ ኣካሊት(objects) የሚሰጥ የወሌ መጠርያ ስም ነው። ሇምሳላ፡- ኳስ የሚሌ መዯብ የእግር ኳስ፣ የመረብ ኳስ፣ የእጅ ኳስ፣ የሕፃናት መጫወቻ ኳስ እና የመሳሰለት ሁለ ያጠቃሇሇ የጋራ መጠርያ ስም ነው።

class Student { …… }

This is the declaration of the class. Note that a class should be declared using the keyword „class‟.

class Student { int age; public int getAge(){ return age; }

Here: class – is a keyword used to declare a class in java. Student – is the user-defined name of the class int – is data type of the field. age – is user-defined name of the field. public - is an access modifier getAge() – is the name of the method {…} - used to enclose body of a given block. return - is a keyboard in java used to return the value returned by a certain functions/methods and the flow control.

Character /ቀሇም/:- is a general name for all letters, digits, punctuation marks and other symbols. ቀሇም ማሇት በቀመሩ ውስጥ የምንፅፈው ማንኛውም ፊዯሌ ወይም ምሌክት ነው። ሇምሳላ፡- &, @, %, 1, 4, 2, a, v, n, ?, , k, p, ...

Comment / አስተያየት / :- is a remark (human-readable-only explanation) in the source code which does not affect the behavior of the program. It is ignored by the compiler. የኮምፒዩተር ቀመር/program/ በመንፅፍበት ጊዜ ሇእያንዲንደ መምሪያ/Instruction/ ሇማብራራት የሚጨምሩት ማብራርያ ነው። በመሆኑም በአርታዔ ቀመር /compiler/ የሚታይ እና የሚተረጏም አይዯሇም።

// this is a single comment /* */ is the multiline comment in Java. Variable / የዲታ መያዣ / :- is a named memory location in which a program can store intermediate results and from which it can read them for use.

ይህ የዲታ መያዣ ነው። ሇጊዜው የሚይዘው ዲታ ስሇማይታወቅ ተተኪ /variable/ ተባሇ። ማንኛውም የኮምፒዩተር ቀመር የሚሰራው ዲታ ሊይ ነው። ቀመሩ ዲታውን ጠቃሚ መረጃ (information) እስኪሆን ድረስ የማቀነባበር፣ የማዯባሇቅ፣ የማንዯባሇሌ፣ የመቀመር ወዘተ ስራ ይስራሌ። ታዱያ ዲታው የሚያገኘው ከኪ ቦርድ፣ ከኔትዎርክ፣ ከራሱ ሃርድ ዱሰክ መዛግብት ወዘተ ሉሆን ይችሊሌ። ይህ ዲታ ቀመሩ

2009 E.C value back to the caller. In short, functions simulate human beings, factories and any machine that works following the basic operation-patterns namely: Input  process  output.

ሇስላት የሚጠቅሙት ግብኣቶች/parameters/ በመውሰድ አንድ ስላት ሰርቶ የሚመሌስ፣ ከቀመሩ ከማንኛውም ክፍሌ ሉጠራ የሚችሌ የቀመሩ ኣካሌ ‘አስላ’ ይባሊሌ። አስላ ሁሇት ዋና ዋና ክፍልች አለት። እነርሱም የአስላ ስርዓት /method definition/ እና የአስላ አሰሪ (ወይም ጠሪ) /method call/ ናቸው። Example :- Method definition /የአስላ ስርዓት/ መግሇጫ 11 int add (int firstNum, int secondNum)

{

ይህ የአስላ ስርዓት ነው፡፡ ሁሇት ክፍልች አለት፡ አንዯኛው (11) ተብል የተመሇከተው የአስላ መሇዮ(method signature) እየተባሇ የሚጠራው ሲሆን ሁሇተኛው ዯግሞ (2 2 ) ተብል የተመሇከተው (የአስላ ሰውነት) method body የሚባሇውን ነው። የአስላ መሇዮ ሶስት ነገሮችን የያዘ ነው፡- የመሌስ ዓይነት (return type)፣ የአስላ ስም (method name) እና የአስላ ግብአት (method parameter) ናቸው። የአስላ ሰውነት አስላው ግብአቶችን ተቀብል የሚያሰሊበት የስላት ትእዛዛት የያዘ (working instructions) በማዕቀፍ ቅንፎች /{….}/ የተከበበው ዋና ሰውነቱ ነው። የመሌስ ዓይነት (return type)፡- አስላው ስላቱን ሰርቶ የሚመሌሰው መሌስ፣ የመሌሱ ዓይነት የሚገሌፅ ነው። የአስላው ስም (method name)፡- የአስላው ስያሜ ነው። ማንኛውም መሇዮ (identifier) መሆን ይችሊሌ። የስላት ግብአት ( method parameter ) ፡- አስላው ሇሚያካሂዯው ስላት የሚወስዯው ግብአት ማሇት ነው። ነገር ግን አስላው ግብአት ሳይወስድም መስራት ይችሊሌ።

22

int summation = 0; summation = firstNum + secondNum; return summation; }

Identifier / መሇዮ / :- Identifier is a formal name used in source code to refer to a variable, method, class, package, etc.

የኮምፒዩተር ቀመር ፀሓፊው በቀመሩ ውስጥ የሚፅፋቸው ማንኛውም የዲታ መያዣ፣ የአይቀየሬ፣ የዲታ ዓይነት፣ የአስላ ወዘተ ስሞች ‘መሇዮ’ እንሊቸዋሇን። መሇዮዎች በምንፅፍበት ጊዜ እንዯ የኮምፒዩተሩ ቋንቋ የምንከተሊቸው ሕጎች አለ። በ ጃቫ ሁሇት ሕጎች አለን። ማንኛውም መሇዮ በፊዯሌ ወይም በግርጌ ጭረት(underscore) ብቻ መጀመር አሇበት። ከመጀመርያው ቀጥል የሚፃፉት ሦስት ነገሮች [ማሇትም ፊዯሊት፣ቁጥሮች ወይም የግርጌ ጭረት] ብቻ ናቸው። ሇምሳላ፡

2009 E.C isSecondYearStudent  _isSecondYearStudent  isSecond YearStudent 

2ndIsSecondYearStudent  is2nd_YearStudent  is2ndYearStudent 

Lite ral / ጥሬ ዋጋ / :- is a value written into the source code of a computer program.

በአንድ የዲታ መያዣ ውስጥ የዲታ ዓይነቱ መሰረት በማድረግ የምናስቀምጠው ዋጋ ‘ጥሬ ዋጋ’ እንሇዋሇን። ሇምሳላ፡- int a = 5; …………………………………….(1) String str = ‚I am student of IoT-SCEE‛; ………...(2) በቁጥር (1) የተመሇከተው ዋጋ /5/ የኢንተጀር ጥሬ ዋጋ ይባሊሌ። በቁጥር (2) የተመሇከተው /I am student of IoT-SCEE/ የሚሌ ዋጋ የስትሪንግ ጥሬ ዋጋ ይባሊሌ።

Loop / ጥምጥም / :- is a sequence of instructions that should be executed repeatedly until or while a particular condition is satisfied.

ቀመሩ አንድን ነገር መሊሌሶ እንዱሰራሌን ስንፈሌግ የምንጠቀምበት የቀመሩ አንደ ሰዋስው (programming construct) ነው። ይህም አንድ ሂዯት ወይም ተግባር ብዙ ጊዜ ተዯጋግሞ እንዱሰሊ የሚያስችሌ ነው። ሇዚህ ዓሊማ የምንጠቀምባቸው የቀመር የጥምጥም ዓይነቶች ሦስት ናቸው። እነዚህም፡- for loop, while loop, do---while loop ይባሊለ።

Object / አካሌ / :- Object, in object-oriented programming, is an instance of a class. An object comprises data and methods that act on the data, and is treated as a discrete entity. Entity is anything in the physical world that can be described by its attributes and behaviors (activities).

በሰው ዘንድ በገሃደ ዓሇም ህሌውናውን የታወቀ፣ በሰው ቋንቋ ሉገሇፅ የሚችሌ ቁስ አካሌ ወይም ተጨባጭ ነገር ነው።

2009 E.C

Lesson-1. Setting up your Java development environment in your computer
and writing, compiling and running simple java programs

Lesson-1 Activity

Task-1 :- Install and configure the Java platform in your computer.

In order to write and run Java program, you need to install and configure the Java platform (i.e., the Java Development Kit- JDK) on your computer system.

To configure the JDK, you have to set the JAVA_HOME environment variable to the root location of the JDK installation directory in your computer. For Example, assume that your Java is installed in C:\Program Files\Java\jdk1.6.0_06 ; where jdk1.6.0_06 is the version of the JDK you installed. Then, use either of the following ways to set the JAVA_HOME environment variable:

i. By writing the following statement in the command prompt: C:> set JAVA_HOME= C:\Program Files\Java\jdk1.6.0_

Objective : at the end of this laboratory session, you will be able to:Configure a java environment (platform) so that you can write, compile and run java programs in any computer.Write, compile and run very simple java programs and understand each of their parts clearly.

Lesson Prerequisite: Students! Before you sit for this lesson in the laboratory, make sure that you have studied:  The difference between Java JDK and Java JRE  The various tools packed in the JDK and their role in running a given java application.  The phases that any java application passes thorough for execution.  The various tools bundled in JDK that are used for compiling, debugging and running java app  The syntax and semantics of java class anatomy discussed in class!!  The syntax of declaring and defining java class!  Commonly used classes such as String , System etc in the package java.lang

2009 E.C ii. Right click on My ComputerPropertiesAdvanced System SettingsEnvironment Variables ; in the system variables part click New button and then write JAVA_HOME in the variable name field and C:\Program Files\Java\jdk1.6.0_06 in the variable value. Click OK on the path edit box and OK on the Environment Variables box. The new setting will go into effect next time you run Command Prompt.

Next set the PATH environment variable to include the JDK bin directory. The PATH variable tells the operating system where to find the java interpreter and the javac compiler. Use either of the above two ways. If you choose to use the first way, the statement you should write in the command prompt is: C:> set PATH=%PATH%;%JAVA_HOME%\bin

To make sure that you have configured the JDK successfully, use the java -version command to see the JDK version and the path it is installed in.

Task-2: Open either of the java editors discussed in class [such as notepad, JCreator, etc] and then write the following simple program.

Here, please be sure to name the text file as OopStudBiT.java (Note: please name your class in the program with your own name by making the first letter of your name capital letter). Java program is case sensitive, so if you type the code in yourself, pay particular attention to the capitalization (Eg. Write OopStudBiT instead of oopStudBiT to name your java class).

//A very simple java program which prints first and second name of the programmer. public class OopStudBiT{ // line- 1 public static void main(String[] ethiopia) { // line- 2 String firstName = "Yohannes"; // line- 3 String secondName = "Zekarias"; // line- 4 System.out.println("Your Full Name is:" + firstName+""+secondName); // line- 5 } }

Now, compile and run the program and you will see the following output.

2009 E.CIn line-5:  System :- is name of abuilt- in class in Java library. It contains the standard input/out streams. It is found in the package java.lang  .(dot) :- is an access operator. It is used to access members (fields and methods) of a class or an object.  out :- is a static reference variable to the standard output stream object of type PrintStream.  print() :- is an instance method defined inside of the PrintStream class. It is an overloaded method with different parameters. It has also another version of its type called println(). The suffix ln means „line‟. By this, println is meant to print the output in new line.  "Your Full Name is:":- is a literal string. Everything under double quotes is a literal string.  +(plus sign): - is a concatenation operator. When it comes between non-string numeric values, it is a mathematical addition operator. However, if either of its operands is String, then it becomes a concatenation operator.

Task-3:- Write the following java program on notepad and run it from the command prompt. Create a temporary folder C:\BiTScee. Using Notepad or another text editor, create a small Java file OopStudBiT.java with the following text: public class OopStudBiT { public static void main(String[] args) { System.out.println("OOP, BiT-SCEE!"); } }

Save your file as OopStudBiT.java in C:\BiTScee. To make sure your file name is OopStudBiT.java , (not OopStudBiT.java.txt ), first choose "Save as file type:" All files , then type in the file name OopStudBiT.java. Run Command Prompt (found under All Programs/Accessories in the Start menu). Type C:> cd
BiTScee
. This makes C:\ BiTScee the current directory. And then, type C:\ BiTScee> dir. This displays the directory contents. You should see OopStudBiT.java among the files.

2009 E.C  Now, it is time to compile your program. To compile your program, you use javac.exe , which is the java compiler, as follows: C:\ BiTScee> javac OopStudBiT.java  Now, you should see nothing but the next system prompt: C:\ BiTScee > The compiler javac creates the OopStudBiT.class file. You should see OopStudBiT.java and OopStudBiT.class among the files.

 To run your program, you use java , which is the java interpreter, as follows: C:\ BiTScee> java OopStudBiT. This runs the Java interpreter.

Finally, you should see the program output as:

If the system cannot find javac , check the set path command. If javac runs but you get errors, check your Java program. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java OopStudBiT command.  አስተውሌ! አስተውሌ! አስተውሌ! Java is case-sensitive!

Lesson-1 Exercises

  1. Write a program that accepts two numbers from the keyboard. The main() method should first accept the two numbers and then passes these values to methods named add(), sub(), mul() and div(), to calculate the sum, difference, product and quotient of the numbers, respectively. Finally, the main() method should accept the returned result and display it on the consol. [Hint: your java program will have five methods of which one is the mandatory main() method. ]

  2. Write a program that calculates the area of a rectangle. The main() method should first accept the width and height of the rectangle and then passes these values to method named rectangleArea(). Method rectangleArea() calculates the area of the rectangle using the formula width*height and return the result. main() method should accept the returned result and display it.

  3. What do you think is the output of the following code fragmentation? final int N = 100; for (int i=0;i<N/2;++i){ for(int j=N;j>=2;--j){ Sytem.out.println(“I am learning Java!!”); } Sytem.out.println(“I found it simple, portable and secured!!”);

OOP, BiT-SCEE!

2009 E.C

How to accept input from the keyword?

The following are some of the ways that Java offers us to accept input from a standard input device, the keyboard.

a) Using the Scanner class One of the strengths of Java is the huge libraries of code available to you. This is code that has been written to do specific jobs. All you need to do is to reference which library you want to use, and then call a method into action. One really useful class that handles input from a user is called the Scanner class. The Scanner class can be found in the java.util library. To use the Scanner class, you need to reference it in your code. This is done with the keyword import.

import java.util.Scanner;

The import statement needs to go just above the Class statement: import java.util.Scanner; public class StringVariables { … }

This tells java that you want to use a particular class in a particular library - the Scanne r class, which is located in the java.util library [i.e., the package where the class is located].

The next thing you need to do is to create an object from the Scanner class. (A class is just a bunch of code. It doesn't do anything until you create a new object from it.)

To create a new Scanner object the code is this: Scanner userInput = new Scanner( System.in );

So instead of setting up an int variable or a String variable, we're setting up a Scanne r variable. We've called ours userInput. After an equals sign, we have the keyword new. This is used to create new objects from a class. The object we're creating is from the Scanner class. In between round brackets we have to tell java that this will be System Input (System.in).

2009 E.C To get the user input, you can call into action one of the many methods available to your new Scanner object. One of these methods is called next(). This gets the next string of text that a user types on the keyboard:

String firstName; firstName = userInput.next( ); So after our userInput object we type a dot to access all instance fields and methods of the Scanner class. String firstName; System.out.print("Enter your first name: "); firstName = userInput.next(); Notice that we've used print rather than println like last time. The difference between the two is that println will move the cursor to a new line after the output, but print stays on the same line.

We'll add a prompt for a second name, as well: String secondName; System.out.print("Enter your family name: "); secondName = userInput.next( );

This is the same code, except that java will now store whatever the user types into our secondName variable instead of our firstName variable.

To print out the input, we can add the following: String full_name; full_name = firstName + " " + secondName; System.out.println("You are " + full_name);

We've set up another String variable, full_name. We're storing whatever is in the two variables firstName and secondName. In between the two, we've added a space. The final line prints it all out in the Output window.

So, all together, your entire code looks the following code:

የቴክኖልጂ ትምህርት በኢትዮጲያኛ ብንማረውና ብንረዲው፣ በኢትዮጲያኛ ብንተነትነው፣ ሇኢትዮጵያ እናውሇዋሇን!