Category: Java

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 = […]

File handling

FILE HANDLING: File handling in java comes under input output (IO) operations. Classes used in file handling in Java are present Java IO package (java.io). Some of the file handling operations are: Create file Delete file Read file Write file Changes file permissions, etc. Program for create operation in file handling: package com.journaldev.files; import java.io.File; […]

CLOSE THE DATABASE CONNECTION

CLOSE THE DATABASE CONNECTION: It is not necessary to close the connection, but open database can cause problems, so its better to close the database connection. For this close( ) method is used. Syntax: stmt.close( ); Here, stmt is statement object to be closed. cn.close( ); Here, cn is the connection object to be closed.

Process the result

The result of the SQL statement (SQL queries) are stored in a ResultSet object. To retrieve the data from the ResultSet we need to use the getXXX methods. These methods retrieve the data and convert it to a Java data type. There is a separate getXXX methods for each datatype. For example, getString( ) is […]

Execute an SQL Statement

EXECUTE AN SQL STATEMENT: There are three methods to execute the statement, which are execute( ), executeQuery( ) and executeUpdate( ). Syntax: stmt.execute( ); stmt.execute( String str); Syntax: stmt.executeQuery( ); stmt.executeQuery( String str); Syntax: stmt.executeUpdate( ); stmt.executeUpdate( String str );

JDBC

JDBC: JDBC stands for Java Database Connectivity, which is a set of Java APIs used for executing SQL statements. This Java API consists of a set of classes and interfaces to enable programmers to write Java Database applications. JDBC is a software layer that allows developers to write real client projects in Java. JDBC API […]

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:

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() {          […]

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:

Graphics in Applet

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 […]

Creating an Executable Applet

CREATING AN EXECUTABLE APPLET: Executable applet is nothing but the .class file of applet, which is obtained by compiling the source code of the applet. Compiling the applet is exactly the same as compiling Java application programs. The compiled output file called “FisrtApplet.class” [as shown in below example] should be placed in the same directory […]

Applets life cycle

APPLETS LIFE CYCLE: Applet life cycle means the various states through which applet passes during its object creation and removal. Each state is a method. There are total five states (methods), which are shown as : These methods are called automatically. init(), start(), stop(), destroy() methods are defined in java.applet.Applet class. paint() method is defined […]

Writing applets

WRITING  APPLETS: An applet is a Java program written in a special format to have a graphical user interface, which allows a user to interact with a program by keyboard and mouse, or by performing other familiar actions. The basic structure of a Java applet:   import java.applet.Applet;   import java.awt.*;        import java.awt.event.*;     public class […]

Applets Vs Applications

APPLETS VS APPLICATIONS: APPLET APPLICATION For execution requires a browser or applet viewer. For execution requires JRE. In it main() is not present. In it main() is present. Can access only browsers services. Can access any software or data available  on the system. No security requirements. Requires high security. Stand alone in nature. No third […]

Applet

LOCAL APPLETS: A local applet is that which is stored on a local system. We can create our own applet by own design and embed them into web pages. When local applet embedded in the web page, web page will search the local system directories, find the local applet and execute it. Local applets doesn’t […]

Array in Java

ARRAY: An array is a data structure which holds similar types of elements. An array is an object that stores a list of items. Each slot in an array holds individual elements. Each element should be a single datatype element, such that integer, strings, char etc. DECLARING ARRAY ELEMENTS: It indicates the name of the […]

Constructor in JAVA

CONSTRUCTORS: A constructor is a method which called when an instance of an object is created. If programmer do not provide a constructor for a class, Java will automatically create a default constructor with no parameters and doesn’t initialise any fields. Default constructor is called if we specify the new keyword without passing parameters. For […]

Defining a class

DEFINING A CLASS : A class is the basic element of object oriented programming.A class defines the shape and behavior of an object.A class is a template for multiple object with similar feature. Any concept represented in a class is encapsulated in a class. When an application is written, classes of objects are defined. To […]

Data types JAVA

Java has eight primitive data types-Boolean – value either true or falseChar – character valueByte – integral valueShort – integral valueint – integral valuelong – integral valuefloat – floating point valuedouble – floating point value Strings are class type which references to an instance of class String.

Constants and Variables in Java

A constant in Java is an unchangeable value give to a variable. For example: int AGE = 28; public class ConstantExample { } The final keyword:A final keyword tells Java compiler that the change in value assign to the variable is not allowed. Constant naming conventions:Use Upper case letters, for spaces use underscores. For example: […]

C++ Versus JAVA

C++ JAVA Platform dependent. Platform independent. Mainly used for system programming. Mainly used for application programming. Support multiple inheritances. Does not support multiple inheritance. Support goto statement. Does not support goto statement. Support operator overloading. Does not support operator overloading Uses compiler only. Uses compiler and interpreter both. Supports both call by value and call […]

Java virtual machine

Compiler write the JAVA bytecode, converting to normal language is JVMs work. A JAVA Virtual Machine runs your code as well as other people’s JAVA code on your computer. Which makes JAVA a platform independent. A JVM is written in javascript. What does the Java Virtual Machine do? Java Virtual Machine act as interpreter between […]

Java statements

It is an instruction, which tells JAVA what to do next. For example:int age = 28; Above example is a basic statement. Above type of statements is called an assignment statement because it sets a value of a variable. A statements ends with a semicolon (;).JAVA stops processing statement when it read the semicolon. Some […]

Tokens

Tokens are the JAVA elements which are identified by the JAVA compiler.Tokens are meaningful to the compiler.JAVA tokens include: Identifiers Operators Reserved words separators Constants Operations Special characters etc,

JAVA environment

JAVA environments include following things. JDKJREJVM JDK(Java Development Kit) : Its purpose is to provide development environments for software developers.It includes development tools such as the Java compiler, Jar, Javadoc, and a debugger. JRE(Java Runtime Environment) : It contains the parts of the Java libraries which are required to run Java programs. It is for […]

Can Java have same name variable

Why Java not having same name variables ?Ans. As a computer faculty, I found following reason during practise; Consider this example: class EEN { public static void main(String args[]) { int b = 10; String b = “EasyExamNotes.com”; System.out.println(b); }  } In above Java program example, there are two variables with name b, one is […]