Learn Java
304 subscribers
92 photos
1 video
94 files
84 links
یاد گیری زبان برنامه نویسی جاوا و نکات و مفاهیم
کاربردی ان



@parsa8113
@bardiademon
Download Telegram
Maximum sum submatrix:
O(n^6)
public static void main(String[] args) {
int num[][]= {{-111,-18,-66},{-852,-112,12},{-132,-179,699}};
int max = Integer.MIN_VALUE;
for (int i = 0; i < num.length; i++) {
for (int j = i; j < num.length; j++) {
for (int k = 0; k <num[0].length ; k++) {

for (int l = k; l < num[0].length; l++) {
int nu=0;
for (int m = i; m <= j; m++) {
for (int n = k; n <=l ; n++) {
nu+=num[m][n];
}
}
if(nu>max){
max=nu;
}
}
}
}
}
System.out.println(max);
}
👍5
Longest common subsequence(Dynamic programming):
 public static int LCS(char[] a, int ai, int bi, char[] b, int match, int r[][]) {

if (ai == -1 || bi == -1) {
return 0;
}

if (r[ai][bi] > -1) {
return r[ai][bi];
}
if (a[ai] == b[bi]) {
int q = LCS(a, ai - 1, bi - 1, b, match + 1, r);
r[ai][bi] = q + 1;
return r[ai][bi];
}
int p2 = LCS(a, ai, bi - 1, b, match, r);
int p3 = LCS(a, ai - 1, bi, b, match, r);
int max = Math.max(p3, p2);
r[ai][bi] = Math.max(r[ai][bi], max);
return max;


}


Driver code:
   public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String a= scanner.nextLine();
String b=scanner.nextLine();
int r[][] = new int[a.length()][b.length()];
for (int i = 0; i < r.length; i++) {
for (int j = 0; j < r[i].length; j++) {
r[i][j]=-1;
}
}

System.out.println(LCS(a.toCharArray(),a.length()-1,b.length()-1,b.toCharArray(),0,r));
}
👍6
Longest Increasing subsequence(LIS):
Dynamic programming bottom up approach
import java.util.Arrays;

public class LongestIncreasingSubsequence {
public static void main(String[] args) {
int a[] = {3, 10, 2, 1, 20};
int length[] = new int[a.length];
Arrays.fill(length,1);
for (int i = 1; i < a.length; i++) {
int q= 1;
for (int j = 0; j <= i; j++) {
if (a[i]>a[j]){
if(q<=length[j]) {
q = length[j] + 1;

}
}

}
length[i]=q;
}
System.out.println(max(length));
}
public static int max(int a[]){
int max=-1;
for (int i = 0; i < a.length; i++) {
if(max<a[i]){
max=a[i];
}
}
return max;
}
}
wumpus_world.zip
1.5 MB
بازی ومپوس مبتنی بر منطق مرتبه اول با java fx
👏5👍1
Learn Java
https://youtu.be/NuMxO2jZlQ0?si=a91oeI454SiKWcX8
Swing 🔥🔥🔥🔥🔥🔥🔥
🤩3
👍5
22 شهریور روز گرامیداشت برنامه نویسان
{ Happy Programmer's Day }
👍6
Maximum Sum Submatrix Algorithm:

Kadane Algorithm(O(n)):
public int maxSubArraySum(int[] arr) {
int maxSoFar = arr[0];
int maxEndingHere = arr[0];

for (int i = 1; i < arr.length; i++) {

maxEndingHere = Math.max(arr[i], maxEndingHere + arr[i]);


maxSoFar = Math.max(maxSoFar, maxEndingHere);
}

return maxSoFar;
}

DP Bottm-up approach (O(n^2)):
    static int maxSum(int arr[]){
int dp[][] = new int[arr.length][arr.length];


for (int i = 0; i < dp.length; i++) {
dp[i][i]=arr[i];
for (int j = i+1; j < dp.length; j++) {
dp[i][j]=Math.max(arr[j],dp[i][j-1]+arr[j]);

}
}

return dp[0][arr.length-1];
}
👍5🤯1🤬1
اتصال به mongodb در جاوا با استفاده از فایل کانفیگ
package org.project..database;


import com.mongodb.client.*;
import org.bson.Document;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class MongoDBHandler {
private MongoClient mongoClient;
private MongoDatabase database;
private MongoCollection<Document> collection;

public MongoDBHandler(String configFilePath) {
try {
Properties properties = new Properties();
properties.load(new FileInputStream(configFilePath));
System.out.println(properties);
String host = properties.getProperty("remote_host");
int port = Integer.parseInt(properties.getProperty("remote_port"));
String dbName = properties.getProperty("remote_database");
String username = properties.getProperty("remote_user");
String password = properties.getProperty("remote_password");
String collectionName = properties.getProperty("remote_collection");

String uri = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/" + dbName;
mongoClient = MongoClients.create(uri);

database = mongoClient.getDatabase(dbName);
collection = database.getCollection(collectionName);

} catch (IOException e) {
e.printStackTrace();
}
//System.exit(0);
}

public void insertDocument(Document document) {
collection.insertOne(document);
}

public FindIterable<Document> readDocuments() {
return collection.find();
}

public long countDocuments() {
return collection.countDocuments();
}

public void updateDocument(Document query, Document update) {
collection.updateOne(query, new Document("$set", update));
}

public void deleteDocument(Document query) {
collection.deleteOne(query);
}

public MongoCollection<Document> getCollection() {
return collection;
}
}


فایل کانفیگ :

remote_host = localhost
remote_database = java
remote_user = parsa
remote_password = kav
remote_port = 27020
remote_collection = users
👍6
hibernate-sample.zip
133.7 KB
موجودیت ضعیف در Hibernate
مثال پیاده سازی دفترچه تلفن
👍4