Professional Documents
Culture Documents
prcakton rrjedhn e kontrollit t ekzekutimit t njpasnjshm t urdhrave prkatse. Struktura e kontrollit e invokimit t metods
PRANUESI.METODA(ARGUMENTET)
prcakton rrjedhn e kontrollit t pauzimit t ekzekutimit n vendin e invokimit ashtu q urdhrat e emrtuara me METODA n PRANUESI t ekzekutohen n vijim.
import javax.swing.*; /** Llogarit sasin e metelikeve. * Input: dy numra t dhn n rreshtin komandues sasia e eurove dhe sasia e centve * * Output: sasit e metelikve */ public class MakeChange { public static void main (String[] args) { int euros = new Integer(args[0]).intValue(); if ( euros < 0 ) { JOptionPane.showMessageDialog(null, "Gabim: euro negativ " + euros); }
else { int cents = new Integer(args[1]).intValue(); if ( cents < 0 ) { JOptionPane.showMessageDialog(null, "Gabim: cent negativ " + cents); } else { if ( cents > 99 ) { JOptionPane.showMessageDialog(null, "Gabim: n cent " + cents); }
else { int money = (euros * 100) + cents; System.out.println("Gjysma = " + (money / 50)); money = money % 50; System.out.println("Njzetqindarka = " + (money / 20)); money = money % 20; System.out.println("Dhjetqindarka = " + (money / 10)); money = money % 10; System.out.println("Pesqindarka = " + (money / 5)); money = money % 5; System.out.println("Qindarka = " + money); } } } } }
lexo eurot; euro negative? gabim lexo centt; cent negativ? gabim
cents > 99 ?
gabim
import javax.swing.*; /** Llogarit sasin e metelikeve * Input: dy numra t dhn n rreshtin komandues sasia e eurove dhe sasia e centve * * Output: sasit e metelikve */ public class MakeChange4 { public static void main (String[] args) { boolean ok = true; // memoron korrektsin e t dhnave hyrse int euros = new Integer(args[0]).intValue(); int cents = new Integer(args[1]).intValue(); if ( euros < 0 ) { JOptionPane.showMessageDialog(null, "Gabim: euro negativ " + euros); ok = false; // gabimi mohon korrektsin }
if ( ok ) // eurot jan n rregull, shqyrto centt { if ( cents < 0 ) { JOptionPane.showMessageDialog(null, "Gabim: cent negativ " + cents); ok = false; } if ( cents > 99 ) { JOptionPane.showMessageDialog(null, "Gabim: n cent " + cents); ok = false; } }
if ( ok )
// eurot dhe centt jan n rregull, // llogarit prgjegjjen. { int money = (euros * 100) + cents; System.out.println("Gjysma = " + (money / 50)); money = money % 50; System.out.println("Njzetqindarka = " + (money / 20)); money = money % 20; System.out.println("Dhjetqindarka = " + (money / 10)); money = money % 10; System.out.println("Pesqindarka = " + (money / 5)); money = money % 5; System.out.println("Qindarka = " + money); } } }
Operatort logjik
import javax.swing.*; /** Llogarit sasin e metelikeve * Input: dy numra t dhn n rreshtin komandues sasia e eurove dhe sasia e centve * * Output: sasit e metelikve */ public class MakeChange { public static void main (String[] args) { int euros = new Integer(args[0]).intValue(); int cents = new Integer(args[1]).intValue(); if ( (euros < 0) || (cents < 0) || (cents > 99) ) { JOptionPane.showMessageDialog(null, "Gabim n hyrje " + euros + " " + cents); }
else { int money = (euros * 100) + cents; System.out.println("Gjysma = " + (money / 50)); money = money % 50; System.out.println("Njzetqindarka = " + (money / 20)); money = money % 20; System.out.println("Dhjetqindarka = " + (money / 10)); money = money % 10; System.out.println("Pesqindarka = " + (money / 5)); money = money % 5; System.out.println("Qindarka = " + money); } } }
Operator logjik: operator i cili mundson t shkruhen dy krahasime n nj shprehje t tipit boolean.
Operatori Semantika Konjuksioni dhe:
true && true => true true && false => false false && SH2 => false Disjunksioni ose: false || false => false false || true => true true || SH2 => true Negacioni jo: !true => false !false => true
SH1 || SH2
!SH
Operatort logjik kan precedenc m t ult sesa operatort relacional; p.sh., n versionin e fundit t aplikacionit MakeChange kemi mundur t shkruajm
if ( euros < 0 || cents < 0 || cents > 99 )
Prjashtim (exception): gabim i cili ndodh gjat ekzekutimit t nj programi. Zakonisht, prjashtimi ndalon programin (themi se sht hedhur nj prjashtim).
Prjashtim (exception): gabim i cili ndodh gjat ekzekutimit t nj programi. Zakonisht, prjashtimi ndalon programin (themi se sht hedhur nj prjashtim). Hedhja eksplicite e nj prjashtimi mund t bhet me an t strukturs kontrolluese:
throw new RuntimeException(MESAZHI);
Urdhri switch
N disa raste, urdhri switch sht nj alternativ pr urdhrat if-else t vendosur prbrenda. Sintaksa:
switch (SHPREHJA) { case VLERA1 : { URDHRAT1; break; } case VLERA2 : { URDHRAT2; break; } ... case VLERAn : { URDHRATn; break; } default : { URDHRATn+1; } }
Disenjimi i modelit
Konstruktori:
BankAccount(int amount)
inicializon konton
Metodat:
boolean deposit(int amount)
deponon sasin e dhn n konton; kthen true pr sukses dhe false pr mossukses trheq sasin e dhn nga kontoja; kthen true pr sukses dhe false pr mossukses kthen balansin e kontos
int getBalance()
import javax.swing.*; /** Modelon nj konto. */ public class BankAccount { private int balance; // invariant: balance >= 0 /** Konstruktori. * @param amount - balansi inicial */ public BankAccount(int amount) { if ( amount >= 0 ) { balance = amount; } else { balance = 0; } }
/** Deponon t holla n konto. * @param amount - sasia pr deponim * @return true nse trheqja e suksesshme, false prndryshe */ public boolean deposit(int amount) { boolean result = false; if ( amount < 0 ) { JOptionPane.showMessageDialog(null, "Gabim: depositi " + amount); } else { balance = balance + amount; result = true; } return result; }
/** Trheq t holla nga kontoja. * @param amount - sasia pr trheqje * @return true nse trheqja e suksesshme, false prndryshe */ public boolean withdraw(int amount) { boolean result = false; if ( amount < 0 ) { JOptionPane.showMessageDialog(null, "Gabim: trheqja " + amount); } else if ( amount > balance ) { JOptionPane.showMessageDialog(null, "Gabim: trheqja tejkalon balansin"); } else { balance = balance - amount; result = true; } return result; }
/** getBalance kthen balansin vijues * @return balansi */ public int getBalance() { return balance; } }
AccountManager main(...)
AccountController processTransactions()
BankReader BankAccount char readCommand() int readAmount() BankWriter showTransaction(String message, int amount) showTransaction(String message) boolean deposit(int amount) boolean withdraw(int amount) int getBalance()
View komponentet
Metodat:
char readCommand(String message) int readAmount()
lexon linj t re komanduese; kthen karakterin e par t linjs kthen vlern numerike nga linja e lexuar s fundi
Konstruktori:
BankWriter(String title, BankAccount a)
Metodat:
showTransaction(String message, int amount) showTransaction(String message)
import javax.swing.*; /** Lexon transakcione bankiere. */ public class BankReader { private String inputLine; /** Konstruktori. */ public BankReader() { inputLine = ""; } /** Lexon linj t re komanduese. * @param message - prompti pr shfrytzuesin * @return karakterin e par t komands */ public char readCommand(String message) { inputLine = JOptionPane.showInputDialog(message).trim().toUpperCase(); return inputLine.charAt(0); }
/** Kthen vlern numerike nga linja komanduese. * @return vlera e konvertuar n cent */ public int readAmount() { int answer = 0; String s = inputLine.substring(1, inputLine.length()); s = s.trim(); if ( s.length() > 0) { double money = new Double(s).doubleValue(); answer = (int)(money * 100); } else { JOptionPane.showMessageDialog(null, "Gabim: transakcion pa vler"); } return answer; } }
import javax.swing.*; import java.awt.*; import java.text.*; /** Shkruan transakcione bankiere. */ public class BankWriter extends JPanel { private int width = 300; private int height = 200; private BankAccount account; private String lastTransaction = "";
/** Konstruktori. * @param title - titulli i dritares grafike * @param a - kontoja bankiere q afishohet */ public BankWriter(String title, BankAccount a) { account = a; JFrame f = new JFrame(); f.getContentPane().add(this); f.setTitle(title); f.setSize(width, height); f.setBackground(Color.white); f.setVisible(true); }
public void paintComponent(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, width, height); g.setColor(Color.black); int textMargin = 50; int textBaseline = 50; g.drawString(lastTransaction, textMargin, textBaseline); g.drawString("Balanci vijues = " + unconvert(account.getBalance()) + " E", textMargin, textBaseline + 20); } /** Reformaton centt n euro dhe cent. */ private String unconvert(int amount) { double money = amount / 100.0; return new DecimalFormat("0.00").format(money); }
/** Afishon transakcion. * @param message - transakcioni * @param amount - sasia e transakcionit */ public void showTransaction(String message, int amount) { lastTransaction = message + " " + unconvert(amount) + " E"; this.repaint(); } /** Afishon transakcion. * @param message - transakcioni */ public void showTransaction(String message) { lastTransaction = message; this.repaint(); } }
Konstruktimi i kontrollerit
Algoritmi i kontrollerit pr procesimin e nj transakcioni: 1. Lexo komandn. 2. N qoft se komanda sht M, ather prfundo procesimin. 3. Prndryshe: (a) N rast se komanda sht D, lexo sasin e depositit, kryej depositin dhe asho transakcionin. (b) N rast se komanda sht T, lexo sasin e trheqjes, kryej trheqjen dhe asho transakcionin. (c) Prndryshe, komanda sht jolegale, prandaj ankohu.
AccountManager main(...)
AccountController processTransactions()
1
BankReader
1 2
BankAccount
char readCommand() int readAmount() BankWriter showTransaction(String message, int amount) showTransaction(String message) boolean deposit(int amount)
/** Starton aplikacionin pr menagjim kontosh. */ public class AccountManager2 { public static void main(String[] args) { BankReader reader = new BankReader(); BankAccount primaryAccount = new BankAccount(0); BankWriter primaryWriter = new BankWriter("Kontoja primare", primaryAccount); BankAccount secondaryAccount = new BankAccount(0); BankWriter secondaryWriter = new BankWriter("Kontoja sekondare", secondaryAccount); AccountController2 controller = new AccountController2(reader, primaryWriter, primaryAccount secondaryWriter, secondaryAccount); controller.processTransactions(); } }