├── Lab5 ├── bin │ └── .gitignore ├── src │ └── ro │ │ └── ase │ │ └── acs │ │ ├── interfaces │ │ └── Taxable.java │ │ ├── classes │ │ ├── Vehicle.java │ │ └── Car.java │ │ └── main │ │ └── Main.java ├── .classpath ├── .project └── .settings │ └── org.eclipse.jdt.core.prefs ├── Lab6 ├── car.txt ├── car.bin ├── object.bin ├── bin │ └── ro │ │ └── ase │ │ └── acs │ │ ├── main │ │ ├── Main.class │ │ └── NewMain.class │ │ ├── classes │ │ ├── Car.class │ │ └── Vehicle.class │ │ └── interfaces │ │ └── Taxable.class ├── src │ └── ro │ │ └── ase │ │ └── acs │ │ ├── interfaces │ │ └── Taxable.java │ │ ├── classes │ │ ├── Vehicle.java │ │ └── Car.java │ │ └── main │ │ ├── Main.java │ │ └── NewMain.java ├── .classpath ├── .project └── .settings │ └── org.eclipse.jdt.core.prefs ├── README.md ├── Lab12 ├── database.db ├── libs │ ├── sqlite-jdbc-3.30.1.jar │ └── mongo-java-driver-3.12.4.jar ├── bin │ └── ro │ │ └── ase │ │ └── acs │ │ ├── nosql │ │ └── MongoProgMain.class │ │ └── sql │ │ └── SQLiteProgMain.class ├── .project ├── .classpath ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── ro │ └── ase │ └── acs │ ├── nosql │ └── MongoProgMain.java │ └── sql │ └── SQLiteProgMain.java ├── Lab1 ├── bin │ ├── Main.class │ └── Student.class ├── .classpath ├── .project ├── src │ ├── Main.java │ └── Student.java └── .settings │ └── org.eclipse.jdt.core.prefs ├── Lab2 ├── bin │ └── ro │ │ └── ase │ │ └── acs │ │ ├── main │ │ └── Main.class │ │ └── classes │ │ ├── Car.class │ │ └── FuelType.class ├── src │ └── ro │ │ └── ase │ │ └── acs │ │ ├── classes │ │ ├── FuelType.java │ │ └── Car.java │ │ └── main │ │ └── Main.java ├── .classpath ├── .project └── .settings │ └── org.eclipse.jdt.core.prefs ├── Lab3 ├── bin │ └── ro │ │ └── ase │ │ └── acs │ │ ├── main │ │ └── Main.class │ │ ├── classes │ │ ├── Car.class │ │ └── Vehicle.class │ │ └── interfaces │ │ └── Taxable.class ├── src │ └── ro │ │ └── ase │ │ └── acs │ │ ├── interfaces │ │ └── Taxable.java │ │ ├── classes │ │ ├── Vehicle.java │ │ └── Car.java │ │ └── main │ │ └── Main.java ├── .classpath ├── .project └── .settings │ └── org.eclipse.jdt.core.prefs ├── Lab4 ├── bin │ └── ro │ │ └── ase │ │ └── acs │ │ ├── main │ │ └── Main.class │ │ ├── classes │ │ ├── Car.class │ │ └── Vehicle.class │ │ └── interfaces │ │ └── Taxable.class ├── src │ └── ro │ │ └── ase │ │ └── acs │ │ ├── interfaces │ │ └── Taxable.java │ │ ├── classes │ │ ├── Vehicle.java │ │ └── Car.java │ │ └── main │ │ └── Main.java ├── .classpath ├── .project └── .settings │ └── org.eclipse.jdt.core.prefs ├── Lab7 ├── bin │ └── ro │ │ └── ase │ │ └── acs │ │ ├── main │ │ ├── Main.class │ │ └── Main$1.class │ │ ├── classes │ │ └── Sum.class │ │ └── interfaces │ │ ├── Moveable.class │ │ ├── BinaryOperator.class │ │ └── UnaryOperator.class ├── src │ └── ro │ │ └── ase │ │ └── acs │ │ ├── interfaces │ │ ├── Moveable.java │ │ ├── UnaryOperator.java │ │ └── BinaryOperator.java │ │ ├── classes │ │ └── Sum.java │ │ └── main │ │ └── Main.java ├── .classpath ├── .project └── .settings │ └── org.eclipse.jdt.core.prefs ├── Lab8 ├── bin │ └── ro │ │ └── ase │ │ └── acs │ │ ├── main │ │ └── Main.class │ │ └── classes │ │ ├── SyncedThread.class │ │ └── UnsyncedThread.class ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── ro │ └── ase │ └── acs │ ├── classes │ ├── SyncedThread.java │ └── UnsyncedThread.java │ └── main │ └── Main.java ├── Lab9 ├── bin │ └── ro │ │ └── ase │ │ └── acs │ │ ├── main │ │ └── Main.class │ │ └── classes │ │ ├── ArraySummingThread.class │ │ └── ArraySummingCallable.class ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── ro │ └── ase │ └── acs │ ├── classes │ ├── ArraySummingThread.java │ └── ArraySummingCallable.java │ └── main │ └── Main.java ├── Lab10 ├── bin │ └── ro │ │ └── ase │ │ └── acs │ │ ├── client │ │ └── UdpClient.class │ │ └── server │ │ └── UdpServer.class ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── ro │ └── ase │ └── acs │ ├── client │ └── UdpClient.java │ └── server │ └── UdpServer.java └── Lab11 ├── bin └── ro │ └── ase │ └── acs │ ├── client │ └── TcpClient.class │ └── server │ └── TcpServer.class ├── .classpath ├── .project ├── .settings └── org.eclipse.jdt.core.prefs └── src └── ro └── ase └── acs ├── client └── TcpClient.java └── server └── TcpServer.java /Lab5/bin/.gitignore: -------------------------------------------------------------------------------- 1 | /ro/ 2 | -------------------------------------------------------------------------------- /Lab6/car.txt: -------------------------------------------------------------------------------- 1 | Renault 2 | 90 3 | blue 4 | 1500 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java2020 2 | Java Programming Lab Examples 3 | -------------------------------------------------------------------------------- /Lab6/car.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab6/car.bin -------------------------------------------------------------------------------- /Lab6/object.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab6/object.bin -------------------------------------------------------------------------------- /Lab12/database.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab12/database.db -------------------------------------------------------------------------------- /Lab1/bin/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab1/bin/Main.class -------------------------------------------------------------------------------- /Lab1/bin/Student.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab1/bin/Student.class -------------------------------------------------------------------------------- /Lab12/libs/sqlite-jdbc-3.30.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab12/libs/sqlite-jdbc-3.30.1.jar -------------------------------------------------------------------------------- /Lab2/bin/ro/ase/acs/main/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab2/bin/ro/ase/acs/main/Main.class -------------------------------------------------------------------------------- /Lab3/bin/ro/ase/acs/main/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab3/bin/ro/ase/acs/main/Main.class -------------------------------------------------------------------------------- /Lab4/bin/ro/ase/acs/main/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab4/bin/ro/ase/acs/main/Main.class -------------------------------------------------------------------------------- /Lab6/bin/ro/ase/acs/main/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab6/bin/ro/ase/acs/main/Main.class -------------------------------------------------------------------------------- /Lab7/bin/ro/ase/acs/main/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab7/bin/ro/ase/acs/main/Main.class -------------------------------------------------------------------------------- /Lab8/bin/ro/ase/acs/main/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab8/bin/ro/ase/acs/main/Main.class -------------------------------------------------------------------------------- /Lab9/bin/ro/ase/acs/main/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab9/bin/ro/ase/acs/main/Main.class -------------------------------------------------------------------------------- /Lab2/bin/ro/ase/acs/classes/Car.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab2/bin/ro/ase/acs/classes/Car.class -------------------------------------------------------------------------------- /Lab3/bin/ro/ase/acs/classes/Car.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab3/bin/ro/ase/acs/classes/Car.class -------------------------------------------------------------------------------- /Lab4/bin/ro/ase/acs/classes/Car.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab4/bin/ro/ase/acs/classes/Car.class -------------------------------------------------------------------------------- /Lab6/bin/ro/ase/acs/classes/Car.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab6/bin/ro/ase/acs/classes/Car.class -------------------------------------------------------------------------------- /Lab6/bin/ro/ase/acs/main/NewMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab6/bin/ro/ase/acs/main/NewMain.class -------------------------------------------------------------------------------- /Lab7/bin/ro/ase/acs/classes/Sum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab7/bin/ro/ase/acs/classes/Sum.class -------------------------------------------------------------------------------- /Lab7/bin/ro/ase/acs/main/Main$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab7/bin/ro/ase/acs/main/Main$1.class -------------------------------------------------------------------------------- /Lab12/libs/mongo-java-driver-3.12.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab12/libs/mongo-java-driver-3.12.4.jar -------------------------------------------------------------------------------- /Lab2/src/ro/ase/acs/classes/FuelType.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | public enum FuelType { 4 | gas, diesel, electric 5 | } 6 | -------------------------------------------------------------------------------- /Lab3/bin/ro/ase/acs/classes/Vehicle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab3/bin/ro/ase/acs/classes/Vehicle.class -------------------------------------------------------------------------------- /Lab4/bin/ro/ase/acs/classes/Vehicle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab4/bin/ro/ase/acs/classes/Vehicle.class -------------------------------------------------------------------------------- /Lab6/bin/ro/ase/acs/classes/Vehicle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab6/bin/ro/ase/acs/classes/Vehicle.class -------------------------------------------------------------------------------- /Lab10/bin/ro/ase/acs/client/UdpClient.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab10/bin/ro/ase/acs/client/UdpClient.class -------------------------------------------------------------------------------- /Lab10/bin/ro/ase/acs/server/UdpServer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab10/bin/ro/ase/acs/server/UdpServer.class -------------------------------------------------------------------------------- /Lab11/bin/ro/ase/acs/client/TcpClient.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab11/bin/ro/ase/acs/client/TcpClient.class -------------------------------------------------------------------------------- /Lab11/bin/ro/ase/acs/server/TcpServer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab11/bin/ro/ase/acs/server/TcpServer.class -------------------------------------------------------------------------------- /Lab2/bin/ro/ase/acs/classes/FuelType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab2/bin/ro/ase/acs/classes/FuelType.class -------------------------------------------------------------------------------- /Lab12/bin/ro/ase/acs/nosql/MongoProgMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab12/bin/ro/ase/acs/nosql/MongoProgMain.class -------------------------------------------------------------------------------- /Lab12/bin/ro/ase/acs/sql/SQLiteProgMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab12/bin/ro/ase/acs/sql/SQLiteProgMain.class -------------------------------------------------------------------------------- /Lab3/bin/ro/ase/acs/interfaces/Taxable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab3/bin/ro/ase/acs/interfaces/Taxable.class -------------------------------------------------------------------------------- /Lab4/bin/ro/ase/acs/interfaces/Taxable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab4/bin/ro/ase/acs/interfaces/Taxable.class -------------------------------------------------------------------------------- /Lab6/bin/ro/ase/acs/interfaces/Taxable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab6/bin/ro/ase/acs/interfaces/Taxable.class -------------------------------------------------------------------------------- /Lab7/bin/ro/ase/acs/interfaces/Moveable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab7/bin/ro/ase/acs/interfaces/Moveable.class -------------------------------------------------------------------------------- /Lab7/src/ro/ase/acs/interfaces/Moveable.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.interfaces; 2 | 3 | public interface Moveable { 4 | public void move(); 5 | } 6 | -------------------------------------------------------------------------------- /Lab8/bin/ro/ase/acs/classes/SyncedThread.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab8/bin/ro/ase/acs/classes/SyncedThread.class -------------------------------------------------------------------------------- /Lab8/bin/ro/ase/acs/classes/UnsyncedThread.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab8/bin/ro/ase/acs/classes/UnsyncedThread.class -------------------------------------------------------------------------------- /Lab7/bin/ro/ase/acs/interfaces/BinaryOperator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab7/bin/ro/ase/acs/interfaces/BinaryOperator.class -------------------------------------------------------------------------------- /Lab7/bin/ro/ase/acs/interfaces/UnaryOperator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab7/bin/ro/ase/acs/interfaces/UnaryOperator.class -------------------------------------------------------------------------------- /Lab9/bin/ro/ase/acs/classes/ArraySummingThread.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab9/bin/ro/ase/acs/classes/ArraySummingThread.class -------------------------------------------------------------------------------- /Lab9/bin/ro/ase/acs/classes/ArraySummingCallable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BogdanIancu/Java2020/HEAD/Lab9/bin/ro/ase/acs/classes/ArraySummingCallable.class -------------------------------------------------------------------------------- /Lab7/src/ro/ase/acs/interfaces/UnaryOperator.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.interfaces; 2 | 3 | public interface UnaryOperator { 4 | public double operate(double operand); 5 | } 6 | -------------------------------------------------------------------------------- /Lab3/src/ro/ase/acs/interfaces/Taxable.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.interfaces; 2 | 3 | public interface Taxable { 4 | public static final int MIN_TAX = 50; 5 | 6 | public double computeTax(); 7 | } 8 | -------------------------------------------------------------------------------- /Lab4/src/ro/ase/acs/interfaces/Taxable.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.interfaces; 2 | 3 | public interface Taxable { 4 | public static final int MIN_TAX = 50; 5 | 6 | public double computeTax(); 7 | } 8 | -------------------------------------------------------------------------------- /Lab5/src/ro/ase/acs/interfaces/Taxable.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.interfaces; 2 | 3 | public interface Taxable { 4 | public static final float MIN_TAX = 5; 5 | 6 | public float computeTax(); 7 | } 8 | -------------------------------------------------------------------------------- /Lab6/src/ro/ase/acs/interfaces/Taxable.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.interfaces; 2 | 3 | public interface Taxable { 4 | public static final float MIN_TAX = 5; 5 | 6 | public float computeTax(); 7 | } 8 | -------------------------------------------------------------------------------- /Lab7/src/ro/ase/acs/interfaces/BinaryOperator.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.interfaces; 2 | 3 | @FunctionalInterface 4 | public interface BinaryOperator { 5 | public double operate(double operand1, double operand2); 6 | } 7 | -------------------------------------------------------------------------------- /Lab7/src/ro/ase/acs/classes/Sum.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | import ro.ase.acs.interfaces.BinaryOperator; 4 | 5 | public class Sum implements BinaryOperator { 6 | 7 | @Override 8 | public double operate(double operand1, double operand2) { 9 | return operand1 + operand2; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Lab1/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lab2/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lab3/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lab4/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lab5/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lab6/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lab7/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lab8/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lab9/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lab10/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lab11/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Lab1/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab1 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab1/src/Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | System.out.println("Hello, World!"); 6 | 7 | Student s = new Student(); 8 | System.out.println(s.getGrade()); 9 | 10 | Student s2 = new Student("Ionel", 9.5f); 11 | System.out.println(s2.getName()); 12 | 13 | Student s3 = s2.myClone(); 14 | s3.setName("Gigel"); 15 | System.out.println(s2.getName()); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Lab2/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab2 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab3/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab3 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab4/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab4 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab5/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab5 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab6/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab6 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab7/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab7 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab8/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab8 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab9/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab9 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab10/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab10 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab11/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab11 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab12/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab12 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Lab1/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab10/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab11/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab12/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Lab12/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab2/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab3/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab4/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab5/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab6/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab7/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab8/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab9/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Lab9/src/ro/ase/acs/classes/ArraySummingThread.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | public class ArraySummingThread extends Thread { 4 | private int[] array; 5 | private int startIndex; 6 | private int endIndex; 7 | private long sum; 8 | 9 | public ArraySummingThread(int[] array, int startIndex, 10 | int endIndex) { 11 | this.array = array; 12 | this.startIndex = startIndex; 13 | this.endIndex = endIndex; 14 | } 15 | 16 | @Override 17 | public void run() { 18 | super.run(); 19 | for(int i = startIndex; i < endIndex; i++) { 20 | sum += array[i]; 21 | } 22 | } 23 | 24 | public long getSum() { 25 | return sum; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Lab9/src/ro/ase/acs/classes/ArraySummingCallable.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | import java.util.concurrent.Callable; 4 | 5 | public class ArraySummingCallable implements Callable{ 6 | private int[] array; 7 | private int startIndex; 8 | private int endIndex; 9 | 10 | public ArraySummingCallable(int[] array, int startIndex, 11 | int endIndex) { 12 | this.array = array; 13 | this.startIndex = startIndex; 14 | this.endIndex = endIndex; 15 | } 16 | 17 | @Override 18 | public Long call() throws Exception { 19 | long sum = 0; 20 | for(int i = startIndex; i < endIndex; i++) { 21 | sum += array[i]; 22 | } 23 | return sum; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Lab1/src/Student.java: -------------------------------------------------------------------------------- 1 | 2 | public class Student { 3 | private String name; 4 | private float grade; 5 | 6 | public Student() { 7 | name = ""; 8 | grade = 0; 9 | } 10 | 11 | public Student(String name, float grade) { 12 | this.name = name; 13 | this.grade = grade; 14 | } 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | 24 | public float getGrade() { 25 | return grade; 26 | } 27 | 28 | public void setGrade(float grade) { 29 | this.grade = grade; 30 | } 31 | 32 | public Student myClone() { 33 | Student copy = new Student(); 34 | copy.name = name; 35 | copy.grade = grade; 36 | return copy; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Lab2/src/ro/ase/acs/main/Main.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.main; 2 | 3 | import ro.ase.acs.classes.Car; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Car c1 = new Car("Tesla", 40000); 9 | System.out.println(c1.getProducer()); 10 | 11 | Car c2 = (Car)c1.clone(); 12 | c1.setPrice(30000); 13 | c1.setProducer("Honda"); 14 | System.out.println(c2.getPrice()); 15 | System.out.println(c2.getProducer()); 16 | 17 | int[] vector = new int[] { 100, 200, 150 }; 18 | c1.setDistances(vector); 19 | vector[0] = 5000; 20 | 21 | int[] distances = c1.getDistances(); 22 | distances[0] = 8000; 23 | 24 | for(int x : distances) { 25 | System.out.println(x); 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Lab3/src/ro/ase/acs/classes/Vehicle.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | public abstract class Vehicle implements Cloneable { 4 | private String name; 5 | private int speed; 6 | 7 | public Vehicle() { 8 | name = ""; 9 | speed = 1; 10 | } 11 | 12 | public Vehicle(String name, int speed) { 13 | this.name = name; 14 | this.speed = speed; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public int getSpeed() { 22 | return speed; 23 | } 24 | 25 | @Override 26 | public Object clone() throws CloneNotSupportedException { 27 | Vehicle copy = (Vehicle)super.clone(); 28 | copy.name = name; 29 | copy.speed = speed; 30 | return copy; 31 | } 32 | 33 | public abstract void move(); 34 | } 35 | -------------------------------------------------------------------------------- /Lab4/src/ro/ase/acs/classes/Vehicle.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | public abstract class Vehicle implements Cloneable { 4 | private String name; 5 | private int speed; 6 | 7 | public Vehicle() { 8 | name = ""; 9 | speed = 1; 10 | } 11 | 12 | public Vehicle(String name, int speed) { 13 | this.name = name; 14 | this.speed = speed; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public int getSpeed() { 22 | return speed; 23 | } 24 | 25 | @Override 26 | public Object clone() throws CloneNotSupportedException { 27 | Vehicle copy = (Vehicle)super.clone(); 28 | copy.name = name; 29 | copy.speed = speed; 30 | return copy; 31 | } 32 | 33 | public abstract void move(); 34 | } 35 | -------------------------------------------------------------------------------- /Lab5/src/ro/ase/acs/classes/Vehicle.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | public abstract class Vehicle implements Cloneable { 4 | private String name; 5 | private int speed; 6 | 7 | public Vehicle() { 8 | name = ""; 9 | speed = 1; 10 | } 11 | 12 | public Vehicle(String name, int speed) { 13 | this.name = name; 14 | this.speed = speed; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public int getSpeed() { 22 | return speed; 23 | } 24 | 25 | @Override 26 | public Object clone() throws CloneNotSupportedException { 27 | Vehicle copy = (Vehicle)super.clone(); 28 | copy.name = name; 29 | copy.speed = speed; 30 | return copy; 31 | } 32 | 33 | public abstract void move(); 34 | } 35 | -------------------------------------------------------------------------------- /Lab3/src/ro/ase/acs/main/Main.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.main; 2 | 3 | import ro.ase.acs.classes.Car; 4 | import ro.ase.acs.classes.Vehicle; 5 | import ro.ase.acs.interfaces.Taxable; 6 | 7 | public class Main { 8 | 9 | public static void main(String[] args) { 10 | Car car = new Car("Dacia", 90, "black", 1400); 11 | System.out.println(car.getName()); 12 | 13 | Taxable t = car; 14 | double tax = t.computeTax(); 15 | System.out.println(tax); 16 | 17 | Vehicle v = new Car(); 18 | v.move(); 19 | 20 | if(car instanceof Cloneable) { 21 | Car c2; 22 | try { 23 | c2 = (Car)car.clone(); 24 | c2.setCapacity(1900); 25 | System.out.println(car.getCapacity()); 26 | } catch (CloneNotSupportedException e) { 27 | // TODO Auto-generated catch block 28 | e.printStackTrace(); 29 | } 30 | } 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Lab8/src/ro/ase/acs/classes/SyncedThread.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | import java.util.Random; 4 | 5 | //same class as SyncedThread 6 | //this class uses a lock in order to avoid 7 | //concurrent access to the shared variables 8 | public class SyncedThread implements Runnable { 9 | private static Random random = new Random(); 10 | private static int a = 0; 11 | private static int b = 0; 12 | private static Object lock = new Object(); 13 | 14 | private String name; 15 | 16 | public SyncedThread(String name) { 17 | this.name = name; 18 | } 19 | 20 | private void method() { 21 | synchronized (lock) { 22 | System.out.printf("%s: a=%d b=%d\r\n", name, a, b); 23 | a++; 24 | try { 25 | Thread.sleep(random.nextInt(3000)); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | b++; 30 | } 31 | } 32 | 33 | @Override 34 | public void run() { 35 | for(int i = 0; i < 3; i++) { 36 | method(); 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Lab8/src/ro/ase/acs/classes/UnsyncedThread.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | import java.util.Random; 4 | 5 | //a class which wants to start another thread 6 | //should inherit Thread or runnable 7 | public class UnsyncedThread extends Thread { 8 | private static Random random = new Random(); 9 | private static int a = 0; 10 | private static int b = 0; 11 | 12 | public UnsyncedThread(String name) { 13 | super(name); 14 | } 15 | 16 | private void method() { 17 | System.out.printf("%s: a=%d b=%d\r\n", getName(), a, b); 18 | a++; 19 | try { 20 | //we are pausing the Thread for a random period 21 | //to simulate an ongoing operation 22 | Thread.sleep(random.nextInt(3000)); 23 | } catch (InterruptedException e) { 24 | e.printStackTrace(); 25 | } 26 | b++; 27 | } 28 | 29 | //just the code from this method 30 | //is executed in a new thread 31 | @Override 32 | public void run() { 33 | super.run(); 34 | for(int i = 0; i < 3; i++) { 35 | method(); 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Lab6/src/ro/ase/acs/classes/Vehicle.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | import java.io.Serializable; 4 | 5 | //a class should implement Serializable in order 6 | //to be serialized 7 | public abstract class Vehicle implements Cloneable, Serializable { 8 | //this field is used to identify the version of the class 9 | //and it should be incremented when the class changes 10 | private static final long serialVersionUID = 1L; 11 | private String name; 12 | private int speed; 13 | 14 | public Vehicle() { 15 | name = ""; 16 | speed = 1; 17 | } 18 | 19 | public Vehicle(String name, int speed) { 20 | this.name = name; 21 | this.speed = speed; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public int getSpeed() { 29 | return speed; 30 | } 31 | 32 | @Override 33 | public Object clone() throws CloneNotSupportedException { 34 | Vehicle copy = (Vehicle)super.clone(); 35 | copy.name = name; 36 | copy.speed = speed; 37 | return copy; 38 | } 39 | 40 | public abstract void move(); 41 | } 42 | -------------------------------------------------------------------------------- /Lab8/src/ro/ase/acs/main/Main.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.main; 2 | 3 | import ro.ase.acs.classes.SyncedThread; 4 | import ro.ase.acs.classes.UnsyncedThread; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) { 9 | //Please uncomment the following lines to see 10 | //a race condition in action 11 | 12 | //In order to start a new thread, the start method is called 13 | //not run, which will cause it to be executed in the existing thread 14 | 15 | // UnsyncedThread t1 = new UnsyncedThread("Thread 1"); 16 | // t1.start(); 17 | // UnsyncedThread t2 = new UnsyncedThread("Thread 2"); 18 | // t2.start(); 19 | 20 | SyncedThread t3 = new SyncedThread("Thread 3"); 21 | //objects from classes that inherit thread should be passed 22 | //as parameters to the Thread constructor in order to call the start method 23 | new Thread(t3).start(); 24 | 25 | SyncedThread t4 = new SyncedThread("Thread 4"); 26 | new Thread(t4).start(); 27 | 28 | //because Runnable is a functional interface 29 | //it can be used with lambda expressions 30 | Runnable r = () -> { 31 | try { 32 | Thread.sleep(2000); 33 | } catch (InterruptedException e) { 34 | e.printStackTrace(); 35 | } 36 | System.out.println("Message from another thread"); 37 | }; 38 | new Thread(r).start(); 39 | 40 | System.out.println("Main ended"); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Lab3/src/ro/ase/acs/classes/Car.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | import ro.ase.acs.interfaces.Taxable; 4 | 5 | public final class Car extends Vehicle implements Taxable { 6 | private String color; 7 | private int capacity; 8 | 9 | public Car() { 10 | super(); 11 | color = "white"; 12 | capacity = 49; 13 | } 14 | 15 | public Car(String name, int speed, String color, int capacity) { 16 | super(name, speed); 17 | this.color = color; 18 | this.capacity = capacity; 19 | } 20 | 21 | public String getColor() { 22 | return color; 23 | } 24 | 25 | public void setColor(String color) { 26 | this.color = color; 27 | } 28 | 29 | public int getCapacity() { 30 | return capacity; 31 | } 32 | 33 | public void setCapacity(int capacity) { 34 | this.capacity = capacity; 35 | } 36 | 37 | @Override 38 | public double computeTax() { 39 | float tax = 0; 40 | if(capacity < 2000) { 41 | tax = (float)capacity / 1000 * 50; 42 | } 43 | else { 44 | tax = (float)capacity / 1000 * 100; 45 | } 46 | return tax < MIN_TAX ? MIN_TAX : tax; 47 | } 48 | 49 | @Override 50 | public final void move() { 51 | System.out.println("The car is moving"); 52 | } 53 | 54 | @Override 55 | public Object clone() throws CloneNotSupportedException { 56 | Car copy = (Car)super.clone(); 57 | copy.color = color; 58 | copy.capacity = capacity; 59 | return copy; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Lab4/src/ro/ase/acs/main/Main.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.main; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | import ro.ase.acs.classes.Car; 8 | import ro.ase.acs.classes.Vehicle; 9 | import ro.ase.acs.interfaces.Taxable; 10 | 11 | public class Main { 12 | 13 | public static void main(String[] args) { 14 | Car car = new Car("Dacia", 90, "black", 1400); 15 | System.out.println(car); 16 | 17 | Taxable t = car; 18 | double tax = t.computeTax(); 19 | System.out.println(tax); 20 | 21 | Vehicle v = new Car(); 22 | v.move(); 23 | 24 | if(car instanceof Cloneable) { 25 | Car c2; 26 | try { 27 | c2 = (Car)car.clone(); 28 | c2.setCapacity(1900); 29 | System.out.println(car.getCapacity()); 30 | } catch (CloneNotSupportedException e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | Integer x = 5; 35 | int y = x; 36 | System.out.println(y); 37 | 38 | List list = new ArrayList<>(); 39 | list.add(4); 40 | list.add(5); 41 | list.add(6); 42 | 43 | for(int i = 0; i < list.size(); i++) { 44 | System.out.println(list.get(i)); 45 | } 46 | 47 | list.remove(2); 48 | list.add(1, 3); 49 | System.out.println(); 50 | for(Integer i : list) { 51 | System.out.println(i); 52 | } 53 | 54 | list.set(0, 9); 55 | System.out.println(); 56 | 57 | for(Iterator it = list.iterator(); it.hasNext(); ) { 58 | System.out.println(it.next()); 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Lab12/src/ro/ase/acs/nosql/MongoProgMain.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.nosql; 2 | 3 | import java.util.Date; 4 | 5 | import org.bson.Document; 6 | 7 | import com.mongodb.MongoClient; 8 | import com.mongodb.client.FindIterable; 9 | import com.mongodb.client.MongoCollection; 10 | import com.mongodb.client.MongoCursor; 11 | import com.mongodb.client.MongoDatabase; 12 | 13 | public class MongoProgMain { 14 | 15 | public static void main(String[] args) { 16 | MongoClient mongoClient = 17 | new MongoClient("localhost", 27017); 18 | MongoDatabase db = mongoClient.getDatabase("test"); 19 | 20 | if(db.getCollection("employees") != null) { 21 | db.getCollection("employees").drop(); 22 | } 23 | 24 | db.createCollection("employees"); 25 | Document document = 26 | new Document().append("name", "Gigel Ionescu"). 27 | append("birthdate", new Date()). 28 | append("address", "Stefan cel Mare nr 20"). 29 | append("salary", 2000); 30 | MongoCollection collection = 31 | db.getCollection("employees"); 32 | collection.insertOne(document); 33 | 34 | Document document2 = 35 | new Document().append("name", "Ionel Popescu"). 36 | append("address", "Mihai Bravu 20"). 37 | append("salary", 4000); 38 | collection.insertOne(document2); 39 | 40 | collection = db.getCollection("employees"); 41 | FindIterable it = collection.find(); 42 | MongoCursor cursor = it.iterator(); 43 | 44 | while(cursor.hasNext()) { 45 | System.out.println(cursor.next()); 46 | } 47 | 48 | mongoClient.close(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Lab4/src/ro/ase/acs/classes/Car.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | import ro.ase.acs.interfaces.Taxable; 4 | 5 | public final class Car extends Vehicle implements Taxable { 6 | private String color; 7 | private int capacity; 8 | 9 | public Car() { 10 | super(); 11 | color = "white"; 12 | capacity = 49; 13 | } 14 | 15 | public Car(String name, int speed, String color, int capacity) { 16 | super(name, speed); 17 | this.color = color; 18 | this.capacity = capacity; 19 | } 20 | 21 | public String getColor() { 22 | return color; 23 | } 24 | 25 | public void setColor(String color) { 26 | this.color = color; 27 | } 28 | 29 | public int getCapacity() { 30 | return capacity; 31 | } 32 | 33 | public void setCapacity(int capacity) { 34 | this.capacity = capacity; 35 | } 36 | 37 | @Override 38 | public double computeTax() { 39 | float tax = 0; 40 | if(capacity < 2000) { 41 | tax = (float)capacity / 1000 * 50; 42 | } 43 | else { 44 | tax = (float)capacity / 1000 * 100; 45 | } 46 | return tax < MIN_TAX ? MIN_TAX : tax; 47 | } 48 | 49 | @Override 50 | public final void move() { 51 | System.out.println("The car is moving"); 52 | } 53 | 54 | @Override 55 | public Object clone() throws CloneNotSupportedException { 56 | Car copy = (Car)super.clone(); 57 | copy.color = color; 58 | copy.capacity = capacity; 59 | return copy; 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return "Car [color=" + color + ", capacity=" + capacity + ", name=" + getName() + ", speed=" 65 | + getSpeed() + "]"; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Lab11/src/ro/ase/acs/client/TcpClient.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.client; 2 | 3 | import java.io.DataInputStream; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.OutputStream; 8 | import java.net.Socket; 9 | import java.util.Scanner; 10 | 11 | public class TcpClient extends Thread { 12 | private Socket socket; 13 | 14 | public TcpClient(Socket socket) { 15 | this.socket = socket; 16 | } 17 | 18 | @Override 19 | public void run() { 20 | super.run(); 21 | 22 | while(true) { 23 | InputStream inputStream; 24 | try { 25 | inputStream = socket.getInputStream(); 26 | DataInputStream dataInputStream = 27 | new DataInputStream(inputStream); 28 | String message = dataInputStream.readUTF(); 29 | System.out.println(message); 30 | } catch (IOException e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | } 35 | 36 | public static void main(String[] args) { 37 | Scanner scanner = new Scanner(System.in); 38 | System.out.print("Your name: "); 39 | String name = scanner.nextLine(); 40 | try(Socket socket = new Socket("localhost", 7777)) { 41 | TcpClient client = new TcpClient(socket); 42 | client.start(); 43 | while(true) { 44 | OutputStream outputStream = socket.getOutputStream(); 45 | DataOutputStream dataOutputStream = 46 | new DataOutputStream(outputStream); 47 | String message = scanner.nextLine(); 48 | dataOutputStream.writeUTF( 49 | String.format("%s: %s", name, message)); 50 | } 51 | } 52 | catch(Exception e) { 53 | e.printStackTrace(); 54 | } 55 | scanner.close(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Lab10/src/ro/ase/acs/client/UdpClient.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.client; 2 | 3 | import java.io.IOException; 4 | import java.net.DatagramPacket; 5 | import java.net.DatagramSocket; 6 | import java.net.InetAddress; 7 | import java.net.SocketException; 8 | import java.util.Scanner; 9 | 10 | public class UdpClient extends Thread { 11 | private DatagramSocket socket; 12 | 13 | public UdpClient() { 14 | try { 15 | socket =new DatagramSocket(); 16 | } catch (SocketException e) { 17 | e.printStackTrace(); 18 | } 19 | } 20 | 21 | @Override 22 | public void run() { 23 | super.run(); 24 | while(true) { 25 | byte[] buffer = new byte[256]; 26 | DatagramPacket receivedPacket = 27 | new DatagramPacket(buffer, buffer.length); 28 | try { 29 | socket.receive(receivedPacket); 30 | String receivedMessage = new String(receivedPacket.getData()); 31 | System.out.println(receivedMessage); 32 | } catch (IOException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } 37 | 38 | public static void main(String args[]) { 39 | Scanner scanner = new Scanner(System.in); 40 | UdpClient client = new UdpClient(); 41 | client.start(); 42 | try { 43 | InetAddress serverAddress = 44 | InetAddress.getByName("localhost"); 45 | int serverPort = 7777; 46 | 47 | while(true) { 48 | String message = scanner.nextLine(); 49 | byte[] bytes = message.getBytes(); 50 | DatagramPacket packet = 51 | new DatagramPacket(bytes, bytes.length, 52 | serverAddress, serverPort); 53 | client.socket.send(packet); 54 | } 55 | } 56 | catch(IOException e) { 57 | e.printStackTrace(); 58 | } 59 | scanner.close(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Lab2/src/ro/ase/acs/classes/Car.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | public class Car { 4 | private String producer; 5 | private float price; 6 | private FuelType fuelType; 7 | private int[] distances; 8 | 9 | public Car() { 10 | producer = ""; 11 | price = 4000; 12 | fuelType = FuelType.gas; 13 | distances = new int[1]; 14 | distances[0] = 100; 15 | } 16 | 17 | public Car(String producer, float price) { 18 | this.producer = producer; 19 | this.price = price; 20 | fuelType = FuelType.gas; 21 | } 22 | 23 | public float getPrice() { 24 | return price; 25 | } 26 | 27 | public void setPrice(float price) { 28 | this.price = price; 29 | } 30 | 31 | public FuelType getFuelType() { 32 | return fuelType; 33 | } 34 | 35 | public void setFuelType(FuelType fuelType) { 36 | this.fuelType = fuelType; 37 | } 38 | 39 | public String getProducer() { 40 | return producer; 41 | } 42 | 43 | public void setProducer(String producer) { 44 | this.producer = producer; 45 | } 46 | 47 | public int[] getDistances() { 48 | if(distances != null) { 49 | int[] copy = new int[distances.length]; 50 | System.arraycopy(distances, 0, copy, 0, distances.length); 51 | return copy; 52 | } 53 | else { 54 | return null; 55 | } 56 | } 57 | 58 | public void setDistances(int[] distances) { 59 | if(distances != null) { 60 | this.distances = new int[distances.length]; 61 | for(int i = 0; i < distances.length; i++) { 62 | this.distances[i] = distances[i]; 63 | } 64 | } 65 | } 66 | 67 | @Override 68 | public Object clone() { 69 | Car copy = new Car(); 70 | copy.producer = producer; 71 | copy.price = price; 72 | copy.fuelType = fuelType; 73 | copy.distances = getDistances(); 74 | return copy; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Lab10/src/ro/ase/acs/server/UdpServer.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.server; 2 | 3 | import java.io.IOException; 4 | import java.net.DatagramPacket; 5 | import java.net.DatagramSocket; 6 | import java.net.InetAddress; 7 | import java.net.SocketException; 8 | import java.util.Scanner; 9 | 10 | public class UdpServer extends Thread { 11 | private final int port = 7777; 12 | private DatagramSocket socket; 13 | 14 | public UdpServer() { 15 | try { 16 | socket = new DatagramSocket(port); 17 | System.out.println("Server started on port " + port); 18 | } catch (SocketException e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | 23 | @Override 24 | public void run() { 25 | super.run(); 26 | while(true) { 27 | byte[] buffer = new byte[256]; 28 | DatagramPacket packet = new DatagramPacket(buffer, buffer.length); 29 | try { 30 | socket.receive(packet); 31 | String receivedMessage = new String(packet.getData()); 32 | System.out.println(receivedMessage); 33 | } catch (IOException e) { 34 | e.printStackTrace(); 35 | } 36 | } 37 | } 38 | 39 | public static void main(String[] args) { 40 | Scanner scanner = new Scanner(System.in); 41 | UdpServer server = new UdpServer(); 42 | try { 43 | byte[] buffer = new byte[256]; 44 | DatagramPacket packet = new DatagramPacket(buffer, buffer.length); 45 | server.socket.receive(packet); 46 | String receivedMessage = new String(packet.getData()); 47 | System.out.println(receivedMessage); 48 | 49 | InetAddress clientAddress = packet.getAddress(); 50 | int clientPort = packet.getPort(); 51 | server.start(); 52 | while(true) { 53 | String message = scanner.nextLine(); 54 | byte[] bytes = message.getBytes(); 55 | DatagramPacket packetToBeSend = 56 | new DatagramPacket(bytes, bytes.length, 57 | clientAddress, clientPort); 58 | server.socket.send(packetToBeSend); 59 | } 60 | } 61 | catch(IOException e) { 62 | e.printStackTrace(); 63 | } 64 | scanner.close(); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Lab5/src/ro/ase/acs/main/Main.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.main; 2 | 3 | import java.util.HashMap; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | import java.util.Map; 7 | import java.util.Set; 8 | import java.util.TreeSet; 9 | import java.util.Vector; 10 | 11 | import ro.ase.acs.classes.Car; 12 | import ro.ase.acs.classes.Vehicle; 13 | import ro.ase.acs.interfaces.Taxable; 14 | 15 | public class Main { 16 | 17 | static void add(Car x, Car y) { 18 | x = new Car(x.getName(), x.getSpeed(), x.getColor(), x.getCapacity()); 19 | x.setCapacity(x.getCapacity() + y.getCapacity()); 20 | } 21 | 22 | public static void main(String[] args) { 23 | Taxable t = new Car("Dacia", 100, "red", 1400); 24 | System.out.println(t.computeTax()); 25 | 26 | Vehicle v = new Car(); 27 | v.move(); 28 | 29 | Car c = new Car("Ford", 90, "white", 1000); 30 | Car c2 = null; 31 | if(c instanceof Cloneable) { 32 | try { 33 | c2 = (Car)c.clone(); 34 | c.setColor("black"); 35 | System.out.println(c2); 36 | } catch (CloneNotSupportedException e) { 37 | e.printStackTrace(); 38 | } 39 | } 40 | 41 | List list = new Vector<>(); 42 | list.add(5); 43 | list.add(4); 44 | list.add(1); 45 | 46 | list.add(3, 7); 47 | list.remove(0); 48 | 49 | for(int i = 0; i < list.size(); i++) { 50 | System.out.println(list.get(i)); 51 | } 52 | 53 | System.out.println(); 54 | 55 | for(Integer x : list) { 56 | System.out.printf("%d ", x); 57 | } 58 | 59 | list.add(0, 9); 60 | System.out.println(); 61 | 62 | for(Iterator it = list.iterator(); it.hasNext(); ) { 63 | System.out.println(it.next()); 64 | } 65 | 66 | Set set = new TreeSet(); 67 | set.add(c); 68 | c2.setCapacity(1600); 69 | set.add(c2); 70 | 71 | for(Car x : set) { 72 | System.out.println(x); 73 | } 74 | 75 | Map map = new HashMap(); 76 | map.put(c, "Ionel Ionescu"); 77 | map.put(c2, "Georgel Georgescu"); 78 | Car c3 = null; 79 | try { 80 | c3 = (Car)c.clone(); 81 | } catch (CloneNotSupportedException e) { 82 | e.printStackTrace(); 83 | } 84 | map.put(c3, "Petru Petrescu"); 85 | 86 | for(Car x : map.keySet()) { 87 | System.out.printf("%s : ", x.toString()); 88 | System.out.println(map.get(x)); 89 | } 90 | 91 | add(c, c2); 92 | System.out.println(c.getCapacity()); 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /Lab11/src/ro/ase/acs/server/TcpServer.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.server; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.DataInputStream; 5 | import java.io.DataOutputStream; 6 | import java.io.IOException; 7 | import java.io.InputStream; 8 | import java.io.InputStreamReader; 9 | import java.io.OutputStream; 10 | import java.net.ServerSocket; 11 | import java.net.Socket; 12 | import java.net.URL; 13 | import java.net.URLConnection; 14 | import java.util.List; 15 | import java.util.Vector; 16 | 17 | public class TcpServer extends Thread { 18 | private static List clients = new Vector<>(); 19 | private Socket socket; 20 | 21 | public TcpServer(Socket socket) { 22 | this.socket = socket; 23 | } 24 | 25 | public String receive() { 26 | String message = ""; 27 | InputStream inputStream; 28 | try { 29 | inputStream = socket.getInputStream(); 30 | DataInputStream dataInputStream = 31 | new DataInputStream(inputStream); 32 | message = dataInputStream.readUTF(); 33 | } catch (IOException e) { 34 | e.printStackTrace(); 35 | } 36 | return message; 37 | } 38 | 39 | public void send(String message) { 40 | for(Socket s : clients) { 41 | OutputStream outputStream; 42 | try { 43 | outputStream = s.getOutputStream(); 44 | DataOutputStream dataOutputStream = 45 | new DataOutputStream(outputStream); 46 | dataOutputStream.writeUTF(message); 47 | } catch (IOException e) { 48 | e.printStackTrace(); 49 | } 50 | } 51 | } 52 | 53 | @Override 54 | public void run() { 55 | super.run(); 56 | while(true) { 57 | String message = receive(); 58 | send(message); 59 | } 60 | } 61 | 62 | public static void main(String[] args) { 63 | try(ServerSocket server = new ServerSocket(7777)) { 64 | System.out.println("Server started on port 7777"); 65 | URL url = new URL("http://google.ro"); 66 | URLConnection connection = url.openConnection(); 67 | InputStream inputStream = connection.getInputStream(); 68 | InputStreamReader reader = new InputStreamReader(inputStream); 69 | BufferedReader bufferedReader = new BufferedReader(reader); 70 | System.out.println(bufferedReader.readLine()); 71 | 72 | while(true) { 73 | Socket socket = server.accept(); 74 | clients.add(socket); 75 | TcpServer serverInstance = new TcpServer(socket); 76 | serverInstance.start(); 77 | } 78 | } 79 | catch(Exception e) { 80 | e.printStackTrace(); 81 | } 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /Lab7/src/ro/ase/acs/main/Main.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.main; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | import java.util.Random; 6 | import java.util.stream.Collectors; 7 | 8 | import ro.ase.acs.classes.Sum; 9 | import ro.ase.acs.interfaces.BinaryOperator; 10 | import ro.ase.acs.interfaces.Moveable; 11 | import ro.ase.acs.interfaces.UnaryOperator; 12 | 13 | public class Main { 14 | 15 | public static int transform(int x) { 16 | if(x % 2 == 0) { 17 | return x / 2; 18 | } 19 | else { 20 | return 2 * x; 21 | } 22 | } 23 | 24 | public static void main(String[] args) { 25 | Random random = new Random(); 26 | BinaryOperator operator; 27 | 28 | if(random.nextInt() % 2 == 0) { 29 | operator = new Sum(); 30 | } 31 | else { 32 | operator = new BinaryOperator() { 33 | 34 | @Override 35 | public double operate(double operand1, double operand2) { 36 | return operand1 - operand2; 37 | } 38 | }; 39 | } 40 | 41 | System.out.println(operator.operate(5, 3)); 42 | 43 | new Sum().operate(3, 2); 44 | 45 | operator = (o1, o2) -> o1 * o2; 46 | System.out.println(operator.operate(3, 5)); 47 | 48 | operator = (x, y) -> { double sum = x + y; return sum / 2; }; 49 | 50 | Moveable m = () -> System.out.println("The car is moving"); 51 | m.move(); 52 | 53 | UnaryOperator op = o -> ++o; 54 | System.out.println(op.operate(5)); 55 | 56 | List list = Arrays.asList(3, 4, 1, 5, 1, 8, 12); 57 | long nb = list.stream().filter(e -> (e % 2 == 0)).count(); 58 | System.out.println(nb); 59 | 60 | List newList = list.stream().distinct(). 61 | filter(e -> e < 8).sorted().collect(Collectors.toList()); 62 | for(Integer i : newList) { 63 | System.out.println(i); 64 | } 65 | 66 | List stringList = Arrays.asList("something", "something else", "a", "ab", "abc"); 67 | String result = stringList.stream(). 68 | filter(s -> s.length() > 2).collect(Collectors.joining(", ")); 69 | System.out.println(result); 70 | 71 | list.stream().distinct().sorted().map(e -> e * e).forEach(e -> System.out.println(e)); 72 | 73 | random.ints().limit(5).sorted().forEach(System.out::println); 74 | 75 | list.stream().map(Main::transform).forEach(System.out::println); 76 | 77 | String filteredList = stringList.parallelStream(). 78 | filter(e -> e.startsWith("a")).collect(Collectors.joining(", ")); 79 | System.out.println(filteredList); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /Lab5/src/ro/ase/acs/classes/Car.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | import ro.ase.acs.interfaces.Taxable; 4 | 5 | public final class Car extends Vehicle implements Taxable, Comparable { 6 | private String color; 7 | private int capacity; 8 | 9 | public Car() { 10 | super(); 11 | color = "black"; 12 | capacity = 50; 13 | } 14 | 15 | public Car(String name, int speed, String color, int capacity) { 16 | super(name, speed); 17 | this.color = color; 18 | this.capacity = capacity; 19 | } 20 | 21 | public String getColor() { 22 | return color; 23 | } 24 | 25 | public void setColor(String color) { 26 | this.color = color; 27 | } 28 | 29 | public int getCapacity() { 30 | return capacity; 31 | } 32 | 33 | public void setCapacity(int capacity) { 34 | this.capacity = capacity; 35 | } 36 | 37 | @Override 38 | public Object clone() throws CloneNotSupportedException { 39 | Car copy = (Car)super.clone(); 40 | copy.color = color; 41 | copy.capacity = capacity; 42 | return copy; 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | StringBuilder builder = new StringBuilder(); 48 | builder.append("Car [color="); 49 | builder.append(color); 50 | builder.append(", capacity="); 51 | builder.append(capacity); 52 | builder.append(", name="); 53 | builder.append(getName()); 54 | builder.append(", speed="); 55 | builder.append(getSpeed()); 56 | builder.append("]"); 57 | return builder.toString(); 58 | } 59 | 60 | @Override 61 | public float computeTax() { 62 | float tax = 0; 63 | if(capacity < 2000) { 64 | tax = (float)capacity / 1000 * 50; 65 | } 66 | else { 67 | tax = (float)capacity / 1000 * 100; 68 | } 69 | return (tax > MIN_TAX) ? tax : MIN_TAX; 70 | } 71 | 72 | @Override 73 | public final void move() { 74 | System.out.println("The car is moving with " + getSpeed() + 75 | " km/h"); 76 | } 77 | 78 | @Override 79 | public int compareTo(Car o) { 80 | if(capacity < o.capacity) { 81 | return -1; 82 | } 83 | else if(capacity == o.capacity) { 84 | return 0; 85 | } 86 | else { 87 | return 1; 88 | } 89 | } 90 | 91 | @Override 92 | public boolean equals(Object obj) { 93 | if(obj instanceof Car) { 94 | Car o = (Car)obj; 95 | return getName().equals(o.getName()) && getSpeed() == o.getSpeed() && 96 | color.equals(o.color) && capacity == o.capacity; 97 | } 98 | return false; 99 | } 100 | 101 | @Override 102 | public int hashCode() { 103 | return 31* (getName().hashCode() + getSpeed()) * color.hashCode() + capacity; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /Lab6/src/ro/ase/acs/main/Main.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.main; 2 | 3 | import java.util.HashMap; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | import java.util.Map; 7 | import java.util.Set; 8 | import java.util.TreeSet; 9 | import java.util.Vector; 10 | 11 | import ro.ase.acs.classes.Car; 12 | import ro.ase.acs.classes.Vehicle; 13 | import ro.ase.acs.interfaces.Taxable; 14 | 15 | public class Main { 16 | 17 | //just a sample method to demonstrate that 18 | //parameters are passed always by value in Java 19 | static void add(Car x, Car y) { 20 | x = new Car(x.getName(), x.getSpeed(), x.getColor(), x.getCapacity()); 21 | x.setCapacity(x.getCapacity() + y.getCapacity()); 22 | } 23 | 24 | public static void main(String[] args) { 25 | Taxable t = new Car("Dacia", 100, "red", 1400); 26 | System.out.println(t.computeTax()); 27 | 28 | Vehicle v = new Car(); 29 | v.move(); 30 | 31 | Car c = new Car("Ford", 90, "white", 1000); 32 | Car c2 = null; 33 | //the instanceof operator checks 34 | //if the Car inherits Cloneable 35 | if(c instanceof Cloneable) { 36 | try { 37 | //a deep copy is made 38 | c2 = (Car)c.clone(); 39 | c.setColor("black"); 40 | //toString is called when we print the object 41 | System.out.println(c2); 42 | } catch (CloneNotSupportedException e) { 43 | e.printStackTrace(); 44 | } 45 | } 46 | 47 | //Generics in Java accept reference types only 48 | //We need to use the wrapper class Integer 49 | //instead of the int primitive 50 | List list = new Vector<>(); 51 | list.add(5); 52 | list.add(4); 53 | list.add(1); 54 | 55 | list.add(3, 7); 56 | list.remove(0); 57 | 58 | //classic for loop 59 | for(int i = 0; i < list.size(); i++) { 60 | System.out.println(list.get(i)); 61 | } 62 | 63 | System.out.println(); 64 | 65 | //enhanced for 66 | for(Integer x : list) { 67 | System.out.printf("%d ", x); 68 | } 69 | 70 | list.add(0, 9); 71 | System.out.println(); 72 | 73 | //iterator based loop 74 | for(Iterator it = list.iterator(); it.hasNext(); ) { 75 | System.out.println(it.next()); 76 | } 77 | 78 | //Set accepts unique elements only 79 | //the TreeSet needs the compareTo method 80 | Set set = new TreeSet(); 81 | set.add(c); 82 | c2.setCapacity(1600); 83 | set.add(c2); 84 | 85 | for(Car x : set) { 86 | System.out.println(x); 87 | } 88 | 89 | //Map saves unique keys and the value that correspond 90 | //to these keys 91 | //HashMap uses the hashCode implementation in order to 92 | //insert the elements in a specific order 93 | Map map = new HashMap(); 94 | map.put(c, "Ion Ionescu"); 95 | map.put(c2, "Gigel Georgescu"); 96 | Car c3 = null; 97 | try { 98 | c3 = (Car)c.clone(); 99 | } catch (CloneNotSupportedException e) { 100 | e.printStackTrace(); 101 | } 102 | map.put(c3, "Petru Petrescu"); 103 | 104 | for(Car x : map.keySet()) { 105 | System.out.printf("%s : ", x.toString()); 106 | System.out.println(map.get(x)); 107 | } 108 | 109 | add(c, c2); 110 | System.out.println(c.getCapacity()); 111 | } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /Lab12/src/ro/ase/acs/sql/SQLiteProgMain.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.sql; 2 | 3 | import java.sql.Connection; 4 | import java.sql.Date; 5 | import java.sql.DriverManager; 6 | import java.sql.PreparedStatement; 7 | import java.sql.ResultSet; 8 | import java.sql.SQLException; 9 | import java.sql.Statement; 10 | 11 | public class SQLiteProgMain { 12 | 13 | public static void main(String[] args) { 14 | Connection connection = null; 15 | try { 16 | Class.forName("org.sqlite.JDBC"); 17 | connection = 18 | DriverManager.getConnection("jdbc:sqlite:database.db"); 19 | connection.setAutoCommit(false); 20 | createTable(connection); 21 | insertValues(connection); 22 | selectData(connection); 23 | 24 | } catch (ClassNotFoundException e) { 25 | e.printStackTrace(); 26 | } catch (SQLException e) { 27 | e.printStackTrace(); 28 | } 29 | finally { 30 | if(connection != null) { 31 | try { 32 | connection.close(); 33 | } catch (SQLException e) { 34 | e.printStackTrace(); 35 | } 36 | } 37 | } 38 | } 39 | 40 | public static void createTable(Connection connection) { 41 | String sqlDrop = "DROP TABLE IF EXISTS employees"; 42 | String sqlCreate = "CREATE TABLE employees(id INTEGER PRIMARY KEY, " + 43 | "name TEXT, birthdate LONG, address TEXT, salary REAL)"; 44 | 45 | Statement statement; 46 | try { 47 | statement = connection.createStatement(); 48 | statement.executeUpdate(sqlDrop); 49 | statement.executeUpdate(sqlCreate); 50 | statement.close(); 51 | connection.commit(); 52 | } catch (SQLException e) { 53 | e.printStackTrace(); 54 | } 55 | } 56 | 57 | public static void insertValues(Connection connection) { 58 | String sqlInsert = "INSERT INTO employees VALUES(1, 'Ionel Popescu', 1589874134752, " + 59 | "'Stefan cel Mare nr 20', 2000)"; 60 | 61 | String sqlInsertWithParams = "INSERT INTO employees(name, birthdate, address, salary) " + 62 | "VALUES(?, ?, ?, ?)"; 63 | try { 64 | Statement statement = connection.createStatement(); 65 | statement.executeUpdate(sqlInsert); 66 | statement.close(); 67 | connection.commit(); 68 | 69 | PreparedStatement preparedStatement = 70 | connection.prepareStatement(sqlInsertWithParams); 71 | preparedStatement.setString(1, "Gigel Ionescu"); 72 | preparedStatement.setLong(2, Date.valueOf("1995-05-17").getTime()); 73 | preparedStatement.setString(3, "Mihai Bravu nr 15"); 74 | preparedStatement.setDouble(4, 4000); 75 | 76 | preparedStatement.executeUpdate(); 77 | preparedStatement.close(); 78 | connection.commit(); 79 | } catch (SQLException e) { 80 | e.printStackTrace(); 81 | } 82 | } 83 | 84 | public static void selectData(Connection connection) { 85 | String sqlSelect = "SELECT * FROM employees"; 86 | try { 87 | Statement statement = connection.createStatement(); 88 | ResultSet rs = statement.executeQuery(sqlSelect); 89 | while(rs.next()) { 90 | int id = rs.getInt("id"); 91 | System.out.println("id: " + id); 92 | String name = rs.getString("name"); 93 | System.out.println("name: " + name); 94 | long birthDate = rs.getLong("birthdate"); 95 | System.out.println("birthdate: " + new Date(birthDate)); 96 | String address = rs.getString("address"); 97 | System.out.println("address: " + address); 98 | double salary = rs.getDouble("salary"); 99 | System.out.println("salary: " + salary); 100 | } 101 | rs.close(); 102 | statement.close(); 103 | } catch (SQLException e) { 104 | e.printStackTrace(); 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /Lab9/src/ro/ase/acs/main/Main.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.main; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.concurrent.ExecutionException; 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.Future; 9 | import java.util.concurrent.TimeUnit; 10 | 11 | import ro.ase.acs.classes.ArraySummingCallable; 12 | import ro.ase.acs.classes.ArraySummingThread; 13 | 14 | public class Main { 15 | 16 | public static void main(String[] args) { 17 | final int NB_OF_ELEMENTS = 500_000_000; 18 | final int NB_OF_THREADS = 4; 19 | 20 | int[] array = new int[NB_OF_ELEMENTS]; 21 | 22 | for(int i = 0; i < NB_OF_ELEMENTS; i++) { 23 | array[i] = i + 1; 24 | } 25 | 26 | long sum = 0; 27 | long startTime = System.currentTimeMillis(); 28 | 29 | for(int i = 0; i < NB_OF_ELEMENTS; i++) { 30 | sum += array[i]; 31 | } 32 | 33 | long endTime = System.currentTimeMillis(); 34 | 35 | System.out.printf("Sequantial sum=%d computed in %d ms\r\n", 36 | sum, endTime-startTime); 37 | 38 | sum = 0; 39 | startTime = System.currentTimeMillis(); 40 | 41 | ArraySummingThread[] threads = 42 | new ArraySummingThread[NB_OF_THREADS]; 43 | for(int i = 0; i < NB_OF_THREADS; i++) { 44 | threads[i] = new ArraySummingThread(array, 45 | i * NB_OF_ELEMENTS / NB_OF_THREADS, 46 | (i + 1) * NB_OF_ELEMENTS / NB_OF_THREADS); 47 | threads[i].start(); 48 | } 49 | 50 | for(int i = 0; i < NB_OF_THREADS; i++) { 51 | try { 52 | threads[i].join(); 53 | } catch (InterruptedException e) { 54 | e.printStackTrace(); 55 | } 56 | } 57 | 58 | for(int i = 0; i < NB_OF_THREADS; i++) { 59 | sum += threads[i].getSum(); 60 | } 61 | 62 | endTime = System.currentTimeMillis(); 63 | 64 | System.out.printf("Thread array sum=%d computed in %d ms\r\n", 65 | sum, endTime-startTime); 66 | 67 | sum = 0; 68 | startTime = System.currentTimeMillis(); 69 | 70 | ExecutorService threadPool = 71 | Executors.newFixedThreadPool(NB_OF_THREADS); 72 | threads = new ArraySummingThread[NB_OF_THREADS]; 73 | for(int i = 0; i < NB_OF_THREADS; i++) { 74 | threads[i] = new ArraySummingThread(array, 75 | i * NB_OF_ELEMENTS / NB_OF_THREADS, 76 | (i + 1 ) * NB_OF_ELEMENTS / NB_OF_THREADS); 77 | threadPool.execute(threads[i]); 78 | } 79 | threadPool.shutdown(); 80 | 81 | try { 82 | threadPool.awaitTermination(30, TimeUnit.MINUTES); 83 | } catch (InterruptedException e) { 84 | e.printStackTrace(); 85 | } 86 | 87 | for(int i = 0; i < NB_OF_THREADS; i++) { 88 | sum += threads[i].getSum(); 89 | } 90 | 91 | endTime = System.currentTimeMillis(); 92 | 93 | System.out.printf("Threadpool sum=%d computed in %d ms\r\n", 94 | sum, endTime-startTime); 95 | 96 | sum = 0; 97 | startTime = System.currentTimeMillis(); 98 | 99 | ArraySummingCallable[] callables = 100 | new ArraySummingCallable[NB_OF_THREADS]; 101 | List> results = new ArrayList<>(); 102 | threadPool = Executors.newFixedThreadPool(NB_OF_THREADS); 103 | for(int i = 0; i < NB_OF_THREADS; i++) { 104 | callables[i] = new ArraySummingCallable(array, 105 | i * NB_OF_ELEMENTS / NB_OF_THREADS, 106 | (i + 1) * NB_OF_ELEMENTS / NB_OF_THREADS); 107 | results.add(threadPool.submit(callables[i])); 108 | } 109 | threadPool.shutdown(); 110 | 111 | for(Future f : results) { 112 | try { 113 | sum += f.get(); 114 | } catch (InterruptedException | ExecutionException e) { 115 | e.printStackTrace(); 116 | } 117 | } 118 | 119 | endTime = System.currentTimeMillis(); 120 | 121 | System.out.printf("Callable sum=%d computed in %d ms\r\n", 122 | sum, endTime-startTime); 123 | } 124 | 125 | } 126 | -------------------------------------------------------------------------------- /Lab6/src/ro/ase/acs/main/NewMain.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.main; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.BufferedWriter; 5 | import java.io.DataInputStream; 6 | import java.io.DataOutputStream; 7 | import java.io.FileInputStream; 8 | import java.io.FileOutputStream; 9 | import java.io.IOException; 10 | import java.io.InputStreamReader; 11 | import java.io.OutputStreamWriter; 12 | import java.util.Scanner; 13 | 14 | import ro.ase.acs.classes.Car; 15 | 16 | public class NewMain { 17 | 18 | public static void main(String[] args) { 19 | //Reading information from the Console example 20 | Scanner scanner = new Scanner(System.in); 21 | 22 | String yourName = ""; 23 | System.out.print("Name: "); 24 | yourName = scanner.nextLine(); 25 | 26 | System.out.print("Age: "); 27 | int age = scanner.nextInt(); 28 | 29 | System.out.println("Name = " + yourName); 30 | System.out.println("Age = " + age); 31 | 32 | scanner.close(); 33 | 34 | Car car = new Car("Renault", 90, "blue", 1500); 35 | 36 | //writing values into a text file 37 | try { 38 | FileOutputStream fileOutputStream = 39 | new FileOutputStream("car.txt"); 40 | OutputStreamWriter outputWriter = 41 | new OutputStreamWriter(fileOutputStream); 42 | BufferedWriter writer = 43 | new BufferedWriter(outputWriter); 44 | writer.write(car.getName()); 45 | writer.write(System.lineSeparator()); 46 | Integer speed = car.getSpeed(); 47 | writer.write(speed.toString()); 48 | writer.write(System.lineSeparator()); 49 | writer.write(car.getColor()); 50 | writer.write(System.lineSeparator()); 51 | Integer capacity = car.getCapacity(); 52 | writer.write(capacity.toString()); 53 | writer.close(); 54 | } catch (IOException e) { 55 | e.printStackTrace(); 56 | } 57 | 58 | //reading values from a text file 59 | try { 60 | FileInputStream fileInputStream = 61 | new FileInputStream("car.txt"); 62 | InputStreamReader inputStreamReader = new 63 | InputStreamReader(fileInputStream); 64 | BufferedReader reader = 65 | new BufferedReader(inputStreamReader); 66 | String name = reader.readLine(); 67 | int speed = Integer.parseInt(reader.readLine()); 68 | String color = reader.readLine(); 69 | int capacity = Integer.parseInt(reader.readLine()); 70 | reader.close(); 71 | Car c1 = new Car(name, speed, color, capacity); 72 | System.out.println(c1); 73 | } catch (IOException e) { 74 | e.printStackTrace(); 75 | } 76 | 77 | //writing data into a binary file 78 | //field-by-field approach 79 | try { 80 | FileOutputStream binaryOutputStream = 81 | new FileOutputStream("car.bin"); 82 | DataOutputStream dataOutputStream = 83 | new DataOutputStream(binaryOutputStream); 84 | dataOutputStream.writeUTF(car.getName()); 85 | dataOutputStream.writeInt(car.getSpeed()); 86 | dataOutputStream.writeUTF(car.getColor()); 87 | dataOutputStream.writeInt(car.getCapacity()); 88 | dataOutputStream.close(); 89 | } catch (IOException e) { 90 | e.printStackTrace(); 91 | } 92 | 93 | //reading data from a binary file 94 | //field-by-field approach 95 | try(FileInputStream binaryInputStream = 96 | new FileInputStream("car.bin"); DataInputStream dataInputStream = 97 | new DataInputStream(binaryInputStream)) { 98 | //the try from above is called "try with resources" 99 | //because it automatically closes all the streams specified 100 | //between the round brackets 101 | String name = dataInputStream.readUTF(); 102 | int speed = dataInputStream.readInt(); 103 | String color = dataInputStream.readUTF(); 104 | int capacity = dataInputStream.readInt(); 105 | Car c2 = new Car(name, speed, color, capacity); 106 | System.out.println(c2); 107 | } 108 | catch(IOException e) { 109 | e.printStackTrace(); 110 | } 111 | 112 | //object serialization 113 | //we don't need try-catch because the exception 114 | //is handled in the Car class 115 | car.serialize(); 116 | 117 | //object deserialization 118 | //we need try-catch because the exception 119 | //is thrown further by the method 120 | try { 121 | Car c3 = Car.deserialize(); 122 | System.out.println(c3); 123 | } catch (ClassNotFoundException | IOException e) { 124 | e.printStackTrace(); 125 | } 126 | } 127 | 128 | } 129 | -------------------------------------------------------------------------------- /Lab6/src/ro/ase/acs/classes/Car.java: -------------------------------------------------------------------------------- 1 | package ro.ase.acs.classes; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.FileNotFoundException; 5 | import java.io.FileOutputStream; 6 | import java.io.IOException; 7 | import java.io.ObjectInputStream; 8 | import java.io.ObjectOutputStream; 9 | 10 | import ro.ase.acs.interfaces.Taxable; 11 | 12 | //Car is Serializable because it extends Vehicle 13 | //which is Serializable 14 | public final class Car extends Vehicle implements Taxable, Comparable { 15 | private static final long serialVersionUID = 1L; 16 | private String color; 17 | private int capacity; 18 | 19 | public Car() { 20 | super(); 21 | color = "black"; 22 | capacity = 50; 23 | } 24 | 25 | public Car(String name, int speed, String color, int capacity) { 26 | super(name, speed); 27 | this.color = color; 28 | this.capacity = capacity; 29 | } 30 | 31 | public String getColor() { 32 | return color; 33 | } 34 | 35 | public void setColor(String color) { 36 | this.color = color; 37 | } 38 | 39 | public int getCapacity() { 40 | return capacity; 41 | } 42 | 43 | public void setCapacity(int capacity) { 44 | this.capacity = capacity; 45 | } 46 | 47 | @Override 48 | public Object clone() throws CloneNotSupportedException { 49 | Car copy = (Car)super.clone(); 50 | copy.color = color; 51 | copy.capacity = capacity; 52 | return copy; 53 | } 54 | 55 | //The toString method is called in order to 56 | //convert to object into a String 57 | //for example when we want to display it to the console 58 | // 59 | //String concatenation is not the best approach 60 | //so we used StringBuilder instead 61 | @Override 62 | public String toString() { 63 | StringBuilder builder = new StringBuilder(); 64 | builder.append("Car [color="); 65 | builder.append(color); 66 | builder.append(", capacity="); 67 | builder.append(capacity); 68 | builder.append(", name="); 69 | builder.append(getName()); 70 | builder.append(", speed="); 71 | builder.append(getSpeed()); 72 | builder.append("]"); 73 | return builder.toString(); 74 | } 75 | 76 | @Override 77 | public float computeTax() { 78 | float tax = 0; 79 | if(capacity < 2000) { 80 | tax = (float)capacity / 1000 * 50; 81 | } 82 | else { 83 | tax = (float)capacity / 1000 * 100; 84 | } 85 | return (tax > MIN_TAX) ? tax : MIN_TAX; 86 | } 87 | 88 | @Override 89 | public final void move() { 90 | System.out.println("The car is moving with " + getSpeed() + 91 | " km/h"); 92 | } 93 | 94 | //compareTo method from the Comparable interface 95 | //is used by TreeSet or TreeMap collections 96 | //in order to insert the elements/keys 97 | @Override 98 | public int compareTo(Car o) { 99 | if(capacity < o.capacity) { 100 | return -1; 101 | } 102 | else if (capacity == o.capacity) { 103 | return 0; 104 | } 105 | else { 106 | return 1; 107 | } 108 | } 109 | 110 | 111 | //every time we override hashCode we should override equals too 112 | //and vice-versa 113 | //for two objects for which equals return true, the hashCode 114 | //should be the same (the opposite is not true) 115 | @Override 116 | public boolean equals(Object obj) { 117 | if(obj instanceof Car) { 118 | Car o = (Car)obj; 119 | return getName().equals(o.getName()) && getSpeed() == o.getSpeed() && 120 | color.equals(o.color) && capacity == o.capacity; 121 | } 122 | return false; 123 | } 124 | 125 | //usually we use prime numbers in order to generate unique integer values 126 | //the probability to have collisions is lower in this manner 127 | @Override 128 | public int hashCode() { 129 | return (31 * getName().hashCode() + getSpeed()) * 31 * color.hashCode() + capacity; 130 | } 131 | 132 | //binary serialization 133 | public void serialize() { 134 | FileOutputStream fileOutputStream = null; 135 | ObjectOutputStream outputStream = null; 136 | try { 137 | fileOutputStream = 138 | new FileOutputStream("object.bin"); 139 | if(fileOutputStream != null) { 140 | outputStream = 141 | new ObjectOutputStream(fileOutputStream); 142 | outputStream.writeObject(this); 143 | } 144 | } catch(FileNotFoundException e) { 145 | e.printStackTrace(); 146 | } catch (IOException e) { 147 | e.printStackTrace(); 148 | } 149 | finally { 150 | if(outputStream != null) { 151 | try { 152 | outputStream.close(); 153 | } catch (IOException e) { 154 | e.printStackTrace(); 155 | } 156 | } 157 | } 158 | } 159 | 160 | //binary deserialization 161 | public static Car deserialize() throws IOException, ClassNotFoundException { 162 | FileInputStream fileInputStream = 163 | new FileInputStream("object.bin"); 164 | ObjectInputStream objectInputStream = 165 | new ObjectInputStream(fileInputStream); 166 | Car c = (Car)objectInputStream.readObject(); 167 | objectInputStream.close(); 168 | return c; 169 | } 170 | } 171 | --------------------------------------------------------------------------------