Java 1
Java 1
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:
5/15/2020
SE 211 5
Course Teacher: S A M Matiur Rahman
The First Object-Oriented Program in Java
5/15/2020
SE 211 6
Course Teacher: S A M Matiur Rahman
Components of a Java Program
import javax.swing.*;
class LectureI_1 {
myWindow.setSize(300, 200);
}
} Use an Object
5/15/2020
SE 211 7
Course Teacher: S A M Matiur Rahman
Program Diagram for LectureI_1
setVisible(true)
5/15/2020
SE 211 8
Course Teacher: S A M Matiur Rahman
Dependency Relationship
LectureI_1
myWindow:JFrame
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
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.
Customer customer;
customer = new Costomer();
customer = new Costomer();
customer
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.
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,
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.*;
import javax.swing.*;
Import statement.
class LectureI_1 {
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
5/15/2020
SE 211 19
Course Teacher: S A M Matiur Rahman
Method Declaration Elements
Name of
Modifier Modifier Return Type Parameter
Method
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);
5/15/2020
SE 211 22
Course Teacher: S A M Matiur Rahman
Standard Output
System.out.print(“Software Engineering”);
5/15/2020
SE 211 23
Course Teacher: S A M Matiur Rahman
Using the print Method
System.out.print(“SWE 132”);
System.out.print(“My name is”);
System.out.print(“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.
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):
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.
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