Lecture 01
Lecture 01
Lecture 01:
Review of basic programming in Java.
Slides have originally been prepared by Rose Williams, Binghamton University and Kenrick
Mock, University of Alaska Anchorage
Object Oriented Programming
• Object Oriented Programming treats everything in
the world as objects (e.g., automobiles, universities,
people, computers).
■ Semantics
■ define what a statement means
Assignment Compatibility:
System.out.println("hello");
System.out.println("you");
System.out.println();
public class Id {
رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ. د:اﺳﺗﺎذ اﻟﻣﺎدة Your name is Jalal and Your age is Jalal 18
Example: Scanner Demo (1/2)
//********************************************************
// This program demonstrates how to read tokens from
// the console with the Scanner class
//********************************************************
■ A string of character :
■ is an object defined by the String class
■ delimited by double quotation marks ex: "hello", "a"
String title;
String title;
title = "content of the string";
System.out.println("01234567890123456789012");
System.out.println(sentence);
System.out.println("The word \"hate\" starts at index " + position);
sentence = sentence.substring(0, position) + "adore" + ending;
System.out.println("The changed string is:");
System.out.println(sentence);
}
}
System.out.println("01234567890123456789012");
System.out.println(sentence);
System.out.println("The word \"hate\" starts at index " + position);
01234567890123456789012
sentence = sentence.substring(0, position) + "adore" + ending;
System.out.println("The Ichanged
hate string is:");
text processing!
System.out.println(sentence);
The word "hate" starts at index 2
} The changed string is:
I adore text processing!
}
رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ. د:اﺳﺗﺎذ اﻟﻣﺎدة 30
A note on comparing strings
• We cannot use the relational operators to compare
strings (<, ==, …)
• ex: firstString.equals(secondString)
• returns a boolean:
• true if firstString has the same content as secondString
• false otherwise
//. . .
String inputLine;
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
sum = sum + n;
}
System.out.println("After loop");//trace
System.out.println("n == " + n + " sum == " + sum);//trace
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
//. . .
رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ. د:اﺳﺗﺎذ اﻟﻣﺎدة 35
Just checking...
n What is the output produced by the following?
//. . .
int number = 10;
while (number > 0) {
number = number - 2;
if (number == 4)
continue;
System.out.println(number);
}
System.out.println("The end.");
//. . .
رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ. د:اﺳﺗﺎذ اﻟﻣﺎدة 36
Just checking...
n What is the output produced by the following?
import java.util.Scanner;
score 80.0 99.0 75.5 100 85.0 20.5 12.0 30.5 77.0
• declaration + creation:
double[] scores = new double[4];
• An array is declared and created in almost the same way that objects
are declared and created.
رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ. د:اﺳﺗﺎذ اﻟﻣﺎدة 39
Initializing Arrays
• An array can be initialized when it is declared
double[] score = {80, 99.9, 75, 100, 85.5}
• Uninitialized array:
What is wrong?
double[] a;
for (int i = 0; i < 9; i++)
Never created the array
a[i] = 2.5;
رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ. د:اﺳﺗﺎذ اﻟﻣﺎدة 42
Multidimensional Arrays
• A one-dimensional array: stores a list of elements of simple type
(primitive or reference).
char[] a = new char[4]; v i e w
a[0] o n c e u p o n
aa[1][5]
a[1] a t i m e
a[2]
t h e r e w a s
a[3] a l i t t l e
رﺑﯾﻊ ﻣﻔﺗﺎح اﻟﻌﻠواﻧﻲ. د:اﺳﺗﺎذ اﻟﻣﺎدة 43
Using the length Instance Variable
• The instance variable length does not give the total number of indexed
variables in a two-dimensional array.
• page.length is equal to 30
• page[0].length is equal to 100
• 1. data declarations
(instance variables)
Method
declarations
• 2. action declarations
(methods)
• Example:
nameOfClass nameOfObject = new nameOfClass();
• nameOfClass nameOfObject ;