├── .gitattributes ├── Laborator 1 ├── .idea │ ├── .gitignore │ ├── modules.xml │ └── misc.xml ├── src │ ├── clasa │ │ ├── Fuel.java │ │ ├── Vehicle.java │ │ └── Car.java │ ├── Main.java │ └── ex1 │ │ ├── PrimitiveMain.java │ │ ├── Wrappers.java │ │ ├── Operators.java │ │ └── Flow.java ├── out │ └── production │ │ └── Laborator 1 │ │ ├── Main.class │ │ ├── clasa │ │ ├── Car.class │ │ ├── Fuel.class │ │ ├── Vehicle.class │ │ └── Car$Engine.class │ │ └── ex1 │ │ ├── Flow.class │ │ ├── Operators.class │ │ ├── Wrappers.class │ │ └── PrimitiveMain.class └── Laborator 1.iml ├── Laborator 2 ├── Laborator2 │ ├── .idea │ │ ├── .gitignore │ │ ├── misc.xml │ │ └── modules.xml │ ├── out │ │ └── production │ │ │ └── Laborator2 │ │ │ ├── Main.class │ │ │ ├── ex2 │ │ │ ├── Test.class │ │ │ └── Ex2_ArrayBidimensional.class │ │ │ ├── ShallowDeep.class │ │ │ ├── PersoanaDeep.class │ │ │ ├── ex3 │ │ │ └── Persoana.class │ │ │ ├── PersoanaShallow.class │ │ │ └── ex1 │ │ │ └── Ex1_ArrayUnidimensional.class │ ├── src │ │ ├── ex2 │ │ │ ├── Ex2_ArrayBidimensional.java │ │ │ └── Test.java │ │ ├── Main.java │ │ ├── ex1 │ │ │ └── Ex1_ArrayUnidimensional.java │ │ ├── ex4 │ │ │ └── Persoana.java │ │ ├── ShallowDeep.java │ │ └── ex3 │ │ │ └── Persoana.java │ └── Laborator2.iml └── Readme.md ├── Laborator 4 ├── Laborator4 │ ├── .idea │ │ ├── .gitignore │ │ ├── misc.xml │ │ └── modules.xml │ ├── out │ │ └── production │ │ │ └── Laborator4 │ │ │ ├── Main.class │ │ │ ├── Room.class │ │ │ ├── House.class │ │ │ └── Owner.class │ ├── Laborator4.iml │ └── src │ │ ├── ex4.java │ │ ├── Owner.java │ │ ├── House.java │ │ ├── ex5.java │ │ ├── Room.java │ │ ├── ex3.java │ │ ├── ex2.java │ │ ├── ex1.java │ │ ├── Main.java │ │ └── ex6.java └── Readme.md ├── Laborator 7 ├── ex2.bmp ├── ex3.bin └── Readme.md ├── Laborator 3 ├── Laborator 3 │ ├── .idea │ │ ├── .gitignore │ │ ├── misc.xml │ │ └── modules.xml │ ├── out │ │ └── production │ │ │ └── Laborator 3 │ │ │ ├── A.class │ │ │ ├── B.class │ │ │ └── Main.class │ ├── Laborator 3.iml │ └── src │ │ ├── ex3.java │ │ ├── ex1.java │ │ ├── ex5.java │ │ ├── ex4.java │ │ ├── test │ │ └── testMain.java │ │ ├── ex2.java │ │ ├── staticClass.java │ │ ├── ex6.java │ │ └── Main.java └── Readme.md ├── Readme.md ├── Laborator 5 ├── Laborator5 │ ├── src │ │ ├── Main.java │ │ ├── ex2.java │ │ ├── ex1.java │ │ ├── ex3.java │ │ ├── ex5.java │ │ ├── ex4.java │ │ ├── ex8.java │ │ ├── ex7.java │ │ ├── ex6.java │ │ ├── CollectionsSupportDemo.java │ │ └── Lab5Demo.java │ ├── .idea │ │ ├── .gitignore │ │ ├── misc.xml │ │ └── modules.xml │ ├── Laborator5.iml │ └── .gitignore └── ReadMe.md ├── Laborator 10 ├── Lab10 │ ├── .idea │ │ ├── .gitignore │ │ ├── modules.xml │ │ └── misc.xml │ ├── src │ │ ├── Main.java │ │ ├── MyThread.java │ │ ├── ClientApp.java │ │ └── ServerApp.java │ ├── Lab10.iml │ └── .gitignore └── Readme.md ├── Laborator 9 ├── bd │ ├── .idea │ │ ├── .gitignore │ │ ├── modules.xml │ │ ├── misc.xml │ │ └── libraries │ │ │ └── mysql_connector_j_9_3_0.xml │ ├── src │ │ ├── Student.java │ │ ├── DbConnection.java │ │ ├── Main.java │ │ └── StudentService.java │ ├── .gitignore │ └── bd.iml └── Readme.md ├── Info ├── grading.md ├── project.md └── setup.md ├── Laborator 6 └── Readme.md └── Laborator 8 └── Readme.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Laborator 1/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Laborator 2/Laborator2/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Laborator 4/Laborator4/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Laborator 7/ex2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 7/ex2.bmp -------------------------------------------------------------------------------- /Laborator 7/ex3.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 7/ex3.bin -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Laborator 1/src/clasa/Fuel.java: -------------------------------------------------------------------------------- 1 | package clasa; 2 | 3 | public interface Fuel { 4 | void refuel(int amount); 5 | } 6 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Setup [requirements](Info/setup.md). 4 | ## Project [requirements](Info/project.md). 5 | ## Grading [rules](Info/grading.md). -------------------------------------------------------------------------------- /Laborator 1/out/production/Laborator 1/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 1/out/production/Laborator 1/Main.class -------------------------------------------------------------------------------- /Laborator 1/out/production/Laborator 1/clasa/Car.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 1/out/production/Laborator 1/clasa/Car.class -------------------------------------------------------------------------------- /Laborator 1/out/production/Laborator 1/ex1/Flow.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 1/out/production/Laborator 1/ex1/Flow.class -------------------------------------------------------------------------------- /Laborator 1/out/production/Laborator 1/clasa/Fuel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 1/out/production/Laborator 1/clasa/Fuel.class -------------------------------------------------------------------------------- /Laborator 5/Laborator5/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | System.out.println("Hello, World!"); 4 | } 5 | } -------------------------------------------------------------------------------- /Laborator 1/out/production/Laborator 1/clasa/Vehicle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 1/out/production/Laborator 1/clasa/Vehicle.class -------------------------------------------------------------------------------- /Laborator 1/out/production/Laborator 1/ex1/Operators.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 1/out/production/Laborator 1/ex1/Operators.class -------------------------------------------------------------------------------- /Laborator 1/out/production/Laborator 1/ex1/Wrappers.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 1/out/production/Laborator 1/ex1/Wrappers.class -------------------------------------------------------------------------------- /Laborator 2/Laborator2/out/production/Laborator2/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 2/Laborator2/out/production/Laborator2/Main.class -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/out/production/Laborator 3/A.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 3/Laborator 3/out/production/Laborator 3/A.class -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/out/production/Laborator 3/B.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 3/Laborator 3/out/production/Laborator 3/B.class -------------------------------------------------------------------------------- /Laborator 4/Laborator4/out/production/Laborator4/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 4/Laborator4/out/production/Laborator4/Main.class -------------------------------------------------------------------------------- /Laborator 4/Laborator4/out/production/Laborator4/Room.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 4/Laborator4/out/production/Laborator4/Room.class -------------------------------------------------------------------------------- /Laborator 1/out/production/Laborator 1/clasa/Car$Engine.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 1/out/production/Laborator 1/clasa/Car$Engine.class -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/out/production/Laborator 3/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 3/Laborator 3/out/production/Laborator 3/Main.class -------------------------------------------------------------------------------- /Laborator 4/Laborator4/out/production/Laborator4/House.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 4/Laborator4/out/production/Laborator4/House.class -------------------------------------------------------------------------------- /Laborator 4/Laborator4/out/production/Laborator4/Owner.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 4/Laborator4/out/production/Laborator4/Owner.class -------------------------------------------------------------------------------- /Laborator 1/out/production/Laborator 1/ex1/PrimitiveMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 1/out/production/Laborator 1/ex1/PrimitiveMain.class -------------------------------------------------------------------------------- /Laborator 2/Laborator2/out/production/Laborator2/ex2/Test.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 2/Laborator2/out/production/Laborator2/ex2/Test.class -------------------------------------------------------------------------------- /Laborator 2/Laborator2/out/production/Laborator2/ShallowDeep.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 2/Laborator2/out/production/Laborator2/ShallowDeep.class -------------------------------------------------------------------------------- /Laborator 2/Laborator2/out/production/Laborator2/PersoanaDeep.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 2/Laborator2/out/production/Laborator2/PersoanaDeep.class -------------------------------------------------------------------------------- /Laborator 2/Laborator2/out/production/Laborator2/ex3/Persoana.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 2/Laborator2/out/production/Laborator2/ex3/Persoana.class -------------------------------------------------------------------------------- /Laborator 2/Laborator2/out/production/Laborator2/PersoanaShallow.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 2/Laborator2/out/production/Laborator2/PersoanaShallow.class -------------------------------------------------------------------------------- /Laborator 2/Laborator2/out/production/Laborator2/ex2/Ex2_ArrayBidimensional.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 2/Laborator2/out/production/Laborator2/ex2/Ex2_ArrayBidimensional.class -------------------------------------------------------------------------------- /Laborator 2/Laborator2/out/production/Laborator2/ex1/Ex1_ArrayUnidimensional.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vladutzky/Laborator-Java/HEAD/Laborator 2/Laborator2/out/production/Laborator2/ex1/Ex1_ArrayUnidimensional.class -------------------------------------------------------------------------------- /Laborator 10/Lab10/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /Laborator 9/bd/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /Laborator 2/Laborator2/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Laborator 4/Laborator4/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Laborator 9/bd/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Laborator 2/Laborator2/src/ex2/Ex2_ArrayBidimensional.java: -------------------------------------------------------------------------------- 1 | package ex2; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Ex2_ArrayBidimensional { 6 | public static void main(String[] args) { 7 | Test test = new Test(); 8 | System.out.print(test.getVar()); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Laborator 10/Lab10/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Laborator 1/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Laborator 9/bd/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Laborator 1/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Laborator 10/Lab10/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Laborator 2/Laborator2/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Laborator 4/Laborator4/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Laborator 1/src/clasa/Vehicle.java: -------------------------------------------------------------------------------- 1 | package clasa; 2 | 3 | public abstract class Vehicle { 4 | // Abstract Method (to be implemented in subclass) 5 | public abstract void start(); 6 | 7 | // Concrete Method (can be inherited) 8 | public void stop() { 9 | System.out.println("Vehicle stopped"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Laborator 9/bd/.idea/libraries/mysql_connector_j_9_3_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Laborator 2/Laborator2/src/Main.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Arrays; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | int a[], b; 7 | int[] t = new int[10]; 8 | int t2[] = {0, 1, 2, 3,4 ,5, 6, 7, 8, 9}; 9 | System.out.print(Arrays.toString(t2)); 10 | 11 | 12 | 13 | 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Laborator 9/bd/src/Student.java: -------------------------------------------------------------------------------- 1 | class Student { 2 | int id; 3 | String name; 4 | double grade; 5 | 6 | public Student(int id, String name, double grade) { 7 | this.id = id; 8 | this.name = name; 9 | this.grade = grade; 10 | } 11 | 12 | public String toString() { 13 | return id + ": " + name + " - " + grade; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Laborator 10/Lab10/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main extends Thread { 2 | public static int amount = 0; 3 | 4 | public static void main(String[] args) { 5 | Main thread = new Main(); 6 | thread.start(); 7 | System.out.println(amount); 8 | amount++; 9 | System.out.println(amount); 10 | } 11 | 12 | public void run() { 13 | amount++; 14 | } 15 | } -------------------------------------------------------------------------------- /Laborator 2/Laborator2/src/ex2/Test.java: -------------------------------------------------------------------------------- 1 | package ex2; 2 | 3 | public class Test { 4 | private int var; 5 | 6 | public Test(){ } 7 | 8 | public Test(int var) { 9 | this.var = var; 10 | } 11 | 12 | public Test(Test test) { 13 | this.var = test.var; 14 | } 15 | 16 | public int getVar() { 17 | return var; 18 | } 19 | 20 | public void setVar(int var) { 21 | this.var = var; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Laborator 1/Laborator 1.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Laborator 10/Lab10/Lab10.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Laborator 2/Laborator2/Laborator2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/Laborator 3.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Laborator 4/Laborator4/Laborator4.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/Laborator5.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Laborator 9/bd/.gitignore: -------------------------------------------------------------------------------- 1 | ### IntelliJ IDEA ### 2 | out/ 3 | !**/src/main/**/out/ 4 | !**/src/test/**/out/ 5 | 6 | ### Eclipse ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | .sts4-cache 14 | bin/ 15 | !**/src/main/**/bin/ 16 | !**/src/test/**/bin/ 17 | 18 | ### NetBeans ### 19 | /nbproject/private/ 20 | /nbbuild/ 21 | /dist/ 22 | /nbdist/ 23 | /.nb-gradle/ 24 | 25 | ### VS Code ### 26 | .vscode/ 27 | 28 | ### Mac OS ### 29 | .DS_Store -------------------------------------------------------------------------------- /Laborator 10/Lab10/.gitignore: -------------------------------------------------------------------------------- 1 | ### IntelliJ IDEA ### 2 | out/ 3 | !**/src/main/**/out/ 4 | !**/src/test/**/out/ 5 | 6 | ### Eclipse ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | .sts4-cache 14 | bin/ 15 | !**/src/main/**/bin/ 16 | !**/src/test/**/bin/ 17 | 18 | ### NetBeans ### 19 | /nbproject/private/ 20 | /nbbuild/ 21 | /dist/ 22 | /nbdist/ 23 | /.nb-gradle/ 24 | 25 | ### VS Code ### 26 | .vscode/ 27 | 28 | ### Mac OS ### 29 | .DS_Store -------------------------------------------------------------------------------- /Laborator 5/Laborator5/.gitignore: -------------------------------------------------------------------------------- 1 | ### IntelliJ IDEA ### 2 | out/ 3 | !**/src/main/**/out/ 4 | !**/src/test/**/out/ 5 | 6 | ### Eclipse ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | .sts4-cache 14 | bin/ 15 | !**/src/main/**/bin/ 16 | !**/src/test/**/bin/ 17 | 18 | ### NetBeans ### 19 | /nbproject/private/ 20 | /nbbuild/ 21 | /dist/ 22 | /nbdist/ 23 | /.nb-gradle/ 24 | 25 | ### VS Code ### 26 | .vscode/ 27 | 28 | ### Mac OS ### 29 | .DS_Store -------------------------------------------------------------------------------- /Info/grading.md: -------------------------------------------------------------------------------- 1 | # Evaluare laborator 2 | 3 | ## Punctaj maxim laborator: 50.00p 4 | 5 | ## Prezență și activitate: 25.00p 6 | * Minim 8 prezențe cu exerciții rezolvate 7 | 8 | ## Proiect - 25.00p 9 | * Etapa 1: 13p 10 | * cerința 1: 3p 11 | * cerința 2: 10p 12 | 13 | * Etapa 2: 12p 14 | * cerința 1: 6p 15 | * cerința 2: 6p 16 | 17 | ## Important! 18 | * Activitatea și proiectul vor fi punctate la 2 zecimale iar punctajul final nu se va rotunji. 19 | * Nota minimă de promovare a laboratorului: 5.00 (25.00p) -------------------------------------------------------------------------------- /Laborator 4/Laborator4/src/ex4.java: -------------------------------------------------------------------------------- 1 | public class ex4 { 2 | public record Student(String name, int group, double grade) {} 3 | public static void main(String[] args) { 4 | Student s1 = new Student("Alice", 101, 9.5); 5 | Student s2 = new Student("Bob", 102, 8.0); 6 | Student s3 = new Student("Alice", 101, 9.5); 7 | System.out.println(s1); 8 | System.out.println(s2); 9 | System.out.println(s1.equals(s3)); 10 | System.out.println(s1.name()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Laborator 4/Laborator4/src/Owner.java: -------------------------------------------------------------------------------- 1 | public class Owner { 2 | String nume; 3 | 4 | public Owner(String nume) { 5 | this.nume = nume; 6 | } 7 | 8 | public Owner(Owner owner) { 9 | this.nume = owner.nume; 10 | } 11 | 12 | 13 | @Override 14 | public String toString() { 15 | return "Owner{" + 16 | "nume='" + nume + '\'' + 17 | '}'; 18 | } 19 | 20 | public void setNume(String nume) { 21 | this.nume = nume; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Laborator 2/Laborator2/src/ex1/Ex1_ArrayUnidimensional.java: -------------------------------------------------------------------------------- 1 | package ex1; 2 | 3 | import ex2.Test; 4 | 5 | import java.util.Arrays; 6 | 7 | public class Ex1_ArrayUnidimensional { 8 | public static void main(String[] args) { 9 | int[] v = {2,3,4}; 10 | System.out.print(Arrays.toString(v)); 11 | 12 | int[] arr = {1,2,3,4,5,6,7,8,9}; 13 | 14 | for(int i : arr) { 15 | System.out.println(i); 16 | } 17 | System.out.print(Arrays.toString(arr)); 18 | Test test = new Test(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Laborator 9/bd/bd.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Laborator 4/Laborator4/src/House.java: -------------------------------------------------------------------------------- 1 | public class House { 2 | String address; 3 | Owner owner; 4 | Room room; 5 | 6 | public House(String address, Owner owner, Room room) { 7 | this.address = address; 8 | this.owner = new Owner(owner); 9 | this.room = room; 10 | } 11 | 12 | @Override 13 | public String toString() { 14 | return "House{" + 15 | "address='" + address + '\'' + 16 | ", owner=" + owner + 17 | ", room=" + room + 18 | '}'; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/src/ex3.java: -------------------------------------------------------------------------------- 1 | public class ex3 { 2 | static abstract class Animal { 3 | abstract void makeSound(); 4 | } 5 | static class Dog extends Animal { 6 | void makeSound() { 7 | System.out.println("Woof!"); 8 | } 9 | } 10 | static class Cat extends Animal { 11 | void makeSound() { 12 | System.out.println("Meow!"); 13 | } 14 | } 15 | public static void main(String[] args) { 16 | Animal[] animals = { new Dog(), new Cat() }; 17 | for (Animal a : animals) { 18 | a.makeSound(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Laborator 4/Laborator4/src/ex5.java: -------------------------------------------------------------------------------- 1 | public class ex5 { 2 | public static void main(String[] args) { 3 | String str = "Hello World"; 4 | String strModified = str.toLowerCase(); 5 | System.out.println("Original String: " + str); 6 | System.out.println("Modified String: " + strModified); 7 | StringBuilder sb = new StringBuilder("Hello"); 8 | sb.append(" World"); 9 | System.out.println("StringBuilder: " + sb.toString()); 10 | StringBuffer sbuf = new StringBuffer("Hello"); 11 | sbuf.append(" World"); 12 | System.out.println("StringBuffer: " + sbuf.toString()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Laborator 4/Laborator4/src/Room.java: -------------------------------------------------------------------------------- 1 | public class Room { 2 | double width; 3 | double length; 4 | 5 | public Room(double width, double length) { 6 | this.width = width; 7 | this.length = length; 8 | } 9 | 10 | public Room(Room room){ 11 | this.length = room.length; 12 | this.width = room.width; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return "Room{" + 18 | "width=" + width + 19 | ", length=" + length + 20 | '}'; 21 | } 22 | 23 | public void setWidth(double width) { 24 | this.width = width; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Laborator 4/Laborator4/src/ex3.java: -------------------------------------------------------------------------------- 1 | public class ex3 { 2 | public static final class ImmutablePoint { 3 | private final double x; 4 | private final double y; 5 | public ImmutablePoint(double x, double y) { 6 | this.x = x; 7 | this.y = y; 8 | } 9 | public double getX() { 10 | return x; 11 | } 12 | public double getY() { 13 | return y; 14 | } 15 | } 16 | public static void main(String[] args) { 17 | ImmutablePoint point = new ImmutablePoint(3.0, 4.0); 18 | System.out.println("x: " + point.getX() + " y: " + point.getY()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/src/ex1.java: -------------------------------------------------------------------------------- 1 | public class ex1 { 2 | public static int multiply(int a, int b) { 3 | return a * b; 4 | } 5 | public static double multiply(double a, double b, double c) { 6 | return a * b * c; 7 | } 8 | public static int multiply(int[] values) { 9 | int prod = 1; 10 | for (int v : values) { 11 | prod *= v; 12 | } 13 | return prod; 14 | } 15 | public static void main(String[] args) { 16 | System.out.println(multiply(3, 4)); 17 | System.out.println(multiply(2.5, 3.0, 4.0)); 18 | System.out.println(multiply(new int[]{2, 3, 4, 5})); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/src/ex2.java: -------------------------------------------------------------------------------- 1 | public class ex2 { 2 | public static double parseAndDivide(String a, String b) { 3 | return Integer.parseInt(a) / Integer.parseInt(b); 4 | } 5 | public static void main(String[] args) { 6 | try { 7 | System.out.println(parseAndDivide("10", "0")); 8 | } catch (NumberFormatException | ArithmeticException e) { 9 | System.out.println("Exception: " + e.getMessage()); 10 | } 11 | try { 12 | System.out.println(parseAndDivide("ten", "2")); 13 | } catch (NumberFormatException | ArithmeticException e) { 14 | System.out.println("Exception: " + e.getMessage()); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/src/ex5.java: -------------------------------------------------------------------------------- 1 | public class ex5 { 2 | static class Printer { 3 | void print(String s) { 4 | System.out.println(s); 5 | } 6 | void print(int number) { 7 | System.out.println(number); 8 | } 9 | } 10 | static class ColorPrinter extends Printer { 11 | @Override 12 | void print(String s) { 13 | System.out.println("Printing in red: " + s); 14 | } 15 | } 16 | public static void main(String[] args) { 17 | Printer p = new Printer(); 18 | ColorPrinter cp = new ColorPrinter(); 19 | p.print("Test string"); 20 | p.print(100); 21 | cp.print("Test string in color"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Laborator 9/bd/src/DbConnection.java: -------------------------------------------------------------------------------- 1 | import java.sql.Connection; 2 | import java.sql.DriverManager; 3 | import java.sql.SQLException; 4 | 5 | class DbConnection { 6 | private static final String URL = "jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC"; 7 | private static final String USER = "root"; 8 | private static final String PASS = "root"; 9 | 10 | private static Connection connection; 11 | 12 | private DbConnection() {} 13 | 14 | public static Connection getInstance() throws SQLException { 15 | if (connection == null || connection.isClosed()) { 16 | connection = DriverManager.getConnection(URL, USER, PASS); 17 | } 18 | return connection; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Laborator 10/Lab10/src/MyThread.java: -------------------------------------------------------------------------------- 1 | public class MyThread extends Thread { 2 | private String simbol; 3 | 4 | public MyThread(String simbol) { 5 | this.simbol = simbol; 6 | } 7 | 8 | public void run() { 9 | for (int i = 0; i < 100; i++) { 10 | System.out.print(simbol); 11 | try { 12 | Thread.sleep(20); 13 | } catch (InterruptedException e) { 14 | System.out.println(simbol + " interrupted"); 15 | } 16 | } 17 | } 18 | 19 | public static void main(String[] args) { 20 | MyThread a = new MyThread("A"); 21 | MyThread b = new MyThread("B"); 22 | a.start(); 23 | b.start(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/src/ex1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class ex1 { 3 | public static int readInteger() { 4 | Scanner scanner = new Scanner(System.in); 5 | int result = 0; 6 | try { 7 | System.out.print("Enter an integer: "); 8 | result = Integer.parseInt(scanner.nextLine()); 9 | System.out.println("You entered: " + result); 10 | } catch (NumberFormatException e) { 11 | System.out.println("Invalid integer input."); 12 | } finally { 13 | System.out.println("readInteger() execution completed."); 14 | } 15 | return result; 16 | } 17 | public static void main(String[] args) { 18 | readInteger(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/src/ex3.java: -------------------------------------------------------------------------------- 1 | public class ex3 { 2 | static class InvalidGradeException extends Exception { 3 | public InvalidGradeException(String message) { 4 | super(message); 5 | } 6 | } 7 | public static void validateGrade(int grade) throws InvalidGradeException { 8 | if (grade < 1 || grade > 10) 9 | throw new InvalidGradeException("Grade must be between 1 and 10."); 10 | System.out.println("Grade " + grade + " is valid."); 11 | } 12 | public static void main(String[] args) { 13 | try { 14 | validateGrade(5); 15 | validateGrade(12); 16 | } catch (InvalidGradeException e) { 17 | System.out.println("Error: " + e.getMessage()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Laborator 9/bd/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | StudentService service = new StudentService(); 4 | 5 | // Create table if not exists 6 | service.createTable(); 7 | 8 | // Insert students 9 | service.insert("Ana", 9.5); 10 | service.insert("Ion", 8.7); 11 | 12 | // Show all 13 | System.out.println("All students:"); 14 | service.getAll().forEach(System.out::println); 15 | 16 | // Update grade 17 | service.updateGrade(1, 10.0); 18 | 19 | // Show after update 20 | System.out.println("After update:"); 21 | service.getAll().forEach(System.out::println); 22 | 23 | // Delete Ion (id = 2) 24 | service.delete(2); 25 | 26 | // Final state 27 | System.out.println("After deletion:"); 28 | service.getAll().forEach(System.out::println); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/src/ex4.java: -------------------------------------------------------------------------------- 1 | public class ex4 { 2 | interface Insurable { 3 | double getInsuranceCost(); 4 | } 5 | static abstract class Vehicle { 6 | abstract void drive(); 7 | } 8 | static class Motorcycle extends Vehicle implements Insurable { 9 | String brand; 10 | int engineCapacity; 11 | Motorcycle(String brand, int engineCapacity) { 12 | this.brand = brand; 13 | this.engineCapacity = engineCapacity; 14 | } 15 | public void drive() { 16 | System.out.println("Motorcycle " + brand + " is driving"); 17 | } 18 | public double getInsuranceCost() { 19 | return engineCapacity * 1.5; 20 | } 21 | } 22 | public static void main(String[] args) { 23 | Motorcycle m = new Motorcycle("Yamaha", 600); 24 | m.drive(); 25 | System.out.println(m.getInsuranceCost()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Laborator 2/Laborator2/src/ex4/Persoana.java: -------------------------------------------------------------------------------- 1 | package ex4; 2 | 3 | public class Persoana { 4 | private String nume; 5 | private int varsta; 6 | private int id; 7 | private static int nrPersoane; 8 | 9 | 10 | static { 11 | nrPersoane = 0; 12 | } 13 | 14 | { 15 | id = ++nrPersoane; 16 | } 17 | 18 | 19 | public Persoana() { 20 | } 21 | 22 | 23 | public Persoana(String nume, int varsta) { 24 | this.nume = nume; 25 | this.varsta = varsta; 26 | } 27 | 28 | 29 | public String getNume() { 30 | return nume; 31 | } 32 | 33 | public void setNume(String nume) { 34 | this.nume = nume; 35 | } 36 | 37 | 38 | public int getVarsta() { 39 | return varsta; 40 | } 41 | 42 | public void setVarsta(int varsta) { 43 | this.varsta = varsta; 44 | } 45 | 46 | public int getId() { 47 | return id; 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Laborator 10/Lab10/src/ClientApp.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | public class ClientApp { 5 | public static void main(String[] args) throws IOException { 6 | Socket s = new Socket("localhost", 1234); 7 | BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 8 | BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 9 | BufferedReader tastatura = new BufferedReader(new InputStreamReader(System.in)); 10 | 11 | String linie; 12 | System.out.println("Msg(Stop):"); 13 | 14 | while (true) { 15 | System.out.print("Send: "); 16 | linie = tastatura.readLine(); 17 | out.write(linie); 18 | out.newLine(); 19 | out.flush(); 20 | 21 | if (linie.equalsIgnoreCase("STOP")) break; 22 | 23 | System.out.println("Response: " + in.readLine()); 24 | } 25 | 26 | s.close(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/src/test/testMain.java: -------------------------------------------------------------------------------- 1 | //// superclass 2 | //package test; 3 | //class A { 4 | // void method() { 5 | // // a method of class A 6 | // System.out.println("Method A"); 7 | // } 8 | //} 9 | // 10 | //// subclass of class A 11 | //class B extends A { 12 | // void method() { 13 | // // class B method 14 | // System.out.println("Method B"); 15 | // } 16 | //} 17 | // 18 | //// subclass of class B 19 | //class C extends B { 20 | // void method() { 21 | // // class C method 22 | // System.out.println("Method C"); 23 | // } 24 | //} 25 | // 26 | //public class testMain { 27 | // 28 | // public static void main(String[] args) { 29 | // A objA = new A(); 30 | // B objB = new B(); 31 | // C objC = new C(); 32 | // 33 | // obj.method(); // invoke the method() of class A 34 | // obj.method(); // invoke the method() of class B 35 | // obj.method(); // invoke the method() of class C 36 | // } 37 | //} -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/src/ex2.java: -------------------------------------------------------------------------------- 1 | public class ex2 { 2 | static class Vehicle { 3 | String brand; 4 | int year; 5 | Vehicle(String brand, int year) { 6 | this.brand = brand; 7 | this.year = year; 8 | } 9 | void displayInfo() { 10 | System.out.println("Vehicle: " + brand + " (" + year + ")"); 11 | } 12 | } 13 | static class Car extends Vehicle { 14 | int numberOfDoors; 15 | Car(String brand, int year, int numberOfDoors) { 16 | super(brand, year); 17 | this.numberOfDoors = numberOfDoors; 18 | } 19 | @Override 20 | void displayInfo() { 21 | System.out.println("Car: " + brand + " (" + year + "), Doors: " + numberOfDoors); 22 | } 23 | } 24 | public static void main(String[] args) { 25 | Vehicle v = new Vehicle("Toyota", 2015); 26 | Car c = new Car("Honda", 2018, 4); 27 | v.displayInfo(); 28 | c.displayInfo(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/src/staticClass.java: -------------------------------------------------------------------------------- 1 | //class A { 2 | // int dată_membră_non_statică = 1; 3 | // static int dată_membră_statică = 1000; 4 | // void metoda1() { 5 | // System.out.println("Metoda non-statică 1 din clasa A!"); 6 | // } 7 | // static void metoda2() { 8 | // System.out.println("Metoda statică 2 din clasa A!"); 9 | // } 10 | //} 11 | //class B extends A { 12 | // int dată_membră_non_statică = 2; 13 | // static int dată_membră_statică = 2000; 14 | // void metoda1() { 15 | // System.out.println("Metoda non-statică 1 din clasa B!"); 16 | // } 17 | // static void metoda2() { 18 | // System.out.println("Metoda statică 2 din clasa B!"); 19 | // } 20 | //} 21 | //public class staticClass { 22 | // A ob = new B(); //polimorfism 23 | // System.out.println("Data membră non-statică = " + ob.dată_membră_non_statică); 24 | // System.out.println("Data membră statică = " + ob.dată_membră_non_statică); 25 | // ob.metoda1(); 26 | // ob.metoda2(); 27 | //} 28 | // 29 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/src/ex5.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.List; 4 | public class ex5 { 5 | static class Student implements Comparable { 6 | String name; 7 | double grade; 8 | public Student(String name, double grade) { 9 | this.name = name; 10 | this.grade = grade; 11 | } 12 | public int compareTo(Student other) { 13 | return Double.compare(this.grade, other.grade); 14 | } 15 | public String toString() { 16 | return "Student{name='" + name + "', grade=" + grade + "}"; 17 | } 18 | } 19 | public static void main(String[] args) { 20 | List students = new ArrayList<>(); 21 | students.add(new Student("Alice", 9.0)); 22 | students.add(new Student("Bob", 7.5)); 23 | students.add(new Student("Charlie", 8.5)); 24 | Collections.sort(students); 25 | for (Student s : students) { 26 | System.out.println(s); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Laborator 1/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | 7 | Scanner scanner = new Scanner(System.in); 8 | int a = scanner.nextInt(); 9 | scanner.nextLine(); 10 | String b = scanner.nextLine(); 11 | System.out.println(a); 12 | System.out.print(b); 13 | System.out.printf("Hello %d!", a); 14 | 15 | 16 | int[] integers = new int[6]; 17 | integers[2] = 5; 18 | for (int i = 0; i< integers.length; i++){ 19 | System.out.println(integers[i]);} 20 | 21 | ArrayList NR = new ArrayList<>() ; 22 | for (int i = 0; i< NR.size(); i++){ 23 | System.out.println(NR.get(i));} 24 | System.out.println(' '); 25 | NR.add(2); 26 | NR.add(5); 27 | for (int i = 0; i< NR.size(); i++){ 28 | System.out.print(NR.get(i));} 29 | System.out.println(' '); 30 | NR.add(1,8); 31 | 32 | 33 | 34 | for (int i = 0; i< NR.size(); i++){ 35 | System.out.print(NR.get(i));} 36 | System.out.println(' '); 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | } 45 | } -------------------------------------------------------------------------------- /Laborator 4/Laborator4/src/ex2.java: -------------------------------------------------------------------------------- 1 | public class ex2 { 2 | static class Person { 3 | String name; 4 | int age; 5 | public Person(String name, int age) { 6 | this.name = name; 7 | this.age = age; 8 | } 9 | public String toString() { 10 | return "Person{name='" + name + "', age=" + age + "}"; 11 | } 12 | public boolean equals(Object o) { 13 | if (this == o) return true; 14 | if (o == null || getClass() != o.getClass()) return false; 15 | Person person = (Person) o; 16 | return age == person.age && (name != null ? name.equals(person.name) : person.name == null); 17 | } 18 | public int hashCode() { 19 | int result = (name != null ? name.hashCode() : 0); 20 | result = 31 * result + age; 21 | return result; 22 | } 23 | } 24 | public static void main(String[] args) { 25 | Person p1 = new Person("John", 30); 26 | Person p2 = new Person("John", 30); 27 | System.out.println(p1.equals(p2)); 28 | System.out.println(p1.hashCode() == p2.hashCode()); 29 | System.out.println(p1); 30 | System.out.println(p2); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Laborator 10/Readme.md: -------------------------------------------------------------------------------- 1 | # Laborator – Threads și Socket-uri în Java 2 | 3 | ## Exercițiul 1 – Fire cu Thread și Runnable 4 | Creează două fire care rulează în paralel și afișează caractere diferite ('A' și 'B') de 100 de ori fiecare. Testează cu `.start()` și `.run()` pentru a vedea diferențele. 5 | 6 | ## Exercițiul 2 – Utilizarea `sleep()` și `join()` 7 | Modifică exercițiul 1 astfel încât firul principal să aștepte terminarea celor două fire. Adaugă pauze între afișări cu `Thread.sleep(50)`. 8 | 9 | ## Exercițiul 3 – Oprirea unui fir cu `interrupt()` 10 | Creează un fir care afișează numere infinit. După ce utilizatorul tastează "stop" în consola, firul este întrerupt și se oprește. 11 | 12 | ## Exercițiul 4 – Contor sincronizat 13 | Creează un contor global și două fire care îl incrementează de 10.000 ori. Arată rezultatul fără și cu `synchronized` pentru a evidenția diferența. 14 | 15 | ## Exercițiul 5 – Producător–Consumator 16 | Implementează două fire: unul produce un număr, celălalt îl consumă, folosind o coadă partajată (`LinkedList`). Sincronizează folosind `wait()` și `notify()`. 17 | 18 | ## Exercițiul 6 – Chat simplu cu socket-uri 19 | Creează un server și un client care comunică prin mesaje text. Chatul continuă până când unul din participanți trimite mesajul „STOP”. -------------------------------------------------------------------------------- /Laborator 5/Laborator5/src/ex4.java: -------------------------------------------------------------------------------- 1 | public class ex4 { 2 | interface Shape { 3 | double getArea(); 4 | double getPerimeter(); 5 | } 6 | static class Circle implements Shape { 7 | double radius; 8 | public Circle(double radius) { 9 | this.radius = radius; 10 | } 11 | public double getArea() { 12 | return Math.PI * radius * radius; 13 | } 14 | public double getPerimeter() { 15 | return 2 * Math.PI * radius; 16 | } 17 | } 18 | static class Rectangle implements Shape { 19 | double width; 20 | double height; 21 | public Rectangle(double width, double height) { 22 | this.width = width; 23 | this.height = height; 24 | } 25 | public double getArea() { 26 | return width * height; 27 | } 28 | public double getPerimeter() { 29 | return 2 * (width + height); 30 | } 31 | } 32 | public static void main(String[] args) { 33 | Shape[] shapes = { new Circle(3), new Rectangle(4, 5) }; 34 | for (Shape s : shapes) { 35 | System.out.println("Area: " + s.getArea() + ", Perimeter: " + s.getPerimeter()); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/src/ex8.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | public class ex8 { 4 | static class Student { 5 | int id; 6 | String name; 7 | double grade; 8 | public Student(int id, String name, double grade) { 9 | this.id = id; 10 | this.name = name; 11 | this.grade = grade; 12 | } 13 | public String toString() { 14 | return "Student{id=" + id + ", name='" + name + "', grade=" + grade + "}"; 15 | } 16 | } 17 | public static void main(String[] args) { 18 | HashMap students = new HashMap<>(); 19 | students.put(1, new Student(1, "Alice", 9.0)); 20 | students.put(2, new Student(2, "Bob", 8.5)); 21 | students.put(3, new Student(3, "Charlie", 7.0)); 22 | for (Map.Entry entry : students.entrySet()) { 23 | System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); 24 | } 25 | Student s = students.get(2); 26 | s.grade = 9.5; 27 | for (Map.Entry entry : students.entrySet()) { 28 | System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Laborator 1/src/ex1/PrimitiveMain.java: -------------------------------------------------------------------------------- 1 | package ex1; 2 | 3 | public class PrimitiveMain { 4 | 5 | public static void main(String[] args) { 6 | // byte 7 | byte byteValue = 127; 8 | System.out.println("byteValue = " + byteValue); 9 | 10 | // short 11 | short shortValue = 32767; 12 | System.out.println("shortValue = " + shortValue); 13 | 14 | // int 15 | int intValue = 2147483647; 16 | System.out.println("intValue = " + intValue); 17 | 18 | // long 19 | long longValue = 9223372036854775807L; 20 | System.out.println("longValue = " + longValue); 21 | 22 | // float 23 | float floatValue = 3.4028235E38f; 24 | System.out.println("floatValue = " + floatValue); 25 | 26 | // double 27 | double doubleValue = 1.7976931348623157E308; 28 | System.out.println("doubleValue = " + doubleValue); 29 | 30 | // char 31 | char charValue = 'A'; 32 | System.out.println("charValue = " + charValue); 33 | 34 | char charValue2 = '\u021A'; 35 | System.out.println("charValue2 = " + charValue2); 36 | 37 | // boolean 38 | boolean booleanValue = true; 39 | System.out.println("booleanValue = " + booleanValue); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Laborator 4/Laborator4/src/ex1.java: -------------------------------------------------------------------------------- 1 | public class ex1 { 2 | static class Room { 3 | double width; 4 | double length; 5 | public Room(double width, double length) { 6 | this.width = width; 7 | this.length = length; 8 | } 9 | public Room(Room r) { 10 | this.width = r.width; 11 | this.length = r.length; 12 | } 13 | } 14 | static class Owner { 15 | String name; 16 | public Owner(String name) { 17 | this.name = name; 18 | } 19 | } 20 | static class House { 21 | String address; 22 | Room diningRoom; 23 | Owner owner; 24 | public House(String address, Room diningRoom, Owner owner) { 25 | this.address = address; 26 | this.diningRoom = new Room(diningRoom); 27 | this.owner = owner; 28 | } 29 | } 30 | public static void main(String[] args) { 31 | Room room = new Room(5.0, 4.0); 32 | Owner owner = new Owner("John"); 33 | House house = new House("123 Main St", room, owner); 34 | room.width = 10.0; 35 | owner.name = "Mike"; 36 | System.out.println("House diningRoom width: " + house.diningRoom.width); 37 | System.out.println("House owner name: " + house.owner.name); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/src/ex7.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Comparator; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | public class ex7 { 7 | static class Product { 8 | int id; 9 | String name; 10 | double price; 11 | public Product(int id, String name, double price) { 12 | this.id = id; 13 | this.name = name; 14 | this.price = price; 15 | } 16 | public String toString() { 17 | return "Product{id=" + id + ", name='" + name + "', price=" + price + "}"; 18 | } 19 | } 20 | public static void main(String[] args) { 21 | List products = new ArrayList<>(); 22 | products.add(new Product(1, "Apple", 1.5)); 23 | products.add(new Product(2, "Banana", 1.0)); 24 | products.add(new Product(3, "Orange", 2.0)); 25 | Iterator itr = products.iterator(); 26 | while (itr.hasNext()) { 27 | System.out.println(itr.next()); 28 | } 29 | Collections.sort(products, new Comparator() { 30 | public int compare(Product p1, Product p2) { 31 | return Double.compare(p1.price, p2.price); 32 | } 33 | }); 34 | for (Product p : products) { 35 | System.out.println(p); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/src/ex6.java: -------------------------------------------------------------------------------- 1 | import java.sql.SQLException; 2 | public class ex6 { 3 | interface DatabaseActions { 4 | void connect(); 5 | void executeQuery(String query) throws SQLException; 6 | } 7 | static class FakeDatabase implements DatabaseActions { 8 | boolean online; 9 | public FakeDatabase(boolean online) { 10 | this.online = online; 11 | } 12 | public void connect() { 13 | if (!online) throw new IllegalStateException("Database is offline"); 14 | System.out.println("Connected to database"); 15 | } 16 | public void executeQuery(String query) throws SQLException { 17 | if (query == null || !query.trim().toUpperCase().startsWith("SELECT")) 18 | throw new SQLException("Invalid query"); 19 | System.out.println("Query executed: " + query); 20 | } 21 | } 22 | public static void main(String[] args) { 23 | FakeDatabase db1 = new FakeDatabase(false); 24 | try { 25 | db1.connect(); 26 | } catch (IllegalStateException e) { 27 | System.out.println("Connection error: " + e.getMessage()); 28 | } 29 | FakeDatabase db2 = new FakeDatabase(true); 30 | try { 31 | db2.connect(); 32 | db2.executeQuery("UPDATE table"); 33 | } catch (IllegalStateException e) { 34 | System.out.println("Connection error: " + e.getMessage()); 35 | } catch (SQLException e) { 36 | System.out.println("Query error: " + e.getMessage()); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Laborator 9/Readme.md: -------------------------------------------------------------------------------- 1 | # 💾 Laborator 9 2 | Link MYSQL : https://dev.mysql.com/downloads/installer/ 3 | Link Driver : https://dev.mysql.com/downloads/connector/j/ 4 | 5 | ## 🟢 Partea I – JDBC (bază de date) 6 | 7 | ### ✅ Exercițiul 1 – Salvare în baza de date 8 | Creează o clasă `Produs` cu atribute `id`, `nume`, `pret`. 9 | Creează un `ProdusService` care: 10 | - Creează tabela `produse` în baza de date. 11 | - Inserează un produs primit de la utilizator (folosind `PreparedStatement`). 12 | - Afișează toate produsele din tabel. 13 | 14 | ### ✅ Exercițiul 2 – Căutare și actualizare 15 | Extinde `ProdusService`: 16 | - Permite căutarea unui produs după nume. 17 | - Dacă produsul este găsit, actualizează prețul său. 18 | - Dacă nu este găsit, afișează un mesaj. 19 | 20 | ### ✅ Exercițiul 3 – Ștergere în masă 21 | Creează o metodă care: 22 | - Primește un preț `minim` ca parametru. 23 | - Șterge toate produsele mai ieftine decât acel preț. 24 | - Afișează câte produse au fost șterse. 25 | 26 | --- 27 | 28 | ## 🟠 Partea II – Serializare / Deserializare 29 | 30 | ### ✅ Exercițiul 4 – Serializare obiect simplu 31 | Creează o clasă `Persoana` care implementează `Serializable`. 32 | Scrie un program care: 33 | - Creează un obiect `Persoana` și îl salvează într-un fișier `persoana.ser`. 34 | - Încarcă apoi acel obiect și îl afișează în consolă. 35 | 36 | ### ✅ Exercițiul 5 – Serializarea unei liste 37 | Creează o listă de `Persoana` (cu minim 3 instanțe). 38 | - Serializează lista într-un fișier `persoane.ser`. 39 | - Deserializați lista și afișați toate persoanele. 40 | 41 | ### ✅ Exercițiul 6 – Externalizare cu câmp ignorat 42 | Creează o clasă `Carte` care implementează `Externalizable`. 43 | Aceasta are: `titlu`, `autor`, `parolaAcces` (care nu se salvează). 44 | - La scriere (`writeExternal`), omite `parolaAcces`. 45 | - La citire (`readExternal`), setează `parolaAcces` cu `"necunoscut"`. -------------------------------------------------------------------------------- /Laborator 4/Laborator4/src/Main.java: -------------------------------------------------------------------------------- 1 | ////import java.util.Objects; 2 | //// 3 | //record numeRecord(int var, double var2){} 4 | //class Om{ 5 | // int varsta; 6 | // numeRecord rcd; 7 | // public Om(int varsta) { 8 | // this.varsta = varsta; 9 | // rcd = new numeRecord(2,3); 10 | // } 11 | // public int fct(){ 12 | // return rcd.var(); 13 | // } 14 | // 15 | //} 16 | //final class Person { 17 | // private final String name; 18 | // private final Om om; 19 | // private int age; 20 | // 21 | // public Person(String name, int age, Om om) { 22 | // this.name = name; 23 | // this.age = age; 24 | // this.om = new Om(om.varsta); 25 | // 26 | // } 27 | //// 28 | //// 29 | //// @Override 30 | //// public String toString() { 31 | //// return "Person{name='" + name + "', age=" + age + "}"; 32 | //// } 33 | //// 34 | //// 35 | //// @Override 36 | //// public boolean equals(Object o) { 37 | //// if (this == o) return true; 38 | //// if (!(o instanceof Person)) return false; 39 | //// Person person = (Person) o; 40 | //// return age == person.age && Objects.equals(name, person.name); 41 | //// } 42 | //// 43 | //// 44 | //// @Override 45 | //// public int hashCode() { 46 | //// return Objects.hash(name, age); 47 | //// } 48 | ////} 49 | //record Ceva(int x, float y){} 50 | //class altaClasa{} 51 | //final class WASF{ 52 | // final private int nr; 53 | // final altaClasa cls; 54 | // final Ceva cv; 55 | // public WASF(int nr, altaClasa cls) { 56 | // this.nr = nr; 57 | // this.cls = new altaClasa(); 58 | // this.cv = new Ceva(2,3); 59 | // } 60 | // public int val(){ 61 | // return cv.x(); 62 | // } 63 | //} 64 | 65 | public class Main { 66 | public static void main(String[] args) { 67 | String s = "abc"; 68 | String a = new String("abc"); 69 | System.out.print(a.equals(s)); 70 | } } 71 | -------------------------------------------------------------------------------- /Laborator 1/src/ex1/Wrappers.java: -------------------------------------------------------------------------------- 1 | package ex1; 2 | 3 | public class Wrappers { 4 | 5 | public static void main(String[] args) { 6 | 7 | // byte 8 | Byte byteValue = Byte.valueOf((byte)126); 9 | // Byte byteValue = Byte.valueOf(126) -> error: incompatible types: int cannot be converted to byte 10 | System.out.println("byteValue = " + byteValue); 11 | 12 | 13 | // Boxing: converting a primitive type to a wrapper class 14 | 15 | // short 16 | Short shortValue = 32767; 17 | System.out.println("shortValue = " + shortValue); 18 | 19 | // int 20 | Integer intValue = 2147483647; 21 | System.out.println("intValue = " + intValue); 22 | 23 | // long 24 | Long longValue = 9223372036854775807L; 25 | System.out.println("longValue = " + longValue); 26 | 27 | // float 28 | Float floatValue = 3.4028235E38f; 29 | System.out.println("floatValue = " + floatValue); 30 | 31 | // double 32 | Double doubleValue = 1.7976931348623157E308; 33 | System.out.println("doubleValue = " + doubleValue); 34 | 35 | // char 36 | Character charValue = 'A'; 37 | System.out.println("charValue = " + charValue); 38 | 39 | // boolean 40 | Boolean booleanValue = true; 41 | System.out.println("booleanValue = " + booleanValue); 42 | 43 | 44 | // Unboxing: converting a wrapper class to a primitive type 45 | int primitiveIntValue = intValue - 1; 46 | System.out.println("primitiveIntValue = " + primitiveIntValue); 47 | 48 | if (booleanValue) { 49 | System.out.println("booleanValue is true"); 50 | } 51 | 52 | if (longValue == Long.MAX_VALUE) { 53 | System.out.println("longValue is equal to Long.MAX_VALUE"); 54 | } 55 | 56 | if (intValue + floatValue < Float.MAX_VALUE) { 57 | System.out.println("intValue + floatValue is less than Float.MAX_VALUE"); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /Laborator 7/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ### Exercițiul 1 – Copiere fișier text 4 | **Cerință:** 5 | Scrie un program care citește un fișier `.txt` caracter cu caracter (folosind `FileReader`) și îl copiază într-un alt fișier. Afișează numărul total de caractere copiate. 6 | 7 | --- 8 | 9 | ### Exercițiul 2 – Prelucrare fișier binar (imagine BMP) 10 | **Cerință:** 11 | Scrie un program care citește un fișier `.bmp`, păstrează header-ul și inversează culorile fiecărui pixel (complementul RGB). Folosește `FileInputStream` și `FileOutputStream`. 12 | 13 | --- 14 | 15 | ### Exercițiul 3 – Folosirea RandomAccessFile 16 | **Cerință:** 17 | Creează un fișier binar în care scrii 10 valori `double`. Apoi, folosește `RandomAccessFile` pentru a: 18 | - afișa valoarea aflată la poziția 5, 19 | - înlocui valoarea de la poziția 3 cu dublul ei, 20 | - afișa toate valorile după modificare. 21 | 22 | --- 23 | 24 | ## Partea II: Lambda, Stream API, Clase Anonime 25 | 26 | ### Exercițiul 4 – Clasă anonimă cu interfață 27 | **Cerință:** 28 | Creează o interfață `Operatie` cu o metodă `double calculeaza(double a, double b)`. Creează o clasă anonimă care implementează această interfață și realizează adunarea și scăderea a două numere. 29 | 30 | --- 31 | 32 | ### Exercițiul 5 – Expresii lambda și sortare 33 | **Cerință:** 34 | Definește o clasă `Persoana` cu nume și vârstă. Creează o listă de persoane și sorteaz-o: 35 | - după vârstă, crescător (cu lambda), 36 | - după nume, alfabetic (cu `Comparator.comparing()` și method reference). 37 | 38 | --- 39 | 40 | ### Exercițiul 6 – Stream API – filtrare și mapare 41 | **Cerință:** 42 | Creează o listă de `Student`, fiecare cu nume și medie. Folosește Stream API pentru a: 43 | - filtra studenții cu media peste 8, 44 | - extrage doar numele acestora într-o listă, 45 | - afișa rezultatul cu `forEach`. 46 | 47 | --- 48 | 49 | ### Exercițiul 7 – Sumă și medie cu reduce 50 | **Cerință:** 51 | Creează un `List` cu note. Folosește `stream.reduce()` pentru a calcula: 52 | - suma tuturor notelor, 53 | - media notelor. 54 | """ -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/src/ex6.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Comparator; 3 | import java.util.List; 4 | public class ex6 { 5 | static abstract class Shape { 6 | abstract double getArea(); 7 | } 8 | static class Rectangle extends Shape { 9 | double width; 10 | double height; 11 | Rectangle(double width, double height) { 12 | this.width = width; 13 | this.height = height; 14 | } 15 | double getArea() { 16 | return width * height; 17 | } 18 | } 19 | interface Colorable { 20 | String getColor(); 21 | } 22 | static class Circle extends Shape implements Colorable { 23 | double radius; 24 | Circle(double radius) { 25 | this.radius = radius; 26 | } 27 | double getArea() { 28 | return Math.PI * radius * radius; 29 | } 30 | public String getColor() { 31 | return "blue"; 32 | } 33 | } 34 | public static void main(String[] args) { 35 | List shapes = new ArrayList<>(); 36 | shapes.add(new Rectangle(5, 4)); 37 | shapes.add(new Circle(3)); 38 | shapes.add(new Rectangle(2, 8)); 39 | shapes.add(new Circle(1.5)); 40 | for (Shape s : shapes) { 41 | System.out.print("Area: " + s.getArea()); 42 | if (s instanceof Colorable) { 43 | System.out.print(", Color: " + ((Colorable)s).getColor()); 44 | } 45 | System.out.println(); 46 | } 47 | shapes.sort(new Comparator() { 48 | public int compare(Shape s1, Shape s2) { 49 | return Double.compare(s1.getArea(), s2.getArea()); 50 | } 51 | }); 52 | System.out.println("Sorted shapes:"); 53 | for (Shape s : shapes) { 54 | System.out.print("Area: " + s.getArea()); 55 | if (s instanceof Colorable) { 56 | System.out.print(", Color: " + ((Colorable)s).getColor()); 57 | } 58 | System.out.println(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Laborator 4/Laborator4/src/ex6.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | public class ex6 { 4 | static class Owner { 5 | String name; 6 | public Owner(String name) { 7 | this.name = name; 8 | } 9 | } 10 | static class House { 11 | Address address; 12 | Owner owner; 13 | public House(Address address, Owner owner) { 14 | this.address = new Address(address); 15 | this.owner = owner; 16 | } 17 | } 18 | public static final class Address { 19 | private final String city; 20 | private final String street; 21 | public Address(String city, String street) { 22 | this.city = city; 23 | this.street = street; 24 | } 25 | public Address(Address other) { 26 | this.city = other.city; 27 | this.street = other.street; 28 | } 29 | public String getCity() { 30 | return city; 31 | } 32 | public String getStreet() { 33 | return street; 34 | } 35 | public String toString() { 36 | return city + ", " + street; 37 | } 38 | } 39 | public record Student(String name, int group, double grade) {} 40 | public static void main(String[] args) { 41 | Address address = new Address("New York", "5th Avenue"); 42 | Owner owner = new Owner("Michael"); 43 | House house = new House(address, owner); 44 | Student s1 = new Student("Alice", 101, 9.5); 45 | Student s2 = new Student("Bob", 102, 8.0); 46 | Student s3 = new Student("Charlie", 101, 7.5); 47 | StringBuilder report = new StringBuilder(); 48 | report.append("House Information:\n"); 49 | report.append("Address: ").append(house.address.toString()).append("\n"); 50 | report.append("Owner: ").append(house.owner.name).append("\n\n"); 51 | report.append("Student Information:\n"); 52 | report.append(s1.toString()).append("\n"); 53 | report.append(s2.toString()).append("\n"); 54 | report.append(s3.toString()).append("\n"); 55 | System.out.println(report.toString()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Laborator 1/src/ex1/Operators.java: -------------------------------------------------------------------------------- 1 | package ex1; 2 | 3 | public class Operators { 4 | 5 | public static void main(String[] args) { 6 | int a = 20; 7 | int b = 11; 8 | 9 | // arithmetic 10 | System.out.println("a + b = " + (a + b)); 11 | System.out.println("a - b = " + (a - b)); 12 | System.out.println("a * b = " + (a * b)); 13 | System.out.println("a / b = " + (a / b)); 14 | System.out.println("a % b = " + (a % b)); 15 | 16 | System.out.println("a++ = " + (a++)); 17 | System.out.println("++a = " + (++a)); 18 | System.out.println("a-- = " + (a--)); 19 | System.out.println("--a = " + (--a)); 20 | 21 | System.out.println("a > b = " + (a > b)); 22 | System.out.println("a < b = " + (a < b)); 23 | System.out.println("a >= b = " + (a >= b)); 24 | System.out.println("a <= b = " + (a <= b)); 25 | System.out.println("a == b = " + (a == b)); 26 | System.out.println("a != b = " + (a != b)); 27 | 28 | // bit-wise 29 | System.out.println("a & b = " + (a & b)); 30 | System.out.println("a | b = " + (a | b)); 31 | System.out.println("a ^ b = " + (a ^ b)); 32 | System.out.println("~a = " + ~a); 33 | System.out.println("a << 2 = " + (a << 2)); 34 | System.out.println("a >> 2 = " + (a >> 2)); 35 | System.out.println("a >>> 2 = " + (a >>> 2)); // unsigned shift right (adds a 0 to the leftmost position) 36 | 37 | // logical 38 | boolean x = true; 39 | boolean y = false; 40 | 41 | System.out.println("x && y = " + (x && y)); 42 | System.out.println("x & y = " + (x & y)); 43 | System.out.println("x || y = " + (x || y)); 44 | System.out.println("x | y = " + (x | y)); 45 | System.out.println("!x = " + !x); 46 | 47 | 48 | // String concatenation 49 | String s1 = "Hello,"; 50 | String s2 = "World"; 51 | System.out.println("s1 + s2 = " + s1 + s2); 52 | 53 | // instanceof 54 | Object object = "Hello, World!"; 55 | if (object instanceof String) { 56 | System.out.println("object is a String"); 57 | } 58 | 59 | } 60 | } -------------------------------------------------------------------------------- /Laborator 5/ReadMe.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Exercițiul 1 – Introducere în try-catch-finally 4 | 5 | **Cerințe:** 6 | 7 | - Scrieți o metodă `readInteger()` care citește un număr întreg și tratează `NumberFormatException`. 8 | - Utilizați blocurile `try`, `catch` și `finally`. 9 | - Afișați mesaje relevante pentru fiecare caz. 10 | 11 | --- 12 | 13 | ## Exercițiul 2 – Multi-catch și Ordinea Blocurilor Catch 14 | 15 | **Cerințe:** 16 | 17 | - Scrieți o metodă `parseAndDivide(String a, String b)` care aruncă `NumberFormatException` și `ArithmeticException`. 18 | - Tratați ambele într-un bloc `multi-catch`. 19 | - Apelați metoda din `main()` cu date greșite pentru a testa ambele excepții. 20 | 21 | --- 22 | 23 | ## Exercițiul 3 – Excepții Personalizate și Propagare 24 | 25 | **Cerințe:** 26 | 27 | - Definiți `InvalidGradeException` extinzând `Exception`. 28 | - Creați metoda `validateGrade(int grade)` care aruncă excepția dacă nota nu e între 1 și 10. 29 | - Tratați excepția în `main()`. 30 | 31 | --- 32 | 33 | ## Exercițiul 4 – Interfețe și Implementare 34 | 35 | **Cerințe:** 36 | 37 | - Creați interfața `Shape` cu metodele `getArea()` și `getPerimeter()`. 38 | - Implementați-o în clasele `Circle` și `Rectangle`. 39 | - Creați un array de tip `Shape` și afișați rezultatele pentru fiecare formă. 40 | 41 | --- 42 | 43 | ## Exercițiul 5 – Comparable pentru Sortare 44 | 45 | **Cerințe:** 46 | 47 | - Clasa `Student` să implementeze `Comparable`. 48 | - Suprascrieți `compareTo` pentru a sorta după notă. 49 | - Creați o listă de studenți, sortați-o și afișați rezultatul. 50 | 51 | --- 52 | 53 | ## Exercițiul 6 – Combinația Excepții + Interfețe 54 | 55 | **Cerințe:** 56 | 57 | - Interfața `DatabaseActions` cu metode `connect()` și `executeQuery(String)`. 58 | - Clasa `FakeDatabase` să arunce `IllegalStateException` dacă e offline și `SQLException` pentru query invalid. 59 | - Tratați ambele excepții separat în `main()`. 60 | 61 | --- 62 | 63 | ## Exercițiul 7 – Gestionare cu List 64 | 65 | **Cerințe:** 66 | 67 | - Clasa `Product` cu `id`, `name`, `price`. 68 | - Folosiți un `ArrayList`, parcurgeți-l și sortați-l după preț. 69 | - Folosiți `Iterator` și `Comparator`. 70 | 71 | --- 72 | 73 | ## Exercițiul 8 – Gestionare cu Map 74 | 75 | **Cerințe:** 76 | 77 | - Clasa `Student` cu `id`, `name`, `grade`. 78 | - Folosiți un `HashMap`. 79 | - Afișați conținutul și actualizați nota unui student. 80 | 81 | --- -------------------------------------------------------------------------------- /Laborator 6/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | ## Exercițiul 1 – Utilizarea enum pentru stări de comandă 3 | 4 | **Cerințe:** 5 | - Definește un `enum` numit `OrderStatus` cu valorile: `PLACED`, `SHIPPED`, `DELIVERED`, `CANCELLED`. 6 | - Creează o clasă `Order` cu atribute: `id`, `status`. 7 | - Adaugă metode care schimbă starea comenzii și o metodă `printStatus()`. 8 | 9 | --- 10 | 11 | ## Exercițiul 2 – Enum cu câmpuri și metode 12 | 13 | **Cerințe:** 14 | - Creează un `enum` numit `Currency` cu valori: `EUR`, `USD`, `GBP`, fiecare având un curs față de RON. 15 | - Fiecare constantă are un atribut `exchangeRate`. 16 | - Creează o metodă `convertToRON(double amount)` care folosește cursul. 17 | 18 | --- 19 | 20 | ## Exercițiul 3 – Shallow vs Deep Copy 21 | 22 | **Cerințe:** 23 | - Creează clasa `Departament` cu atribute `nume` și `cod`. 24 | - Creează clasa `Angajat` cu nume și un obiect `Departament`. 25 | - Fă o implementare `clone()` shallow și una deep în `Angajat`. 26 | - Testează prin modificarea obiectului copiat și observă efectul asupra originalului. 27 | 28 | --- 29 | 30 | ## Exercițiul 4 – Cloneable și instanceof 31 | 32 | **Cerințe:** 33 | - Creează o clasă `Carte` care implementează `Cloneable`. 34 | - Suprascrie metoda `clone()` și folosește `instanceof` pentru verificarea. 35 | - Creează o metodă statică care primește un obiect și îl clonează dacă este `Cloneable`. 36 | 37 | --- 38 | 39 | ## Exercițiul 5 – Clasă generică Box 40 | 41 | **Cerințe:** 42 | - Creează o clasă generică `Box` cu metodele `setValue(T)` și `getValue()`. 43 | - Testează folosind tipuri diferite: `String`, `Integer`, `Student`. 44 | 45 | --- 46 | 47 | ## Exercițiul 6 – Metodă generică cu wildcard 48 | 49 | **Cerințe:** 50 | - Creează o metodă `printList(List)` care afișează toate elementele. 51 | - Testează cu `List`, `List`, etc. 52 | 53 | --- 54 | 55 | ## Exercițiul 7 – Interfață cu metode default și static 56 | 57 | **Cerințe:** 58 | - Creează interfața `Logger` cu: 59 | - metodă `default void log(String msg)` 60 | - metodă `static boolean isEmpty(String str)` 61 | - Creează o clasă care implementează `Logger` și folosește aceste metode. 62 | 63 | --- 64 | 65 | ## Exercițiul 8 – Interfețe `sealed`, `non-sealed` și `final` 66 | 67 | **Cerințe:** 68 | - Creează o clasă `Animal` definită ca `sealed`. 69 | - Permite doar clasele `Pisica` (`final`) și `Caine` (`non-sealed`) să o extindă. 70 | - Creează instanțe și testează comportamentul în `main`. 71 | 72 | --- -------------------------------------------------------------------------------- /Laborator 1/src/ex1/Flow.java: -------------------------------------------------------------------------------- 1 | package ex1; 2 | 3 | public class Flow { 4 | 5 | public static void main(String[] args) { 6 | //if-else if-else 7 | 8 | int a = 10; 9 | int b = 20; 10 | 11 | if (a > b) { 12 | System.out.println("a is greater than b"); 13 | } else if (a < b) { 14 | System.out.println("b is greater than a"); 15 | } else { 16 | System.out.println("a is equal to b"); 17 | } 18 | 19 | // for loop 20 | for (int i = 0; i < 3; i++) { 21 | System.out.println("for loop, i = " + i); 22 | } 23 | 24 | // enhanced for loop 25 | for (int i : new int[] {1, 2, 3}) { 26 | System.out.println("enhanced for loop, i = " + i); 27 | } 28 | 29 | 30 | 31 | 32 | int[] integers = new int[]{1, 2, 3, 4}; 33 | 34 | for (int i : integers) { 35 | if ( i == 2) { 36 | continue; 37 | } else if ( i == 3) { 38 | System.out.println("enhanced for loop, breaking, i = " + i); 39 | break; 40 | } 41 | System.out.println("enhanced for loop, i = " + i); 42 | } 43 | 44 | // while loop 45 | int i = 0; 46 | while (i < 3) { 47 | System.out.println("while loop, i = " + i); 48 | i++; 49 | } 50 | 51 | // do while loop 52 | i = 0; 53 | do { 54 | System.out.println("do while loop, i = " + i); 55 | i++; 56 | } while (i < 3); 57 | 58 | // switch 59 | i = 1; 60 | switch (i) { 61 | case 1: 62 | System.out.println("switch - i is 1"); 63 | break; 64 | case 2: 65 | System.out.println("switch - i is 2"); 66 | break; 67 | default: 68 | System.out.println("switch - i is neither 1 nor 2"); 69 | } 70 | 71 | String myString = "test"; 72 | switch (myString) { 73 | case "test": 74 | System.out.println("switch - myString is test"); 75 | break; 76 | case "not test": 77 | System.out.println("switch - myString is not test"); 78 | break; 79 | default: 80 | System.out.println("switch - myString is something else"); 81 | } 82 | 83 | 84 | ; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/src/CollectionsSupportDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class CollectionsSupportDemo { 4 | 5 | public static void main(String[] args) { 6 | // 1. ArrayList 7 | List fruits = new ArrayList<>(); 8 | fruits.add("Apple"); 9 | fruits.add("Banana"); 10 | fruits.add("Orange"); 11 | fruits.add("Banana"); // duplicate allowed 12 | 13 | System.out.println("ArrayList contents:"); 14 | for (String fruit : fruits) { 15 | System.out.println(fruit); 16 | } 17 | 18 | // 2. LinkedList used as a queue 19 | LinkedList numbers = new LinkedList<>(); 20 | numbers.add(10); 21 | numbers.add(20); 22 | numbers.add(30); 23 | 24 | System.out.println("\nLinkedList as Queue:"); 25 | System.out.println("Head: " + numbers.peek()); 26 | numbers.poll(); 27 | System.out.println("After poll: " + numbers); 28 | 29 | // 3. HashSet 30 | Set animals = new HashSet<>(); 31 | animals.add("Cat"); 32 | animals.add("Dog"); 33 | animals.add("Cat"); // duplicate ignored 34 | animals.add("Elephant"); 35 | 36 | System.out.println("\nHashSet (no duplicates):"); 37 | for (String animal : animals) { 38 | System.out.println(animal); 39 | } 40 | 41 | // 4. TreeSet 42 | Set scores = new TreeSet<>(); 43 | scores.add(50); 44 | scores.add(90); 45 | scores.add(30); 46 | 47 | System.out.println("\nTreeSet (sorted):"); 48 | for (Integer score : scores) { 49 | System.out.println(score); 50 | } 51 | 52 | // 5. HashMap 53 | Map students = new HashMap<>(); 54 | students.put(1, "Ana"); 55 | students.put(2, "Ion"); 56 | students.put(3, "Maria"); 57 | 58 | System.out.println("\nHashMap contents:"); 59 | for (Map.Entry entry : students.entrySet()) { 60 | System.out.println("ID: " + entry.getKey() + " -> Name: " + entry.getValue()); 61 | } 62 | 63 | // 6. TreeMap 64 | Map capitals = new TreeMap<>(); 65 | capitals.put("RO", "Bucharest"); 66 | capitals.put("FR", "Paris"); 67 | capitals.put("DE", "Berlin"); 68 | 69 | System.out.println("\nTreeMap (sorted by keys):"); 70 | for (Map.Entry entry : capitals.entrySet()) { 71 | System.out.println(entry.getKey() + " => " + entry.getValue()); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /Laborator 9/bd/src/StudentService.java: -------------------------------------------------------------------------------- 1 | import java.sql.*; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | 5 | class StudentService { 6 | 7 | public void createTable() { 8 | try (Connection con = DbConnection.getInstance(); 9 | Statement stmt = con.createStatement()) { 10 | stmt.executeUpdate(""" 11 | CREATE TABLE IF NOT EXISTS students ( 12 | id INT AUTO_INCREMENT PRIMARY KEY, 13 | name VARCHAR(100), 14 | grade DOUBLE 15 | ) 16 | """); 17 | } catch (SQLException e) { 18 | e.printStackTrace(); 19 | } 20 | } 21 | 22 | public void insert(String name, double grade) { 23 | String sql = "INSERT INTO students (name, grade) VALUES (?, ?)"; 24 | try (Connection con = DbConnection.getInstance(); 25 | PreparedStatement ps = con.prepareStatement(sql)) { 26 | ps.setString(1, name); 27 | ps.setDouble(2, grade); 28 | ps.executeUpdate(); 29 | } catch (SQLException e) { 30 | e.printStackTrace(); 31 | } 32 | } 33 | 34 | public List getAll() { 35 | List list = new ArrayList<>(); 36 | String sql = "SELECT * FROM students"; 37 | try (Connection con = DbConnection.getInstance(); 38 | Statement stmt = con.createStatement(); 39 | ResultSet rs = stmt.executeQuery(sql)) { 40 | while (rs.next()) { 41 | list.add(new Student( 42 | rs.getInt("id"), 43 | rs.getString("name"), 44 | rs.getDouble("grade") 45 | )); 46 | } 47 | } catch (SQLException e) { 48 | e.printStackTrace(); 49 | } 50 | return list; 51 | } 52 | 53 | public void updateGrade(int id, double newGrade) { 54 | String sql = "UPDATE students SET grade=? WHERE id=?"; 55 | try (Connection con = DbConnection.getInstance(); 56 | PreparedStatement ps = con.prepareStatement(sql)) { 57 | ps.setDouble(1, newGrade); 58 | ps.setInt(2, id); 59 | ps.executeUpdate(); 60 | } catch (SQLException e) { 61 | e.printStackTrace(); 62 | } 63 | } 64 | 65 | public void delete(int id) { 66 | String sql = "DELETE FROM students WHERE id=?"; 67 | try (Connection con = DbConnection.getInstance(); 68 | PreparedStatement ps = con.prepareStatement(sql)) { 69 | ps.setInt(1, id); 70 | ps.executeUpdate(); 71 | } catch (SQLException e) { 72 | e.printStackTrace(); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Info/project.md: -------------------------------------------------------------------------------- 1 | # Programare Orientată pe Obiecte - proiect 2 | 3 | Fiecare student va lucra la un proiect individual. Proiectul este structurat în mai multe etape. 4 | Condiția de punctare a proiectelor: 5 | - să nu prezinte erori de compilare 6 | - să se implementeze/abordeze toate cerințele date 7 | 8 | ## Condiții de punctare 9 | * fără erori de compilare 10 | * prezentarea în cadrul laboratorului 11 | 12 | ## Termene de predare 13 | * prima etapă: 14 - 18 Aprilie 14 | * a doua etapă: 2 - 6 Iunie 15 | 16 | ## Etapa 1 17 | 18 | ### 1. Definirea sistemului 19 | Să se creeze o listă pe baza temei alese cu cel puțin 10 acțiuni / interogări care se pot face în cadrul sistemului și o listă cu cel puțin 8 tipuri de obiecte. Prezentați sistemul, împreună cu acțiunile / obiectele alese. 20 | 21 | ### 2. Implementare 22 | Să se implementeze în limbajul Java o aplicație pe baza celor definite pa primul punct. 23 | Aplicația va conține: 24 | - clase simple cu atribute private / protected și metode de acces; 25 | - cel puțin 2 colecții diferite, capabile să gestioneze obiectele definite anterior (ex. List, Set, Map etc.), dintre care cel puțin una să fie sortată. În cazul în care până la data de predare a primei etape nu se parcurg colecțiile, se vor folosi array-uri uni/bidimensionale; 26 | - utilizarea moștenirii pentru crearea de clase adiționale și utilizarea lor în cadrul colecțiilor; 27 | - cel puțin o clasă serviciu, care să expună operațiile sistemului; 28 | - o clasă Main, din care sunt făcute apeluri către servicii. 29 | 30 | ## Etapa 2 31 | 32 | ### 1. Extindeți proiectul din prima etapă, prin realizarea persistenței utilizând o bază de date relațională și JDBC. 33 | Se vor realiza servicii care să expună operații CRUD (CREATE, READ, UPDATE, DELETE) pentru cel puțin 4 dintre clasele definite. Se vor realiza servicii singleton generice pentru citirea și scrierea în/din baza de date. 34 | 35 | ### 2. Realizarea unui serviciu de audit 36 | Se va realiza un serviciu care va scrie într-un fișier de tip CSV de fiecare dată când este executată una din acțiunile descrise în prima etapă. Structura fișierului: *nume_acțiune, timestamp*. 37 | 38 | 39 | # Teme sugerate 40 | 1. catalog (student, materie, profesor) 41 | 2. biblioteca (secțiuni, cărți, autori, cititori) 42 | 3. programare cabinet medical (client, medic, programare) 43 | 4. gestiune stocuri magazin (categorii, produse, distribuitori) 44 | 5. aplicație bancară (conturi, extras de cont, tranzacții, carduri, servicii) 45 | 6. platformă e-learning (cursuri, utilizatori, cursanți, quiz-uri) 46 | 7. sistem licitații (licitații, bids, produse, utilizatori) 47 | 8. platforma food delivery (localuri, comenzi, șoferi, useri) 48 | 9. platformă împrumut cărți - tip bookster (companii afiliate, utilizatori, cărți) 49 | 10. platformă e-ticketing (evenimente, locații, clienți) -------------------------------------------------------------------------------- /Laborator 8/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | ## Exercițiul 1 – Gestionare listă de studenți cu `ArrayList` 3 | 4 | **Cerințe:** 5 | - Creează o clasă `Student` cu nume, grupă și medie. 6 | - Creează un `ArrayList`. 7 | - Adaugă cel puțin 6 studenți, cu unele medii identice. 8 | - Sortează studenții după medie descrescător. 9 | - Găsește și afișează studentul cu media cea mai mare. 10 | - Caută studenți cu nume care încep cu o literă introdusă de utilizator. 11 | 12 | --- 13 | 14 | ## Exercițiul 2 – `LinkedList` ca listă de taskuri 15 | 16 | **Cerințe:** 17 | - Creează o listă dublu înlănțuită `LinkedList` pentru taskuri. 18 | - Adaugă taskuri la început și sfârșit. 19 | - Marchează un task ca finalizat (prin `remove()`). 20 | - Afișează taskurile în ordine inversă folosind `descendingIterator()`. 21 | - Creează o funcție care mută toate taskurile care conțin cuvântul „urgent” în fruntea listei. 22 | 23 | --- 24 | 25 | ## Exercițiul 3 – `HashSet` și `TreeSet`: mulțimi de orașe 26 | 27 | **Cerințe:** 28 | - Creează două colecții: `HashSet` și `TreeSet`. 29 | - Adaugă 10 orașe, unele duplicate. 30 | - Compară mărimea și conținutul mulțimilor. 31 | - Afișează toate orașele în ordine alfabetică (cu `TreeSet`). 32 | - Verifică dacă un oraș citit de la tastatură există în `HashSet`. 33 | 34 | --- 35 | 36 | ## Exercițiul 4 – `TreeSet` cu obiecte și `Comparable` 37 | 38 | **Cerințe:** 39 | - Creează o clasă `Produs` cu `nume` și `pret`, care implementează `Comparable` (după preț). 40 | - Creează un `TreeSet` și adaugă produse. 41 | - Afișează produsele sortate crescător după preț. 42 | - Creează un al doilea `TreeSet` cu un `Comparator` care sortează descrescător după nume. 43 | 44 | --- 45 | 46 | ## Exercițiul 5 – Catalog de note cu `HashMap` 47 | 48 | **Cerințe:** 49 | - Creează un `HashMap>` unde cheia e numele studentului și valoarea e lista de note. 50 | - Adaugă 4–5 studenți cu note. 51 | - Calculează media fiecărui student. 52 | - Afișează doar studenții cu medii ≥ 8. 53 | - Afișează perechiile `nume: notă1, notă2...` folosind `entrySet()`. 54 | 55 | --- 56 | 57 | ## Exercițiul 6 – Prețuri sortate cu `TreeMap` 58 | 59 | **Cerințe:** 60 | - Creează un `TreeMap` în care cheia e un produs, valoarea e prețul. 61 | - Adaugă cel puțin 6 produse. 62 | - Afișează lista produselor sortate alfabetic. 63 | - Calculează prețul total. 64 | - Caută și afișează produsul cel mai scump. 65 | 66 | --- 67 | 68 | ## Exercițiul 7 – Simulare `Queue` și `Deque` 69 | 70 | **Cerințe:** 71 | - Simulează coada de la un ghișeu folosind `Queue`. Adaugă clienți și procesează-i (`poll()`). 72 | - Creează un `Deque` pentru acțiuni în aplicație (undo/redo). 73 | - Adaugă acțiuni cu `addFirst()`. 74 | - Realizează undo prin `removeFirst()` și arată starea listei după fiecare operație. 75 | 76 | --- -------------------------------------------------------------------------------- /Laborator 10/Lab10/src/ServerApp.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | import java.util.*; 4 | 5 | class LoggerThread extends Thread { 6 | private final List messages; 7 | 8 | public LoggerThread(List messages) { 9 | this.messages = messages; 10 | } 11 | 12 | public void run() { 13 | try { 14 | while (true) { 15 | synchronized (messages) { 16 | while (messages.isEmpty()) messages.wait(); 17 | String msg = messages.remove(0); 18 | System.out.println("LOG: " + msg); 19 | } 20 | Thread.sleep(500); 21 | } 22 | } catch (InterruptedException e) { 23 | System.out.println("Logger thread interrupted"); 24 | } 25 | } 26 | } 27 | 28 | class Processor implements Runnable { 29 | private final List messages; 30 | 31 | public Processor(List messages) { 32 | this.messages = messages; 33 | } 34 | 35 | public void run() { 36 | try { 37 | while (true) { 38 | synchronized (messages) { 39 | while (messages.isEmpty()) messages.wait(); 40 | String msg = messages.remove(0).toUpperCase(); 41 | System.out.println("Processed: " + msg); 42 | } 43 | Thread.sleep(300); 44 | } 45 | } catch (InterruptedException e) { 46 | System.out.println("Processor thread interrupted"); 47 | } 48 | } 49 | } 50 | 51 | public class ServerApp { 52 | public static void main(String[] args) throws Exception { 53 | ServerSocket ss = new ServerSocket(1234); 54 | System.out.println("Server is listening port 1234..."); 55 | Socket s = ss.accept(); 56 | 57 | BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 58 | BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 59 | 60 | List sharedMessages = new ArrayList<>(); 61 | 62 | LoggerThread logger = new LoggerThread(sharedMessages); 63 | Thread processor = new Thread(new Processor(sharedMessages)); 64 | logger.start(); 65 | processor.start(); 66 | 67 | String linie; 68 | while (!(linie = in.readLine()).equalsIgnoreCase("STOP")) { 69 | synchronized (sharedMessages) { 70 | sharedMessages.add(linie); 71 | sharedMessages.notifyAll(); 72 | } 73 | 74 | out.write("Message received\n"); 75 | out.flush(); 76 | 77 | if (linie.equalsIgnoreCase("INTERRUPT")) { 78 | logger.interrupt(); 79 | processor.interrupt(); 80 | break; 81 | } 82 | } 83 | 84 | s.close(); 85 | ss.close(); 86 | System.out.println("Server stop"); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /Laborator 2/Laborator2/src/ShallowDeep.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | class PersoanaShallow { 3 | private String nume; 4 | private int varsta; 5 | private double[] venit; 6 | 7 | public PersoanaShallow(String nume, int varsta, double[] venit) { 8 | this.nume = nume; 9 | this.varsta = varsta; 10 | 11 | this.venit = venit; 12 | } 13 | 14 | 15 | public PersoanaShallow(PersoanaShallow other) { 16 | this.nume = other.nume; 17 | this.varsta = other.varsta; 18 | 19 | this.venit = other.venit; 20 | } 21 | 22 | public void setVenit(int luna, double suma) { 23 | if (luna >= 0 && luna < venit.length) { 24 | venit[luna] = suma; 25 | } 26 | } 27 | 28 | public double getVenit(int luna) { 29 | if (luna >= 0 && luna < venit.length) 30 | return venit[luna]; 31 | return 0; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "PersoanaShallow{" + 37 | "nume='" + nume + '\'' + 38 | ", varsta=" + varsta + 39 | ", venit=" + Arrays.toString(venit) + 40 | '}'; 41 | } 42 | } 43 | 44 | 45 | class PersoanaDeep { 46 | private String nume; 47 | private int varsta; 48 | private double[] venit; 49 | 50 | public PersoanaDeep(String nume, int varsta, double[] venit) { 51 | this.nume = nume; 52 | this.varsta = varsta; 53 | 54 | this.venit = new double[venit.length]; 55 | for (int i = 0; i < venit.length; i++) { 56 | this.venit[i] = venit[i]; 57 | } 58 | } 59 | 60 | // Constructor de copiere (deep copy) 61 | public PersoanaDeep(PersoanaDeep other) { 62 | this.nume = other.nume; 63 | this.varsta = other.varsta; 64 | // this.venit = other.venit; 65 | this.venit = new double[other.venit.length]; 66 | for (int i = 0; i < other.venit.length; i++) { 67 | this.venit[i] = other.venit[i]; 68 | } 69 | } 70 | 71 | public void setVenit(int luna, double suma) { 72 | if(luna >= 0 && luna < venit.length) { 73 | venit[luna] = suma; 74 | } 75 | } 76 | 77 | public double getVenit(int luna) { 78 | if(luna >= 0 && luna < venit.length) 79 | return venit[luna]; 80 | return 0; 81 | } 82 | 83 | @Override 84 | public String toString() { 85 | return "PersoanaDeep{" + 86 | "nume='" + nume + '\'' + 87 | ", varsta=" + varsta + 88 | ", venit=" + Arrays.toString(venit) + 89 | '}'; 90 | } 91 | 92 | } 93 | 94 | public class ShallowDeep { 95 | public static void main(String[] args) { 96 | double[] venitInitial = {2000.25, 3000.50, 4000.75}; 97 | PersoanaShallow ps1 = new PersoanaShallow("Ana", 25, venitInitial); 98 | PersoanaShallow ps2 = new PersoanaShallow(ps1); 99 | ps2.setVenit(1, 5000); 100 | 101 | System.out.println("Shallow copy:"); 102 | System.out.println("Original: " + ps1); 103 | System.out.println("Copia: " + ps2); 104 | 105 | 106 | // Demonstrarea deep copy 107 | double[] venitInitialDeep = {2000.25, 3000.50, 4000.75}; 108 | PersoanaDeep pd1 = new PersoanaDeep("Ion", 30, venitInitialDeep); 109 | PersoanaDeep pd2 = new PersoanaDeep(pd1); 110 | 111 | 112 | pd2.setVenit(1, 5000); 113 | 114 | System.out.println("\nDeep"); 115 | System.out.println("Original: " + pd1); 116 | System.out.println("Copy: " + pd2); 117 | 118 | 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /Laborator 2/Laborator2/src/ex3/Persoana.java: -------------------------------------------------------------------------------- 1 | package ex3; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Persoana { 6 | private String nume; 7 | private int varsta; 8 | private double[] venit; // venit pentru 12 luni 9 | private static int nrPersoane = 0; 10 | private int id; // ID unic pentru fiecare persoana 11 | 12 | // Bloc static de init 13 | static { 14 | nrPersoane = 0; 15 | System.out.println("Bloc static: nrPersoane initializat la 0"); 16 | } 17 | 18 | // Bloc nestatic 19 | { 20 | id = ++nrPersoane; 21 | System.out.println("Bloc de inițializare: ID atribuit: " + id); 22 | } 23 | 24 | // Constructor implicit 25 | public Persoana() { 26 | this.nume = "Necunoscut"; 27 | this.varsta = 0; 28 | this.venit = new double[12]; // 12 luni 29 | } 30 | 31 | // Constructor parametrizat 32 | public Persoana(String nume, int varsta, double[] venit) { 33 | this.nume = nume; 34 | this.varsta = varsta; 35 | // Deep copy a array-ului venit 36 | this.venit = new double[12]; 37 | for (int i = 0; i < Math.min(venit.length, 12); i++) { 38 | this.venit[i] = venit[i]; 39 | } 40 | } 41 | 42 | // Constructor de copiere 43 | public Persoana(Persoana other) { 44 | this.nume = other.nume; 45 | this.varsta = other.varsta; 46 | // Deep copy al array-ului venit 47 | this.venit = new double[12]; 48 | for (int i = 0; i < other.venit.length; i++) { 49 | this.venit[i] = other.venit[i]; 50 | } 51 | // ID-ul se atribuie din nou prin blocul nestatic, deci nu copiem id-ul existent 52 | } 53 | 54 | // Getteri și setteri 55 | public String getNume() { 56 | return nume; 57 | } 58 | public void setNume(String nume) { 59 | this.nume = nume; 60 | } 61 | public int getVarsta() { 62 | return varsta; 63 | } 64 | public void setVarsta(int varsta) { 65 | this.varsta = varsta; 66 | } 67 | public double[] getVenit() { 68 | return venit; 69 | } 70 | public void setVenit(double[] venit) { 71 | // Realizam deep copy 72 | this.venit = new double[12]; 73 | for (int i = 0; i < Math.min(venit.length, 12); i++) { 74 | this.venit[i] = venit[i]; 75 | } 76 | } 77 | public double getVenitLuna(int luna) { 78 | if (luna >= 0 && luna < venit.length) { 79 | return venit[luna]; 80 | } 81 | return 0; 82 | } 83 | public void setVenitLuna(int luna, double suma) { 84 | if (luna >= 0 && luna < venit.length) { 85 | this.venit[luna] = suma; 86 | } 87 | } 88 | 89 | @Override 90 | public String toString() { 91 | return "Persoana{" + 92 | "id=" + id + 93 | ", nume='" + nume + '\'' + 94 | ", varsta=" + varsta + 95 | ", venit=" + Arrays.toString(venit) + 96 | '}'; 97 | } 98 | 99 | 100 | public static void main(String[] args) { 101 | // Testare constructor implicit 102 | Persoana p1 = new Persoana(); 103 | p1.setNume("Felix ?"); 104 | p1.setVarsta(30); 105 | p1.setVenit(new double[]{1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100}); 106 | System.out.println(p1); 107 | 108 | // Testare constructor parametrizat 109 | double[] venitMaria = {2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100}; 110 | Persoana p2 = new Persoana("Otilia Margulescu", 28, venitMaria); 111 | System.out.println(p2); 112 | 113 | // Testare constructor de copiere 114 | Persoana p3 = new Persoana(p2); 115 | p3.setNume("Cealalta Otilia"); 116 | p3.setVenitLuna(0, 5000); 117 | System.out.println("Dupa modificare:"); 118 | System.out.println("Original: " + p2); 119 | System.out.println("Copie: " + p3); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /Laborator 1/src/clasa/Car.java: -------------------------------------------------------------------------------- 1 | package clasa; 2 | 3 | public class Car extends Vehicle implements Fuel { 4 | 5 | 6 | private static final int MAX_SPEED = 240; 7 | 8 | 9 | private String brand; 10 | private String model; 11 | private int year; 12 | private double fuelLevel; 13 | 14 | private static int totalCars = 0; 15 | 16 | // Constructors 17 | public Car() { 18 | this("Unknown", "Unknown", 2000, 0); 19 | } 20 | 21 | public Car(String brand, String model, int year, double fuelLevel) { 22 | this.brand = brand; 23 | this.model = model; 24 | this.year = year; 25 | this.fuelLevel = fuelLevel; 26 | totalCars++; // Increment car count 27 | } 28 | 29 | public Car(Car another) { 30 | this.brand = another.brand; 31 | this.model = another.model; 32 | this.year = another.year; 33 | this.fuelLevel = another.fuelLevel; 34 | } 35 | 36 | // Getters/Setters 37 | public String getBrand() { return brand; } 38 | public void setBrand(String brand) { this.brand = brand; } 39 | 40 | public String getModel() { return model; } 41 | public void setModel(String model) { this.model = model; } 42 | 43 | public int getYear() { return year; } 44 | public void setYear(int year) { this.year = year; } 45 | 46 | public double getFuelLevel() { return fuelLevel; } 47 | public void setFuelLevel(double fuelLevel) { this.fuelLevel = fuelLevel; } 48 | 49 | public static int getTotalCars() { 50 | return totalCars; 51 | } 52 | 53 | // Implementing abstract and interface methods 54 | @Override 55 | public void start() { 56 | System.out.println(brand + " " + model + " is starting."); 57 | } 58 | 59 | @Override 60 | public void refuel(int amount) { 61 | this.fuelLevel += amount; 62 | System.out.println("Refueled " + amount + " liters. Current fuel level: " + fuelLevel); 63 | } 64 | 65 | // Overloaded Methods 66 | public void accelerate() { 67 | System.out.println("Accelerating..."); 68 | } 69 | 70 | public void accelerate(int speed) { 71 | if (speed > MAX_SPEED) { 72 | System.out.println("Speed limit exceeded"); 73 | } else { 74 | System.out.println("Accelerating to " + speed + " km/h"); 75 | } 76 | } 77 | 78 | // Inner Class 79 | class Engine { 80 | private int horsepower; 81 | 82 | public Engine(int horsepower) { 83 | this.horsepower = horsepower; 84 | } 85 | 86 | public void displayInfo() { 87 | System.out.println("Engine Horsepower: " + horsepower); 88 | } 89 | } 90 | 91 | // Exception handling example 92 | public void drive(int distance) { 93 | try { 94 | if (fuelLevel <= 0) { 95 | throw new Exception("Fuel is empty"); 96 | } 97 | System.out.println("Driving " + distance + " km"); 98 | fuelLevel -= distance * 0.1; // Example fuel consumption 99 | } catch (Exception e) { 100 | System.out.println("Error: " + e.getMessage()); 101 | } 102 | } 103 | 104 | // Generic method example 105 | public void showData(T data) { 106 | System.out.println("Data: " + data); 107 | } 108 | 109 | // Static block 110 | static { 111 | System.out.println("Car class loaded"); 112 | } 113 | 114 | // Main method (entry point) 115 | public static void main(String[] args) { 116 | // Creating Objects 117 | Car car1 = new Car("Toyota", "Corolla", 2021, 50); 118 | Car car2 = new Car("BMW", "M3", 2023, 60); 119 | 120 | // Using Methods 121 | car1.start(); 122 | car2.accelerate(200); 123 | car1.refuel(20); 124 | 125 | // Exception Handling Test 126 | car1.drive(600); 127 | 128 | // Static Method Call 129 | System.out.println("Total Cars: " + Car.getTotalCars()); 130 | 131 | // Inner Class Usage 132 | Car.Engine engine = car1.new Engine(300); 133 | engine.displayInfo(); 134 | 135 | // Generic Method Call 136 | car2.showData("This is a test string"); 137 | car2.showData(12345); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /Laborator 3/Laborator 3/src/Main.java: -------------------------------------------------------------------------------- 1 | //class Messenger { 2 | // // Metodă de bază 3 | // public void sendMessage(String message) { 4 | // System.out.println("Sending message: " + message); 5 | // } 6 | // 7 | // // Overload 1: cu destinatar 8 | // public void sendMessage(String message, String recipient) { 9 | // System.out.println("Sending message: " + message + " to " + recipient); 10 | // } 11 | // 12 | // // Overload 2: repetă mesajul de un anumit număr de ori 13 | // public void sendMessage(String message, int times) { 14 | // for (int i = 0; i < times; i++) { 15 | // System.out.println("Sending message: " + message); 16 | // } 17 | // } 18 | //} 19 | // 20 | //// Subclasa: ColoredMessenger 21 | //class ColoredMessenger extends Messenger { 22 | // // Suprascrierea metodei sendMessage pentru a afișa un mesaj colorat 23 | // @Override 24 | // public void sendMessage(String message) { 25 | // System.out.println("Sending colored message: " + message); 26 | // } 27 | // public void sendMessage(double x){ 28 | // System.out.println(x); 29 | // } 30 | //} 31 | // 32 | //// Clasa principală 33 | //public class Main { 34 | // public static void main(String[] args) { 35 | // // Instanță de bază 36 | // Messenger normalMessenger = new Messenger(); 37 | // // Instanță de subclasă 38 | // Messenger coloredMessenger = new ColoredMessenger(); 39 | // 40 | // // Apeluri folosind instanța de tip Messenger 41 | // System.out.println("Normal Messenger:"); 42 | // normalMessenger.sendMessage("Hello World"); 43 | // normalMessenger.sendMessage("Hello World", "Alice"); 44 | // normalMessenger.sendMessage("Hello World", 3); 45 | // 46 | // // Apeluri folosind instanța de tip ColoredMessenger 47 | // System.out.println("\nColored Messenger:"); 48 | // coloredMessenger.sendMessage("Hello World"); // Apel override 49 | // coloredMessenger.sendMessage("Hello World", "Bob"); // Apel overload (metoda neschimbată) 50 | // coloredMessenger.sendMessage("Hello World", 2); // Apel overload (metoda neschimbată) 51 | // } 52 | //} 53 | //class Unu{ 54 | // int x; 55 | // 56 | // Unu(int x){ 57 | // x = 2; 58 | // } 59 | // public int returneaza(){ 60 | // return 2; 61 | // } 62 | //} 63 | //class Doi extends Unu{ 64 | // int x; 65 | // Doi(){ 66 | // super(2); 67 | // x = super.returneaza(); 68 | // } 69 | // @Override 70 | // public int returneaza() { 71 | // return 5; 72 | // } 73 | // public int returneazaBaza(){ 74 | // int x = super.returneaza(); 75 | // return x; 76 | // } 77 | 78 | 79 | class A { 80 | int dată_membră_non_statică = 1; 81 | static int dată_membră_statică = 1000; 82 | void metoda1() { 83 | System.out.println("Metoda non-statică 1 din clasa A!"); 84 | } 85 | static void metoda2() { 86 | System.out.println("Metoda statică 2 din clasa A!"); 87 | } 88 | } 89 | class B extends A { 90 | int dată_membră_non_statică = 2; 91 | static int dată_membră_statică = 2000; 92 | void metoda1() { 93 | System.out.println("Metoda non-statică 1 din clasa B!"); 94 | } 95 | static void metoda2() { 96 | System.out.println("Metoda statică 2 din clasa B!"); 97 | } 98 | } 99 | 100 | 101 | public class Main { 102 | public static void main(String[] args) { 103 | A ob = new B(); //polimorfism 104 | System.out.println("Data membră non-statică = " + ob.dată_membră_non_statică); 105 | System.out.println("Data membră statică = " + ob.dată_membră_statică); 106 | ob.metoda1(); 107 | ob.metoda2(); 108 | 109 | } 110 | 111 | //downcasting bun!! 112 | //Inginer p = null; 113 | //if(b instanceof Inginer) 114 | // p = (Inginer)b; 115 | 116 | } 117 | 118 | 119 | 120 | // A ob = new B(); //polimorfism 121 | // System.out.println("Data membră non-statică = " + ob.dată_membră_non_statică); 122 | // System.out.println("Data membră statică = " + ob.dată_membră_non_statică); 123 | // ob.metoda1(); 124 | // ob.metoda2(); 125 | // 126 | // } -------------------------------------------------------------------------------- /Info/setup.md: -------------------------------------------------------------------------------- 1 | # Initial setup 2 | 3 | 4 | ## 1. JDK 5 | 6 | You will need to set a Java Development Kit (JDK), in order to develop Java applications. 7 | 8 | - download the open-source build of Java 21 from: https://www.oracle.com/java/technologies/downloads/#java21 9 | - in `C:\Program Files`, create a new folder called `Java` 10 | - extract the content of the build in this folder 11 | - in System Variables, create a new variable called `JAVA_HOME`, pointing to the path of the build (i.e. `C:\Program Files\Java\open-jdk-21.0.2`) 12 | - in System Variables, add to the `Path` variable the location of the bin folder (i.e. `C:\Program Files\Java\open-jdk-21.0.2\bin`) 13 | 14 | To test that everything works as expected, open a Terminal and execute `java -version` 15 | 16 | ## 2. Maven 17 | 18 | This will be useful in the future, when you will want to use projects already developed, from a central repository 19 | 20 | - download the latest version of Maven from: https://maven.apache.org/ 21 | - in `C:\Program Files`, create a new folder called `Maven` 22 | - extract the content of the archive in this folder 23 | - in System Variables, create a new variable called `MAVEN_HOME`, pointing to the path of the build (i.e. `C:\Program Files\Maven\maven-apache-3.9.6`) 24 | - in System Variables, add to the `Path` variable the location of the bin folder (i.e. `C:\Program Files\Maven\maven-apache-3.9.6\bin`) 25 | 26 | To test that everything works as expected, open a Terminal and execute `mvn -v` 27 | 28 | ## 3. Git 29 | 30 | This will be useful in order to push your work to a repo and also to have easy access to the materials 31 | 32 | - download and install the latest version of Git from: https://git-scm.com/downloads 33 | 34 | To test that everything works as expected, open a Terminal and execute `git -v` 35 | 36 | ## 4. IntelliJ IDEA Ultimate 37 | 38 | This will be the IDE in which we'll solve exercises from the lab. 39 | 40 | - download and install the latest version of IntelliJ from: https://www.jetbrains.com/idea/download/ 41 | - in order to be able to use it, you will need to create an account on Jetbrains, then to confirm your student status here: https://www.jetbrains.com/shop/eform/students/. IMPORTANT: in this page, you need to use your student account (**@s.unibuc.ro**) 42 | 43 | ## 5. Docker 44 | 45 | This will be used in the last part of the lab, when we will use databases, in order to have an easier management of it. 46 | 47 | - download and install the latest version of Docker Desktop from: https://www.docker.com/products/docker-desktop/ (Docker Personal) 48 | 49 | To test that everything works as expected, open a Terminal and execute `docker -v` 50 | 51 | ## 6. GitHub account 52 | 53 | If you don't already have an account, you will need to create one here: https://github.com/. 54 | In order push/commit your work during the lab, you'll need to create your own repository. 55 | 56 | Go through the following steps: 57 | ### Step 1: Create a new repository 58 | * Follow the steps under "Create a repository" from https://docs.github.com/en/repositories/creating-and-managing-repositories/quickstart-for-repositories 59 | * repository name e.g. "POO", "POO_II", etc. 60 | * set repo to be private 61 | * add a README file 62 | * under .gitignore select "Java" 63 | 64 | ### Step 2: Generate a SSH key 65 | * Follow the steps from https://git-scm.com/book/ms/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key 66 | 67 | ### Step 3: Add your newly generated key to GitHub 68 | * Follow the steps from https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account 69 | 70 | ### Step 4: Clone your repository locally 71 | * Follow the steps from https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository 72 | * make sure you change directory to your desired location before cloning 73 | 74 | ### Step 5: Manually Set SSH Key in Git Config 75 | * Run in Terminal: git config --global core.sshCommand "ssh -i ~/.ssh/id_rsa" (or your given path/key name) 76 | 77 | ### Step 6: Open project in IntelliJ 78 | * start IntelliJ -> "File" -> "New project from Existing Sources" -> select project 79 | * name your project (e.g. "LabPOO") - don't add any lab number, we'll do this under directories 80 | 81 | ### Step 5: Setup JDK 82 | * start Intellij -> File -> Project Structure -> Project Settings - Project -> Select your pre-installed JDK 21 83 | 84 | -------------------------------------------------------------------------------- /Laborator 4/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | ## Exercițiul 1 – Moștenire versus Compoziție 3 | 4 | **Cerințe:** 5 | 6 | - Definește două clase care modelează o relație HAS_A: 7 | - **Clasa `Room`:** 8 | - Atribute: `double width`, `double length`. 9 | - Un constructor parametrizat și un constructor de copiere. 10 | - **Clasa `House`:** 11 | - Atribute: `String address`, `Room diningRoom`, `Owner owner`. 12 | - În relația cu `Room`, folosește **compoziția**: inițializează intern o copie a obiectului primit (prin constructor de copiere). 13 | - În relația cu `Owner` (o altă clasă pe care o definești simplu cu atribute precum `String name`), folosește **agregarea**: stochează referința directă, fără a crea o copie. 14 | - În metoda `main`, demonstrează diferența: 15 | - Dacă se modifică obiectul `Room` trecut ca argument, observați că House păstrează o copie proprie (compoziție). 16 | - Dacă se modifică obiectul `Owner`, modificarea se reflectă și în casă (agregare). 17 | 18 | --- 19 | 20 | ## Exercițiul 2 – Suprascrierea Metodelor din Clasa Object 21 | 22 | **Cerințe:** 23 | 24 | - Creează o clasă `Person` cu următoarele atribute: 25 | - `String name` 26 | - `int age` 27 | - Suprascrie metodele din clasa `Object`: 28 | - **`toString()`** – returnează un șir reprezentativ, de exemplu: `"Person{name='John', age=30}"` 29 | - **`equals(Object o)`** – compară două obiecte `Person` pe baza valorilor atributelor. 30 | - **`hashCode()`** – generează un cod hash consistent cu `equals()`. 31 | - În metoda `main`, creează două instanțe de `Person` cu aceleași valori și demonstrează: 32 | - Că `equals()` returnează `true`. 33 | - Că hashCode-ul este același. 34 | - Afișarea cu `toString()`. 35 | 36 | --- 37 | 38 | ## Exercițiul 3 – Crearea unei Clase Imutabile 39 | 40 | **Cerințe:** 41 | 42 | - Definește o clasă imutabilă `ImmutablePoint` care reprezintă un punct cu coordonate: 43 | - **Atribute:** 44 | - `private final double x` 45 | - `private final double y` 46 | - Reguli: 47 | - Clasa trebuie să fie `final`. 48 | - Atributele sunt private și finale. 49 | - Nu se definesc setteri, doar un constructor parametrizat și metode getter. 50 | - În metoda `main`, creează un obiect `ImmutablePoint` și demonstrează că valorile nu se pot modifica după crearea obiectului. 51 | 52 | --- 53 | 54 | ## Exercițiul 4 – Utilizarea Records 55 | 56 | **Cerințe:** 57 | 58 | - Creează un record `Student` cu componentele: 59 | - `String name` 60 | - `int group` 61 | - `double grade` 62 | - În metoda `main`, creează cel puțin două instanțe de `Student` și: 63 | - Afișează-le utilizând metoda implicită `toString()`. 64 | - Compară-le cu metoda `equals()` și afișează rezultatul. 65 | - Demonstrează utilizarea componentelor (ex.: `student.name()`). 66 | 67 | --- 68 | 69 | ## Exercițiul 5 – Lucrul cu Șiruri de Caractere 70 | 71 | **Cerințe:** 72 | 73 | - Scrie un program care să ilustreze diferențele dintre clasele: 74 | - **`String`:** Demonstrează imutabilitatea – modificarea unui șir prin metode (ex.: `toUpperCase()`) nu schimbă șirul original. 75 | - **`StringBuilder`:** Demonstrează modul în care se poate modifica șirul (ex.: folosind `append()`, `insert()`, `delete()`). 76 | - **`StringBuffer`:** Similar cu `StringBuilder`, dar menționează că metodele sale sunt thread-safe. 77 | - În metoda `main`, realizează următoarele: 78 | - Creează un `String` și afișează rezultatul unei operații de modificare (de exemplu, `toLowerCase()`) fără a modifica șirul original. 79 | - Creează un `StringBuilder`, modifică-l prin concatenare și afișează rezultatul. 80 | - (Opțional) Demonstrează diferența de performanță sau explică diferența conceptuală între `StringBuilder` și `StringBuffer`. 81 | 82 | --- 83 | 84 | ## Exercițiul 6 – Mini Aplicație Integrată 85 | 86 | **Cerințe:** 87 | 88 | - Creează o mini aplicație care combină mai multe concepte: 89 | - Definește o clasă `House` folosind compoziția (vezi Exercițiul 1) și o clasă `Owner`. 90 | - Creează o clasă imutabilă `Address` (cu atribute precum `city` și `street`). 91 | - Utilizează un record `Student` pentru a reprezenta datele unui student. 92 | - Folosește `StringBuilder` pentru a construi un raport sumar care să includă: 93 | - Informații despre o casă (adresă, proprietar). 94 | - Informații despre studenți. 95 | - În metoda `main`, creează instanțe ale acestor clase și generează raportul, demonstrând reutilizarea codului și imutabilitatea datelor. 96 | 97 | --- 98 | 99 | -------------------------------------------------------------------------------- /Laborator 2/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Exercițiul 1 – Array unidimensional 4 | 5 | **Cerințe:** 6 | 7 | - **Declarație și inițializare:** 8 | - Declarați un array de tip `int` cu o dimensiune fixă (ex.: 10 elemente). 9 | - Inițializați array-ul cu valori (de exemplu, `{1, 2, 3, ..., 10}`) la momentul declarației sau prin atribuirea valorilor într-o buclă. 10 | 11 | - **Parcurgere și afișare:** 12 | - Parcurgeți array-ul folosind o buclă `for` clasică (cu indicii elementelor) și afișați fiecare valoare. 13 | - Parcurgeți array-ul folosind o buclă "enhanced for" (for-each) și afișați valorile. 14 | - Afișați întregul array într-o singură linie utilizând metoda `Arrays.toString()`. 15 | 16 | --- 17 | 18 | ## Exercițiul 2 – Array bidimensional 19 | 20 | **Cerințe:** 21 | 22 | - **Crearea array-ului:** 23 | - Creați un array bidimensional de tip `int` unde fiecare linie poate avea un număr diferit de coloane (ex.: prima linie cu 3 elemente, a doua cu 4 elemente, etc.). 24 | 25 | - **Parcurgerea și afișarea:** 26 | - Parcurgeți array-ul folosind bucle imbricate (o buclă pentru rânduri și o buclă pentru coloane) și afișați elementele astfel încât fiecare linie să fie afișată pe o linie nouă. 27 | - Alternativ, folosiți bucle "enhanced for" pentru parcurgere. 28 | - (Opțional) Afișați array-ul complet folosind metoda `Arrays.deepToString()`. 29 | 30 | --- 31 | 32 | ## Exercițiul 3 – Definirea clasei "Persoana" 33 | 34 | **Cerințe:** 35 | 36 | - **Atribute:** 37 | - Creați clasa `Persoana` cu următoarele atribute: 38 | - `private String nume;` 39 | - `private int varsta;` 40 | - `private double[] venit;` (reprezentând veniturile pentru 12 luni) 41 | - `private static int nrPersoane;` (pentru contorizarea instanțelor) 42 | - *(Opțional)* un atribut `id` pentru identificarea unică a fiecărei instanțe. 43 | 44 | - **Constructori:** 45 | - **Constructor implicit:** 46 | - Inițializați `nume` cu o valoare implicită (ex.: "Necunoscut"). 47 | - Inițializați `varsta` cu 0. 48 | - Alocați array-ul `venit` pentru 12 luni. 49 | - **Constructor parametrizat:** 50 | - Permiteți inițializarea atributelor `nume`, `varsta` și `venit` (primiți ca parametru o listă de valori pentru array-ul `venit`). 51 | - **Constructor de copiere:** 52 | - Creați un constructor care primește un obiect de tip `Persoana` și copiază atributele. 53 | - Realizați o **deep copy** pentru array-ul `venit` (alocați un nou array și copiați elementele, pentru a evita partajarea referinței). 54 | 55 | - **Metode suplimentare:** 56 | - Implementați metoda `toString()` pentru a returna un șir de caractere ce conține informațiile despre persoană (nume, vârstă, venituri etc.). 57 | 58 | 59 | --- 60 | 61 | ## Exercițiul 4 – Metode accesor și blocuri de inițializare 62 | 63 | **Cerințe:** 64 | 65 | - **Metode accesor:** 66 | - Implementați metodele `get` și `set` pentru fiecare atribut din clasa `Persoana` (ex.: `getNume()`, `setNume(String nume)`, `getVarsta()`, `setVarsta(int varsta)`, etc.). 67 | 68 | - **Blocuri de inițializare:** 69 | - Adăugați un **bloc static** în clasa `Persoana` pentru inițializarea variabilelor statice (de exemplu, setați `nrPersoane` la 0). 70 | - Adăugați un **bloc nestatic** care să se execute înaintea oricărui constructor, pentru atribuirea unui ID unic fiecărei instanțe (ex.: `this.id = ++nrPersoane;`). 71 | 72 | --- 73 | 74 | ## Exercițiul 5 – Mini aplicație: Sistem de gestionare a studenților 75 | 76 | **Cerințe:** 77 | 78 | - **Definirea clasei `Student`:** 79 | - Creați clasa `Student` cu următoarele atribute: 80 | - `private String nume;` 81 | - `private int varsta;` 82 | - `private double medie;` 83 | - Implementați cel puțin doi constructori: unul implicit și unul parametrizat. 84 | - Implementați metodele `get` și `set` pentru toate atributele. 85 | - Faceți ca clasa să implementeze interfața `Comparable` pentru a putea sorta studenții (de exemplu, după medie). 86 | 87 | - **Gestionarea colecției de studenți:** 88 | - În clasa principală, declarați și inițializați un array de studenți cu cel puțin 5 elemente. 89 | - Implementați o metodă pentru afișarea tuturor studenților din array. 90 | - Implementați o metodă pentru adăugarea sau modificarea unui student din array. 91 | 92 | - **Sortare și căutare:** 93 | - Sortează array-ul de studenți folosind `Arrays.sort()`, bazat pe criteriul de sortare definit (de exemplu, media). 94 | - După sortare, implementați funcționalitatea de căutare a unui student folosind `Arrays.binarySearch()`, căutând după medie (sau alt criteriu relevant). 95 | 96 | - **(Opțional) Pattern Singleton:** 97 | - Discutați sau implementați un pattern de tip Singleton pentru o clasă `Manager` care să coordoneze operațiunile de adăugare, modificare și afișare a studenților. 98 | 99 | --- -------------------------------------------------------------------------------- /Laborator 3/Readme.md: -------------------------------------------------------------------------------- 1 | ## Exercițiul 1 – Supraincarcarea Metodelor 2 | 3 | **Cerințe:** 4 | 5 | - Definește o clasă `MathUtil` care conține metode numite `multiply`: 6 | - **`multiply(int a, int b)`** – returnează produsul celor două numere întregi. 7 | - **`multiply(double a, double b, double c)`** – returnează produsul celor trei numere reale. 8 | - **`multiply(int[] values)`** – calculează și returnează produsul tuturor elementelor dintr-un array de întregi. 9 | - În metoda `main`, apelează fiecare variantă a metodei și afișează rezultatele. 10 | 11 | ## Exercițiul 2 – Moștenire și Suprascriere 12 | 13 | **Cerințe:** 14 | 15 | - Definește o clasă de bază `Vehicle` care conține: 16 | - **Atribute:** 17 | - `String brand` 18 | - `int year` 19 | - **Constructor:** 20 | - Un constructor parametrizat care inițializează atributele `brand` și `year`. 21 | - **Metodă:** 22 | - `displayInfo()` – afișează informațiile despre vehicul (brandul și anul de fabricație). 23 | 24 | - Definește o subclasă `Car` care extinde clasa `Vehicle` și adaugă: 25 | - **Atribut suplimentar:** 26 | - `int numberOfDoors` 27 | - **Suprascriere:** 28 | - Suprascrie metoda `displayInfo()` pentru a include și informația despre numărul de uși, de exemplu, afișând: 29 | `Car: (), Doors: ` 30 | 31 | - În metoda `main`, efectuează următoarele: 32 | - Creează o instanță de `Vehicle`. 33 | - Creează o instanță de `Car`. 34 | - Apelează metoda `displayInfo()` pentru fiecare instanță și observă diferențele de comportament între metoda din clasa de bază și cea suprascrisă în subclasă. 35 | 36 | ## Exercițiul 3 – Polimorfism cu Clase Abstracte 37 | 38 | **Cerințe:** 39 | 40 | - Definește o clasă abstractă `Animal` care conține: 41 | - **Metodă abstractă:** 42 | - `makeSound()` – metoda abstractă ce va fi implementată de clasele derivate. 43 | - Creează două clase concrete, `Dog` și `Cat`, care extind `Animal` și implementează metoda `makeSound()` astfel: 44 | - **`Dog`:** afișează "Woof!" 45 | - **`Cat`:** afișează "Meow!" 46 | - În metoda `main`, efectuează următoarele: 47 | - Creează un array de tip `Animal` care conține instanțe de `Dog` și `Cat`. 48 | - Iterează prin array și apelează metoda `makeSound()` pentru fiecare element pentru a demonstra polimorfismul. 49 | 50 | ## Exercițiul 4 – Interfețe și Implementare 51 | 52 | **Cerințe:** 53 | 54 | - Definește o interfață `Insurable` care declară metoda: 55 | - **`double getInsuranceCost()`** – returnează costul asigurării. 56 | - Definește o clasă abstractă `Vehicle` care conține: 57 | - **Metodă abstractă:** 58 | - `void drive()` – care va fi implementată de clasele derivate. 59 | - Creează o clasă `Motorcycle` care extinde `Vehicle` și implementează interfața `Insurable`: 60 | - **Atribute:** 61 | - `String brand` 62 | - `int engineCapacity` 63 | - **Implementare:** 64 | - Metoda `drive()` afișează un mesaj specific, de exemplu: 65 | `"Motorcycle [brand] is driving"` 66 | - Metoda `getInsuranceCost()` calculează costul asigurării, de exemplu: 67 | `engineCapacity * 1.5` 68 | - În metoda `main`, efectuează următoarele: 69 | - Creează o instanță de `Motorcycle`. 70 | - Apelează metoda `drive()` și afișează costul asigurării folosind metoda `getInsuranceCost()`. 71 | ## Exercițiul 5 – Overriding versus Overloading 72 | 73 | **Cerințe:** 74 | 75 | - Definește o clasă `Printer` cu următoarele metode: 76 | - **`print(String s)`** – afișează șirul primit. 77 | - **`print(int number)`** – afișează numărul primit. 78 | - Definește o subclasă `ColorPrinter` care extinde `Printer` și suprascrie metoda `print(String s)` astfel încât să afișeze șirul primit împreună cu un indiciu de culoare (de exemplu, "Printing in red: \"). 79 | - În metoda `main`, efectuează următoarele: 80 | - Creează o instanță de `Printer` și una de `ColorPrinter`. 81 | - Demonstrează apelarea metodelor supraincarcate din clasa `Printer`. 82 | - Demonstrează apelarea metodei suprascrise din clasa `ColorPrinter`. 83 | - Discută diferența dintre supraincarcare (overloading) și suprascriere (overriding). 84 | 85 | ## Exercițiul 6 – Mini Aplicație: Gestionarea Formelor Geometrice 86 | 87 | **Cerințe:** 88 | 89 | - Definește o clasă abstractă `Shape` cu metoda abstractă: 90 | - `double getArea()` – returnează aria formei. 91 | - Creează două clase: 92 | - **`Rectangle`**: 93 | - **Atribute:** 94 | - `double width` 95 | - `double height` 96 | - **Implementare:** 97 | - Metoda `getArea()` returnează `width * height`. 98 | - **`Circle`**: 99 | - **Atribut:** 100 | - `double radius` 101 | - **Implementare:** 102 | - Metoda `getArea()` returnează `Math.PI * radius * radius`. 103 | - Definește o interfață `Colorable` care declară metoda: 104 | - `String getColor()` – returnează culoarea formei. 105 | - Fă ca clasa `Circle` să implementeze interfața `Colorable` și să returneze o culoare prestabilită (ex.: "blue"). 106 | - În metoda `main`, efectuează următoarele: 107 | - Creează o listă (sau un array) de tip `Shape` care conține instanțe de `Rectangle` și `Circle`. 108 | - Iterează prin listă, afișând aria fiecărei forme; pentru obiectele care implementează `Colorable`, afișează și culoarea. 109 | - Sortează formele după arie folosind un `Comparator` (fără a implementa `Comparable`) și afișează rezultatele sortate. 110 | 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /Laborator 5/Laborator5/src/Lab5Demo.java: -------------------------------------------------------------------------------- 1 | // ------------------------ EXCEPTIONS DEMO ------------------------ 2 | import java.sql.SQLException; 3 | import java.util.Comparator; 4 | import java.util.InputMismatchException; 5 | import java.util.Scanner; 6 | 7 | class InvalidGradeException extends Exception { 8 | public InvalidGradeException(String message) { 9 | super(message); 10 | } 11 | } 12 | 13 | class ExceptionExamples { 14 | 15 | public static int readInteger() { 16 | Scanner scanner = new Scanner(System.in); 17 | try { 18 | System.out.print("Enter an integer: "); 19 | return scanner.nextInt(); 20 | } catch (InputMismatchException | InterruptedException e) { 21 | System.out.println("Not a valid integer."); 22 | return -1; 23 | } finally { 24 | System.out.println("Attempted to read integer."); 25 | } 26 | } 27 | 28 | public static int parseAndDivide(String a, String b) { 29 | try { 30 | int x = Integer.parseInt(a); 31 | int y = Integer.parseInt(b); 32 | return x / y; 33 | } catch (NumberFormatException e) { 34 | System.out.println("Exception caught: " + e.getMessage()); 35 | return 0; 36 | } 37 | catch (ArithmeticException e) { 38 | System.out.println("Exception caught: " + e.getMessage()); 39 | return 0; 40 | } 41 | } 42 | 43 | public static void validateGrade(int grade) throws InvalidGradeException { 44 | if (grade < 1 || grade > 10) { 45 | throw new InvalidGradeException("Grade must be between 1 and 10."); 46 | } 47 | System.out.println("Grade is valid."); 48 | } 49 | } 50 | 51 | // ------------------------ INTERFACES DEMO ------------------------ 52 | 53 | interface Shape { 54 | double getArea(); 55 | double getPerimeter(); 56 | } 57 | 58 | class Circle implements Shape { 59 | private double radius; 60 | 61 | public Circle(double radius) { 62 | this.radius = radius; 63 | } 64 | 65 | public double getArea() { 66 | return Math.PI * radius * radius; 67 | } 68 | 69 | public double getPerimeter() { 70 | return 2 * Math.PI * radius; 71 | } 72 | } 73 | 74 | class Rectangle implements Shape { 75 | private double width, height; 76 | 77 | public Rectangle(double width, double height) { 78 | this.width = width; 79 | this.height = height; 80 | } 81 | 82 | public double getArea() { 83 | return width * height; 84 | } 85 | 86 | public double getPerimeter() { 87 | return 2 * (width + height); 88 | } 89 | } 90 | 91 | class Student implements Comparable { 92 | private String name; 93 | private double grade; 94 | 95 | public Student(String name, double grade) { 96 | this.name = name; 97 | this.grade = grade; 98 | } 99 | 100 | public double getGrade() { 101 | return grade; 102 | } 103 | 104 | public String toString() { 105 | return name + " (grade: " + grade + ")"; 106 | } 107 | 108 | public int compareTo(Student other) { 109 | return Double.compare(other.grade, this.grade); // descending 110 | } 111 | } 112 | 113 | // Simulated database service with interface 114 | interface DatabaseActions { 115 | void connect() throws IllegalStateException; 116 | void executeQuery(String query) throws SQLException; 117 | } 118 | 119 | class FakeDatabase implements DatabaseActions { 120 | private boolean isDown; 121 | 122 | public FakeDatabase(boolean isDown) { 123 | this.isDown = isDown; 124 | } 125 | 126 | public void connect() { 127 | if (isDown) { 128 | throw new IllegalStateException("Database server is offline."); 129 | } 130 | System.out.println("Connected to database."); 131 | } 132 | 133 | public void executeQuery(String query) throws SQLException { 134 | if (query == null || query.isEmpty()) { 135 | throw new SQLException("Query is invalid."); 136 | } 137 | System.out.println("Executing query: " + query); 138 | } 139 | } 140 | 141 | // ------------------------ MAIN CLASS ------------------------ 142 | 143 | public class Lab5Demo { 144 | public static void main(String[] args) { 145 | // Exception 1: try-catch-finally 146 | int number = ExceptionExamples.readInteger(); 147 | 148 | // Exception 2: multi-catch 149 | ExceptionExamples.parseAndDivide("10", "0"); 150 | ExceptionExamples.parseAndDivide("abc", "2"); 151 | 152 | // Exception 3: custom exception + propagation 153 | try { 154 | ExceptionExamples.validateGrade(11); 155 | } catch (InvalidGradeException e) { 156 | System.out.println("Caught custom exception: " + e.getMessage()); 157 | } 158 | 159 | // Interface 1: Shape example 160 | Shape[] shapes = { new Circle(3), new Rectangle(4, 5) }; 161 | for (Shape s : shapes) { 162 | System.out.println("Area: " + s.getArea() + ", Perimeter: " + s.getPerimeter()); 163 | } 164 | 165 | // Interface 2: Comparable example 166 | java.util.List students = new java.util.ArrayList<>(); 167 | students.add(new Student("Ana", 9.2)); 168 | students.add(new Student("Ion", 8.7)); 169 | students.add(new Student("Maria", 9.5)); 170 | java.util.Collections.sort(students); 171 | students.sort(Comparator.comparingDouble(Student::getGrade)); 172 | System.out.println("Sorted students:"); 173 | for (Student s : students) { 174 | System.out.println(s); 175 | } 176 | 177 | // Interface 3: Exception + Interface 178 | DatabaseActions db = new FakeDatabase(true); 179 | try { 180 | db.connect(); 181 | } catch (IllegalStateException e) { 182 | System.out.println("Database connection failed: " + e.getMessage()); 183 | } 184 | 185 | db = new FakeDatabase(false); 186 | try { 187 | db.connect(); 188 | db.executeQuery(""); // will throw SQLException 189 | } catch (SQLException e) { 190 | System.out.println("SQL Exception: " + e.getMessage()); 191 | } 192 | } 193 | } --------------------------------------------------------------------------------