introduction a java program to print “fibonacci number” series given integer n. the fibonacci...

50
CSC 308 2.0 System Development with Java Budditha Hettige Department of Statistics and Computer Science 1 Introduction Budditha Hettige

Upload: vuongtuong

Post on 05-May-2018

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

CSC 308 2.0

System Development with Java

Budditha Hettige

Department of Statistics and Computer Science

1

Introduction

Budditha Hettige

Page 2: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

JAVA

• Is a programming language created by James Gosling from

Sun Microsystems in 1991

• Is a general-purpose, class-based, object-oriented

Programming language

• is intended to let application developers

“write once, run anywhere.”

• Current version Java SE 7 (July 28, 2011)

2 Budditha Hettige

Page 3: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Java cont…

• Java programming language consists

– Java compiler:

Java compiler translates Java coding into

byte-code

– Java virtual machine(JVM)

Java virtual machine interprets this byte-code and runs the program

– Java class libraries

Java Class Library is a set of dynamically loadable libraries that Java applications can call at run time

3 Budditha Hettige

Page 4: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Java Virtual Machine (JVM)

Architecture

JAVA Source File (.java)

JAVA Bite code (.class)

Java Compiler Java Compiler

Java Virtual Machine

Java Virtual Machine

Memory Manager Memory Manager

Byte code Verifier Byte code Verifier

Interpreter Interpreter

Java API Java API

4 Budditha Hettige

Page 5: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Characteristics

Java has the following properties

• Platform independent

• Object-orientated programming language

• Strongly-typed programming language

• Interpreted and compiled language

• Automatic memory management

5 Budditha Hettige

Page 6: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Simple Java Program

• A class with a main Method

public class FirstProgram { public static void main(String[] args) { System.out.println("Hello World"); } }

6 Budditha Hettige

Page 7: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Requirements for Class name

• Class name must begin with letter of the

alphabet

• Contains only letters, digits, underscores or

dollar sign

• Cannot be a language reserved keywords

(public, class etc)

• Name cannot be following values (true, false

or null)

7 Budditha Hettige

Page 8: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Java Keywords

8 Budditha Hettige

Page 9: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Java Class name standard

• Begin with uppercase letter

• No spaces

• Emphasizes new word with an initial uppercase letter

Example

EmployeRecords

Student

9 Budditha Hettige

Page 10: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Your first Java program

• Open a text editor (text pad, Notepad etc.)

• Type the following sample

• Save program as “FirstProgram.java”

public class FirstProgram { public static void main(String[] args) { System.out.println("Hello World"); } }

10 Budditha Hettige

Page 11: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Your first java program

11 Budditha Hettige

Page 12: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Compile and RUN

• Using command prompt go to the place where in your

java file

• To Compile: type javac <space><fileName>

Javac FirstProgram.java

• To Run: type java<space><fileName>

Java FirstProgram

12 Budditha Hettige

Page 13: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Java Programming with IDE

• IDE : integrated design environment

• consists of

– source code editor

– compiler and/or an interpreter

– build automation tools

– Debugger

– Construction of a GUI

– Class browser

– Object inspector

– Etc.

13 Budditha Hettige

Page 14: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Programming IDEs

• Eclipse

Eclipse is a multi-language software development environment

http://www.eclipse.org/

• Netbeans

http://netbeans.org/

• Microsoft Visual Studio

http://www.microsoft.com/visualstudio/en-us

14 Budditha Hettige

Page 15: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Java Programming

15 Budditha Hettige

Page 16: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Comments

• Comments are nonexecuting statements

• Line comments // line comments

• Block comments

/*

block comments

*/

• Javadoc comments

/**

Javadoc comments

*/

16 Budditha Hettige

Page 17: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Print Output on Command window

• Function

– System.out.println(“Some Text”);

– System.out.print(“Some Text”);

• Example

public class FirstProgram { public static void main(String[] args) {

System.out.println("Hello World"); } }

17 Budditha Hettige

Page 18: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Escape Sequences

• A character preceded by a backslash (\) is an escape sequence

• has special meaning to the compiler

Escape Sequence Description

\t Insert a tab in the text at this point.

\b Insert a backspace in the text at this point.

\n Insert a newline in the text at this point.

\r Insert a carriage return in the text at this point.

\f Insert a formfeed in the text at this point.

\' Insert a single quote character in the text at this point.

\" Insert a double quote character in the text at this point.

\\ Insert a backslash character in the text at this point.

18 Budditha Hettige

Page 19: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

ASCII art with JAVA

• ASCII art is a graphic design technique that uses computers

for presentation and consists of pictures pieced together from

the 95 printable (from a total of 128) characters defined by the

ASCII Standard from

19 Budditha Hettige

Page 20: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

ASCII art with JAVA cont..

System.out.println(" ");

System.out.println(" @ @ @ ");

System.out.println(" @ @ @ @ ");

System.out.println(" @ @ @ ");

System.out.println(" \\|/ ");

System.out.println(" | | | ");

System.out.println(" | | | ");

System.out.println(" | | | ");

System.out.println(" | | | ");

System.out.println(" ( ) ");

System.out.println(" ^^^^^ ");

20 Budditha Hettige

Page 21: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Java Data types

• The eight primitive data types supported by the Java

programming language

Keyword Description Size/Format

byte Byte-length integer 8-bit two's complement

short Short integer 16-bit two's complement

int Integer 32-bit two's complement

long Long integer 64-bit two's complement

float Single-precision floating point 32-bit IEEE

double Double-precision floating point 64-bit IEEE

char A single character 16-bit Unicode character

boolean A boolean value (true or false) true or false

21 Budditha Hettige

Page 22: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

limits of integer in java

type Size Range

name bytes bits minimum maximum

byte 1 8 -128 +127

short 2 16 -32,768 +32,767

int 4 32 -2,147,483,648 +2,147,483,647

long 8 64 -9,223,372,036,854,775,808 +9,223,372,036,854,775,807

22 Budditha Hettige

Page 23: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Variables

• Declaration

DataType <space> VaribaleName

• Example: int count;

int studentAge = 0;

float GPA = 3.56;

System.out.println(“Student GPA : ” + GPA);

23 Budditha Hettige

Page 24: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Input from Keyboard

• Use Scanner

– Import java.util.Scanner;

import java.util.Scanner;

– Create a Scanner object to Scan input from command window

Scanner input = new Scanner(System.in);

– Read value from keyboard

Use methods (nextInt()- interger, nextLine()- String, nextFloat()- float, nextDouble()- double etc.)

value = input.nextInt();

24 Budditha Hettige

Page 25: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Example 1:

• Create a java program to read 2 integer values and

print the average.

25 Budditha Hettige

Page 26: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Example 1: Solution import java.util.Scanner; public class FundamantalApp { public static void main(String[] args) { Scanner input = new Scanner(System.in); int value1, value2; float avg; System.out.println("Enter value1 :"); value1 = input.nextInt(); System.out.println("Enter value1 :"); value2 = input.nextInt(); avg = (value1 + value2)/2; System.out.println("Average is "+ avg); } }

26 Budditha Hettige

Page 27: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Output Formatting

• Use printf function

System.out.printf(“Values %d”, value1);

Can insert with and justification for the format

System.out.printf("%5d", 0);

5 field width

System.out.printf("%-5d", 0); - Left justify,

System.out.printf("'%.2f'", 10.3456);

Two positions after the decimal

Conversion type Conversion type

27 Budditha Hettige

Page 28: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Format specifies

%c character

%d decimal (integer) number (base 10)

%e exponential floating-point number

%f floating-point number

%i integer (base 10)

%o octal number (base 8)

%s a string of characters

%u unsigned decimal (integer) number

%x number in hexadecimal (base 16)

%% print a percent sign

\% print a percent sign

28 Budditha Hettige

Page 29: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Method Declaration

• Method declaration contains the following

– Optional access modifiers (Public, private, protected, static)

– Return type (void, int, char …)

– Name

– Open parenthesis

– Optional list of arguments

– A close parenthesis

• Example

– public static void printName(){}

– public int calulateAverage(int x, int y){}

29 Budditha Hettige

Page 30: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Method Calling

public static void main(String[] args) { float avg;

printName(); Avg = calulateAverage(10,34);

}

30 Budditha Hettige

Page 31: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Classes

• Create a class with method (No main method)

class student

{

public void printName()

{

System.out.println("I am a Student class");

}

}

• Create a public all with main method

31 Budditha Hettige

Page 32: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Classes contd..

• Create a class object

student st = new student();

• Call the method through the object

st.printName();

32 Budditha Hettige

Page 33: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Sample code class student

{

public void printName()

{

System.out.println("I am a Student class");

}

}

public class FundamantalApp

{

public static void main(String[] args)

{

student st = new student();

st.printName();

}

}

33 Budditha Hettige

Page 34: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

if-then-else Statement

• Syntax if (Condition) { statement(s) } else { statement(s) }

• Example if (isMoving)

{

currentSpeed--;

}

else

{

System.err.println("The bicycle has already stopped!");

}

34 Budditha Hettige

Page 35: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Loops

35 Budditha Hettige

Page 36: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

For Loop

• Syntax for (initialization; termination; increment)

{

statement(s)

}

• Example

for(int i=1; i<11; i++)

{

System.out.println("Count is: " + i);

}

Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6 Count is: 7 Count is: 8 Count is: 9 Count is: 10

36 Budditha Hettige

Page 37: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Example

1. Write a java program to print the following figure

a) * b) 1

** 12

*** 123

**** 1234

***** 12345

2. Create a java program to print “Fibonacci number” series given integer n. The Fibonacci

number has the following integer sequence: (use an array to store the number series)

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144…

Mathematically

Fn = Fn-1 + Fn-2 Where (F0 = 0, F1 = 1)

37 Budditha Hettige

Page 38: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Answers

a) for(int i=1; i<= 5 ;i++)

{

for(int j=0; j < i; j++)

{

System.out.print("*");

}

System.out.println("");

}

b) int limit = 20; long[] series = new long[limit]; series[0] = 0; series[1] = 1; //create the Fibonacci series and store it in an array for(int i=2; i < limit; i++) { series[i] = series[i-1] + series[i-2]; } //print the Fibonacci series numbers System.out.println("Fibonacci Series upto " + limit); for(int i=0; i< limit; i++) { System.out.print(series[i] + " "); }

38 Budditha Hettige

Page 39: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

While Loop

• Syntax

while (expression)

{

statement(s)

}

• Example

int count = 1;

while (count < 11)

{

System.out.println("Count is: " + count);

count++;

}

Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6 Count is: 7 Count is: 8 Count is: 9 Count is: 10

39 Budditha Hettige

Page 40: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Example

Create a java program to identify the given number is

Palindrome number or not.

– Read number as a interger

– Find the bnumber is Palindrome or not

– Print the results

40 Budditha Hettige

Page 41: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Answers

int number = orgnumber;

int reversedNumber = 0;

int temp=0;

//reverse the number

while(number > 0)

{

temp = number % 10;

number = number / 10;

reversedNumber = reversedNumber * 10 + temp;

}

if(orgnumber == reversedNumber)

System.out.println(orgnumber + " is a palindrome number");

else

System.out.println(orgnumber + " is not a palindrome number");

}

41 Budditha Hettige

Page 42: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Do-while Loop

• Syntax

do

{

statement(s)

} while (expression);

Example int count = 1;

do {

System.out.println("Count is: " + count);

count++;

}

while (count <= 11);

Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6 Count is: 7 Count is: 8 Count is: 9 Count is: 10

42 Budditha Hettige

Page 43: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Loops comparation

For while do-while

43 Budditha Hettige

Page 44: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Exercise do-while loop

• Using do-while loop, create a java program to display the

selected option in the following menu;

[1] insert data

[2] read data

[3] print data

[4] Exit the program

44 Budditha Hettige

Page 45: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Switch Statement

• The switch statement can have a number of possible execution

paths

45 Budditha Hettige

Page 46: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Exercise

• Create a java method to calculates the number

of days in a particular month: – 1, 3, 5, 7, 8, 10, 12 – 31 days

– If current year is divide by 4 and divide by 100 or divide by

400 is leaf year (29 days for February)

46 Budditha Hettige

Page 47: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Answer

int month = 2;

int year = 2000;

int numDays = 0;

switch (month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12: numDays = 31;

break;

case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) ||(year % 400 == 0) ) numDays = 29; else numDays = 28; break; default: System.out.println("Invalid month."); break; } System.out.println("Number of Days = " + numDays);

47 Budditha Hettige

Page 48: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Array

• Array declaration

Type [] Name = new type[Count];

• Example :

int[] anArray = new int[10];

anArray[0] = 40;

anArray[0] = 40;

int[] anArray = {100, 200, 300, 400, 500};

48 Budditha Hettige

Page 49: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

2D Array

• Declaration

Type [] [] name; name = new type[Count][];

name[0] = new type[Num];

name[1] = new type[Num];

. . . name[Count-1] = new type[Num];

• Example:

int[][] data; data = new int[10][]; data[0] = new int[5]; data[1] = new int[20];

49 Budditha Hettige

Page 50: Introduction a java program to print “Fibonacci number” series given integer n. The Fibonacci number has the following integer sequence: (use an array to store the number series)

Example

Create a menu driven java program to do the following

– Create a class namely student, to store the following student information

student(indexNo, name, address, email, gender)

– Use an array to implement the program (Assume that, maximum number of student in the class 10)

– Your menu should have the following options

Department information system

[1] Enter new student

[2] Change student information

[3] Delete an existing student

[4] Print student information

[5] Exit the program

– Your program should have the following features 1. when you are going enter a new student, If it is already 10 students are in the

array print the message “no any space available for enter new student”,

2. After a student deleted, system give a way to insert a new student

50 Budditha Hettige