0% found this document useful (0 votes)
6 views31 pages

Java 1

Uploaded by

Siam Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views31 pages

Java 1

Uploaded by

Siam Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

“If you can't fly then run, if you

can't run then walk, if you can't walk


then crawl, but whatever you do you
have to keep moving forward.”
― Martin Luther King Jr.
(January 15, 1929 - April 4, 1968)
SE 211
5/15/2020 1
Course Teacher: S A M Matiur Rahman
SE 211 (Object Oriented Concepts)

S A M Matiur Rahman
Associate Professor & Associate Head
Software Engineering Department
Permanent Campus, Ashulia

SE 211
5/15/2020 2
Course Teacher: S A M Matiur Rahman
SE 211
5/15/2020 3
Course Teacher: S A M Matiur Rahman
Getting Started with
Java

5/15/2020
SE 211 4
Course Teacher: S A M Matiur Rahman
Objectives

Objectives:

Identify the components of a typical Java


program.
Write simple Java program to perform
calculation.
Get input from the console using the Scanner
class.
Use identifiers to name variables, constants,
methods and class.

5/15/2020
SE 211 5
Course Teacher: S A M Matiur Rahman
The First Object-Oriented Program in Java

The program illustrated the fundamental


concept of Object Oriented Programming
(OOP) in Java.
An object-oriented program uses objects.

This program displays a window on the screen.

The size of the window is set to 300 pixels wide


and 200 pixels high. Its title is set to My First
Java Program.

5/15/2020
SE 211 6
Course Teacher: S A M Matiur Rahman
Components of a Java Program
import javax.swing.*;

class LectureI_1 {

public static void main(String[ ] args) {


JFrame myWindow;
Declare a name of
an object
myWindow = new JFrame( );
Create an object

myWindow.setSize(300, 200);

myWindow.setTitle(“My First Java Program”);


myWindow.setVisible(true);

}
} Use an Object

5/15/2020
SE 211 7
Course Teacher: S A M Matiur Rahman
Program Diagram for LectureI_1

LectureI_1 set Size(300, 200)

setTitle(“My First Java Program”) myWindow:JFrame

setVisible(true)

5/15/2020
SE 211 8
Course Teacher: S A M Matiur Rahman
Dependency Relationship

LectureI_1
myWindow:JFrame

Instead of showing all messages, it can be summarized


by showing only dependency relationship. The diagram
shows that LectureI_1 “depends” on the service
provided by myWindow.

5/15/2020
SE 211 9
Course Teacher: S A M Matiur Rahman
Object Declaration

Class Name
This class must be defined Object Name
before declaration can be One object is declared here
stated

JFrame myWindow

More Vehicle car1, car2, car3…….


Student mana, hisham, saira……..
Example Account customer

5/15/2020
SE 211 10
Course Teacher: S A M Matiur Rahman
Object Creation
Object Name Class Name Argument
Name of the object is An instance of this No arguments are
created here. class is created. used here.

myWindow = new Jframe ( );

new operator dynamically allocates


memory for an object

More car1 = new Vehicle( );


mana = new Student( );
Example customer = new Account( );
5/15/2020
SE 211 11
Course Teacher: S A M Matiur Rahman
Name vs. Object

Customer customer;
customer = new Costomer();
customer = new Costomer();

customer

Created with the second


Created with the :Customer :Customer new. Reference to the first
first new
Customer object is lost.

5/15/2020
SE 211 12
Course Teacher: S A M Matiur Rahman
Sending a message
Object Name Method Name Argument
Name of the object to The name of the The argument we are
which we are sending a message we are passing with the
message. sending. message.

myWindow . setVisible ( true );

dot operator links the name of the object with the name of the method.

car1.startEngine( );
More student.setName(“Mana”);
Example account.deposit(500.0);
SE 211
5/15/2020 13
Course Teacher: S A M Matiur Rahman
Components of a Java Program
A typical Java Program composed of

comments,

import statements and

class declaration.

5/15/2020
SE 211 14
Course Teacher: S A M Matiur Rahman
Program Component: Comments
/*
Lecture_I Sample program: Displaying a window
File: LectureI_1.java
*/
import javax.swing.*;

class LectureI_1 { Comment with


multiple lines.
public static void main(String[ ] args) {
Jframe myWindow;
myWindow = new JFrame( );
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java
Program”);
myWindow.setVisible(true);
Single line
}// end of main method comment.
}// end of class
5/15/2020
SE 211 15
Course Teacher: S A M Matiur Rahman
Program Component: Import Statement
/*
Lecture_I Sample program: Displaying a window
File: LectureI_1.java
*/

import javax.swing.*;
Import statement.
class LectureI_1 {

public static void main(String[ ] args) {


Jframe myWindow;
myWindow = new JFrame( );
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);

} // end of main method


} // end of class
5/15/2020
SE 211 16
Course Teacher: S A M Matiur Rahman
Import statement: syntax and semantics

Package Name Class Name


Name of the package that contains Name of the classes we want to
the classes we want to use. import. Use asterisk to import all
classes.

<package name> . <class name>;


e.g. dorm . resident ;

More import javax.swing.JFrame;


import java.util.*;
Example import com.edu.progcourse.*;

5/15/2020
SE 211 17
Course Teacher: S A M Matiur Rahman
Components of a Java Program
/*Lecture_I Sample program: Displaying a window
File: LectureI_1.java
*/ Class declaration
import javax.swing.*;

class LectureI_1 {
public static void main(String[ ] args) {
JFrame myWindow;
myWindow = new JFrame( );
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);

}
}

5/15/2020
SE 211 18
Course Teacher: S A M Matiur Rahman
Components of a Java Program

/*Lecture_I Sample program: Displaying a window


File: LectureI_1.java Method
*/ declaration
import javax.swing.*;
class LectureI_1 {

public static void main(String[ ] args) {


JFrame myWindow;
myWindow = new JFrame( );
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
}

5/15/2020
SE 211 19
Course Teacher: S A M Matiur Rahman
Method Declaration Elements
Name of
Modifier Modifier Return Type Parameter
Method

public static void main (String[ ] args){

Method body
JFrame myWindow;
myWindow = new JFrame( );
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);

5/15/2020
SE 211 20
Course Teacher: S A M Matiur Rahman
Template for Simple Java Programs
/* Lecture_I Sample program: Displaying a window
Comment with
File: LectureI_1.java
multiple lines.
*/
Import
import javax.swing.*; Statement

class LectureI_1 {
Class Name
public static void main(String[ ] args) {

Jframe myWindow;
myWindow = new JFrame( );
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);

} // end of main method


Method body
} // end of class
5/15/2020
SE 211 21
Course Teacher: S A M Matiur Rahman
Use of Standard Classes
 Application Programming Interfaces (APIs): Visit
web site: docs.oracle.com/javase/7/docs/api/

 Learning how to use standard Java classes is the


initial step towards mastering OOP. Before we can
learn how to define our classes, we need to learn
how to use existing (pre-defined or built-in)
classes.

 We will introduce following standard classes here:


- Scanner
- Math

5/15/2020
SE 211 22
Course Teacher: S A M Matiur Rahman
Standard Output

 Using print of System.out (an instance of the


PrintStream class) is a simple way to display a result of
a computation to the user

System.out.print(“Software Engineering”);

The result appears on the


console window. The actual
Software Engineering appearance of the console
windows differs depending
on the Java tool (eclipse,
JCreator, etc.,) you use.

5/15/2020
SE 211 23
Course Teacher: S A M Matiur Rahman
Using the print Method

 The print method will continue printing from the


end of the currently displayed output.

System.out.print(“SWE 132”);
System.out.print(“My name is”);
System.out.print(“Muhammed Prince”);

SWE 132 My name is Muhammed Prince

5/15/2020
SE 211 24
Course Teacher: S A M Matiur Rahman
Using the println method
 The println method will skip to the next line after
printing out its argument.

System.out.println(“SWE 132”);
System.out.println(“My name is”);
System.out.println(“Muhammed Prince”);

SWE 132
My name is
Muhammed Prince

5/15/2020
SE 211 25
Course Teacher: S A M Matiur Rahman
Standard Input
 Using a Scanner object is a simple way to input
data from the standard input System.in, which
accepts input from the keyboard.

 First we have to associate a Scanner object to


System.in as follows:

import java.util.Scanner;
Scanner scanner;
scanner = new Scanner (System.in);

5/15/2020
SE 211 26
Course Teacher: S A M Matiur Rahman
Reading from Standard Input
 After Scanner object is setup, we can read data
from the key board.
 The following inputs the first name (String):

System.out.print(“Enter your first name:”);


String firstName = scanner.next();
System.out.println (“Nice to meet you,” + firstName
+ ”.”);

1. Prompt is displayed
Enter your first name: Muhammed Prince ENTER 2. Data is entered
Nice to meet you Muhammed Prince
3. Result is printed

5/15/2020
SE 211 27
Course Teacher: S A M Matiur Rahman
Use of Standard Classes (Math)
 The following program computed the length of the hypotenuse
of right triangle given the lengths of its two opposite sides.
/*LectureI_1 Sample program: Use of Built-in Math class
File: LectureI_2.java
*/
class LectureI_2 {
public static void main(String[ ] args) {
double a = 3.0, b = 4.0;
// c is dynamically initialized Use of Math class.

double c = Math . sqrt(a*a + b*b);


Use of built-in method sqrt(),
which is a member of Math class.

System.out.println(“Hypotenuse is: “ + c);


} // end of main method
} // end of class
5/15/2020
SE 211 28
Course Teacher: S A M Matiur Rahman
Identifiers
 Identifiers are the name of class, method, variable and
constant.
 Identifiers must follow the following rules:
 sequence of characters that consists of letters, digits,
underscore and dollar sign.
 must start with a letter, underscore or dollar sign but not
with a digit.
 cannot be java reserved (key) words.
 cannot be true, false or null.
 can be of any length.

Example: $2, ComputeArea, area, radius, and showMessageDialog


.
2A and d+4 are not because they do not follow the rules.

5/15/2020
SE 211 29
Course Teacher: S A M Matiur Rahman
Identifiers
// Scanner is in the java.util package Identifiers are:
import java.util.Scanner; LectureI_3
public class LectureI_3 { input
public static void main(String[] args) { radius
area
// Create a Scanner object
Scanner input = new Scanner(System.in);
// Prompt the user to enter a radius
System.out.print("Enter a number for radius: ");
double radius = input.nextDouble()
// Compute area
double area = radius * radius * 3.14159;
// Display result
System.out.println("The area for the circle of radius "
+ radius + " is " + area);
}
}
5/15/2020 SE 211 30
Course Teacher: S A M Matiur Rahman
5/15/2020
SE 211 31
Course Teacher: S A M Matiur Rahman

You might also like