Documente Academic
Documente Profesional
Documente Cultură
class Example
{
public static void main(String args[])
{
System.out.println("This is a simple Java program.");
}
}
Output:
2.
class Array {
public static void main(String args[])
{
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");
}
}
Output:
class TwoDArray
{
public static void main(String args[])
{
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++)
{
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Output:
Output:
Output:
Output:
Output:
Output:
Output:
interface Area
{
final static float pi=3.14F;
float compute (float x, float y);
}
class Rectangle implements Area
{
public float compute(float x, float y)
{
return(x*y);
}
}
class Circle implements Area
{
public float compute (float x, float y)
{
return (pi*x*x);
}
}
class InterfaceTest
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Output
Test.class file will be created. After wards run the following command for importing
the package into the main class MainPack
D:\Java\bin>javac MainPack.java
D:\Java\bin>java MainPack
Output:
Output:
{
public void run()
{
for (int k=1;k<5;k++)
{
System.out.println(" k="+k);
}
System.out.println("Exit from C");
}
}
class ThreadExample
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}
OutPut:
class ThreadDemo
{
public static void main(String arg[])
{
try{
for(int i=5;i>0;i--)
{
System.out.println("main Thread"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("main thread exiting.............");
}
}
Output:
System.out.println(e.getMessage());
}
finally
{
System.out.println("I am always here");
}
}
}
Output:
Output:
Ouput:
try
{
// Open an output stream
fout = new FileOutputStream ("myfile.txt");
System.exit(-1);
}
}
}
To read from a file
We create a FileInputStream object, read the text, and display it to the user.
import java.io.*;
public class IOread
{
// Main method
public static void main (String args[])
{
// Stream to read file
FileInputStream fin;
try
{
// Open an input stream
fin = new FileInputStream ("myfile.txt");
// Read a line of text
System.out.println( new DataInputStream(fin).readLine() );
// Close our input stream
fin.close();
}
}
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to read from file");
System.exit(-1);
}
Output:
class TCPServer
{
public static void main(String argv[]) throws IOException
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new
DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
class TCPClient
{
public static void main(String argv[]) throws IOException
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader( new
InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream
(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
import java.awt.*;
import java.applet.Applet;
public class Applet1 extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello",30,30);
g.drawString("I am smart",40,40);
g.drawString("I am good",50,50);
g.drawString("I am a good Dancer",60,60);
g.drawString("Have fun n enjoy this class",70,70);
g.drawString("God bless u all",80,80);
}
}
/*
<applet code= "Applet1.class" width = 500 height = 500>
</applet>
*/
Output:
Run file as
C:\java\bin> javac Applet1.java
C:\java\bin>appletviewer Applet1.java
20. WAP to draw a rectangle and fill color into it using an Applet.
import java.awt.*;
import java.applet.*;
/*
<applet code= "Applet2.class" width = 500 height = 500>
</applet>
*/
Output:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public Example1() {
setTitle("Simple example");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Output:
21.2 WAP to create a simple window and add a button to it using Swings in Java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public Example2() {
initUI();
}
panel.setLayout(null);
panel.add(quitButton);
setTitle("Quit button");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Output:
21.3 Code to add a Tooltip and add a button to a window created using Swing
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public Example3() {
initUI();
}
panel.setLayout(null);
panel.setToolTipText("A Panel container");
panel.add(button);
setTitle("Tooltip");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Output: