0% found this document useful (0 votes)
2 views

lect01-week1-Definitions

The document outlines the course details for Programming2 (CC272) taught by Dr. Layla Abou-Hadeed, including assessment criteria and topics covered such as programming paradigms and Java programming. It emphasizes academic honesty and provides an overview of various programming paradigms like object-oriented, functional, and logic programming. Additionally, it includes essential information about Java, its advantages and disadvantages, and basic programming concepts such as classes, methods, and constants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lect01-week1-Definitions

The document outlines the course details for Programming2 (CC272) taught by Dr. Layla Abou-Hadeed, including assessment criteria and topics covered such as programming paradigms and Java programming. It emphasizes academic honesty and provides an overview of various programming paradigms like object-oriented, functional, and logic programming. Additionally, it includes essential information about Java, its advantages and disadvantages, and basic programming concepts such as classes, methods, and constants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Programming2

(CC272)
Click to add text
Click to add text

Dr. Layla Abou-Hadeed


• Contact Details for Professor
Name of Professor: Layla Abou-Hadeed
E-mail: [email protected]
[email protected]
Office hours:

10:36 2
• Course Assessment:
• Assignments 40%
• Mid-term examination 10%
• Final examination 50%

10:36 3
• Course breakdown
Object-oriented design
UML
Design patterns

10:36 4
• Academic Honesty Statement
Academic dishonesty is NOT tolerated in this
course.
Cheating and plagiarism can result in ZERO in
CW and/or exam

10:36 5
Introduction

Programming Paradigms

10:36 6
Programming Paradigms
A programming paradigm is a way in which a computer
language looks at the problem to be solved.
• Imperative: Programming with an explicit sequence of
commands that update state.
• Declarative: Programming by specifying the result you
want, not how to get it.
• Structured: Programming with clean, goto-free, nested
control structures.
• Procedural: Imperative programming with procedure
calls.
• Object-Oriented: Programming by defining objects that send
messages to each other.
Behrouz Forouzan and Firouz Mosharraf
10:36 https://cs.lmu.edu/~ray/notes/paradigms/ 7
Programming Paradigms
• Functional : Programming with function calls that
avoid any global state.
• Logic (Rule-based): Programming by specifying a set
of facts and rules. An engine infers the answers to
questions.
• Constraint: Programming by specifying a set of
constraints. An engine finds the values that meet the
constraints.
• Aspect-Oriented: Programming cross-cutting
concerns applied transparently.

10:36 https://cs.lmu.edu/~ray/notes/paradigms/ 8
Procedural Programming

Behrouz Forouzan and Firouz Mosharraf


http://www.thomsonlearning.co.uk/forouzan
10:36 9
Object-Oriented Programming

Behrouz Forouzan and Firouz Mosharraf


10:36 http://www.thomsonlearning.co.uk/forouzan 10
Functional Programming

For example, we can define a primitive function called first that extracts the first
element of a list. It may also have a function called rest that extracts all the elements
except the first. A program can define a function that extracts the third element of a
list by combining these two functions

Behrouz Forouzan and Firouz Mosharraf


http://www.thomsonlearning.co.uk/forouzan
10:36 11
Logic Programming
Logical reasoning is based on deduction. Some statements (facts) are given
that are assumed to be true, and the logician uses solid rules of logical
reasoning to deduce new statements (facts). For example, the famous rule of
deduction in logic is:

In Prolog: meaning

John is a human
A human is mortal
The user can then ask:

Is John mortal ?

and the program will respond with yes


Adapted from Behrouz Forouzan and Firouz Mosharraf
10:36 http://www.thomsonlearning.co.uk/forouzan
12
Constraint Programming
• Backtracking (BT) Algorithm:
• Assume A, B, C and D has values from {1, 2}
A
assign value to variable 1
check consistency
B
until all variables labelled 1 2
Solution : A=1, B=2, C=1, D=1
C
1 2 1

D
1 2 1 2 1

A = D, B ≠ D, A+C < 4
Roman Barták Charles University, Prague
[email protected]
Aspect-Oriented Programming

https://www.albinsblog.com/2020/11/aspect-oriented-progra
10:36 mming-aop-with-aem.html#.YF2FaNJKhdg 14
Aspect-Oriented Programming

https://www.slideshare.net/anumod4u/aspect-oriented-pro
10:36 gramming-presentation 15
JAVA Programming Language

Based on Lecture slides prepared by Dr.


10:36 16
Walid Aly
JAVA
C 1970

C++ 1979

JAVA 1991

http://java.sun.com/

C# 2000

Based on Lecture slides prepared by Dr.


10:36
Walid Aly 17
What can JAVA Do???
Web Application
Desktop Application

Mobile Application
Network Application

Based on Lecture slides prepared by Dr.


10:36 18
Walid Aly
Advantages of JAVA
Java is simple.
Java is object-oriented.
Java is platform-independent.
Java embraces Distributed computing.
Java is secure.
Java is reliable.
Java is multithreaded.
Automatic memory management
Java is free!

Disadvantages of JAVA

Slower
memory-consuming
Single-pattern language (Every thing is an object)
statically typed language
10:36
Easy to decompile (need java
Based on obfuscator)
Lecture slides prepared by Dr.
19
Walid Aly
Java Basic Information
▪ Source code is first written in plain text files ending with the .java extension.
▪ JAVA source files are compiled into .class files by the java compiler.
▪ The .class file contains bytecodes which is the machine language of the Java Virtual
Machine(Java VM).
▪ The JVM runs your application by running the .class file.
Through the Java VM, the same application is capable of running on multiple platforms.

MyProgram.java MyProgram.class
class MyProgram …………
………
{ ……….
……..
}

Based on Lecture slides prepared by Dr.


10:36 20
Walid Aly
A Simple Java Program
Keyword Identifier for Class name

// this program prints “Hello World” to screen Comment


public class HelloWorld
Modifier
{
public static void main(String[] args) Main method declaration
{
System.out.println("Hello World!");
} Statement Calling the
method for printing to
} screen

HelloWorld.java

Based on Lecture slides prepared by Dr.


10:36 21
Walid Aly
The main method
• The main method is called automatically when
the class is executed
• The main method signature is
public static void main (String [] args)
▪ The argument of main method must be an array of
string ,however it can take any name
▪ The main must be public ,void and static

Based on Lecture slides prepared by Dr.


10:36 22
Walid Aly
Methods in java
A method is a set of code which is referred to by name and can be called (invoked) at any
point in a program simply by utilizing the method's name.

Modifiers: The modifier, which is optional, tells the compiler how to call the method.
This defines the access type of the method.

Based on Lecture slides prepared by Dr.


10:36 AM 23
Walid Aly
Calling methods in Java
• The easiest way to call a method is to declare it as public static
• In the same class call the method using its name
• In another class , call the method as classname.methodName

class A class B
{ {
public static void m1() public static void main (String [ ] arg )
{ {
System.out.println(“Hello 2”); System.out.println(“Hello 1”);
} A.m1();
public static void main (String [ ] arg ) System.out.println(“Hello 3”);
{ }
System.out.println(“Hello 1”); }
m1();
System.out.println(“Hello 3”);
}
}

Based on Lecture slides prepared by Dr.


10:36 AM 24
Walid Aly
Declaring constants
• Java does not directly support constants. However, a static final
variable is effectively a constant.
• The static modifier causes the variable to be available without the
need to create an object of the class where it is defined.
• The final modifier causes the variable to be unchangeable
• Java constants are normally declared in ALL CAPS

class Math
{
public static final double PI=3.14;

Based on Lecture slides prepared by Dr.


10:36 AM 25
Walid Aly
Modifiers
// author: Ahmed Ali
public class Car{
String color;
private int speed;
private int numOfDoors=4;
public void incrementSpeed(int value){
Java uses certain reserved words called modifiers …………………..
that specify the properties of the data, methods, }
and classes and how they can be used.
For example: public ,private, final public void decrementSpeed(int value){
private int speed; speed=speed-value;
public static void getNumberOfCars(){ } }
public int getSpeed(){
…………………..
}
public boolean isMoving()
{
…………………..
}
public static void getNumberOfCars()
{
}
}
Based on Lecture slides prepared by Dr.
10:36 AM 26
Walid Aly
Example

Based on Lecture slides prepared by Dr.


10:36 AM 27
Walid Aly
Brief Introduction to objects and classes
We work with physical objects all the time: televisions, cars, customers, reports, light bulbs,printers.
A software object is a representation of a thing that we want to manipulate in our program.
A class is a template that defines what an object's data and methods will be.
objects share two characteristics: state and behavior.
The state of an object is represented by variables and behavior by a set of methods
OOP reflects how people think of real world.
class Circle class Student
class className { {
{ double radius; String name;
………………….. double GPA;
Variables describing state double getArea() int registerationNumber;
{ …………………..
Methods describing behavior ………………… void registerUnit()
} {
} ……………………. …………………
} }
void payFees(){……}
Student object 1 Student object 2 }
name =Ahmed Aly name =Mohamed Fathi
registration number=245245 registration number=34345
GPA=3.2 GPA=2.2
registerUnit registerUnit
payFees BasedpayFees
on Lecture slides prepared by Dr.
10:36 AM 28
Walid Aly
A Simple Java class

// author: Ahmed Ali


package racing;
public class Car{
String color;
private int speed;
private int numOfDoors=4;
public void incrementSpeed(int value){
…………………..
}
public void decrementSpeed(int value){
…………..
}
public int getSpeed(){
…………………..
}
public boolean isMoving()
{
…………………..
}
public static void getNumberOfCars()
{…………………
}
Based on Lecture slides prepared by Dr.
10:36 AM } Walid Aly
29
Package
A package is a collection of related classes. package racing;
If the class belongs to a certain package , The file should be class Car
in a folder with the same name as package. {
a class can only be in one package
Placing the package declaration at the top of your source
file includes these classes in a package with the name that }
you specify. package graphics;
ex: package graphics; class Button
If no package is explicitly declared, Java places your classes {
into a default package containing all files in the same folder.

The Java platform provides an enormous class library ,organized as packages.


This library is known as the "Application Programming Interface“(API).

JAVA API Documentation

Based on Lecture slides prepared by Dr.


10:36 AM 30
Walid Aly
Class Libraries
• A class library is a collection of classes that we
can use when developing programs
• The Java standard class library is part of any Java
development environment
• The Java class library is sometimes referred to as
the Java API

Based on Lecture slides prepared by Dr.


10:36 AM 31
Walid Aly
The Java API
• Get comfortable navigating the online Java API
documentation

Based on Lecture slides prepared by Dr.


10:36 AM 32
Walid Aly
33
Java API packages (a subset).

JAVA API Documentation

Based on Lecture slides prepared by Dr.


10:36 AM
Walid Aly
Java classes
public class A
Classes in your program might be your own classes or classes {
that already exist in java API. public static int x;
A Java class has static and non static variables public int y;
Java class has static and non static methods public A()
Java class has one or more Constructors {
Constructors are special type of methods , they must have the …………..
same name as class , and with no return type. }
We use constructor to create objects from classes public A(int z)
Variables, methods and constructors are known as class {
members …………..
}
class className public static void m1()
{ {
……………..
Variables describing state }
Constructors
Methods describing behavior public void m2()
} {
……………..
}
Based on Lecture slides prepared by Dr.
}
10:36 AM 34
Walid Aly
How to use the class Members
public class A{
public static int x=10;
public int y=20;
public A()
{
System.out.println("Creating Object");
}
public A(int z)
{
System.out.println("Creating Object with z="+z);
}
public static void m1()
{
System.out.println("Hi from m1");
}
public void m2()
{
System.out.println("Hi from m2");
}
}

Based on Lecture slides prepared by Dr.


10:36 AM 35
Walid Aly
How to use the class Members
public class A{
Static members are accessed by the name of class
public static int x=10;
ex : A.x; A.m1(); public int y=20;
To use non static members you have to create objects public A()
from the class using constructor {
ex: A a1 =new A(); System.out.println("Creating Object");
and then access non static members with the object }
you created public A(int z)
{
ex : a1.y ; a1.m2();
System.out.println("Creating Object with z="+z);
}
public static void m1()
{
System.out.println("Hi from m1");
}
public void m2()
{
System.out.println("Hi from m2");
}
}

Based on Lecture slides prepared by Dr.


10:36 AM 36
Walid Aly
How to use the class Members
public class A{
Static members are accessed by the name of class
public static int x=10;
ex : A.x; A.m1(); public int y=20;
To use non static members you have to create objects public A()
from the class using constructor {
ex: A a1 =new A(); System.out.println("Creating Object");
and then access non static members with the object }
you created public A(int z)
{
ex : a1.y ; a1.m2();
System.out.println("Creating Object with z="+z);
}
public class Test{ public static void m1()
public static void main (String [] arg){ {
System.out.println("Hi from m1");
//access static members
}
A.m1(); public void m2()
int i=A.x; {
System.out.println("x="+i); System.out.println("Hi from m2");
//access instance members }
A a1=new A(); }
a1.m2();
int j=a1.y;
System.out.println("y="+j);
}
}
Based on Lecture slides prepared by Dr.
10:36 AM 37
Walid Aly
import statements 38

A Class full name is its package name.class name


import statements specify the packages of the classes used in your Java programs

Without import
......................
javax.swing.JApplet x=………………………………..

With import
import javax.swing.JApplet;
......................
JApplet x=…………………………….

you can have any number of import statements


You can import all classes in a package using * ex : import java.util.*;
java.lang package is imported by the compiler into all programs.
package GUI;
A Java source file has the following elements in this specific order. import java.awt.Event;
1. An optional package statement import java.util.*;
class A
2. Zero or more import statements,
{
3. Any number of class and interface definitions may follow ….
10:36 AM
Based on Lecture slides prepared by Dr. }
Walid Aly
Using classes from Java API
Package :java.lang
Class Math

Package :java.lang
Class Integer

Package :javax.swing
Class JOptionPane

Package :java.util
Class Random
Based on Lecture slides prepared by Dr.
10:36 AM 39
Walid Aly
class java.lang.Math
Method Summary import java.lang.Math;
public static double abs(double a) public class Test
public static int round(float a) {
public static double ceil(double a) public static void main (String [] arg)
public static double cos(double a) {
public static double floor(double a) int a=Math.abs(-4);
System.out.println("a="+a);
public static double max(double a, double b)
public static double min(double a, double b)
int b=Math.round(4.6f);
public static double pow(double a, double b)
int c=Math.max(4,7);
double d=Math.random();
public static double random() }
public static double sin(double a) }
public static double sqrt(double a)

Based on Lecture slides prepared by Dr.


10:36 AM 40
Walid Aly
Example 1:Displaying Text in a Message Dialog Box
Welcome.java
import javax.swing.JOptionPane;
public class Welcome {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Welcome to Java!“);
}
}

When you supply a null argument,


the JOptionPane dialog will be centered
on the user’s screen. When you supply
a JFrame reference, the dialog is centered
on that JFrame

Based on Lecture slides prepared by Dr. Walid Aly


And
10:36 AM https://alvinalexander.com/java/joptionpane-showmessagedialog-examples-1/ 41
Example 2 : Getting Input from Input Dialogs

import javax.swing.JOptionPane;
public class WelcomeInMessageDialogBox {
public static void main(String[] args) {
String name=JOptionPane.showInputDialog(null, "Plz enter your name “);
JOptionPane.showMessageDialog(null, "Welcome "+ name +" to Java!“);
}
}

WelcomeInMessageDialogBox.java

Run

Based on Lecture slides prepared by Dr.


10:36 AM 42
Walid Aly
Example 3: Converting Strings to Numbers
To convert a string into an int value, use the parseInt method in the Integer class, as follows:

Package :java.lang
Class Integer
static int parseInt(String s)
Parses the string argument as a signed decimal integer.

int intValue = Integer.parseInt(intString);

///////////
String s1=“14”;
int i=Integer.parseInt(s1);

Based on Lecture slides prepared by Dr.


10:36 AM 43
Walid Aly
import javax.swing.JOptionPane;
public class Adder {
public static void main(String[] args) {
String num1=JOptionPane.showInputDialog(null, "Please enter first number");
String num2=JOptionPane.showInputDialog(null, "Please enter second number");
int x=Integer.parseInt(num1);
int y=Integer.parseInt(num2);
int z=x+y;
JOptionPane.showMessageDialog(null, "the sum is "+ z);

}
}

Based on Lecture slides prepared by Dr.


10:36 AM 44
Walid Aly
class java.util.Random
import java.util.Random;
public class Test
{
Constructor public static void main (String [] arg)
Random( ) {
Methods Random rnd=new Random();
public int nextInt(int n) int x=0;
Returns a pseudorandom, uniformly distributed int value for(int i=0; i<10;i++)
between 0 (inclusive) and the specified value (exclusive) {
x=rnd.nextInt(11);
System.out.println("x="+x);
}
}
}

Based on Lecture slides prepared by Dr.


10:36 AM 45
Walid Aly
Creating a random guess Game
import java.util.Random;
import javax.swing.JOptionPane;
class GuessingGame{
public static void main (String [] arg){
int numofTrials=5;
Random rnd=new Random();
int secretNum=rnd.nextInt(11);
JOptionPane.showMessageDialog(null,"Choose a number between 0 and 10 \n you have only 5 trials");
boolean sucess=false;
String guessString="";
int guess=0;
for (int i=0;i<numofTrials;i++){
guessString=JOptionPane.showInputDialog(null,"What is your guess");
guess=Integer.parseInt(guessString);

Based on Lecture slides prepared by Dr.


10:36 AM 46
Walid Aly
if (guess==secretNum)
{
JOptionPane.showMessageDialog(null,"Congratulations ");
sucess=true;
break;
}
if (guess>secretNum)
{
JOptionPane.showMessageDialog(null,"Too Big ");
}
if (guess<secretNum)
{
JOptionPane.showMessageDialog(null,"Too small ");
}
}// end loop
if(!(sucess))
JOptionPane.showMessageDialog(null,"You Lost ");
}//end of main method
}//end of class

Based on Lecture slides prepared by Dr.


10:36 AM 47
Walid Aly
Example :Reading from Console
Java.util.Scanner
public Scanner(InputStream source)Constructs a new Scanner that
.produces values scanned from the specified input stream
Hint: System.in represents the Console
()public int nextInt
.Scans the next token of the input as an int
()public double nextDouble
.Scans the next token of the input as a double

Based on Lecture slides prepared by Dr.


10:36 AM 48
Walid Aly
import java.util.Scanner;
class ScannerDemo
{
public static void main (String [] arg)
{
Scanner scan=new Scanner(System.in);
System.out.println("Please Enter first number");
int num1=scan.nextInt();
System.out.println("Please Enter Second number");
int num2=scan.nextInt();
int sum=num1+num2;
System.out.println("The sum of " + num1 +" and "+num2+"="+sum);

}
}

Based on Lecture slides prepared by Dr.


10:36 AM 49
Walid Aly
Useful Links

http://thenewboston.org/tutorials.php

Based on Lecture slides prepared by Dr.


10:36 AM 50
Walid Aly

You might also like