Now Starting regular Codes Of Java
You Want To practice these Codes in Regular basis
You Want To practice these Codes in Regular basis
// #3 Arrays in Java
public class MyClass {
public static void main(String args[]) {
int arr[] = {2, 3, 6};
//Array declaration and initialization
System.out.println(arr[0]);
//Showing Array First element
}
}
public class MyClass {
public static void main(String args[]) {
int arr[] = {2, 3, 6};
//Array declaration and initialization
System.out.println(arr[0]);
//Showing Array First element
}
}
👍5
//#4 Declaring & iniitializing Array
public class MyClass {
public static void main(String args[]) {
int arr[]; //declaring array
arr = new int[20]; // allocating memory
arr[0]= 55; //iniitializing first element
arr[1]= 33; //initi.... 2nd Element
System.out.println(arr[0]);
}
}
public class MyClass {
public static void main(String args[]) {
int arr[]; //declaring array
arr = new int[20]; // allocating memory
arr[0]= 55; //iniitializing first element
arr[1]= 33; //initi.... 2nd Element
System.out.println(arr[0]);
}
}
👍7
// #5 Atithmetic Operations in Java
public class MyClass {
public static void main(String args[]) {
// declaring variables
int add, sub, div, mul, rem;
// 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
rem= x%y; // reminder
// 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 = "+ rem);
}
}
public class MyClass {
public static void main(String args[]) {
// declaring variables
int add, sub, div, mul, rem;
// 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
rem= x%y; // reminder
// 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 = "+ rem);
}
}
👍7🤣2
//#6 2 Number Adder(Calculator) from user input
// This for Scan anything entered by user (user inputs)
import java.util.Scanner;
class myClass {
public static void main(String[] args) {
int x,y,Sum;
Scanner s = new Scanner(System.in);
// it will take input from keyboard
System.out.println("Enter 1st Number: ");
x = s.nextInt(); // scanning
System.out.println("Enter 2nd Number: ");
y = s.nextInt(); // scanning
Sum = x+y;
System.out.println("Sum = "+ Sum);
}
}
// This for Scan anything entered by user (user inputs)
import java.util.Scanner;
class myClass {
public static void main(String[] args) {
int x,y,Sum;
Scanner s = new Scanner(System.in);
// it will take input from keyboard
System.out.println("Enter 1st Number: ");
x = s.nextInt(); // scanning
System.out.println("Enter 2nd Number: ");
y = s.nextInt(); // scanning
Sum = x+y;
System.out.println("Sum = "+ Sum);
}
}
👍4
Jiske pass Laptop nhi hai 👇
Mobile best Compiler's
C Compiler Click Here
C++ Compiler Click Here
Python Compiler Click Here
Java Compiler Click Here
Sql Compiler Click Here
Universal Compilers for mobile
Replit (web or app) Click Here
Jdoodle(web or app) Click Here
Programize (web) Search web
For any help Freely Say here
Mobile best Compiler's
C Compiler Click Here
C++ Compiler Click Here
Python Compiler Click Here
Java Compiler Click Here
Sql Compiler Click Here
Universal Compilers for mobile
Replit (web or app) Click Here
Jdoodle(web or app) Click Here
Programize (web) Search web
For any help Freely Say here
👍8
// Percentage Calculator in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// #For_Taking_Total_Sub_and_Max_Marks
System.out.print("Total subjects: ");
int tsub = sc.nextInt();
System.out.print("\nMax Marks: ");
float mmark = sc.nextInt();
System.out.println("");
int [] arr = new int[tsub];
// #For_taking_Subject_values
for (int i =0; i< tsub;i++){
System.out.print("Enter Marks of "+(i+1)+": ");
arr[i] = sc.nextInt();
}
float percent, avg=0, iper;
// #For_Showing_Results_and_Percentage
for (int i =0; i< tsub;i++){
System.out.println("\nMarks of "+(i+1)+" is "+arr[i]);
iper = ((arr[i]/mmark)*100);
System.out.println("Percentage of "+(i+1)+" is "+ iper);
avg = avg + iper;
}
percent = avg/tsub;
System.out.println("\nTotal Percent: "+ percent+ "%");
}
} // @Java_Codes_Pro
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// #For_Taking_Total_Sub_and_Max_Marks
System.out.print("Total subjects: ");
int tsub = sc.nextInt();
System.out.print("\nMax Marks: ");
float mmark = sc.nextInt();
System.out.println("");
int [] arr = new int[tsub];
// #For_taking_Subject_values
for (int i =0; i< tsub;i++){
System.out.print("Enter Marks of "+(i+1)+": ");
arr[i] = sc.nextInt();
}
float percent, avg=0, iper;
// #For_Showing_Results_and_Percentage
for (int i =0; i< tsub;i++){
System.out.println("\nMarks of "+(i+1)+" is "+arr[i]);
iper = ((arr[i]/mmark)*100);
System.out.println("Percentage of "+(i+1)+" is "+ iper);
avg = avg + iper;
}
percent = avg/tsub;
System.out.println("\nTotal Percent: "+ percent+ "%");
}
} // @Java_Codes_Pro
👍4
//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
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
// 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