Category: Java program

Define a class to declare an integer array of size n and accept the elements into the array.

import java.util.Scanner;class LinearSearch{public static void main(String args[]){int c, n, search, array[];Scanner in = new Scanner(System.in);System.out.println(“Enter number of elements”);n = in.nextlnt();array= new int[n];System.out.println(“Enter those” +n+ “elements”);for (c = 0; c < n; c++)array[c] = in.nextlnt();System.out.println(“Enter value to find”);search = in.nextlnt();for (c = 0; c < n; c++){if (array[c] = search) /Searching element is present/{System.out.println(array[c]);break;}}if (c = […]

Java program File handling create operation

Program for create operation in file handling: package com.journaldev.files; import java.io.File; import java.io.IOException; public class FileHandling {       public static void main(String[] args) throws IOException {                File file = new File(“profjayesh.txt”);                          boolean createNewFile = file.createNewFile();                          System.out.println(“File Created = “+createNewFile);                }       } OUTPUT: File Created = true

Java program Applet image display

APPLET IMAGE DISPLAY: import java.awt.*; import java.applet.*;     public class AppletDisplayImage extends Applet {  Image picture;  public void init() { picture = getImage (getDocumentBase(), http://nebula.wsimg.com/a418036bd85efa32bdb96f5dbb77e203?AccessKeyId=F7C1E0F1DEF38C4D96C2&disposition=0&alloworigin=1 ); setBackground(Color.yellow);  }  public void paint(Graphics g) { g.drawImage(picture, 40,40, this);  }    } OUTPUT:

Java program Applet digital clock

APPLET DIGITAL CLOCK: import java.applet.*;  import java.awt.*;  import java.util.*;  import java.text.*;    public class AppletDigitalClock extends Applet implements Runnable {       Thread th = null;     int hours=0, minutes=0, seconds=0,  miliSeconds=0;    java.lang.String timeString = “”;       public void init() {        setBackground( Color.white);     }       public void start() {          […]

Java program Applet mouse event handling

APPLET MOUSE EVENT: import java.awt.*; import java.awt.event.*; import java.applet.*; public class MouseApplet extends Applet implements MouseMotionListener{         public void init(){                     addMouseMotionListener(this);                     setBackground(Color.WHITE);                     }                                         public void mouseDragged(MouseEvent me){                     Graphics g=getGraphics();                     g.setColor(Color.black);                     g.drawOval(me.getX(),me.getY(), 10, 10);                     }                     public void mouseMoved(MouseEvent me){}                     } OUTPUT:

Javaprogram Graphic Applets

APPLET GRAPHICS: import java.applet.Applet; import java.awt.*; public class Graphic extends Applet{ public void paint(Graphics g){ g.setColor(Color.yellow); g.fillRect(0,0,200,30); g.setColor(Color.black); g.drawString(“Namashkar”,50, 50); g.setColor(Color.red); g.drawLine(200,30,200,300); g.setColor(Color.green); g.drawOval(0,100,200,30); g.setColor(Color.gray); g.drawArc(60,150,30,30,30,360); g.setColor(Color.cyan); g.fillArc(0,150,30,30,0,360); g.setColor(Color.magenta); g.fillArc(0,150,30,30,0,180); g.setColor(Color.ORANGE); g.fillArc(0,150,30,30,0,90); g.setColor(Color.LIGHT_GRAY); g.fillArc(30,150,30,30,0,360); g.fill3DRect(0, 200, 100, 100, true); } } OUTPUT: TO EXECUTE APPLET IN WEB BROWSER: Type this code in text editor, and […]

Java program main function with object

/**              * @Prof. Jayesh              * Here two classes are created each having main function              * A class calls the main() of another class              */ class FunctionOne{             void show()             {                         System.out.println(“I am in FunctionsOne”);                                      }                         public static void main(String args[])             {             FunctionsInJava fij = […]

Java Nested Method

PROGRAM: To show nested methods. /**              * @Prof. Jayesh              * Here we call a method from main().              * Methods are in same class so known as nested method.              */ import java.util.Scanner; public class ExampleOfNestedMethod {             static int sum()             {                         int a,b,c;                         System.out.println(“Enter the value of a”);                         Scanner scn=new […]

Java program use of continue

PROGRAM: To show use of Continue statement. /**              * @Prof. Jayesh              * continue is a control statement.              * continue brings compiler to the top of execution structure.              */ public class ExampleOfContinue {             public static void main(String args[])             {             int num[]={1,2,3,4,5,6,7,8,9,10};             System.out.println(“Show all numbers except 5”);             int […]

Java Do While program

PROGRAM: To show use of Do While. public class DoWhile {             public static void main(String args[])             {                         int i=1;                         do                         {                                     System.out.println(i);                                     i++;                         }                         while(i<=10);             } } OUTPUT: 1 2 3 4 5 6 7 8 9 10

Java Decrement operator

PROGRAM: To show use of decrement operator. /**              * @Prof. Jayesh              * Decrement operator uses ‘ – ‘.              */ public class DecrementOperator {             public static void main(String args[])             {                         int a=10,b=20;                         a–;                         –b;                         System.out.println(“a = “+a);                         System.out.println(“b = “+b);                         System.out.println(“a+b = “+(a+b));                         System.out.println(“a– […]

Java constant

PROGRAM: To show constant. /**              * @Prof. Jayesh              * Here age is constant, which have fix value 28.              */ public class ConstantClass {             public static void main(String args[]){                         int age = 28;                         System.out.println(“age= “+Age);             } } OUTPUT: age= 28

Java Inheritance

PROGRAM: A program to show simple inheritance. /**              * @Prof. Jayesh              * Here two classes are created each having main function              * A class calls the main() of another class              */ public class Papa {             void show()             {                         System.out.println(“I am in class Papa”);             } } public class […]

Java array program

PROGRAM: Program to show use of array. /**              * @Prof. Jayesh              * Here an array variable a[], is printing its elements using for loop.              */ public class ArrayProgram {             public static void main(String args[])             {                         int a[]={1,2,3,4,5};                         int i;                         System.out.println(“Array members are: “);                         for(i=0;i

Java program arithmetic operators

PROGRAM: To show use of Arithmetic operators. /**              * @Prof. Jayesh              * Here use of arithmetic operators are shown.              * Arithmetic operators are +, -, /, % etc.              */ public class ArithmeticOperators {             public static void main(String args[])             {                         int a=10,b=20;                         System.out.println(“a=”+a);                         System.out.println(“b=”+b);                         System.out.println(“a+b=”+(a+b));                         System.out.println(“a-b=”+(a-b)); […]

Java Scanner progam

PROGRAM: Simple program to show use of scanner class. import java.util.Scanner; public class AddScanner { public static void main(String args[]) { int x,y,z; System.out.println(“Enter 2 integers to calculate sum”); Scanner in = new Scanner(System.in); x = in.nextInt(); y = in.nextInt(); z=x+y; System.out.println(“Sum of integers =” +z); } } OUTPUT: Enter 2 integers to calculate sum […]

Java program type casting

PROGRAM: import java.util.Scanner; public class IfElse { public static void main(String args[]) { int marksObtained, passingMarks; passingMarks =40; Scanner input = new Scanner(System.in); System.out.println(“Input marks scored by you”); marksObtained = input.nextInt(); if(marksObtained >= passingMarks) {             System.out.println(“You passed the exam”); } else {             System.out.println(“Unfortunately you failed to pass the exam”); } } } OUTPUT: […]

Java program While loop, input.nextInt()

PROGRAM: import java.util.Scanner; public class WhileLoop {             public static void main(String args[])             {             int n;                         Scanner input = new Scanner(System.in);             System.out.println(“Input an integer”);                         while((n = input.nextInt())!=0)             {                         System.out.println(“You entered” +n);                         System.out.println(“Input an Integer”);             }                         System.out.println(“Out of loop”);             } } OUTPUT: Input […]

Java program 10

PROGRAM: public class ForLoop { public static void main(String args[]) { int c; for(c=1; c<=10; c++) {             System.out.println(c); } } } OUTPUT: 1 2 3 4 5 6 7 8 9 10

Java program use of If Else

PROGRAM: import java.util.Scanner; public class IfElse { public static void main(String args[]) { int marksObtained, passingMarks; passingMarks =40; Scanner input = new Scanner(System.in); System.out.println(“Input marks scored by you”); marksObtained = input.nextInt(); if(marksObtained >= passingMarks) {             System.out.println(“You passed the exam”); } else {             System.out.println(“Unfortunately you failed to pass the exam”); } } } OUTPUT: […]

Java program use of Scanner class, nextInt()

PROGRAM: import java.util.Scanner; public class AddNumbers { public static void main(String args[]) { int x,y,z; System.out.println(“Enter 2 integers to calculate sum”); Scanner in = new Scanner(System.in); x = in.nextInt(); y = in.nextInt(); z=x+y; System.out.println(“Sum of integers =” +z); } } OUTPUT: Enter 2 integers to calculate sum 45 52 Sum of integers =97

Java program use of Switch case, break statement

PROGRAM: public class SwitchCase {             public static void main(String args[]) { for(int i=0; i<=3; i++) {             switch(i)             {             case 0:             System.out.println(“i is 0”);             break;             case 1:             System.out.println(“i is 1”);             break;             case 2:                         System.out.println(“i is 2”);                         break;             default:             System.out.println(“i is greater than […]

Java Program to use Nested Switch case

PROGRAM: import java.util.Scanner; public class SwitchCaseWithScanner { public static void main(String args[]) {             int i,j;                         System.out.println(“Enter any number”);             Scanner s1 = new Scanner(System.in);             i = s1.nextInt();                         switch(i)             {             case 1:             System.out.println(“January”);             System.out.println(“Enter value of j”);             j = s1.nextInt();             System.out.println(“j is = “+j); […]

Java program to enter marks, calculate sum, percentage, division etc.

import java.util.Scanner; public class JavaProgram {             public static void main(String args[])             {                         int noOfStudents, i,j,marks, sum=0, percent;                         System.out.println(“Please Enter number of students”);                         Scanner s1 = new Scanner(System.in);                         noOfStudents = s1.nextInt();                         System.out.println(“Entered students are = “+noOfStudents);                                                 for(i=1; i<=noOfStudents; i++)                         {                                     System.out.println(“Enter” +i+ “students marks”);                                     […]

Java program to enter marks

PROGRAM: To make entry of 5 subjects, restrict to enter more than 100 marks for a subject, use of  for, if, Scanner etc. import java.util.Scanner; public class CalculateStudentsResult {              void mains()             {                         int noOfStudents,i,j, sum=0,k, percent;                         System.out.println(“Enter number of students”);                         Scanner s1 = new Scanner(System.in);                         noOfStudents = s1.nextInt();                         […]

Java program to show use of If, For, Scanner

PROGRAM: To make entry of 5 subjects, restrict to enter more than 100 marks for a subject, use of  for, if, Scanner etc. import java.util.Scanner; public class CheckForFailUsingIf {             public static void main(String args[])             {                         int noOfStudents, i,j,marks, sum=0, percent;                         System.out.println(“Please Enter number of students”);                         Scanner s1 = new Scanner(System.in); […]

Java addition program

PROGRAM: public class Addition { public static void main(String args[]) {  int no1, no2, addition;  no1 = 5;  no2 = 9;  addition = no1+no2;  System.out.println(+addition);  System.out.println(“—————–“);  System.out.println(“Sum =” +addition+ “\nThanks”);  System.out.println(“—————–“);  System.out.println(“Sum of “+no1+ “+” +no2 +” = “+addition + “\nThank you”); } } OUTPUT: 9 —————– Sum =9 Thanks —————– Sum of 4+5 = […]

Java program to display message

PROGRAM: To display a message public class PrintSentence {             public static void main(String args[])             {                         System.out.println(“Welcome to Java”);             }             } OUTPUT: Welcome to Java