Java

150 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
150
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java

  1. 1. 2.1.prima era sa citesc de la tastatura numere intregi si sa leintroduc intr-un fisier, pe fiecare linie numarul si patratul lui.import java.io.*;import java.util.*;public class probl2_1{ public static void main(String args[]) { try { BufferedReader stdin = new BufferedReader(newInputStreamReader(System.in)); BufferedWriter out = new BufferedWriter( new FileWriter("fisier.txt")); while( true ) { String s = stdin . readLine (); if(s.equals(" exit ") || s.length()==0)break ; int nr=Integer.parseInt(s); int nr_patrat = nr*nr; out.write(nr + " " + nr_patrat); out.newLine(); } stdin.close(); out.close(); } catch ( IOException e) { System.err.println("Eroare de IO"); e.printStackTrace(); } }}
  2. 2. 2.2.clasa Masina, cu atribut pentru culoare, tip si viteza maximaTrebuia sa faci metoda care sa-ti afiseze masina si sa-ti spuna catemasini de culoare alba au fost create problemapublic class Masina{ static int nr_albe=0; String culoare; String tip; int viteza_max; public Masina(String culoare, String tip, int viteza_max) { this.culoare=culoare; this.tip=tip; this.viteza_max=viteza_max; if (culoare.equals("Alb")) nr_albe++; } public String get_tip() { return this.tip; } public int masini_albe() { return nr_albe; }}
  3. 3. 1.2.scrieti clasa elev care are membrii nume ,varsta, categroriapapucului. sa aibe metoda returneaza_nume, metoda de tiparire si unconstructor care primeste 3 argumente si instanteaza obiectulpublic class Elev{ String nume; int varsta; String categ_papuc; public Elev(String nume,int varsta, String cat) { this.nume=nume; this.varsta=varsta; this.categ_papuc=cat; } public String get_nume() { return this.nume; } public String tiparire() { return nume+varsta+categ_papuc; }}
  4. 4. 1.1.scrieti o metoda care citeste date din fisierul f1.txt. afisatidoar numerele cititeimport java.io.*;import java.util.*;public class probl2_1{ public static void main(String args[]) { try { BufferedReader in=new BufferedReader(new FileReader("fisier_citim.txt")); StreamTokenizer st = new StreamTokenizer(in); int tip = st. nextToken(); while(tip != StreamTokenizer.TT_EOF) { switch(tip) { case StreamTokenizer.TT_WORD : break ; case StreamTokenizer.TT_NUMBER : System . out. println (st.nval ); } tip = st. nextToken (); // Trecem la urmatorul atom } in.close(); } catch ( IOException e) { System.err.println(" Eroare de IO"); } }}
  5. 5. 3.1 sa citesc din fis1.txt si sa afisez litere miciimport java.io.*;import java.util.*;import java.lang.*;public class Fdg{ public static void main(String args[]) { try { BufferedReader In = new BufferedReader(newFileReader("fisier_citim.txt")); int c; while((c = in.read() ) != -1) { char g = (char)c; if(isLowerCase(g) ) System.out.println(g); } in.close(); } catch(IOException e) { System.err.println(" Eroare de IO"); } }}
  6. 6. 3.2 clasa cu etichetapublic class Eticheta{ String text_eticheta; String culoare; int x, y; public Eticheta(String text_eticheta, String culoare,int x, int y) { this.text_eticheta = text_eticheta; this.culoare = culoare; this.x = x; this.y = y; } public void modifica_text(String text_nou) { this.text_eticheta = text_nou; } public String tiparire() { return text_eticheta + " " + culoare + " " + x + " " + y; } public boolean compara(Eticheta x) { if((text_eticheta.equals(x.text_eticheta)) && (culoare.equals(x.culoare))) return true; else return false; }}
  7. 7. 6)o clasa haina care are atributele culoare,material,pretsi urmmetode :una care returneaza pretul,una care verifica daca 2 hainesunt egale(au aceasi culoare si acelasi material),si una care afiseaza hainapublic class haina{ String culoare,material; int pret;}public Haina(int pret,String material,String culoare){ this.pret=pret; this.culoare=culoare; this.material=material;}public String get_pret(){ return this.pret; System.out.println(get_pret());}boolean comparatie(Haina ob1, Haina ob2){ if(ob1.culoare == ob2.culoare && ob1.material == ob2.material) return true; else return false;}public String afisare(){ return material+pret+culoare System.out.println(afisare())}
  8. 8. 7)sa se construiasca o clasa masina si sa se sorteze alfabetic dupamarca folosind colectiiimport java.util.*;class Masina implements Comparable{ String marca; int viteza; public Masina(String marca, int viteza) { this.marca = marca; this.viteza = viteza; } public int compareTo(Object x) { Masina c = (Masina)x; return marca.compareTo(c.marca); }}public class testmasina{ public static void main(String args[]) { ArrayList masini=new ArrayList(); Masina m1 = new Masina("Toyota", 200); Masina m2 = new Masina("Bmw", 240); masini.add(m1); masini.add(m2); Collections.sort(masini); ListIterator it = masini.listIterator(); while(it.hasNext()) { Masina car = (Masina)it.next(); System.out.println(car.marca + " " + car.viteza); } }}
  9. 9. 8)sa se scrie o metoda care cauta numele unui student intr-un hashtable,numele studentului se preia de la tastatura.import java.util.*;import java.io.*;class student{ String nume; int nota; student(String nume,int nota) { this.nume=nume; this.nota=nota; } public String afis() { return ("Nume: "+nume+" Nota: "+nota); }}public class studenthash{ public static void main(String[] args) { Hashtable listaStud = new Hashtable(); student s1 = new student("Ionescu", 9); student s2 = new student("Popescu", 10); student s3 = new student("Miron", 8); listaStud.put(s1.nume, s1); listaStud.put(s2.nume, s2); listaStud.put(s3.nume, s3); try { BufferedReader tst = new BufferedReader(new InputStreamReader(System.in)); String s; while((s = tst. readLine ()).length()!= 0) { student caut=(student) listaStud.get(s); System.out.println(caut.afis()); } tst.close(); } catch(IOException e) { System.err.println("Eroare de IO"); } }}

×