0% found this document useful (0 votes)
4 views61 pages

Java Codes

The document contains multiple Java classes demonstrating various programming concepts such as ArrayLists, exception handling, interfaces, inheritance, and multithreading. It includes examples of arithmetic operations with error handling, usage of collections like ArrayDeque, and date/time formatting. Additionally, it showcases custom exceptions, nested try-catch blocks, and the implementation of interfaces in classes.

Uploaded by

patreaman007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views61 pages

Java Codes

The document contains multiple Java classes demonstrating various programming concepts such as ArrayLists, exception handling, interfaces, inheritance, and multithreading. It includes examples of arithmetic operations with error handling, usage of collections like ArrayDeque, and date/time formatting. Additionally, it showcases custom exceptions, nested try-catch blocks, and the implementation of interfaces in classes.

Uploaded by

patreaman007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

PrintStream;
import [Link];

public class arraylist {


public arraylist() {
}

public static void main(String[] args) {


ArrayList<Integer> l1 = new ArrayList();
ArrayList<Integer> l2 = new ArrayList();
[Link](6);
[Link](7);
[Link](5);
[Link](3);
[Link](1);
[Link](30);
[Link](15);
[Link](25);
[Link](l2);
[Link]([Link](25));

for(int i = 0; i < [Link](); ++i) {


PrintStream var10000 = [Link];
Object var10001 = [Link](i);
[Link]([Link](var10001) + " , ");
}

}
}
import [Link].*;

public class array_deque {


public static void main(String[] args) {

//deque--> double ended que

ArrayDeque<Integer>ad1=new ArrayDeque<>();

[Link](25);
[Link](69);
[Link](75);
[Link](85);
[Link](12);
[Link](36);
[Link](47);

[Link](85);
[Link](ad1);

}
}

// calendar class is a abstract class


import [Link].*;

public class calender_class {


public static void main(String[] args) {
Calendar c =
[Link]([Link]("Asia/Singapore"));
[Link]([Link]());
[Link]([Link]().getID());

}
}
import [Link];
import [Link];

class calexp extends Exception {

@Override
public String toString() {
return " Error ............ \n Number cannot be greater than
100000";
}

@Override
public String getMessage() {
return " Error ...... Multiplication input cannot be greater than
1000";
}

@Override
public String getLocalizedMessage() {
return "Error ...... 2nd number cannot be greater than 1st number";
}

class calculator {

public void addition() throws calexp {


[Link](" ADDITION ");
Scanner sc = new Scanner([Link]);
[Link]("Enter your first Number: ");
int a = [Link]();
[Link]("Enter your second number Number : ");
int b = [Link]();

if (b > 100000) {

throw new calexp();


}

[Link]("Sum of the numbers " + a + " and " + b + " is "


+ (a + b));
}

public void subtraction() throws calexp{


[Link](" SUBTRACTION ");
Scanner sc = new Scanner([Link]);
[Link]("Enter your first Number: ");
int a = [Link]();
[Link]("Enter your second number Number : ");
int b = [Link]();

if(b>a){
throw new calexp();

}
[Link]("Subtraction of " + b + " from " + a + " is " +
(a-b));

}
public void multiplication() throws calexp{
[Link](" MULTIPLICATION ");
Scanner sc = new Scanner([Link]);
[Link]("Enter your first Number: ");
int a = [Link]();
[Link]("Enter your second number Number : ");
int b = [Link]();

if( a*b > 7000){


throw new calexp();

}
[Link]("Multiplication of " + a + " and " + a + " is
" + (a*b));

public void division() throws calexp{


[Link](" DIVISION");
Scanner sc = new Scanner([Link]);
[Link]("Enter your first Number: ");
int a = [Link]();
[Link]("Enter your second number Number : ");
int b = [Link]();

if( b == 7000){
throw new calexp();

}
[Link]("Division of " + a + " by " + b + " is " +
(a/b));

public static class custom_calculator {


public static void main(String[] args) {

calculator calc = new calculator();


for(int i =1 ; i<=1000000000 ; i++) {

Scanner sc = new Scanner([Link]);


[Link]("Enter 1 for Addition ");
[Link]("Enter 2 for Subtration ");
[Link]("Enter 3 for Multiplication ");
[Link]("Enter 4 for Division \n ");

[Link]("Enter your Preference : ");

int num = [Link]();

if (num == 1) {
try {
[Link]();
} catch (Exception e) {
[Link]([Link]());
}
} else if (num == 2) {
try {
[Link]();
} catch (Exception e) {
[Link]([Link]());
}
} else if (num == 3) {

try {
[Link]();
} catch (Exception e) {
[Link]([Link]());
}
} else if (num == 4) {

try {
[Link]();
} catch (Exception e) {
[Link]("Error......cannot divide a number by
zero");
}
}
else{
[Link]("Please enter a correct preference ....");
}

}
}
}

import [Link];
import [Link];

public class date_time_formatter {


public static void main(String[] args) {

//
LocalDateTime dt = [Link]();
// [Link](dt);

DateTimeFormatter df = [Link]("dd-MM-yyyy--E a
");

String mydate = [Link](df);


[Link](mydate);

// DateTimeFormatter df2 = DateTimeFormatter.ISO_LOCAL_DATE;


// String date = [Link](df2);
// [Link](date)
}
}

import [Link].*;

class c1 {
public int x = 5;

protected int y = 10;

int z = 6;
private int a = 78;

public void meth1() {


[Link](x);
[Link](y);
[Link](z);
[Link](a);
}

public class JAVA_access_mod {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


String a = [Link]();

[Link](" you typed ---> " + a);

}
}
import [Link];
import [Link];
import [Link];
import [Link].*;

public class java_API {


public static void main(String[] args) {

LocalDate d = [Link]();
[Link](d);

LocalTime t = [Link]();
[Link](t);

LocalDateTime ldt = [Link]();


[Link](ldt);

}
}

interface bicycle{

int a = 45;

void applybrake( int decrement);


void speedup( int increment );

}
interface hornbycycle{
void applybrake2();
void speedup2();
}

class Avoncycle implements bicycle , hornbycycle {


void blowhorn( ){
[Link](" blow...................");}

public void applybrake( int decrement){


[Link](" Applying brake");
}
public void speedup( int increment ){
[Link](" accelerating");
}

public void applybrake2(){


[Link](" Applying brake for hornbycycle");
}
public void speedup2()
{
[Link](" Speeding up for horn bycycle");

}}

public class JAVA_CWH_INTERFACES {


public static void main(String[] args) {

Avoncycle av =new Avoncycle();


[Link](5);
[Link]();
[Link](av.a);// you can create properties in interfaces
// you cannot modify the properties in interfaces as they are
final..

av.applybrake2();
av.speedup2();

}
}

class classA {
classA(){

[Link](" I am the constructor of class A ");


}

class classB extends classA{

classB( int x){


[Link](" I am the constructor of class b with the
constructor value " + x);

}
}

class classC extends classB {

classC(int x, int y) {
super(x);

[Link](" I am the constructor of class c with the


constructor value " + x + " and " + y);
}
}

class classD extends classC{

classD(int x , int y , int z){


super(x,y);
[Link](" i am the constructor of class D with the
values " + x + " " + y + " " + " " + z) ;
}

public class JAVA_dynamic_method_dispatch {


public static void main(String[] args) {

classD d = new classD(2,3,4);

}
}

import [Link];
public class JAVA_exception {
public static void main(String[] args) {

int a = 6;
int b = 0 ;
try {
int c = a / b;
[Link](c);
}
catch (Exception e){
[Link](" we failed to divide . Reason : ");
[Link](e);
}

}
}
import [Link];

class myexception extends Exception{

@Override
public String toString() {
return " I am toString()";
}

@Override
public String getMessage() {
return " I am getmessage()";
}
}

public class JAVA_exception_class {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link](" Enter the number :");
int a = [Link]();

if (a < 9) {
try {

throw new myexception();


}
catch (Exception e) {
[Link]([Link]());
[Link](e);
}
}

else{
[Link](" You enterd number greater than 9");

}
}

}
import [Link];

class exp extends Exception{

@Override
public String toString() {
return " I am the toString exception....";
}

@Override
public String getMessage() {
return " You entered a number above 100 \n please enter a number
below 100 ";
}
}

public class JAVA_Exception_class_practice {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link](" Enter your number : ");
int num= [Link]();

int a =100;
if( num>=100) {
try {
throw new exp();
}
catch (Exception e ) {

[Link]([Link]());

}
}

[Link](" Sum of " + num +" + " +" 100 is --> " +
(num+100));
}

}
public class JAVA_FInally_block {

public static int greet(){


try{
int a = 50;
int b = 0;
return a/b;
}
catch(ArithmeticException e){
[Link](e);
}

finally {
[Link]("End of this program");
}
return -1;

}
public static void main(String[] args) {

int a = 50;
int b = 10;
while (true) {
try {
[Link](a / b);
}
catch (Exception e) {
[Link](e);
break;
}
finally {
[Link]("I am finally value of b = " + b);
}
b--;
}
}
}
import [Link];

public class JAVA_handling_exceptions {


public static void main(String[] args) {

int [] arr = { 63,56,45,50,100,96}; //0,1,2,3,4,5

Scanner sc = new Scanner([Link]);


[Link](" enter the array index : ");
int ind = [Link]();

[Link](" Enter the number : ");


int numb = [Link]();

try{
[Link](" The value at array index entered is : " +
arr[ind]);
[Link](" The value of array value divided by number
is : " + arr[ind]/numb);
}
catch(ArithmeticException e){
[Link]("Arithmetic exception occured !!!" );
}

catch( ArrayIndexOutOfBoundsException e){


[Link]("Array Index Out Of Bounds Exception occured
!!!" );
}

catch( Exception e ){
[Link](" Some unknown error occured , plzz check
your input ");
}

}
}
interface sampleinterface{
void meth1();
void meth2();
}

interface childsampleinterface extends sampleinterface{


void meth3();
void meth4();

}
class mysamplespace implements childsampleinterface{
public void meth1(){
[Link]();
}
public void meth2(){
[Link]();
}

public void meth3(){


[Link]();
}
public void meth4(){
[Link]();
}

public class JAVA_Inheritance {


public static void main(String[] args) {

mysamplespace my =new mysamplespace();

my.meth1();
my.meth2();
my.meth3();
my.meth4();

}
}
interface cellphone{

void ring();
void keypad();

interface GPS{
void location();
void traffic();
}

interface camera {

void photo();
void video();

private void greet(){


[Link](" hello good morning ");
}
default void record4kvid(){

[Link](" 4k video is recording ");


greet();

}
interface mediaplayer{

void playmusic();
void playlist();

class smartphone implements cellphone,GPS,camera,mediaplayer{

public void ring(){


[Link]("Phone is ringing ");
}
public void keypad(){
[Link]("Dial the number ");
}

public void location(){


[Link](" LIve location ");
}
public void traffic(){
[Link](" Finding best routes ");
}

public void photo() {


[Link](" Taking photo ,...");
}
@Override
public void video() {
[Link](" playimg video ");
}

@Override
public void playmusic() {
[Link]( "playing music");
}

@Override
public void playlist() {
[Link](" fing the best playlist ");
}

public class JAVA_INTERFACES_EXAMPLE {


public static void main(String[] args) {

camera mp = new smartphone();


[Link]();

}
}

interface cellphone2{

void ring();
void keypad();

interface GPS2{
void location();
void traffic();
}

interface camera2 {

void photo();
void video();

private void greet(){


[Link](" hello good morning ");
}
default void record4kvid2(){

[Link](" 4k video is recording ");


greet();

}
interface mediaplayer2{

void playmusic();
void playlist();

class smartphone2 implements cellphone,GPS,camera,mediaplayer{

public void ring(){


[Link]("Phone is ringing ");
}
public void keypad(){
[Link]("Dial the number ");
}

public void location(){


[Link](" LIve location ");
}
public void traffic(){
[Link](" Finding best routes ");
}

public void photo() {


[Link](" Taking photo ,...");
}

@Override
public void video() {
[Link](" playimg video ");
}

@Override
public void playmusic() {
[Link]( "playing music");
}

@Override
public void playlist() {
[Link](" fing the best playlist ");
}

public class JAVA_INTERFACES_EXAMPLE2 {


public static void main(String[] args) {

}
class mythread extends Thread{
public void run() {

for( int i =1 ; i<=100000;i++){


[Link](" Aman " + "\n");
}

}
}

class mythread2 extends Thread{


public void run() {
for( int i =1 ; i<=100000;i++){
[Link](" Sakshi " +"");
}

}
}

public class JAVA_Multithreading {


public static void main(String[] args) {

mythread my = new mythread();


mythread2 my2 = new mythread2();
[Link]();
[Link]();

}
}
import [Link];

public class JAVA_Nested_try_catch {


public static void main(String[] args) {

int [] arr = { 63,56,45,50,100,96};

Scanner sc = new Scanner([Link]);

[Link](" Enter the array index position --> " );


int num = [Link]();

try {
[Link](" Welcome to the video");

try {
[Link]( "Value at the specified index position
is --> " + arr[num]);

} catch (ArrayIndexOutOfBoundsException e) {
[Link](" Sorry this index value does not
index , check your input ");
[Link](" exception in level 2");
}
}

catch (Exception e ){
[Link](" exception is level 1" );
}

}
}
class class1 {

void methd1(){
[Link](" I am method 1 of class 1 ");

}
class class2 extends class1{

void methd1(){
[Link](" i am method 2 of class 2 ");

public class JAVA_overriding {


public static void main(String[] args) {
class1 c1= new class1();
c1.methd1();

}
class Threadc extends Thread {

public void run() {


for( int i=1 ; i<=100 ; i++){
[Link](" Good morning ++++++++");
}
}
}

class Threadd extends Thread{


public void run(){
for( int i=1 ; i<=100 ; i++){
[Link](" Welcome.........." + "\n");
}

}
}

public class JAVA_PQ {


public static void main(String[] args) {

Threadc t1 = new Threadc();


Threadd t2 = new Threadd();

// [Link](10);
// [Link](20);

[Link](6);
[Link](9);

[Link]([Link]());
[Link]([Link]());

// [Link]();
// [Link]();

}
}
import [Link];

public class JAVA_practice_method_Exception {


static void aman() {

for (int i = 1; i < 6; i++) {


try {
int[] arr = {45, 85, 96, 37, 91, 45};
Scanner sc = new Scanner([Link]);
[Link](" Enter array index : ");
int num = [Link]();
[Link](" Value at array index " + num + " is "
+ arr[num]);
break;

} catch (Exception e) {

[Link](" Please enter a valid array index");

public static void main(String[] args) {

aman();

}
}
mport [Link];
class arrexp extends Exception{
@Override
public String toString() {
return "Error";
}
}

public class JAVA_Practice_set {


public static void main(String[] args) {

int[] arr = {2, 56, 36, 8, 94, 554,};

for (int i = 1; i <= 6; i++) {


try {
Scanner sc = new Scanner([Link]);
[Link](" Enter the array's index : ");
int num = [Link]();

[Link]("Array value at index " + num + " is : "


+ arr[num]);
break;
} catch (Exception e) {
[Link](" Please enter a valid array index ");
}

if (i > 5) {
try {
throw new arrexp();
} catch (Exception e) {
[Link]([Link]());
}
}

}
}
}
class thread1 extends Thread {
public thread1 (String name) {
super(name);
}

public void run() {

for (int i = 1; i <= 100; i++) {


[Link](" Hello.... I am thread 1 " + getName());
}
}
}

class thread2 extends Thread {

public void run() {

for (int j = 1; j <= 10; j++) {

[Link](" I am 2nd thread ");


}
}
}

public class JAVA_THREAD_METHODS {


public static void main(String[] args) {

thread1 t1 = new thread1("Aman");


thread2 t2 = new thread2();

[Link]();

try{
[Link]();

}
catch( Exception e ){
[Link](e);
}

[Link]();

}
}
class my_threads_runable1 implements Runnable {
public void run() {
for (int i = 1; i <= 100; i++) {
[Link](" I am a thread 1 not a threat 1");
}
}
}

class my_runnable_2 implements Runnable {

public void run() {


for (int i = 1; i <= 100; i++) {
[Link](" I am a thread 2 not a threat 2" + "\
n");
}
}
}

public class JAVA_Threads2 {


public static void main(String[] args) {

my_threads_runable1 my1 = new my_threads_runable1();

Thread t1 = new Thread(my1);

my_runnable_2 my2 = new my_runnable_2();

Thread t2 = new Thread(my2);

[Link]();
[Link]();

}
}
import [Link];

class exp1 extends Exception{


@Override
public String toString() {
return " Exception 1 generated ............. Invalid Entry..";
}
}

public class JAVA_throw_practice {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link](" Enter the number(positive number only) : ");
int a = [Link]();
if(a<0){
try{
throw new exp1();
}
catch( Exception e){
[Link]([Link]());
}
}
else {
[Link](" your output is --> " + (a - 10));
}

}
}
class negexp extends Exception{
@Override
public String toString() {
return " Radius cannot be negative ";
}

@Override
public String getMessage() {
return " Do not enter negative radius";
}
}

public class JAVA_THROW_throws {

static double area( int r) throws negexp{


if (r < 0)
{
throw new negexp();

return 3.14*r*r;
}

public static void main(String[] args) {

try{
[Link](area(-5));
}
catch(Exception e){
[Link](e);
}

}
}
class myexp extends Exception{
@Override
public String toString() {
return "Radius cannot be negative";
}

@Override
public String getMessage() {
return " Radius cannot be negative ";
}
}

class practice{

double area( int r ) throws myexp{

if(r<0){
throw new myexp();

}
return 3.14*r*r ;
}
}

public class JAVA_THROWS_PRACTICE {


public static void main(String[] args) {

practice p = new practice();


try {
[Link]([Link](-6));
}
catch(Exception e ){
[Link]([Link]());
}
}
}
import [Link];

public class JAVA_while_loop_Important {


public static void main(String[] args) {

int[] arr = { 23,32,45,56,85,23,85};

Scanner sc = new Scanner([Link]);

while(true){
[Link](" Enter the array index : ");
int num = [Link]();

try{

[Link](" Value at the array index entered is


--> " + arr[num]);
break;
}

catch (ArrayIndexOutOfBoundsException e) {

[Link](" Index entered is out of bound ");


}
}
[Link](" Thanks for using this program.......");

}
import [Link].*;

class exception extends Exception{


@Override
public String toString() {
return "This book is not availaible ";
}

class mylibrary extends exception {

public void BookNames() {

ArrayList<String> bk = new ArrayList<>();


[Link]("Availaible books are : ");
[Link]("Psychology");
[Link]("Moral Values");
[Link]("The Legend of Ramayana");
[Link]("Bhagvad Gita");
[Link]("The Holy Bible");
[Link]("Quran");
[Link]("The Medevial History");
for (int i = 0; i <= [Link]() - 1; i++) {
[Link]([Link](i) + " , ");

}
[Link]("\n");
}

public String availaibleBooks() throws exception {


Scanner sc = new Scanner([Link]);
[Link]("Type the bookname to check the avalaibility : ");
String name = [Link]();
ArrayList<String> bk = new ArrayList<>();
[Link]("Psychology");
[Link]("Moral Values");
[Link]("The Legend of Ramayana");
[Link]("Bhagvad Gita");
[Link]("The Holy Bible");
[Link]("Quran");
[Link]("The Medevial History");
if ([Link](name)) {
return "The Book is avalaible ";
} else {
throw new exception();
}
}

public void issuebook()throws exception{


ArrayList<String> bk = new ArrayList<>();
[Link]("Psychology");
[Link]("Moral Values");
[Link]("The Legend of Ramayana");
[Link]("Bhagvad Gita");
[Link]("The Holy Bible");
[Link]("Quran");
[Link]("The Medevial History");

Scanner sc = new Scanner([Link]);


[Link]("Enter your name :");
String name = [Link]();

[Link]("Type the name of the book you want to issue : ");


String booknm = [Link]();
if([Link](booknm)){
[Link]("Processsing.....");
}
else{
throw new exception();
}

[Link](booknm);

[Link](booknm + " is issued " + "to " + name);

[Link]("Issued date and time : ");

Calendar c = [Link]();
[Link]([Link]());

public void addbooks() {


ArrayList<String> bk = new ArrayList<>();
[Link]("Psychology");
[Link]("Moral Values");
[Link]("The Legend of Ramayana");
[Link]("Bhagvad Gita");
[Link]("The Holy Bible");
[Link]("Quran");
[Link]("The Medevial History");
[Link]("Type the bookname you want to add : ");
Scanner sc = new Scanner([Link]);
String addbook = [Link]();
[Link](addbook);

[Link]("Now availaible books are : ");

for (int i = 0; i <= [Link]() - 1; i++) {


[Link]([Link](i) + " , ");

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

for (int i = 1; i <= 1000000000; i++) {


[Link]("Enter 1 to show availaible books ");
[Link]("Enter 2 to check availaible books ");
[Link]("Enter 3 to issue book ");
[Link]("Enter 4 to add books ");
[Link]("Enter 5 to log out");
Scanner sc = new Scanner([Link]);
int command = [Link]();
mylibrary mylb = new mylibrary();
if (command == 1) {
[Link]();
} else if (command == 2) {
try {
[Link]([Link]());
} catch (exception e) {
[Link](e);
}
} else if (command == 3) {

try {
[Link]();
} catch (Exception e) {
[Link](e);
}
} else if (command == 4) {

[Link]();

} else if (command ==5) {


break;

}
}
}
import [Link];
import [Link].*;

public class linked_list {


public static void main(String[] args) {

LinkedList<Integer> l1 = new LinkedList<>();


LinkedList<Integer>l2= new LinkedList<>();

//linked list is the extension of array list.

[Link](6);
[Link](7);
[Link](5);
[Link](3);
[Link](1);

[Link](685);
[Link](30);
[Link](15);
[Link](25);
[Link](685);

// [Link]();
// [Link]([Link](10));
//[Link]([Link](25));

for( int i = 0; i<[Link](); i++){


[Link]([Link](i) + " , ");
}

}
}
class mythr extends Thread {
public mythr(String name) {
super(name);
}

public void run() {

for (int i = 1; i <= 10; i++) {


[Link](" Hello.... I am thread 1 " + getName());
}
}
}

class mythr2 extends Thread{

public void run(){


[Link](" I am 2nd thread ");
}
}

public class prorities {


public static void main(String[] args) {

mythr my1 = new mythr("Aman ( most important)");


mythr my2 = new mythr("Aditya");
mythr my3 = new mythr("Arun");
mythr my4 = new mythr("Sakshi");
mythr my5 = new mythr("Atul");
mythr my6 = new mythr("Tanvi");
mythr my7 = new mythr("Rishi");

[Link](Thread.MAX_PRIORITY);
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MIN_PRIORITY);

[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();

}
}
class rectangle{

int length;
int breadth;
int height;

public void setLength( int l){


length=l;

public void getLength(){


[Link](length);
}

public void setBreadth( int b){


breadth=b;

public void getbreadth(){


[Link](breadth);
}

public void setHeight( int h){


height=h;

public void getheight(){


[Link]();
[Link](height);
}

public class rectangle1 {


public static void main(String[] args) {

rectangle rc = new rectangle();

[Link](5);

[Link](10);
[Link](15);

[Link]();
[Link]();
[Link]();
}
}
deleting and creating a file from java code

package [Link];

import [Link];
import [Link];

public class java_files {


public static void main(String[] args) {

//code to create a new file

File myfile = new File("Aman [Link]");

try {

[Link]();
}
catch (IOException e){
[Link]("Unable to create this file");
}
}

package [Link];

import [Link];
import [Link];
import [Link];

public class reading_to_a_file {


public static void main(String[] args) {

File myfile = new File("Aman [Link]");


try {
Scanner sc = new Scanner(myfile);
while ([Link]()) {
[Link]([Link]());

}
[Link]();
}
catch(FileNotFoundException e){
[Link](e);
}
}

}
package [Link];

import [Link];

/**
* This documentation would tell us about deleting a file from java
* @author Aman Patre
* @version 20003.1
* @since 2005

*/
class library{

public void aman(){


[Link]("Aman ");
}

public class deleting_a_file {


public static void main(String[] args) {

File myfile = new File("[Link]");


if([Link]()){
[Link]("I have deleted " + [Link]());
}
else{
[Link]("Some error occured while deleting the file
");
}
}
}
package [Link];

import [Link];
import [Link];

public class writing_to_a_file {


public static void main(String[] args) {

try {
FileWriter fileWriter = new FileWriter("Aman [Link]");
[Link]("hello ....... ");
[Link]();
} catch (IOException e) {
throw new RuntimeException(e);
}

}
}

Practice questions
package questions;

import [Link];

public class count_number_in_a_number {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter your number : ");
long num = [Link]();

int count=0;

while(num>0)
{
num=num/10;
count++;
}
[Link]("Number of digits in the given number is :" +
count);

}
}

package questions;

import [Link];

public class CountCharacterOccurence {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter your String ");
String s = [Link]();

[Link]("Enter the character which you want to count ");


String a = [Link]();

String str=[Link](a,"");

[Link]("Number of " + a +"'s in the string is : " +


([Link]()-[Link]()));

}
}
package questions;

import [Link];

public class CountingWordsInTheString {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the string");
String s = [Link](); // Aman Patre
int count =1;

for(int i = 0;i<[Link]();i++){

if(([Link](i)==' ')&& ([Link](i+1)!=' ')){


count++;
}
}

[Link]("Number of words in the string : " + count);

}
}

package questions;

import [Link];

public class CreatingNewFileWithJAva {


public static void main(String[] args) {

File myfile = new File("[Link]");

try{
[Link]();
}
catch(Exception e){
[Link](e);
}
}
}
package questions;

import [Link];

import [Link];
import [Link];

public class DuplicateElemntsOfArray {


public static void main(String[] args) {

int num=0;
boolean flag = false;
String[] arr={"Aman" , "Aditya" , "Yash", "Mayank" , " Ravi","Aman"
};

for(int i = 0 ; i<[Link];i++){

for(int j=i+1;j<[Link];j++){

if(arr[i]==arr[j]){

[Link]("Duplicate element is : " + arr[j] + "\


n");
flag=true;
}

if(flag==false){
[Link]("Duplicate element not found ");
}
else{
[Link]("Duplicate elent found ....");
}

}
package questions;

import [Link];

public class equalityOfArray {


public static void main(String[] args) {

int a1[] = {1, 2, 3, 4, 5, 6,};


int a2[] = {1, 2, 3, 4, 5, 6};

//method1

//
// boolean status =[Link](a1,a2);
// if (status==true){
// [Link]("Arrays are equal ");
// }
// else {
// [Link]("Arrays are not equal");
// }

boolean status = true;

if ([Link] == [Link]) {

for (int i = 0; i < [Link]; i++) {

if (a1[i] != a2[i]) {

status = false;
}
else {
status=true;
}
}

else{
status = false;
}

if(status==true){
[Link]("Arrays are equal");
}
else{
[Link]("Arrays are not equal");
}

}
}
package questions;

public class EvenODDNumbersFromarray {


public static void main(String[] args) {

int[] arr = {2, 3, 5, 6, 3, 8, 9, 10};


[Link]("Even numbers are : ");
for (int i = 0; i < [Link]; i++) {

if (arr[i] % 2 == 0) {
[Link]( arr[i]);
}

}
[Link]("Odd numbers are : ");
for (int i = 0; i < [Link]; i++) {

if (arr[i] % 2 != 0) {
[Link]( arr[i]);
}
}
}
}

package questions;

import [Link];

public class factorial {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the number : ");
int num = [Link]();

long fact=1;

for(int i=num; i>=1; i--){

fact=fact*i;
}
[Link](fact);
}
}
package questions;

public class fibonacci_series {


public static void main(String[] args) {

int n1 = 0;
int n2=1;

int sum=0;

for(int i=1 ; i<=10;i++){


sum=n1+n2;

[Link]( " " +sum);

n1=n2;
n2=sum;

}
package questions;

public class largest_of_the_three_numbers {


public static void main(String[] args) {

int a = 10;
int b = 200;
int c = 3;

/*
if(a>b && a>c ){
[Link]("a is largest ");

} else if (b>c && b>a ) {

[Link]("b is the largest ");


}

else if (c>a&&c>b ) {

[Link]("c is the largest ");


}

*/

//using ternary operator

int largest = a>b?a:b;

int greatest = c>largest?c:largest;

[Link]("The bigger number is : "+greatest);

}}
package questions;

public class MaxAndMinValuesOfArray {

public static void main(String[] args) {

int[] a = {10, 200, 30, 4, 50, 600};

int max = a[0];

for (int i = 1; i < [Link]; i++) {


if(a[i]>max){

max=a[i];

}
[Link]("The max value of array is : "+max + "\n");

int min =a[0];


for(int i = 1 ; i<[Link];i++){

if(a[i]<min){
min=a[i];
}

[Link]("The min value of the array is : "+min);

}
}
package questions;

public class missingNumberOfanArray {


public static void main(String[] args) {

int sum = 0;
int add = 0;
int a1 []= {1,2,3,4,5,6,7,9,10};

for(int i = 0 ; i< [Link]; i++){


sum=sum+a1[i];
}

for(int j = 1 ; j<= [Link]+1;j++){

add = add+j;
}
int num = add-sum;

[Link]("The missing number is : "+num);


}
}

package questions;

import [Link];

public class number_of_characters_string {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("enter your string : " );
String str = [Link]();

[Link]( "Number of characters in the given string is


"+[Link]());

}
}
package questions;

import [Link];

public class number_of_even_odd_digits {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the number : ");
int num = [Link]();
int even_count=0;
int odd_count=0;
while(num>0){

if(num%2==0){
even_count++;
}

else{
odd_count++;
}

num = num/10;
}

[Link]("Even --> " + even_count);


[Link]("Odd --> " + odd_count);

}
}
import [Link];

public class palindrome_number {


public static void main(String[] args) {

for (int i = 0; i <= 1000000; i++) {


Scanner sc = new Scanner([Link]);

[Link]("Enter your number : ");


int num = [Link]();
int org_num = num;

int rev = 0;
while (num != 0) {

rev = rev * 10 + num % 10;


num = num / 10;
}
[Link]("Reversed numbere is : " + rev + "\n");

if (rev == org_num) {
[Link]("Enterd number is a palindrome number " +
"\n");
} else {
[Link]("Number is not a palindrome number " +
"\n");
}

}
}

package questions;

import [Link];
import [Link];

public class palindrome_String {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter your String : ");

String str = [Link]();


String org_str = str;

[Link]("Reverse of the entered string is : ");


String str2 = null;
for (int i = [Link]() - 1; i >= 0; i--) {
str2 = [Link]([Link](i));
[Link](str2);
}

[Link]("\n");

if (org_str.equals(str2)) {
[Link]("Entered string is a palindrome string " + "
\n");
}
else {
[Link]("not a palindrome string " + "\n");
}
}
}

package questions;

import [Link];

public class prime_number_verification {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your number : ");
int num = [Link]();

int count=0;

if(num>=1){

for(int i =1;i<=num;i++){

if(num%i==0){
count++;
}
}

if(count==2){
[Link](num + " is a prime number ");
}
else {

[Link](num + " is not a prime number ");


}

}
else{

[Link](" not a natural number ");


}

}
}
package questions;
import [Link];
public class random_number {
public static void main(String[] args) {
//Apraoch1

// Random rc = new Random();


// float num = [Link](1,10);
//
// [Link](num);

//Aproach2

double num = [Link]();


[Link](num);

}
}

package questions;

import [Link];
import [Link];

public class reading {


public static void main(String[] args) {
File myfile= new File("[Link]");
try{
Scanner sc = new Scanner(myfile);
while([Link]()) {
[Link]([Link]());
}
[Link]();
}
catch(Exception e){
[Link]("File not found ....");
}
}
}

package questions;

public class RemoveJunk {


public static void main(String[] args) {
String s = "AMan patre ajay patre";

s=([Link](" \\s", ""));


[Link](s);

}
}

package questions;

import [Link];

public class ReverseWordInAString {


public static void main(String[] args) {

//Approach - 1
// String s = "Welcome to java ";
//
//
// String [] words = [Link](" ");
//
//
//
// String reverseString = " ";
//
// for(String w:words){
// ;
// String reverseword=" ";
// // [Link](w);
//
// for(int j = [Link]()-1; j>=0;j--){
// reverseword=reverseword+[Link](j);
//
// }
//
// reverseString = reverseString+reverseword;
// }
//
//
// [Link](reverseString);

//Approach2

String s = " Aman Patre";


String[] words = [Link](" ");

String reverseword = " ";

for (String w : words) {

StringBuilder sb = new StringBuilder(w);


[Link]();
reverseword = reverseword + [Link]() + " ";
}

[Link](reverseword);
}
}
package questions;

import [Link];

public class reversing_a_number {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the number : ");
int num = [Link]();

// using algorithm

// int rev = 0;
// while (num != 0) {
//
// rev = rev * 10 + num % 10;
// num = num / 10;
// }
//
// [Link]("Reverse number is " +rev);

//logic2

// StringBuffer sb = new StringBuffer([Link](num));


//
//
// [Link]([Link]());

//logic3
// StringBuilder sbl = new StringBuilder();
// [Link](num);
// [Link]([Link]());

}
package questions;

public class reversing_a_String {


public static void main(String[] args) {

//logic1

String str = "AMAN";


[Link]("Original String is : " + str + "\n");
//
// [Link]("Reversed string is : ");
// for(int i = [Link]()-1 ; i>=0;i--){
// [Link]([Link](i));
// }

//logic2

// char a[] = [Link]();


//
// [Link]("Reversed string is : " );
// for(int i = [Link]-1;i>=0;i--){
// [Link](a[i]);
// }

//logic3

StringBuffer sbr = new StringBuffer(str);


[Link]("Reversed string is : " );
[Link]([Link]());

}
}
package questions;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class revesionPractice {


public static void main(String[] args) {

File myfile = new File("[Link]");


try{
Scanner sc = new Scanner(myfile);

while([Link]()){
[Link]([Link]());
}
[Link]();
}
catch (Exception e ){
[Link]("unable to read the data");
}
}
}

package questions;

import [Link];

public class SearchElemntInAray {


public static void main(String[] args) {

int [] arr = {1,2,3,4,5,6,7,8,9};

Scanner sc = new Scanner([Link]);


[Link]("Enter the number which you want to search in the
array : ");

int num = [Link]();

boolean check =false;


for(int i = 0 ; i<[Link];i++){

if(num==arr[i]) {
[Link](arr[i] + " is present in the array ");
check=true;

if(check==false){
[Link](num + " is not present in the array ...");
}
}

package questions;

import [Link];

public class SortingElementsInArray {


public static void main(String[] args) {

int [] arr = {100,300,200};

int n = [Link];

for(int i =0; i<n-1;i++){

for(int j =0; j<n-1;j++){

if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}

}
[Link]([Link](arr));

}
package questions;

import [Link];
import [Link];
import [Link];
import [Link];

public class sortingElementsUsingBuiltInMethods {


public static void main(String[] args) {

//aproach1 -- parallel sort


Integer [] arr = { 10,9,36,4,85,7,2,18};
//
[Link]("Array elements before sorting : " +
[Link](arr));
//
//
// [Link](arr);
// [Link]("Array elements after sorting : " +
[Link](arr));

//aproach 2 -- [Link]

//
// [Link](arr);
//
// [Link]("Array elements after sorting : " +
[Link](arr));

// for descending order sorting

[Link](arr,[Link]());
[Link]([Link](arr));

}
}
package questions;

public class sum_of_all_numbers_in_a_number {


public static void main(String[] args) {

int num = 911;

int sum=0;

while(num>0){

sum=sum+num%10;

num=num/10;
}

[Link](sum);

}
}

package questions;

public class sumOfElementsOfArray {

public static void main(String[] args) {

int sum = 0;
int [] arr = {1,2,3,4,5,6};

for(int i = 0 ; i<[Link];i++){

sum=sum+arr[i];
}

[Link](sum);

}
}
package questions;

public class swaping_two_numbers {


public static void main(String[] args) {

// logic 1
int a = 10;
int b = 20;
[Link]("The values before swapping a = " + a + " b = " +
b );
//
//
// int t = a ;
// a=b;
// b=t;
//
// [Link]("The values after swapping a = " + a + " b = "
+ b );

//logic 2

// a=a+b;
// b=a-b;
// a=a-b;
//
//
// [Link]("Values after swaping a =" + a + " b = " + b
);

//logic3

// a=b/a;
// b=b/a;
// a=a*b;
//
//
// [Link]("Values after swaping a =" + a + " b = " + b
);

//logic4

//by ^ character ( binary operation)..

//
// a=a^b;
// b=a^b;
// a=a^b;
// [Link]("Values after swaping a =" + a + " b = " + b
);

//logic5 single statement

b =a+b-(a=b);

[Link]("Values after swaping a =" + a + " b = " +


b );

}
}

package questions;

import [Link];

public class WritingIntoFile {


public static void main(String[] args) {

try {
FileWriter fileWriter = new FileWriter("[Link]");
[Link]("The VIT legacy, which started from Vellore Engineering
College 35 years ago to " +"\n"+
"four Universities in India, today, is a result of constant efforts
to impart high " + "\n"+
"quality of education. The Govt. of India has recognized VIT as No.
1 Private Institution for " +"\n"+
"Innovation, ARIIA 2019, and the recent recognition, as an
Institution of Eminence, has paved a" +"\n"+
" way to focus more on research with international collaborations
to move up in the global ranking and " +"\n"+
"contribute in the capacity building of our nation.");
[Link]();
}
catch (Exception e){
[Link]("Unable to write unto a file ....");
}
}
}

You might also like