You are on page 1of 8

SEMINAR 9

Ushtrimi 1
Nje shembull qe ilustron disa nga methodat e nje file.
Zgjidhje

import java.io.*;

public class Shembull {

public static void main(String arg[]){

File f=new File("prova.txt");


System.out.println("exist() kthen: "+f.exists());
System.out.println("canRead() kthen: "+f.canRead());
System.out.println("length() kthen: "+f.length());
System.out.println("getAbsolutePath() kthen: "+f.getAbsolutePath());
}
}

Ushtrimi 2
Ndertoni nje program qe heq hapesirat e teperta boshe (rishkruan filen duke mbajtur nje hapesire pas cdo fjale) midis
fjaleve ne nje file me nje path te dhene.

Zgjidhje

import java.io.*;
import java.util.*;

public class HiqHapesirat {


public static void main(String args[]) throws FileNotFoundException {

Scanner input = new Scanner(new File("C:/Desktop/fjalet.txt"));


PrintStream dalja = new PrintStream(new File("C:/Desktop/dalje.txt"));

while (input.hasNextLine()) {
String text = input.nextLine();
rregullo(text, dalja);
rregullo(text, System.out);
}
}

public static void rregullo(String text, PrintStream output) {


Scanner data = new Scanner(text);

if (data.hasNext()) {
output.print(data.next());
while (data.hasNext()) {
output.print(" " + data.next());
}
SEMINAR 9

}
output.println();
}

Ushtrimi 3
Shkruani nje program qe lexon nga nje skedar dhe kthen numrin e fjaleve ne te.

Zgjidhje

import java.io.*;
import java.util.Scanner;

public class NrFjaleve {

public static void main(String args[]) throws FileNotFoundException{

File f=new File("prova.txt");


Scanner in=new Scanner(f);
int nr=0;

while(in.hasNext()){
String fjala=in.next();
nr++;
}

System.out.println(nr);

}
}

Ushtrimi 4
Ndertoni nje program qe lexon nga nje file te dhena per oret e punes se cdo punonjesi dhe afishon emrin dhe totalin
e oreve per secalin prej tyre. Te dhenat ne file jane te organizuar si me poshte:
id emri Oret
100 Besa 7.5 8.5 10.25 8
203 Andi 10.5 11.5 12.5 4

Zgjidhje

import java.io.*;
import java.util.Scanner;

public class OrePune {


public static void main(String args[]) throws FileNotFoundException{
SEMINAR 9

Scanner in=new Scanner(new File("oret.txt"));


while(in.hasNext()){
String text=in.nextLine();
procesLine(text);
}
}

public static void procesLine(String t){


Scanner data=new Scanner(t);
int id=data.nextInt();
String emri=data.next();
double shuma=0.0;

while(data.hasNextDouble()){
shuma+=data.nextDouble();
}

System.out.println("Oret nga "+emri+" #id "+id+"="+shuma);

}
}

Ushtrimi 5

Shkruani nje program qe gjeneron elementet e Serise se Fibonacit dhe i shkruan ato ne nje file. Programi duhet te
lexoj numrin e qelizave qe duhet gjeneruar nga nje file te dhene. Krijoni nje klase qe te testoni programin tuaj.

Zgjidhje

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

public class FibTest {


public static void main(String args[]) throws FileNotFoundException{
Scanner input = new Scanner(new File("C:/Users/iva.tarelli/Desktop/numriqelizave.txt"));
PrintStream dalja = new PrintStream(new
File("C:/Users/iva.tarelli/Desktop/rezultati.txt"));
int nr = 0;
if (input.hasNextLine()) {
nr = input.nextInt();

new FibSek(nr).printo(dalja);
}
}
SEMINAR 9

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class FibSek {

private int fibs[];


private final int minFibs=8;
private final int maxFibs=64;

public FibSek() throws FileNotFoundException{

gjeneroFibSek(maxFibs);

}
public FibSek(int n){
if(n<minFibs)
gjeneroFibSek(minFibs);
else if(n>maxFibs)
gjeneroFibSek(maxFibs);
else gjeneroFibSek(n);
}
public void printo(PrintStream f){
for(int i=0; i<fibs.length; i++)
f.println(fibs[i]);
}
private void gjeneroFibSek(int n){
fibs=new int[n];
fibs[0]=fibs[1]=1;
for(int i=2; i<n; i++){
fibs[i]=fibs[i-1]+fibs[i-2];
}

Ushtrimi 6

Te merret rrezja e rrethit si argument nga rreshti i komandes dhe te printohet siperfaqja e tij. Te trajtohen perjashtimet qe

Zgjidhje

public class LlogaritSiperfaqen {

public static void main(String[] args) {


try{
double x = Double.parseDouble(args[0]);
SEMINAR 9

if (x<=0) throw new IllegalArgumentException ("x<0");


System.out.println(Math.PI * x * x);
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Jep nje argument nga rreshti i komandes");
}
catch (NumberFormatException e){
System.out.println("Duhet te jepnit nje numer double");
}
catch (IllegalArgumentException e){
System.out.println("Argumenti " + e.getMessage());
} System.out.println("Programi!");
}
}

Ushtrimi 7

Cfare do te jete output i programit me poshte? Shpjegoni

public class TestException {

public static void main(String[] args) {


try {
System.out.println(shuma (new int []{1, 2, 3, 4, 5}));
}
catch (Exception e){ e.printStackTrace();
System.out.println (e.getMessage());
System.out.println (e.toString());
System.out.println ("Informacion nga metoda getStackTrace");
StackTraceElement el [] = e.getStackTrace();
for (int i = 0; i < el.length; i++){
System.out.print ("metoda: " + el[i].getMethodName());
System.out.print (el[i].getLineNumber());
System.out.println (el[i].getClassName());
}
}
}
public static int shuma (int [] list){
int rezultati = 0;
for (int i = 0; i <= list.length; i++){
rezultati += list [i];
}
return rezultati;
}
}

Ushtrimi 8

Gjeni outputin e programit:


SEMINAR 9

public class Test {


public static void main(String[] args) {
try {
int value = 30;
if (value < 40)
throw new Exception("vlera eshte shume e vogel");
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
System.out.println("Vazhdimi");
}
}

Detyra

1. Ndertoni nje program qe lexon nga nje file te dhenat per emrin dhe notat e secilit studenti, dhe i afishon ato ne nje
file te ri te rendituara sipas notes mesatare ne rend zbrites. Te dhenat ne file fillestar jane te organizuar psh.

emri Notat
Besa 7.5 8.5 10 8
Andi 10 8.5 7.5 4

2. Cfare do te printohet? Shpjegoni pergjigjen tuaj

public class Test {


public static void main(String[] args) {
try {
method();
System.out.println("After the method call");
}
catch (RuntimeException ex) { System.out.println("RuntimeException
in main");
}
catch (Exception ex) {
System.out.println("Exception in main");
}
}
static void method() throws Exception {
try {
String s ="abc";
System.out.println(s.charAt(3));
}
catch (RuntimeException ex) {
System.out.println("RuntimeException in method()");
}
catch (Exception ex) {
System.out.println("Exception in method()");
}
}
}
SEMINAR 9

3. Cfare tipi Exception do te gjenerojne 6 kodet me poshte?

public class Test { public class Test {


public static void main(String[] args) { public static void main(String[] args) {
System.out.println(1 / 0); Object o = new Object();
} String d = (String)o;
} }
}
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
int[] list = new int[5]; Object o = null;
System.out.println(list[5]); System.out.println(o.toString());
} }
} }
public class Test {
public static void main(String[] args) { public class Test {
String s = "abc"; public static void main(String[] args) {
System.out.println(s.charAt(3)); System.out.println(1.0 / 0);
}
}
SEMINAR 9

pg. 8

You might also like