Java Codes Basic to Advance
2.03K subscribers
4 photos
2 videos
1 file
48 links
Download Telegram
//enum code for advance java

enum Day{
SUN("sunday"), MON("monday"), TUE("tuesday"), WED("wednesday"), THU("thursday"), FRI("friday"), SAT("saturday");
public String Valu;
Day(String valu){
this.Valu = valu;
}

public String getValue(){
return Valu;
}
}

public class Main {
public static void main(String[] args) {
Day d = Day.MON;
System.out.println("Enum name: " + d.name() + " Value is " + d.getValue());

for(Day e: Day.values()){
System.out.println(e.name() + " " +e.getValue());
}
}
} // @Java_Codes_pro
// Serialization - Deserialization Advance java concept

// Warning : for using this code change path of 'filename' (written below in Serializ class) to your file path

import java.io.*;

class Student implements Serializable {
private static final long serialVersionUID = 1L;

private String name;
private int age;
private String address;
// transient int x; if we create this then this will be no serialized

public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}

public void setName(String name) {
this.name = name;
}

public void setAge(int age) {
this.age = age;
}

public void setAddress(String address) {
this.address = address;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getAddress() {
return address;
}

public String get(String z){
return z;
}

public int get(int age){
return age;
}

@Override
public String toString() {
return ("Student name is " + this.getName() + ", age is " + this.getAge() + ", and address is "
+ this.getAddress());
}
}

public class Serializ {
public static void main(String[] args) {
Student student = new Student("Sid", 19, "UP India");
FileOutputStream fileOut = null;
ObjectOutputStream objOut = null;
String filename = "C:\\Users\\mynms\\Desktop\\BCA\\Code\\Java2\\unit 4\\Test.txt";
// Serialization
try {
fileOut = new FileOutputStream(filename);
objOut = new ObjectOutputStream(fileOut);

objOut.writeObject(student);
objOut.close();
fileOut.close();

System.out.println("Object has been serialized: " + student);
} catch (Exception e) {
System.out.println("Not Serialized");
}

// Deserialization
FileInputStream fileIn = null;
ObjectInputStream objIn = null;

try {
fileIn = new FileInputStream(filename);
objIn = new ObjectInputStream(fileIn);

Student object = (Student) objIn.readObject();

System.out.println("Object has been deserialized " + object);

objIn.close();
fileIn.close();

} catch (IOException e) {
System.out.println("IO Exception " + e);
}
catch(ClassNotFoundException e){
System.out.println("Class not found exception " + e);
}
}
} // @Java_Codes_Pro
// File input Output Byte stream : Advance Java
// Warning : to use this code please change following path

import java.io.*;

public class File {
public static void main(String[] args) throws IOException {
FileInputStream inStream = new FileInputStream(
"C:\\Users\\mynms\\Desktop\\BCA\\Code\\Java2\\unit 4\\ReadFrom.txt");

FileOutputStream outStream = new FileOutputStream(
"C:\\Users\\mynms\\Desktop\\BCA\\Code\\Java2\\unit 4\\SendTo.txt");

// Reads byte at a time, it reached end of the file, returns -1
int content;

try {
while ((content = inStream.read()) != -1) {
outStream.write((byte) content);
}
System.out.println("File transfer conpleted successfully");
}

finally{
if (inStream != null)
inStream.close();

if (outStream != null)
outStream.close();
}
}
} // @Java_Codes_Pro
๐Ÿ‘4
// File input Output Byte stream : Advance Java

// Counting spaces, fullstops, characters, words and transfering

// Warning : to use this code please change following path

import java.io.*;

public class filecount {

static boolean isChar(int content){
return (content > 64 && content < 91 || content > 96 && content < 124);
}
public static void main(String[] args) throws IOException {

FileInputStream inStream = new FileInputStream(
"C:\\Users\\mynms\\Desktop\\BCA\\Code\\Java2\\unit 4\\ReadFrom.txt");

FileOutputStream outStream = new FileOutputStream(
"C:\\Users\\mynms\\Desktop\\BCA\\Code\\Java2\\unit 4\\SendTo.txt");

// Reads byte at a time, it reached end of the file, returns -1
int content;
int whitespaces = 0;
int fullstops = 0, chars = 0, words = 0;
boolean w = false;

try {
while ((content = inStream.read()) != -1) {
outStream.write((byte) content);

if(' ' == content)
whitespaces = whitespaces + 1;

if('.' == content)
fullstops += 1;

if(isChar(content)){
w = true;
chars += 1;
}
else if(w){
words += 1;
w = false;
}

}
if(isChar(content)){
w = true;
chars += 1;
}
else if(w){
words += 1;
w = false;
}

System.out.println("Whitespaces: " + whitespaces + "\nCharacters: " + chars + "\nFullstops: " + fullstops + "\nWords: "+ words);

System.out.println("File transfer conpleted successfully");

} finally{
if (inStream != null)
inStream.close();

if (outStream != null)
outStream.close();
}
}

} // @Java_Codes_Pro
๐Ÿ‘2
// Server Code

import java.net.*;
import java.io.*;

class Server {
ServerSocket server;
Socket socket;
BufferedReader br;
PrintWriter out;

// Constructor

public Server() {
try {
server = new ServerSocket(7777);
System.out.println("Server is ready to accept connection");
System.out.println("Waiting for client");
socket = server.accept();

br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());

startReading();
startWritting();

}
catch (Exception e) {
e.printStackTrace();
}

}

public void startReading() {
// thread reading
Runnable r1 = () -> {
System.out.println("Reader started..");

try {
while (true) {
String msg = br.readLine();
if (msg.equals("exit")) {
System.out.println("Client terminated the chat, Please press enter to free port");
socket.close();
break;
}
System.out.println("Client: " + msg);
}
} catch (Exception e) {
System.out.println("Connection Closed");
}
};
new Thread(r1).start();
}

public void startWritting() {
// thread sending data
Runnable r2 = () -> {
System.out.println("Writter started..");
String content = "";
try {
while (!socket.isClosed()) {

BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
content = br1.readLine();
out.println(content);
out.flush();

if (content.equals("exit")) {
System.out.println("You terminated the chat");
socket.close();
break;
}
}
}
catch (Exception e) {
if (content.equals("exit")) {
System.out.println("Connection Closed successfully");
}
else{
System.out.println("Connection terminated unsuccssefully \nTermnate all existing cmd's or check other errors");
}
}
};
new Thread(r2).start();
}

public static void main(String[] args) {
System.out.println("This is server.. going to start server");
new Server();
}
} // Server C
๐Ÿ‘5
import java.io.*;
import java.net.*;

public class Client {
Socket socket;
BufferedReader br;
PrintWriter out;

public Client() {

try {
System.out.println("Connecting to the server.");
socket = new Socket("127.0.0.1", 7777);
System.out.println("Connection successfull..");

br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());

startReading();
startWritting();

} catch (Exception e) {
System.out.println("Error: Firstly run Server Code");
}
}

public void startReading() {
// thread reading
Runnable r1 = () -> {
System.out.println("Reader started..");
try {
while (true) {

String msg = br.readLine();
if (msg.equals("exit")) {
System.out.println("Server terminated the chat, Please press enter to free port");
socket.close();
break;
}
System.out.println("Server: " + msg);
}
} catch (Exception e) {
System.out.println("Connection Closed");
}
};
new Thread(r1).start();
}

public void startWritting() {
// thread sending data
Runnable r2 = () -> {
System.out.println("Writter started..");
String content = "";
try {
while (!socket.isClosed()) {

BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));

content = br1.readLine();

out.println(content);
out.flush();

if (content.equals("exit")) {
System.out.println("You terminated the chat");
socket.close();
break;
}
}
} catch (Exception e) {
if (content.equals("exit")) {
System.out.println("Connection Closed successfully");
}
else{
System.out.println("Connection terminated unsuccssefully \nTermnate all existing cmd's or check other errors");
}
}
};
new Thread(r2).start();
}

public static void main(String[] args) {
System.out.println("Going to start Client..");
new Client();
}
} // Client Code : @Java_Codes_Pro
๐Ÿ‘5
Java Chat Code(2 Program sent Copy both (Server Code and Client Code) run And Practice with them)
// Factotial from recursion in java

import java.util.Scanner;

public class Main {
public static void main(String[] args) {

Scanner s = new Scanner(System.in);
System.out.print("Enter Number: " );
int n = s.nextInt();
if(n>26)
System.out.print("Please enter number less than 25\n" );
else{

System.out.println("Showing factorial for "+ n + " Number" );
System.out.print("\n" +Main.f(n));
}
}

// Factorial value calculation + series printing
static long f(long n){
if (n==1){
System.out.print(n);
return n;
}
else{
System.out.print(n + " * ");
return n*f(n-1);
}
}

} // @Java_Codes_Pro
// Factotial from loop in java
// see recursive approach in upper code
import java.util.Scanner;

public class Main {
public static void main(String[] args) {

Scanner s = new Scanner(System.in);
System.out.print("Enter Number: " );
int n = s.nextInt();
if(n>26)
System.out.print("Please enter number less than 25\n" );
else{

System.out.println("Showing factorial for "+ n + " Number" );
System.out.print("\n" +Main.f(n));
}
}

// Factorial value calculation + series printing
static long f(int n){
long h = 1;
for(int i = n; 0 < i; i-- ){
if(i == 1){
System.out.print(i);
continue;
}
System.out.print(i +" * " );
h = i*h;
}
return h;
}
}
// Join now @Java_Codes_Pro
๐Ÿ‘4
Learn how to create Telegram Bot
Also clear Javascript basics
(Regular video Uploading...)

https://youtube.com/playlist?list=PLjEYzWkdEvxvar65MToPfkA1x4CeZ_bNk
๐Ÿ‘3
// java basic threads example

public class MultithreadingExample {
    public static void main(String[] args) {

        // Creating and starting two threads
        Thread thread1 = new MyThread("Thread 1");
        Thread thread2 = new MyThread("Thread 2");
        thread1.start();
        thread2.start();
    }
}

class MyThread extends Thread {
    private String threadName;

    public MyThread(String name) {
        threadName = name;
    }

    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println(threadName + " - Count: " + i);
            try {
                Thread.sleep(1000); // Pause for 1 second
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
} // @Java_Codes_Pro
๐Ÿ‘4๐Ÿค”1
// #1 Your First Program

class HelloWorld {

public static void main(String[] args){
    System.out.println("Hello, World!");
     }

}
// #2 For Adding 2 numbers

public class MyClass {

public static void main(String args[]) {
int x=10;
int y=25;
int z=x+y;
System.out.println("Sum of x+y = " + z);
}

}
๐Ÿ‘3
// Airthmetic Operations in Java

public class MyClass {
  public static void main(String args[]) {
    
   // declaring variables
     int add, sub, div, mul, r;
    
     // Declaring two varables
     int x=4;
     int y=2;
    
    // performing Operations
     add = x+y;    // Addition
     sub = x-y;     // Subtraction
     div = x/y;      // Division
     mul = x+y;     // Multiply
     r= x%y;     // re m inder
    
   // Showing results
     System.out.println("4 + 2 = "+ add);
     System.out.println("4 - 2 = "+ sub);
     System.out.println("4 * 2 = "+ mul);
     System.out.println("4 / 2 = "+ div);
     System.out.println("4 % 2 = "+ r);
  }
}

// in this i introduced operators in java
// code by @Java_Codes_Pro
// buttons by @IOChannel_Bot
// Compiler @Compiler0bot
๐Ÿ‘2โค1
Are You tried these patterns ?
public class NumberTrianglePattern {
public static void main(String[] args) {
int rows = 5;

for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
} // @Java_Codes_Pro
๐Ÿ‘1
public class StarPyramidPattern {
public static void main(String[] args) {
int rows = 5;

for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
// @Java_Codes_Pro
๐Ÿ‘2
public class DiamondPattern {
public static void main(String[] args) {
int rows = 5;

for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}

for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
// @Java_Codes_pro
๐Ÿ‘1
All Codes
@C_Code5
@CPP_Coding
@Python_Codes_Pro
@Java_Codes_Pro
@jsCode0

Diffrent free Compiler bots
โœ… For group @CodeCompiler_Bot
โœ… For channel @IOChannel_bot
โœ… For Channel Cmpl @Compiler0bot
โœ… For inline @cmpbbot

info in @LogicBots

Discussion
@bca_mca_btech
// java string functions part 1

public class StringFunctionsProgram1 {
public static void main(String[] args) {
// STRING FUNCTIONS - PROGRAM 1

// 1. Length of the String
String str = "Hello, World!";
int length = str.length();
System.out.println("Length of the String: " + length);

// 2. Concatenation of Strings
String newStr = str.concat(" Welcome");
System.out.println("Concatenated String: " + newStr);

// 3. Extracting a Substring
String subStr = str.substring(7);
System.out.println("Substring: " + subStr);

// 4. Converting to Uppercase
String upperStr = str.toUpperCase();
System.out.println("Uppercase String: " + upperStr);

// 5. Converting to Lowercase
String lowerStr = str.toLowerCase();
System.out.println("Lowercase String: " + lowerStr);

// 6. Checking if a String starts with a specific prefix
boolean startsWith = str.startsWith("Hello");
System.out.println("Starts with 'Hello': " + startsWith);

// 7. Checking if a String ends with a specific suffix
boolean endsWith = str.endsWith("!");
System.out.println("Ends with '!': " + endsWith);

// 8. Finding the index of a specific character
int index = str.indexOf("o");
System.out.println("Index of 'o': " + index);

// 9. Replacing characters in a String
String replacedStr = str.replace("o", "x");
System.out.println("Replaced String: " + replacedStr);

// 10. Checking if a String contains a specific sequence of characters
boolean contains = str.contains("World");
System.out.println("Contains 'World': " + contains);
}
}