Java Codes Basic to Advance
2.03K subscribers
4 photos
2 videos
1 file
48 links
Download Telegram
//09 Syntax:Method/Function
public class Method_Fuction {
public static void main(String[] args) {
Print();
}
//Method
static void Print() {
System.out.println("I just got executed!");
}
}
//09 Syntax:Method/Function:parameters
public class Parameters {
public static void main(String[] args) {
fun1("Liam", 5);
fun1("Jenny", 8);
fun1("Anja", 31);
}
static void fun1(String fname, int age) {
System.out.println(fname + " is " + age);
}
}
//09 Syntax:Method/Function:parameters// return value
public class Parameters {
public static void main(String[] args) {
System.out.println(myMethod(3));
}
static int myMethod(int x) {
return 5 + x;
}
}
//09 Syntax:Method/Function:parameters// return value
public class Parameters {
public static void main(String[] args) {
System.out.println(myMethod(5, 3));
}
static int myMethod(int x, int y) {
return x + y;
}
}
//09 Syntax:Method/Function:parameters// if-else
public class Parameters {
public static void main(String[] args) {
checkAge(20);
}
static void checkAge(int age) {
if (age < 18) {
System.out.println("Noty Ho raha B**** ke L****");
} else {
System.out.println("kunDi mat khaDkao raja seedha andar aao raja..");
}
}
}
//09 Syntax:Method/Function:Overloading
public class Overloading {
public static void main(String[] args) {
System.out.println(plusMethod(6, 8));
System.out.println(plusMethod(4.3, 9.2));
}
static int plusMethod(int x, int y) {
return x + y;
}
static double plusMethod(double x, double y) {
return x + y;
}
}
//09 Syntax:Method/Function:Recursion
public class Fibonnaci{
public static void main(String[] args){
int N=10;
System.out.print(n1+","+n2);
pfibonnaci(N-2);
}
static int n1=0,n2=1,ns;
static void pfibonnaci(int N){
if(N>0){
ns=n1+n2;
n1=n2;
n2=ns;
System.out.print(","+ns);
pfibonnaci(N-1);
}
}
}
//10 Syntax:OOPS:Class and Obj
public class Java_C_O{
public static void main(String[] args){
Java_C_O obj1 = new Java_C_O();
Java_C_O obj2 = new Java_C_O();
System.out.println(obj1.x);
System.out.println(obj2.y);
}
int x = 5, y=10;
}
//10 Syntax:OOPS:Class methods
public class Main {
public static void main(String[] args) {
myStaticMethod(); // Call the static method
Main myObj = new Main(); // Create an object of Main
myObj.myPublicMethod(); // Call the public method on the object
}

static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
}
//10 Syntax:OOPS:Class Constructors
public class Main {
public static void main(String[] args) {
Main myCar = new Main(1969, "Mustang");
System.out.println(myCar.modelYear + " " + myCar.modelName);
}

int modelYear;
String modelName;

public Main(int year, String name) {
modelYear = year;
modelName = name;
}

}
//10 Syntax:OOPS:Encapsulation
public class Person {
public static void main(String[] args) {
Person myObj = new Person();
myObj.setName("John"); // Set the value of the name variable to "John"
System.out.println(myObj.getName());
}

private String name; // private = restricted access
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}
//11 Syntax:Enums
public class Main {
public static void main(String[] args) {
Level myVar = Level.Mid;
System.out.println(myVar);
}

enum Level {
Max,
Mid,
Min
}
}
//12 Syntax:user input
import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner Obj = new Scanner(System.in);

System.out.println("Enter name, age and salary:");
String name = Obj.nextLine();
int age = Obj.nextInt();
double salary = Obj.nextDouble();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
👍1
//13 Syntax:Exception handling
public class Main {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Oh No...");
}
}
}
//13 Syntax:Exception handling 02
public class Main {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Oh No...");
} finally {
System.out.println("I will run whatever happen.");
}
}
}
👍1
//13 Syntax:Exception handling 03
public class Main {
public static void main(String[ ] args) {
throw new ArithmeticException("Access denied ");
}
//No Output only a error Msg will be shown
}
Exception in thread "main" java.lang.ArithmeticException: Access denied at Main.main(Main.java:3)

Output of 13 Syntax:Exception handling 03
👍1
//14 Syntax:File Create / write
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class CreateFile {
public static void main(String[] args) {
try {
File Obj = new File("Give Location where to create File");
if (Obj.createNewFile()) {
System.out.println("File created: " + Obj.getName());
FileWriter MWriter = new FileWriter("give Location of file which to write");
MWriter.write("What am I writting");
MWriter.close();
System.out.println("Successfully wrote to the file.");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
//Note This code can only run in Your PC
1👍1
Output:
File created: test.txt
Successfully wrote to the file

Output of 14 Syntax:File Create / write
//14 Syntax:File Read / Info
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFile {
public static void main(String[] args) {
File Obj = new File("Location of text File");
if (Obj.exists()) {
System.out.println("File name: " + Obj.getName());
System.out.println("Absolute path: " + Obj.getAbsolutePath());
System.out.println("Writeable: " + Obj.canWrite());
System.out.println("Readable " + Obj.canRead());
System.out.println("File size in bytes " + Obj.length());
}
else {
System.out.println("The file does not exist.");
}
try {
File ObjR = new File("Location of text file");
Scanner MReader = new Scanner(ObjR);
while (MReader.hasNextLine()) {
String data = MReader.nextLine();
System.out.println(data);
}
MReader.close();
}catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
//Note This code can only run in Your PC
👍2