انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Further exploring the source code

Share |
الكلية كلية تكنولوجيا المعلومات     القسم قسم البرامجيات     المرحلة 2
أستاذ المادة احمد خلفة عبيد العجيلي       09/03/2019 10:17:46
object-oriented programming with java
ahmed al-ajeli
further exploring the source code
ahmed al-ajeli
lecture 3
2
topics to be covered
•extend ticket machine project
•conditional statements
•relational operators
•local variables
•creating multiple objects
object-oriented programming with java
ahmed al-ajeli
3
reflecting on the ticket machines
•their behaviour is inadequate in several ways:
–no checks on the amounts entered.
–no refunds.
–no checks for a sensible initialization.
•how can we do better?
–we need the ability to choose between different courses of action.
4
making choices in everyday life
•if i have enough money left, then i will go out for a meal
•otherwise i will stay home and watch a movie.
if(i have enough money left) { i will go out for a meal } else { i will stay home and watch a movie }
object-oriented programming with java
ahmed al-ajeli
5
making choices in java
if(perform some test) {
do these statements if the test gave a true result
}
else {
do these statements if the test gave a false result
}
‘if’ keyword
boolean condition to be tested
actions if condition is true
actions if condition is false
‘else’ keyword
6
making a choice in the ticket machine
public void insertmoney(int amount)
{
if(amount > 0) {
balance = balance + amount
}
else {
system.out.println("use a positive amount:“
+ amount)
}
}
conditional statement avoids an inappropriate action
object-oriented programming with java
ahmed al-ajeli
7
variables – a recap
•fields are one sort of variable.
–they store values through the life of an object.
–they are accessible throughout the class.
•parameters are another sort of variable:
–they receive values from outside the method.
–they help a method complete its task.
–each call to the method receives a fresh set of values.
–parameter values are short lived.
8
scope and lifetime
•each block defines a new scope.
–class, method and statement.
•scopes may be nested:
–statement block inside another block inside a method body inside a class body.
•scope is static (textual).
•lifetime is dynamic (runtime).
object-oriented programming with java
ahmed al-ajeli
9
public void printticket() { if(balance >= price) { // simulate the printing of a ticket. system.out.println("##################") system.out.println("# the eclipse line") system.out.println("# ticket") system.out.println("# " + price + " cents.") system.out.println("##################") system.out.println() // updating the total collected with the price. total = total + price // reduce the balance by the price. balance = balance – price } else { system.out.println("you must insert at least: " + (price – balance) + " cents.") } }
printticket method
10
relational operators
standard relational operators are provided for the if test
< less than > greater than
<= less than or equal to
>= greater than or equal to
== equal to
!= not equal to
a comparison using a relational operator
is known as a boolean expression,
since it evaluates to a boolean (true or
false) value
object-oriented programming with java
ahmed al-ajeli
11
refund an excess balance unsuccessful attempt
public int refundbalance()
{
// return the amount left.
return balance
// clear the balance.
balance = 0
}
it looks logical, but the language does not allow it.
12
local variables
•methods can define their own, local variables:
–short lived, like parameters.
–the method sets their values – unlike parameters, they do not receive external values.
–used for ‘temporary’ calculation and storage.
–they exist only as long as the method is being executed.
–they are only accessible from within the method.
–they are defined within a particular scope.
object-oriented programming with java
ahmed al-ajeli
13
local variables
public int refundbalance()
{
int amounttorefund
amounttorefund = balance
balance = 0
return amounttorefund
}
a local variable
no visibility
modifier
you can declare and assign a local variable at the same time int amounttorefund int amounttorefund = balance amounttorefund = balance
14
scope and lifetime
•the scope of a field is its whole class.
•the lifetime of a field is the lifetime of its containing object.
•the scope of a local variable is the block in which it is declared.
•the lifetime of a local variable is the time of execution of the block in which it is declared.
object-oriented programming with java
ahmed al-ajeli
15
creating multiple objects (1)
we can create multiple instances of the same class using the following pattern:
classname objname_1, objname_2, …, objname_n
objname_1 = new classname (parameters list_1)
objname_2 = new classname (parameters list_2)
.
.
.
objname_n = new classname (parameters list_n)
16
creating multiple objects (2)
ticketmachine_1
ticketmachine_2
price
balance
total
price
balance
total
400
300
0
0
0
0
ticketmachine ticketmachine_1, ticketmachine_2
ticketmachine_1 = new ticketmachine (400)
ticketmachine_2 = new ticketmachine (300)
example:
object-oriented programming with java
ahmed al-ajeli
17
reviewing a familiar example
public class student
{
private string name
private string id
private int credits
public student(string fullname, string studentid)
{
name = fullname
id = studentid
credits = 0
}
public string getname()
{
return name
}
18
public void changename(string newname)
{
name = newname
}
public string getstudentid()
{
return id
}
public void addcredits(int newcreditpoints)
{
credits += newcreditpoints
}
public int getcredits()
{
return credits
}
public void print()
{
system.out.println(name + ", student id: " + id +
", credits: " + credits)
}
}

المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
الرجوع الى لوحة التحكم