Exam1 03 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Emri:

Programimi dhe algoritmet Nr. indeksit:


Provimi perfundimtar, Forma: A Drejtimi:
Data:

Per pyetjet 12 shqyrtoni aplikacionin AccountManager, per te cilin jane dhene ne vijim komponentet e
modelit, class BankAccount, dhe te input view, class BankReader.

import javax.swing.*;
/** BankAccount menagjon nje konto */
public class BankAccount
{ private int balance; // invariante: balance >= 0
/** Konstruktori
* @param amount - balansi inicial */
public BankAccount(int amount)
{ if ( amount >= 0 )
{ balance = amount; }
else { balance = 0; }
}
/** deposit deponon te holla ne konto
* @param amount - sasia per deponim */
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;
}
/** withdraw terheq te holla nga kontoja
* @param amount - sasia per terheqje
* @return true nese terheqja e suksesshme, false perndryshe */
public boolean withdraw(int amount)
{ boolean result = false;
if ( amount < 0 )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja " + amount);
}
else if ( amount > balance )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja tejkalon balansin");
}
else { balance = balance - amount;
result = true;
}
return result;
}
/** getBalance kthen balansin vijues
* @return balansi */

1
public int getBalance()
{ return balance; }
}

import javax.swing.*;
/** BankReader lexon transakcione bankare */
public class BankReader
{ private String inputLine;
/** Konstruktori */
public BankReader()
{ inputLine = ""; }
/** readCommand lexon linje te re komanduese
* @param message - prompti per shfrytezuesin
* @return karakterin e pare te komandes */
public char readCommand(String message)
{ inputLine = JOptionPane.showInputDialog(message).trim().toUpperCase();
return inputLine.charAt(0);
}
/** readAmount kthen vleren numerike nga linja komanduese
* @return vlera e konvertuar ne 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 vlere");
}
return answer;
}
}

1. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt, deponon 5000, terheq 3000 dhe pastaj afishon balancin ne dritaren koman-
duese.
2. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt dhe nje BankReader objekt, kerkon nga lexuesi qe te lexoje nje komande, kryen
transakcionin ne BankAccount objektin dhe afishon balansin ne dritaren komanduese.
3. Shkruani nje klase EggWriter e cila gjeneron nje dritare grafike qe afishon nje ve (elipse). Secilen
here qe dritarja EggWriter ikonifikohet dhe deikonifikohet veja ringjyroset ne gjysmen e madhesise se
vete te meparme.
4. Diametrat e Diellit dhe Tokes jane perafersisht 1,392,000 km, perkatesisht 13,000 km. Llogaritni:
Vellimin e Diellit ne kilometra kub.
Heresin e vellimit te Diellit ndaj vellimit te Tokes.
Te dy trupat qiellore te konsiderohen sfera. Afishoni dy vlerat e llogaritura ne dritaren komanduese.
(Udhezim: Vellimi i sferes jepet me formulen 34 r3 , ku r eshte rrezja. Emri ne Java per vleren e
konstantes matematike eshte Math.PI.)
5. Eshte dhene metoda

2
public static double divideNonNegative(double x, double y)
{ double answer;
if (x < 0 || y <= 0)
{ answer = -1;}
else { answer = x / y; }
return answer;
}
Cfare rezultati kthehet nga divideNonNegative(0, 1)
6. Shkruani nje aplikacion, TestInt, i cili implementon algoritmin vijues:
Deklaro nje variabel d te tipit double dhe inicializo ate me vleren 1234.5678.
Nxjerr pjesen e plote te vleres se saj dhe ruaje ne nje variabel te tipit int.
Nxjerr kater shifrat e para te pjeses thyesore te vleres se d dhe ruaje ne nje variabel tjeter te tipit
int.
Afisho vleren e variables d duke afishuar dy vlerat e ruajtura si numra te plote.
7. Jane dhene klasat

import java.awt.*;
import javax.swing.*;
/** MyPanel krijon nje panel me ngjyra */
public class MyPanel extends JPanel
{ /** paintComponent vizaton mbi panelin
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ g.setColor(Color.red);
int left = 105;
int top = 70;
int width = 90;
int height = 60;
g.drawRect(left, top, width, height);
int diameter = 40;
g.fillOval(left + width - diameter, top, diameter, diameter);
}
}

import java.awt.*;
import javax.swing.*;
/** FrameTest afishon nje dritare me ngjyra */
public class FrameTest
{ public static void main(String[] args)
{ JFrame myFrame = new JFrame();
myFrame.getContentPane().add(new MyPanel());
myFrame.setTitle("Vizatuesi im i kornizave");
myFrame.setBackground(Color.white);
int width = 300;
int height = 200;
myFrame.setSize(width, height);
myFrame.setVisible(true);
}
}
Modifikoni klasen MyPanel ashtu qe elipsa te ngjyroset ne kendin e djathte te poshtem te drejtkendeshit
te vizatuar.

3
8. Supozojme se int x = 1; dhe double y = 1.5; Llogaritni vleren e shprehjes
(x > y) == (x == y)
9. Shkruani vargun vijues te urdherave ne Java.
Nje variabel, myMoney, inicializohet ne 32.
myMoney zvogelohet per 8.
myMoney katerfishohet.
myMoney resetohet ne 1.
Vlera e myMoney i dergohet ne nje println mesazh objektit System.out.
10. Shkruani nje metode e cila kenaq specifikacionin vijues:

public class Teacher


{ /** scoreToGrade konverton piket ne note
* @param score - numri i pikeve, ne intervalin 0...100
* @return nota sipas skemes vijuese:
* 100...90 = "(10)"; 89...80 = "(9)"; 79...70 -> "(8)";
* 69...60 = "(7)"; 59...50 = "(6)"; 49...0 -> "(5)" */
public String scoreToGrade(int score)
{ // ...
}
}

Shkruani nje aplikacion, TeacherTest, i cili kerkon nga shfrytezuesi te fuse nje numer pikesh dhe
shfrytezon class Teacher per te afishuar noten perkatese.

4
Celesi i provimit A
Per pyetjet 12 shqyrtoni aplikacionin AccountManager, per te cilin jane dhene ne vijim komponentet e
modelit, class BankAccount, dhe te input view, class BankReader.

import javax.swing.*;
/** BankAccount menagjon nje konto */
public class BankAccount
{ private int balance; // invariante: balance >= 0
/** Konstruktori
* @param amount - balansi inicial */
public BankAccount(int amount)
{ if ( amount >= 0 )
{ balance = amount; }
else { balance = 0; }
}
/** deposit deponon te holla ne konto
* @param amount - sasia per deponim */
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;
}
/** withdraw terheq te holla nga kontoja
* @param amount - sasia per terheqje
* @return true nese terheqja e suksesshme, false perndryshe */
public boolean withdraw(int amount)
{ boolean result = false;
if ( amount < 0 )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja " + amount);
}
else if ( amount > balance )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja tejkalon balansin");
}
else { balance = balance - amount;
result = true;
}
return result;
}
/** getBalance kthen balansin vijues
* @return balansi */
public int getBalance()
{ return balance; }
}

1
import javax.swing.*;
/** BankReader lexon transakcione bankare */
public class BankReader
{ private String inputLine;
/** Konstruktori */
public BankReader()
{ inputLine = ""; }
/** readCommand lexon linje te re komanduese
* @param message - prompti per shfrytezuesin
* @return karakterin e pare te komandes */
public char readCommand(String message)
{ inputLine = JOptionPane.showInputDialog(message).trim().toUpperCase();
return inputLine.charAt(0);
}
/** readAmount kthen vleren numerike nga linja komanduese
* @return vlera e konvertuar ne 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 vlere");
}
return answer;
}
}

1. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt, deponon 5000, terheq 3000 dhe pastaj afishon balancin ne dritaren komanduese.
Pergjegjja:

public class TestController


{ public static void main(String[] args)
{ BankAccount account = new BankAccount(0);
account.deposit(5000);
account.withdraw(3000);
int balance = account.getBalance();
System.out.println("Balansi: " + balance);
}
}
2. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt dhe nje BankReader objekt, kerkon nga lexuesi qe te lexoje nje komande, kryen
transakcionin ne BankAccount objektin dhe afishon balansin ne dritaren komanduese.
Pergjegjja:

import java.text.*;
public class TestController2
{ public static void main(String[] args)

2
{ BankReader reader = new BankReader();
BankAccount account = new BankAccount(0);
char c = reader.readCommand("Komanda:");
switch (c)
{ case Q : break;
case D :
{ int amount = reader.readAmount();
account.deposit(amount);
break;
}
case T :
{ int amount = reader.readAmount();
account.withdraw(amount);
break;
}
default : System.out.println("Gabim: komanda " + c);
}
double d = account.getBalance()/100.0;
System.out.println("Balansi: "
+ new DecimalFormat("0.00").format(d));
}
}
3. Shkruani nje klase EggWriter e cila gjeneron nje dritare grafike qe afishon nje ve (elipse). Secilen
here qe dritarja EggWriter ikonifikohet dhe deikonifikohet veja ringjyroset ne gjysmen e madhesise se
vete te meparme.
Pergjegjja:

import javax.swing.*;
import java.awt.*;
/** EggWriter vizaton nje ve */
public class EggWriter extends JPanel
{ private int width;
private int height;
/** Konstruktori */
public EggWriter()
{ width = 300;
height = 2 * width / 3;
JFrame f = new JFrame();
f.getContentPane().add(this);
f.setTitle("Vizatues vezesh");
f.setBackground(Color.white);
f.setSize(width, height);
f.setVisible(true);
}
/** paintComponent vizaton vene
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ int eggHeight = 2 * width / 3;
g.setColor(Color.pink);
g.fillOval(0, height - eggHeight, width, eggHeight);
width = width / 2;
}
/** main per testim */

3
public static void main(String[] args)
{ new EggWriter(); }
}
4. Diametrat e Diellit dhe Tokes jane perafersisht 1,392,000 km, perkatesisht 13,000 km. Llogaritni:
Vellimin e Diellit ne kilometra kub.
Heresin e vellimit te Diellit ndaj vellimit te Tokes.
Te dy trupat qiellore te konsiderohen sfera. Afishoni dy vlerat e llogaritura ne dritaren komanduese.
(Udhezim: Vellimi i sferes jepet me formulen 43 r3 , ku r eshte rrezja. Emri ne Java per vleren e
konstantes matematike eshte Math.PI.)

Pergjegjja:

public class SunEarth


{ public static void main(String[] args)
{ int SolarDiameter = 1392000;
int EarthDiameter = 13000;
double SolarVolume = 4 * Math.PI
* Math.pow(SolarDiameter / 2, 3) / 3;
System.out.println("Vellimi i diellit "
+ SolarVolume + " km3");
System.out.println("Heresi "
+ Math.pow(SolarDiameter / EarthDiameter, 3));
}
}
5. Eshte dhene metoda

public static double divideNonNegative(double x, double y)


{ double answer;
if (x < 0 || y <= 0)
{ answer = -1;}
else { answer = x / y; }
return answer;
}

Cfare rezultati kthehet nga divideNonNegative(0, 1)


Pergjegjja: 0.0
6. Shkruani nje aplikacion, TestInt, i cili implementon algoritmin vijues:
Deklaro nje variabel d te tipit double dhe inicializo ate me vleren 1234.5678.
Nxjerr pjesen e plote te vleres se saj dhe ruaje ne nje variabel te tipit int.
Nxjerr kater shifrat e para te pjeses thyesore te vleres se d dhe ruaje ne nje variabel tjeter te tipit
int.
Afisho vleren e variables d duke afishuar dy vlerat e ruajtura si numra te plote.

Pergjegjja:

4
public class TestInt
{ public static void main(String[] args)
{ double d = 1234.5678
int i1 = (int) d;
d = d - i1; // d -= i1;
d = d * 10000; // d *= 10000;
int i2 = (int) d;
System.out.println(i1 + "." + i2);
}
}

7. Jane dhene klasat

import java.awt.*;
import javax.swing.*;
/** MyPanel krijon nje panel me ngjyra */
public class MyPanel extends JPanel
{ /** paintComponent vizaton mbi panelin
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ g.setColor(Color.red);
int left = 105;
int top = 70;
int width = 90;
int height = 60;
g.drawRect(left, top, width, height);
int diameter = 40;
g.fillOval(left + width - diameter, top, diameter, diameter);
}
}

import java.awt.*;
import javax.swing.*;
/** FrameTest afishon nje dritare me ngjyra */
public class FrameTest
{ public static void main(String[] args)
{ JFrame myFrame = new JFrame();
myFrame.getContentPane().add(new MyPanel());
myFrame.setTitle("Vizatuesi im i kornizave");
myFrame.setBackground(Color.white);
int width = 300;
int height = 200;
myFrame.setSize(width, height);
myFrame.setVisible(true);
}
}

Modifikoni klasen MyPanel ashtu qe elipsa te ngjyroset ne kendin e djathte te poshtem te drejtkendeshit
te vizatuar.
Pergjegjja:

import java.awt.*;
import javax.swing.*;
/** MyPanel krijon nje panel me ngjyra */

5
public class MyPanel extends JPanel
{ /** paintComponent vizaton mbi panelin
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ g.setColor(Color.red);
int left = 105;
int top = 70;
int width = 90;
int height = 60;
g.drawRect(left, top, width, height);
int diameter = 40;
g.fillOval(left + width - diameter, top + height - diameter,
diameter, diameter);
}
}
8. Supozojme se int x = 1; dhe double y = 1.5; Llogaritni vleren e shprehjes
(x > y) == (x == y)
Pergjegjja: true
9. Shkruani vargun vijues te urdherave ne Java.
Nje variabel, myMoney, inicializohet ne 32.
myMoney zvogelohet per 8.
myMoney katerfishohet.
myMoney resetohet ne 1.
Vlera e myMoney i dergohet ne nje println mesazh objektit System.out.

Pergjegjja:

int myMoney = 32;


myMoney = myMoney - 8; // myMoney -=8;
myMoney = myMoney * 4; // myMoney *=4;
myMoney = 1;
System.out.println(myMoney);

10. Shkruani nje metode e cila kenaq specifikacionin vijues:

public class Teacher


{ /** scoreToGrade konverton piket ne note
* @param score - numri i pikeve, ne intervalin 0...100
* @return nota sipas skemes vijuese:
* 100...90 = "(10)"; 89...80 = "(9)"; 79...70 -> "(8)";
* 69...60 = "(7)"; 59...50 = "(6)"; 49...0 -> "(5)" */
public String scoreToGrade(int score)
{ // ...
}
}

Shkruani nje aplikacion, TeacherTest, i cili kerkon nga shfrytezuesi te fuse nje numer pikesh dhe
shfrytezon class Teacher per te afishuar noten perkatese.
Pergjegjja:

6
public class Teacher
{ /** Konverton piket ne note
* @param score - numri i pikeve, ne intervalin 0...100
* @return nota sipas skemes vijuese:
* 100...90 -> "(10)"; 89...80 -> "(9)"; 79...70 -> "(8)";
* 69...60 -> "(7)"; 59...50 -> "(6)"; 49...0 -> "(5)" */
public String scoreToGrade(int score)
{ String grade = "";
if (score <= 100 && score >= 90)
{ grade = "(10)"; }
else if (score <= 89 && score >= 80)
{ grade = "(9)"; }
else if (score <= 79 && score >= 70)
{ grade = "(8)"; }
else if (score <= 69 && score >= 60)
{ grade = "(7)"; }
else if (score <= 59 && score >= 50)
{ grade = "(6)"; }
else if (score <= 49 && score >= 0)
{ grade = "(5)"; }
else { grade = "Error: score " + score; }
return grade;
}
}

import javax.swing.*;
public class TeacherTest
{ public static void main(String[] args)
{ String input = JOptionPane.showInputDialog("Numri i pikeve:");
int score = new Integer(input).intValue();
Teacher t = new Teacher();
String grade = t.scoreToGrade(score);
JOptionPane.showMessageDialog(null, "Per " + score
+ " pike, nota eshte " + grade);
}
}

7
Emri:
Programimi dhe algoritmet Nr. indeksit:
Provimi perfundimtar, Forma: B Drejtimi:
Data:

1. Shkruani nje klase EggWriter e cila gjeneron nje dritare grafike qe afishon nje ve (elipse). Secilen
here qe dritarja EggWriter ikonifikohet dhe deikonifikohet veja ringjyroset ne gjysmen e madhesise se
vete te meparme.
2. Shkruani vargun vijues te urdherave ne Java.
Nje variabel, myMoney, inicializohet ne 32.
myMoney zvogelohet per 7.
myMoney trefishohet.
myMoney resetohet ne 1.
Vlera e myMoney i dergohet ne nje println mesazh objektit System.out.
3. Diametrat e Diellit dhe Tokes jane perafersisht 1,392,000 km, perkatesisht 13,000 km. Llogaritni:
Vellimin e Tokes ne kilometra kub.
Heresin e vellimit te Diellit ndaj vellimit te Tokes.
Te dy trupat qiellore te konsiderohen sfera. Afishoni dy vlerat e llogaritura ne dritaren komanduese.
(Udhezim: Vellimi i sferes jepet me formulen 43 r3 , ku r eshte rrezja. Emri ne Java per vleren e
konstantes matematike eshte Math.PI.)
4. Shkruani nje metode e cila kenaq specifikacionin vijues:

public class Teacher


{ /** scoreToGrade konverton piket ne note
* @param score - numri i pikeve, ne intervalin 0...100
* @return nota sipas skemes vijuese:
* 100...90 = "(10)"; 89...80 = "(9)"; 79...70 -> "(8)";
* 69...60 = "(7)"; 59...50 = "(6)"; 49...0 -> "(5)" */
public String scoreToGrade(int score)
{ // ...
}
}

Shkruani nje aplikacion, TeacherTest, i cili kerkon nga shfrytezuesi te fuse nje numer pikesh dhe
shfrytezon class Teacher per te afishuar noten perkatese.
5. Jane dhene klasat

import java.awt.*;
import javax.swing.*;
/** MyPanel krijon nje panel me ngjyra */
public class MyPanel extends JPanel
{ /** paintComponent vizaton mbi panelin
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ g.setColor(Color.red);
int left = 105;
int top = 70;

1
int width = 90;
int height = 60;
g.drawRect(left, top, width, height);
int diameter = 40;
g.fillOval(left + width - diameter, top, diameter, diameter);
}
}

import java.awt.*;
import javax.swing.*;
/** FrameTest afishon nje dritare me ngjyra */
public class FrameTest
{ public static void main(String[] args)
{ JFrame myFrame = new JFrame();
myFrame.getContentPane().add(new MyPanel());
myFrame.setTitle("Vizatuesi im i kornizave");
myFrame.setBackground(Color.white);
int width = 300;
int height = 200;
myFrame.setSize(width, height);
myFrame.setVisible(true);
}
}

Modifikoni klasen MyPanel ashtu qe elipsa te ngjyroset ne madhesi 80 me 40.


6. Supozojme se int x = 1; dhe double y = 1.5; Llogaritni vleren e shprehjes
x > 0 && x < 10 && (y == 1)

Per pyetjet 78 shqyrtoni aplikacionin AccountManager, per te cilin jane dhene ne vijim komponentet e
modelit, class BankAccount, dhe te input view, class BankReader.

import javax.swing.*;
/** BankAccount menagjon nje konto */
public class BankAccount
{ private int balance; // invariante: balance >= 0
/** Konstruktori
* @param amount - balansi inicial */
public BankAccount(int amount)
{ if ( amount >= 0 )
{ balance = amount; }
else { balance = 0; }
}
/** deposit deponon te holla ne konto
* @param amount - sasia per deponim */
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;

2
}
/** withdraw terheq te holla nga kontoja
* @param amount - sasia per terheqje
* @return true nese terheqja e suksesshme, false perndryshe */
public boolean withdraw(int amount)
{ boolean result = false;
if ( amount < 0 )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja " + amount);
}
else if ( amount > balance )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja tejkalon balansin");
}
else { balance = balance - amount;
result = true;
}
return result;
}
/** getBalance kthen balansin vijues
* @return balansi */
public int getBalance()
{ return balance; }
}

import javax.swing.*;
/** BankReader lexon transakcione bankare */
public class BankReader
{ private String inputLine;
/** Konstruktori */
public BankReader()
{ inputLine = ""; }
/** readCommand lexon linje te re komanduese
* @param message - prompti per shfrytezuesin
* @return karakterin e pare te komandes */
public char readCommand(String message)
{ inputLine = JOptionPane.showInputDialog(message).trim().toUpperCase();
return inputLine.charAt(0);
}
/** readAmount kthen vleren numerike nga linja komanduese
* @return vlera e konvertuar ne 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 vlere");
}
return answer;
}

3
}

7. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt, deponon 6000, terheq 3000 dhe pastaj afishon balancin ne dritaren koman-
duese.
8. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt dhe nje BankReader objekt, kerkon nga lexuesi qe te lexoje nje komande, kryen
transakcionin ne BankAccount objektin dhe afishon balansin ne dritaren komanduese.
9. Shkruani nje aplikacion, TestInt, i cili implementon algoritmin vijues:
Deklaro nje variabel d te tipit double dhe inicializo ate me vleren 9876.5432.
Nxjerr pjesen e plote te vleres se saj dhe ruaje ne nje variabel te tipit int.
Nxjerr kater shifrat e para te pjeses thyesore te vleres se d dhe ruaje ne nje variabel tjeter te tipit
int.
Afisho vleren e variables d duke afishuar dy vlerat e ruajtura si numra te plote.
10. Eshte dhene metoda

public static double divideNonNegative(double x, double y)


{ double answer;
if (x < 0 || y <= 0)
{ answer = -1;}
else { answer = x / y; }
return answer;
}

Cfare rezultati kthehet nga divideNonNegative(1, 0)

4
Celesi i provimit B
1. Shkruani nje klase EggWriter e cila gjeneron nje dritare grafike qe afishon nje ve (elipse). Secilen
here qe dritarja EggWriter ikonifikohet dhe deikonifikohet veja ringjyroset ne gjysmen e madhesise se
vete te meparme.
Pergjegjja:

import javax.swing.*;
import java.awt.*;
/** EggWriter vizaton nje ve */
public class EggWriter extends JPanel
{ private int width;
private int height;
/** Konstruktori */
public EggWriter()
{ width = 300;
height = 2 * width / 3;
JFrame f = new JFrame();
f.getContentPane().add(this);
f.setTitle("Vizatues vezesh");
f.setBackground(Color.white);
f.setSize(width, height);
f.setVisible(true);
}
/** paintComponent vizaton vene
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ int eggHeight = 2 * width / 3;
g.setColor(Color.pink);
g.fillOval(0, height - eggHeight, width, eggHeight);
width = width / 2;
}
/** main per testim */
public static void main(String[] args)
{ new EggWriter(); }
}
2. Shkruani vargun vijues te urdherave ne Java.
Nje variabel, myMoney, inicializohet ne 32.
myMoney zvogelohet per 7.
myMoney trefishohet.
myMoney resetohet ne 1.
Vlera e myMoney i dergohet ne nje println mesazh objektit System.out.
Pergjegjja:

int myMoney = 32;


myMoney = myMoney - 7; // myMoney -=7;
myMoney = myMoney * 3; // myMoney *=3;
myMoney = 1;
System.out.println(myMoney);

1
3. Diametrat e Diellit dhe Tokes jane perafersisht 1,392,000 km, perkatesisht 13,000 km. Llogaritni:
Vellimin e Tokes ne kilometra kub.
Heresin e vellimit te Diellit ndaj vellimit te Tokes.
Te dy trupat qiellore te konsiderohen sfera. Afishoni dy vlerat e llogaritura ne dritaren komanduese.
(Udhezim: Vellimi i sferes jepet me formulen 34 r3 , ku r eshte rrezja. Emri ne Java per vleren e
konstantes matematike eshte Math.PI.)
Pergjegjja:

public class SunEarth


{ public static void main(String[] args)
{ int SolarDiameter = 1392000;
int EarthDiameter = 13000;
double EarthVolume = 4 * Math.PI
* Math.pow(EarthDiameter / 2, 3) / 3;
System.out.println("Vellimi i tokes "
+ EarthVolume + " km3");
System.out.println("Heresi "
+ Math.pow(SolarDiameter / EarthDiameter, 3));
}
}
4. Shkruani nje metode e cila kenaq specifikacionin vijues:

public class Teacher


{ /** scoreToGrade konverton piket ne note
* @param score - numri i pikeve, ne intervalin 0...100
* @return nota sipas skemes vijuese:
* 100...90 = "(10)"; 89...80 = "(9)"; 79...70 -> "(8)";
* 69...60 = "(7)"; 59...50 = "(6)"; 49...0 -> "(5)" */
public String scoreToGrade(int score)
{ // ...
}
}

Shkruani nje aplikacion, TeacherTest, i cili kerkon nga shfrytezuesi te fuse nje numer pikesh dhe
shfrytezon class Teacher per te afishuar noten perkatese.
Pergjegjja:

public class Teacher


{ /** Konverton piket ne note
* @param score - numri i pikeve, ne intervalin 0...100
* @return nota sipas skemes vijuese:
* 100...90 -> "(10)"; 89...80 -> "(9)"; 79...70 -> "(8)";
* 69...60 -> "(7)"; 59...50 -> "(6)"; 49...0 -> "(5)" */
public String scoreToGrade(int score)
{ String grade = "";
if (score <= 100 && score >= 90)
{ grade = "(10)"; }
else if (score <= 89 && score >= 80)
{ grade = "(9)"; }

2
else if (score <= 79 && score >= 70)
{ grade = "(8)"; }
else if (score <= 69 && score >= 60)
{ grade = "(7)"; }
else if (score <= 59 && score >= 50)
{ grade = "(6)"; }
else if (score <= 49 && score >= 0)
{ grade = "(5)"; }
else { grade = "Error: score " + score; }
return grade;
}
}

import javax.swing.*;
public class TeacherTest
{ public static void main(String[] args)
{ String input = JOptionPane.showInputDialog("Numri i pikeve:");
int score = new Integer(input).intValue();
Teacher t = new Teacher();
String grade = t.scoreToGrade(score);
JOptionPane.showMessageDialog(null, "Per " + score
+ " pike, nota eshte " + grade);
}
}
5. Jane dhene klasat

import java.awt.*;
import javax.swing.*;
/** MyPanel krijon nje panel me ngjyra */
public class MyPanel extends JPanel
{ /** paintComponent vizaton mbi panelin
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ g.setColor(Color.red);
int left = 105;
int top = 70;
int width = 90;
int height = 60;
g.drawRect(left, top, width, height);
int diameter = 40;
g.fillOval(left + width - diameter, top, diameter, diameter);
}
}

import java.awt.*;
import javax.swing.*;
/** FrameTest afishon nje dritare me ngjyra */
public class FrameTest
{ public static void main(String[] args)
{ JFrame myFrame = new JFrame();
myFrame.getContentPane().add(new MyPanel());
myFrame.setTitle("Vizatuesi im i kornizave");
myFrame.setBackground(Color.white);
int width = 300;

3
int height = 200;
myFrame.setSize(width, height);
myFrame.setVisible(true);
}
}

Modifikoni klasen MyPanel ashtu qe elipsa te ngjyroset ne madhesi 80 me 40.


Pergjegjja:

import java.awt.*;
import javax.swing.*;
/** MyPanel krijon nje panel me ngjyra */
public class MyPanel extends JPanel
{ /** paintComponent vizaton mbi panelin
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ g.setColor(Color.red);
int left = 105;
int top = 70;
int width = 90;
int height = 60;
g.drawRect(left, top, width, height);
int horDiameter = 80; int verDiameter = 40;
g.fillOval(left + width - horDiameter, top,
horDiameter, verDiameter);
}
}
6. Supozojme se int x = 1; dhe double y = 1.5; Llogaritni vleren e shprehjes
x > 0 && x < 10 && (y == 1)
Pergjegjja: false

Per pyetjet 78 shqyrtoni aplikacionin AccountManager, per te cilin jane dhene ne vijim komponentet e
modelit, class BankAccount, dhe te input view, class BankReader.

import javax.swing.*;
/** BankAccount menagjon nje konto */
public class BankAccount
{ private int balance; // invariante: balance >= 0
/** Konstruktori
* @param amount - balansi inicial */
public BankAccount(int amount)
{ if ( amount >= 0 )
{ balance = amount; }
else { balance = 0; }
}
/** deposit deponon te holla ne konto
* @param amount - sasia per deponim */
public boolean deposit(int amount)
{ boolean result = false;
if ( amount < 0 )
{ JOptionPane.showMessageDialog(null,
"Gabim: depositi " + amount);

4
}
else { balance = balance + amount;
result = true;
}
return result;
}
/** withdraw terheq te holla nga kontoja
* @param amount - sasia per terheqje
* @return true nese terheqja e suksesshme, false perndryshe */
public boolean withdraw(int amount)
{ boolean result = false;
if ( amount < 0 )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja " + amount);
}
else if ( amount > balance )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja tejkalon balansin");
}
else { balance = balance - amount;
result = true;
}
return result;
}
/** getBalance kthen balansin vijues
* @return balansi */
public int getBalance()
{ return balance; }
}

import javax.swing.*;
/** BankReader lexon transakcione bankare */
public class BankReader
{ private String inputLine;
/** Konstruktori */
public BankReader()
{ inputLine = ""; }
/** readCommand lexon linje te re komanduese
* @param message - prompti per shfrytezuesin
* @return karakterin e pare te komandes */
public char readCommand(String message)
{ inputLine = JOptionPane.showInputDialog(message).trim().toUpperCase();
return inputLine.charAt(0);
}
/** readAmount kthen vleren numerike nga linja komanduese
* @return vlera e konvertuar ne 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);
}

5
else { JOptionPane.showMessageDialog(null,
"Gabim: transakcion pa vlere");
}
return answer;
}
}

7. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt, deponon 6000, terheq 3000 dhe pastaj afishon balancin ne dritaren komanduese.
Pergjegjja:

public class TestController


{ public static void main(String[] args)
{ BankAccount account = new BankAccount(0);
account.deposit(6000);
account.withdraw(3000);
int balance = account.getBalance();
System.out.println("Balansi: " + balance);
}
}
8. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt dhe nje BankReader objekt, kerkon nga lexuesi qe te lexoje nje komande, kryen
transakcionin ne BankAccount objektin dhe afishon balansin ne dritaren komanduese.
Pergjegjja:

import java.text.*;
public class TestController2
{ public static void main(String[] args)
{ BankReader reader = new BankReader();
BankAccount account = new BankAccount(0);
char c = reader.readCommand("Komanda:");
switch (c)
{ case Q : break;
case D :
{ int amount = reader.readAmount();
account.deposit(amount);
break;
}
case T :
{ int amount = reader.readAmount();
account.withdraw(amount);
break;
}
default : System.out.println("Gabim: komanda " + c);
}
double d = account.getBalance()/100.0;
System.out.println("Balansi: "
+ new DecimalFormat("0.00").format(d));
}
}
9. Shkruani nje aplikacion, TestInt, i cili implementon algoritmin vijues:

6
Deklaro nje variabel d te tipit double dhe inicializo ate me vleren 9876.5432.
Nxjerr pjesen e plote te vleres se saj dhe ruaje ne nje variabel te tipit int.
Nxjerr kater shifrat e para te pjeses thyesore te vleres se d dhe ruaje ne nje variabel tjeter te tipit
int.
Afisho vleren e variables d duke afishuar dy vlerat e ruajtura si numra te plote.

Pergjegjja:

public class TestInt


{ public static void main(String[] args)
{ double d = 9876.5432
int i1 = (int) d;
d = d - i1; // d -= i1;
d = d * 10000; // d *= 10000;
int i2 = (int) d;
System.out.println(i1 + "." + i2);
}
}

10. Eshte dhene metoda

public static double divideNonNegative(double x, double y)


{ double answer;
if (x < 0 || y <= 0)
{ answer = -1;}
else { answer = x / y; }
return answer;
}

Cfare rezultati kthehet nga divideNonNegative(1, 0)


Pergjegjja: -1.0

7
Emri:
Programimi dhe algoritmet Nr. indeksit:
Provimi perfundimtar, Forma: C Drejtimi:
Data:

Per pyetjet 12 shqyrtoni aplikacionin AccountManager, per te cilin jane dhene ne vijim komponentet e
modelit, class BankAccount, dhe te input view, class BankReader.

import javax.swing.*;
/** BankAccount menagjon nje konto */
public class BankAccount
{ private int balance; // invariante: balance >= 0
/** Konstruktori
* @param amount - balansi inicial */
public BankAccount(int amount)
{ if ( amount >= 0 )
{ balance = amount; }
else { balance = 0; }
}
/** deposit deponon te holla ne konto
* @param amount - sasia per deponim */
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;
}
/** withdraw terheq te holla nga kontoja
* @param amount - sasia per terheqje
* @return true nese terheqja e suksesshme, false perndryshe */
public boolean withdraw(int amount)
{ boolean result = false;
if ( amount < 0 )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja " + amount);
}
else if ( amount > balance )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja tejkalon balansin");
}
else { balance = balance - amount;
result = true;
}
return result;
}
/** getBalance kthen balansin vijues
* @return balansi */

1
public int getBalance()
{ return balance; }
}

import javax.swing.*;
/** BankReader lexon transakcione bankare */
public class BankReader
{ private String inputLine;
/** Konstruktori */
public BankReader()
{ inputLine = ""; }
/** readCommand lexon linje te re komanduese
* @param message - prompti per shfrytezuesin
* @return karakterin e pare te komandes */
public char readCommand(String message)
{ inputLine = JOptionPane.showInputDialog(message).trim().toUpperCase();
return inputLine.charAt(0);
}
/** readAmount kthen vleren numerike nga linja komanduese
* @return vlera e konvertuar ne 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 vlere");
}
return answer;
}
}

1. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt, deponon 7000, terheq 3000 dhe pastaj afishon balancin ne dritaren koman-
duese.
2. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt dhe nje BankReader objekt, kerkon nga lexuesi qe te lexoje nje komande, kryen
transakcionin ne BankAccount objektin dhe afishon balansin ne dritaren komanduese.
3. Shkruani nje klase EggWriter e cila gjeneron nje dritare grafike qe afishon nje ve (elipse). Secilen
here qe dritarja EggWriter ikonifikohet dhe deikonifikohet veja ringjyroset ne gjysmen e madhesise se
vete te meparme.
4. Diametrat e Diellit dhe Tokes jane perafersisht 1,392,000 km, perkatesisht 13,000 km. Llogaritni:
Vellimin e Diellit ne kilometra kub.
Heresin e vellimit te Diellit ndaj vellimit te Tokes.
Te dy trupat qiellore te konsiderohen sfera. Afishoni dy vlerat e llogaritura ne dritaren komanduese.
(Udhezim: Vellimi i sferes jepet me formulen 34 r3 , ku r eshte rrezja. Emri ne Java per vleren e
konstantes matematike eshte Math.PI.)
5. Jane dhene klasat

2
import java.awt.*;
import javax.swing.*;
/** MyPanel krijon nje panel me ngjyra */
public class MyPanel extends JPanel
{ /** paintComponent vizaton mbi panelin
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ g.setColor(Color.red);
int left = 105;
int top = 70;
int width = 90;
int height = 60;
g.drawRect(left, top, width, height);
int diameter = 40;
g.fillOval(left + width - diameter, top, diameter, diameter);
}
}

import java.awt.*;
import javax.swing.*;
/** FrameTest afishon nje dritare me ngjyra */
public class FrameTest
{ public static void main(String[] args)
{ JFrame myFrame = new JFrame();
myFrame.getContentPane().add(new MyPanel());
myFrame.setTitle("Vizatuesi im i kornizave");
myFrame.setBackground(Color.white);
int width = 300;
int height = 200;
myFrame.setSize(width, height);
myFrame.setVisible(true);
}
}

Modifikoni klasen MyPanel ashtu qe drejtkendeshi te vizatohet ne kendin e djathte te poshtem te dritares.

6. Eshte dhene metoda

public static double divideNonNegative(double x, double y)


{ double answer;
if (x < 0 || y <= 0)
{ answer = -1;}
else { answer = x / y; }
return answer;
}

Cfare rezultati kthehet nga divideNonNegative(-1, 2)


7. Shkruani vargun vijues te urdherave ne Java.
Nje variabel, myMoney, inicializohet ne 32.
myMoney zvogelohet per 6.
myMoney dyfishohet.
myMoney resetohet ne 1.

3
Vlera e myMoney i dergohet ne nje println mesazh objektit System.out.
8. Shkruani nje metode e cila kenaq specifikacionin vijues:

public class Teacher


{ /** scoreToGrade konverton piket ne note
* @param score - numri i pikeve, ne intervalin 0...100
* @return nota sipas skemes vijuese:
* 100...90 = "(10)"; 89...80 = "(9)"; 79...70 -> "(8)";
* 69...60 = "(7)"; 59...50 = "(6)"; 49...0 -> "(5)" */
public String scoreToGrade(int score)
{ // ...
}
}

Shkruani nje aplikacion, TeacherTest, i cili kerkon nga shfrytezuesi te fuse nje numer pikesh dhe
shfrytezon class Teacher per te afishuar noten perkatese.
9. Supozojme se int x = 1; dhe double y = 1.5; Llogaritni vleren e shprehjes
(x >= 0 && x <= 1) || (2 <= y)
10. Shkruani nje aplikacion, TestInt, i cili implementon algoritmin vijues:
Deklaro nje variabel d te tipit double dhe inicializo ate me vleren 1234.5678.
Nxjerr pjesen e plote te vleres se saj dhe ruaje ne nje variabel te tipit int.
Nxjerr kater shifrat e para te pjeses thyesore te vleres se d dhe ruaje ne nje variabel tjeter te tipit
int.
Afisho vleren e variables d duke afishuar dy vlerat e ruajtura si numra te plote.

4
Celesi i provimit C
Per pyetjet 12 shqyrtoni aplikacionin AccountManager, per te cilin jane dhene ne vijim komponentet e
modelit, class BankAccount, dhe te input view, class BankReader.

import javax.swing.*;
/** BankAccount menagjon nje konto */
public class BankAccount
{ private int balance; // invariante: balance >= 0
/** Konstruktori
* @param amount - balansi inicial */
public BankAccount(int amount)
{ if ( amount >= 0 )
{ balance = amount; }
else { balance = 0; }
}
/** deposit deponon te holla ne konto
* @param amount - sasia per deponim */
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;
}
/** withdraw terheq te holla nga kontoja
* @param amount - sasia per terheqje
* @return true nese terheqja e suksesshme, false perndryshe */
public boolean withdraw(int amount)
{ boolean result = false;
if ( amount < 0 )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja " + amount);
}
else if ( amount > balance )
{ JOptionPane.showMessageDialog(null,
"Gabim: terheqja tejkalon balansin");
}
else { balance = balance - amount;
result = true;
}
return result;
}
/** getBalance kthen balansin vijues
* @return balansi */
public int getBalance()
{ return balance; }
}

1
import javax.swing.*;
/** BankReader lexon transakcione bankare */
public class BankReader
{ private String inputLine;
/** Konstruktori */
public BankReader()
{ inputLine = ""; }
/** readCommand lexon linje te re komanduese
* @param message - prompti per shfrytezuesin
* @return karakterin e pare te komandes */
public char readCommand(String message)
{ inputLine = JOptionPane.showInputDialog(message).trim().toUpperCase();
return inputLine.charAt(0);
}
/** readAmount kthen vleren numerike nga linja komanduese
* @return vlera e konvertuar ne 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 vlere");
}
return answer;
}
}

1. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt, deponon 7000, terheq 3000 dhe pastaj afishon balancin ne dritaren komanduese.
Pergjegjja:

public class TestController


{ public static void main(String[] args)
{ BankAccount account = new BankAccount(0);
account.deposit(7000);
account.withdraw(3000);
int balance = account.getBalance();
System.out.println("Balansi: " + balance);
}
}
2. Shkruani nje test kontroller, class TestController, i cili permban metoden main, e qe krijon nje
BankAccount objekt dhe nje BankReader objekt, kerkon nga lexuesi qe te lexoje nje komande, kryen
transakcionin ne BankAccount objektin dhe afishon balansin ne dritaren komanduese.
Pergjegjja:

import java.text.*;
public class TestController2
{ public static void main(String[] args)

2
{ BankReader reader = new BankReader();
BankAccount account = new BankAccount(0);
char c = reader.readCommand("Komanda:");
switch (c)
{ case Q : break;
case D :
{ int amount = reader.readAmount();
account.deposit(amount);
break;
}
case T :
{ int amount = reader.readAmount();
account.withdraw(amount);
break;
}
default : System.out.println("Gabim: komanda " + c);
}
double d = account.getBalance()/100.0;
System.out.println("Balansi: "
+ new DecimalFormat("0.00").format(d));
}
}
3. Shkruani nje klase EggWriter e cila gjeneron nje dritare grafike qe afishon nje ve (elipse). Secilen
here qe dritarja EggWriter ikonifikohet dhe deikonifikohet veja ringjyroset ne gjysmen e madhesise se
vete te meparme.
Pergjegjja:

import javax.swing.*;
import java.awt.*;
/** EggWriter vizaton nje ve */
public class EggWriter extends JPanel
{ private int width;
private int height;
/** Konstruktori */
public EggWriter()
{ width = 300;
height = 2 * width / 3;
JFrame f = new JFrame();
f.getContentPane().add(this);
f.setTitle("Vizatues vezesh");
f.setBackground(Color.white);
f.setSize(width, height);
f.setVisible(true);
}
/** paintComponent vizaton vene
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ int eggHeight = 2 * width / 3;
g.setColor(Color.pink);
g.fillOval(0, height - eggHeight, width, eggHeight);
width = width / 2;
}
/** main per testim */

3
public static void main(String[] args)
{ new EggWriter(); }
}
4. Diametrat e Diellit dhe Tokes jane perafersisht 1,392,000 km, perkatesisht 13,000 km. Llogaritni:
Vellimin e Diellit ne kilometra kub.
Heresin e vellimit te Diellit ndaj vellimit te Tokes.
Te dy trupat qiellore te konsiderohen sfera. Afishoni dy vlerat e llogaritura ne dritaren komanduese.
(Udhezim: Vellimi i sferes jepet me formulen 43 r3 , ku r eshte rrezja. Emri ne Java per vleren e
konstantes matematike eshte Math.PI.)
Pergjegjja:

public class SunEarth


{ public static void main(String[] args)
{ int SolarDiameter = 1392000;
int EarthDiameter = 13000;
double SolarVolume = 4 * Math.PI
* Math.pow(SolarDiameter / 2, 3) / 3;
System.out.println("Vellimi i diellit "
+ SolarVolume + " km3");
System.out.println("Heresi "
+ Math.pow(SolarDiameter / EarthDiameter, 3));
}
}
5. Jane dhene klasat

import java.awt.*;
import javax.swing.*;
/** MyPanel krijon nje panel me ngjyra */
public class MyPanel extends JPanel
{ /** paintComponent vizaton mbi panelin
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ g.setColor(Color.red);
int left = 105;
int top = 70;
int width = 90;
int height = 60;
g.drawRect(left, top, width, height);
int diameter = 40;
g.fillOval(left + width - diameter, top, diameter, diameter);
}
}

import java.awt.*;
import javax.swing.*;
/** FrameTest afishon nje dritare me ngjyra */
public class FrameTest
{ public static void main(String[] args)
{ JFrame myFrame = new JFrame();
myFrame.getContentPane().add(new MyPanel());

4
myFrame.setTitle("Vizatuesi im i kornizave");
myFrame.setBackground(Color.white);
int width = 300;
int height = 200;
myFrame.setSize(width, height);
myFrame.setVisible(true);
}
}

Modifikoni klasen MyPanel ashtu qe drejtkendeshi te vizatohet ne kendin e djathte te poshtem te dritares.
Pergjegjja:

import java.awt.*;
import javax.swing.*;
/** MyPanel krijon nje panel me ngjyra */
public class MyPanel extends JPanel
{ /** paintComponent vizaton mbi panelin
* @param g -- penda grafike */
public void paintComponent(Graphics g)
{ g.setColor(Color.red);
int left = 210;
int top = 140;
int width = 90;
int height = 60;
g.drawRect(left, top, width, height);
int diameter = 40;
g.fillOval(left + width - diameter, top,
diameter, diameter);
}
}
6. Eshte dhene metoda

public static double divideNonNegative(double x, double y)


{ double answer;
if (x < 0 || y <= 0)
{ answer = -1;}
else { answer = x / y; }
return answer;
}

Cfare rezultati kthehet nga divideNonNegative(-1, 2)


Pergjegjja: -1.0
7. Shkruani vargun vijues te urdherave ne Java.
Nje variabel, myMoney, inicializohet ne 32.
myMoney zvogelohet per 6.
myMoney dyfishohet.
myMoney resetohet ne 1.
Vlera e myMoney i dergohet ne nje println mesazh objektit System.out.

Pergjegjja:

5
int myMoney = 32;
myMoney = myMoney - 6; // myMoney -=6;
myMoney = myMoney * 2; // myMoney *=2;
myMoney = 1;
System.out.println(myMoney);

8. Shkruani nje metode e cila kenaq specifikacionin vijues:

public class Teacher


{ /** scoreToGrade konverton piket ne note
* @param score - numri i pikeve, ne intervalin 0...100
* @return nota sipas skemes vijuese:
* 100...90 = "(10)"; 89...80 = "(9)"; 79...70 -> "(8)";
* 69...60 = "(7)"; 59...50 = "(6)"; 49...0 -> "(5)" */
public String scoreToGrade(int score)
{ // ...
}
}

Shkruani nje aplikacion, TeacherTest, i cili kerkon nga shfrytezuesi te fuse nje numer pikesh dhe
shfrytezon class Teacher per te afishuar noten perkatese.
Pergjegjja:

public class Teacher


{ /** Konverton piket ne note
* @param score - numri i pikeve, ne intervalin 0...100
* @return nota sipas skemes vijuese:
* 100...90 -> "(10)"; 89...80 -> "(9)"; 79...70 -> "(8)";
* 69...60 -> "(7)"; 59...50 -> "(6)"; 49...0 -> "(5)" */
public String scoreToGrade(int score)
{ String grade = "";
if (score <= 100 && score >= 90)
{ grade = "(10)"; }
else if (score <= 89 && score >= 80)
{ grade = "(9)"; }
else if (score <= 79 && score >= 70)
{ grade = "(8)"; }
else if (score <= 69 && score >= 60)
{ grade = "(7)"; }
else if (score <= 59 && score >= 50)
{ grade = "(6)"; }
else if (score <= 49 && score >= 0)
{ grade = "(5)"; }
else { grade = "Error: score " + score; }
return grade;
}
}

import javax.swing.*;
public class TeacherTest
{ public static void main(String[] args)
{ String input = JOptionPane.showInputDialog("Numri i pikeve:");
int score = new Integer(input).intValue();
Teacher t = new Teacher();

6
String grade = t.scoreToGrade(score);
JOptionPane.showMessageDialog(null, "Per " + score
+ " pike, nota eshte " + grade);
}
}
9. Supozojme se int x = 1; dhe double y = 1.5; Llogaritni vleren e shprehjes
(x >= 0 && x <= 1) || (2 <= y)
Pergjegjja: true
10. Shkruani nje aplikacion, TestInt, i cili implementon algoritmin vijues:
Deklaro nje variabel d te tipit double dhe inicializo ate me vleren 1234.5678.
Nxjerr pjesen e plote te vleres se saj dhe ruaje ne nje variabel te tipit int.
Nxjerr kater shifrat e para te pjeses thyesore te vleres se d dhe ruaje ne nje variabel tjeter te tipit
int.
Afisho vleren e variables d duke afishuar dy vlerat e ruajtura si numra te plote.

Pergjegjja:

public class TestInt


{ public static void main(String[] args)
{ double d = 1234.5678
int i1 = (int) d;
d = d - i1; // d -= i1;
d = d * 10000; // d *= 10000;
int i2 = (int) d;
System.out.println(i1 + "." + i2);
}
}

You might also like