object oriented programming (oop) lab # 1 ta. maram & ta. mubaraka ta. kholood & ta. aamal

49
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Upload: natalie-cole

Post on 21-Jan-2016

237 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Object Oriented Programming (OOP)

LAB # 1TA. Maram & TA. Mubaraka

TA. Kholood & TA. Aamal

Page 2: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

What is JAVA? The Java programming language has a long

history. It started in 1991 when Sun Microsystems began Their Green Project. The goal of The Green Project was to create a new portable programming language, that could be used to create applications to run on multiple operating systems without having to recompile or port the code. The original name of the language was Oak, for a large oak tree that stood outside the windows of the developers' offices.

But between the time the project began and the time the language was released, it was renamed as Java supposedly because of the amount of coffee that the developers were drinking. Java was first released to the public in 1995 and thereafter saw a rapid evolution and change. Now java is every where.

Page 3: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Java is used

To built Web applications. Mobile applications except (IOS) like

iphone and ipad. Many other applications..

Page 4: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Java .…

Is not the simplest language nor the difficult one.

Is “C-Style” language. That means if you know any language like C, C++,C#, PHP, You are going to understand the language.

It is object oriented language. That means you have to deal with every thing as object.

Page 5: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

What is JVM? The Java Virtual Machine (JVM) is an

abstraction layer between a Java application and the underlying platform. As the name implies, the JVM acts as a “virtual” machine or processor. To the bytecodes comprising the program, they are communicating with a physical machine; however, they are actually interacting with the JVM.

Page 6: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

What is JVM?(Java

VirtualMachine)

Page 7: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

The main syntax of the language Import ……………………… ( import the library you need in the

program, sometimes you don’t need to import any libraray).

public class <class name> {

public static void main (String [] args){

<statement 1>;

<statement 2>;

…………………

} (close the main function)

} (close the class)

Page 8: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Output statement

System.out.println(“<any thing you want to display>”);

System is a final class from java.lang package which is defined already in the java. That means you don’t need to import any package to run this statement.

Page 9: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Example #1 Hello world program

public class Welcome { public static void main(String[] args) { System.out.println("hello world!! "); } }

Page 10: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

How can we write a program in Eclipse?

1)File → new → Java Project

Page 11: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

How can we write a program in Eclipse?

2) Write the Project name, and Change the execution environment JRE to (use default JRE) Then press Finish

Page 12: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

How can we write a program in Eclipse?

* We can see our project in Package Explorer * The

execution of the project is shown here.

* If there is any errors in the program, we can find the errors detail in problems tap .

Page 13: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

How can we write a program in Eclipse?

3) Write click on the src folder, choose new then class

Page 14: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

How can we write a program in Eclipse?

4) Write a name of class, put a check on public static void main (String args[]) Then press Finish

Page 15: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

How can we write a program in Eclipse?

Now, we can write our program inside the main method.

Page 16: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

How can we write a program in Eclipse?

Page 17: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

After that we need to run the program…

From Run menu, Choose Run and check the program you want to run. Then click OK.

You will be able to see the results in console view.

How can we write a program in Eclipse?

Page 18: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Defining Variables <variable type> <Variable name> ; Or <variable type> <variable name> = <variable

value>; For example int x; int x=5; Variables can be String, int, double, char and so

on.

Page 19: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Example #2 Write a program that add two numbers 5

and 3. Then display the result Solution:

Page 20: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Input statement To allow the user to insert anything , you have to define Scanner

object and to do that

First, you need to import a package called java.util.Scanner

Import java.util.Scanner;

Second, create a scanner which obtain input from the user

Scanner input =new Scanner (System.in);

Finally, You can use your new object (input) to insert any type of data.

For example, To insert integer number

int x=input.nextint();

And to insert a string

String x=input.nextLine();

Page 21: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Example # 3 Write a program that asks user to insert two

integers, add them and display the result. Solution :

Page 22: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Condition- If statement if (<condition>)

Statement ;

else

Statement ;

Or

if (<condition>)

{Statements; }

else if (<condition>) {

Statements; }

else

{statements;}

Page 23: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Switch statement switch (variable){

case <value>:

Statements;

break;

case<value>:

Statements;

break;

default:

Statements;

Break;

}

Page 24: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Assignment 1:

Conditional Statements and Switch

Case For each of the following exercise, perform each of the following steps:

a) Read the problem statement.

b) Formulate the algorithm using pseudo-code

c) Write a Java program.

d) Test, debug and execute the Java program.

e) Process three complete sets of data.

Page 25: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

  Write a Java program that asks the user to input a number between 1 and 20 and prints a sentence which indicates if the number is either within the range, too high or too low.

 Exercise 1:

Page 26: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Pseudo Code: Prompt the user to enter a number between 1 and 20 Input the number

If the number is between 1 and 20 Print “The number is within the range” Else If the number is larger than 20 Print “The number is too high” Else Print “The number is too low”

Page 27: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 1: Solution

Page 28: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 1: Testing

Page 29: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Write a Java program that asks the user to input two numbers x and y and prints the absolute value of x-y.

 Exercise 2:

Page 30: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 2: Solution

Page 31: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 2: Testing

Page 32: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 3:

Write a Java program that asks the user to enter the current month and outputs whether the month is in Winter, Spring, Summer or Autumn.Winter starts 21/12,Spring starts 21/3, Summer starts 21/6, Autumn starts 21/9.

Page 33: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Exercise #3 (analysis) winter starts (21/12) and ends (20/3)

Spring starts (21/3) and ends (20/6)

Summer starts (21/6) and ends (20/9)

Autumn starts (21/9) and ends (20/12) If the month is 1 or 2 (it must be winter regardless day)….

If the month is 4 or 5 (it must be spring)….

If the month is 7 or 8 (it must summer )….

If the month is 10 or 11 (it must Autumn )….

Page 34: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 3: Solution

Page 35: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 3: Testing

Page 36: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 5:

Create a program which asks the user for 3 numbers representing the year, month and day e.g2014 10 08 and then outputs in the form 8th October 2014.

Page 37: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Exercise # 5 Hint #1 -Order of days 1st 11th 22nd2nd 12th 23rd3rd 13th 24th4th 14th 25th5th 15th 26th6th 16th 27th7th 17th 28th8th 18th 29th9th 19th 30th10th 20th 31st

  21st  

Page 38: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

Exercise #5Hint #2 - Order of months

1.January 2.February,3. March, 4.April, 5.May, 6.June, 7.July, 8.August, 9.September, 10.October, 11.November12.December

Page 39: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 5: Solution

Page 40: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 5: Testing

Page 41: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 6:

Write a Java program that, given as input three integers representing a date as day, month, year, prints out the number day, month and year for the following day's date.Suppose that the month has always 30 days.

Page 42: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 6: Solution

Page 43: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 6: Testing

Page 44: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 7:

Write a Java program that given as input three integers representing a time as second, minute, and hour, prints out the number second, minute and hour for the following second's time.

Page 45: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 7: Solution

Page 46: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 7: Testing

Page 47: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 8:

Write a Java program that, given as input three integers representing a date as day, month, year, one integer representing number of days, print out the new date which is the entered date added to the number of days. Suppose that the month has always 30 days.

Page 48: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 8: Solution

Page 49: Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

 Exercise 8: Testing