You are on page 1of 23

//Program to print hello

class Hello
{
public static void main(String args[])
{
System.out.println("Hello");
}
}
OUTPUT

C:/javac Hello.java

C:/java Hello
Hello
//Program to accept two numbers from user and
perform arithmetic operations on them

class Aopertn
{
public static void main(String h[])
{
int choice,a,b;
choice= Integer.parseInt(h[0]);
a= Integer.parseInt(h[1]);
b= Integer.parseInt(h[2]);

System.out.println("The first integer is " +a);


System.out.println("The second integer is " +b);
System.out.println("enter your choice");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. multiplication");
System.out.println("4.division");
if(choice == 1)
System.out.println( "Sum is:"+ " " + (a+b));
else if(choice == 2)
System.out.println("Difference is:"+" "+(a-b));

else if(choice == 3)
System.out.println("Multiplication is:"+" "+(a*b));
else
System.out.println("Division is:"+ " "+(a/b));

}
}
OUTPUT

C:/javac Aopertn.java

C:/java Aopertn 2 3 2
The first integer is 3
The second integer is 2
enter your choice
1. Addition
2. Subtraction
3. multiplication
4.division
Difference is:1
// Program to accept character from user and check
whether it is a vowel or not

class Vowel
{
public static void main(String args[])
throws java.io.IOException{
char ch;
System.out.println("enter the character");
ch = (char) System.in.read();
if(ch == 'a' || ch == 'A' || ch == 'e' || ch =='E' || ch =='i'
||ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
System.out.println("The character is a vowel");
else
System.out.println("The character is not a vowel");
}
}
OUTPUT

C:/javac Vowel.java

C:/java Vowel
Enter the character q
The character is not a vowel
//Program to create your own exceptions

class MyEx extends Exception


{
public String message()
{
return"unable to withdraw\n You have a low balance";
}
}
class Bank
{
public static void main(String h[])
{
double bal=4000,w;
w=Double.parseDouble(h[0]);
try
{
if(w>bal)
{
throw new MyEx();
}
else
{
System.out.println("success");
}
}
catch(MyEx e)
{
System.out.println(e.message());
}
}
}
OUTPUT

C:/javac Bank.java

C:/java Bank55
Success
//Program to display the command line arguments

class Cla
{
public static void main(String h[])
{
System.out.println(" output of commandline argument
is:");
System.out.println(h[0]);
System.out.println(h[1]);
}
}
OUTPUT

C:/javac Cla.java

C:/java Cla chandan 1


output of commandline argument is
chandan
1
//Program to print Hello without using main

class Hello1
{
static
{
System.out.println("Hello");
}
}
OUTPUT

C:/javac Hello1.java

C:/java Hello1
Hello
Exception in thread “main“ java.lang.NoSuchMethodError : main
//program to demonstrate method overloading

class addd
{
void sum(int a,int b)
{
System.out.println("sum is" +(a+b));
}
void sum(int a,int b,int c)
{
System.out.println("sum is" +(a+b+c));
}
void sum(int a,int b,int c,int d)
{
System.out.println("sum is" +(a+b+c+d));
}
}
class Add
{
public static void main(String h[])
{
int x,y,z,w;
addd ob = new addd();
x= Integer.parseInt(h[0]);
y= Integer.parseInt(h[1]);
z= Integer.parseInt(h[2]);
w= Integer.parseInt(h[3]);
ob.sum(x,y);
ob.sum(x,y,z);
ob.sum(x,y,z,w);
}
}
OUTPUT

C:/javac Add.java

C:/java Add 4 8 9 11
Sum is 12
Sum is 21
Sum is 32
//Program to draw human face

import java.awt.*;
import java.applet.*;//applet package

public class Face extends Applet


{
public void paint(Graphics g)
{
g.drawArc(30,50,200,200,0,360); // Main Face
g.drawArc(82,80,30,30,20,160); //Left Eye Brow
g.fillArc(85,85,25,25,0,360); //Left Eye
g.drawArc(152,80,30,30,20,160); //Right Eye Brow
g.fillArc(155,85,25,25,0,360); //Right Eye
g.drawRect(90,190,80,20); //Mouth
g.drawLine(130,135,115,160); //Nose Left Line
g.drawLine(130,135,145,160); // Nose Right Line
g.drawLine(115,160,145,160); // Nose Base
}
}
//Face.html
<html>
<p> This file launches the 'Face' applet: Face.class! </p>
<applet code="Face.class" height=200 width=320>
No Java?!
</applet>
</html>
OUTPUT

C:/javac Face.java

C:/appletviewer Face.html
//Program to create child frame window within a
applet

import java.applet.Applet;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class SampleFrame extends Frame {
SampleFrame(String title) {
super(title);
setLocation(100, 100);
MyFrameWindowAdapter adapter = new
MyFrameWindowAdapter(this);
addWindowListener(adapter);
}
public void paint(Graphics g) {
g.drawString("This is in frame window", 10, 40);
}
}
class MyFrameWindowAdapter extends WindowAdapter {
SampleFrame sampleFrame;
public MyFrameWindowAdapter(SampleFrame sampleFrame) {
this.sampleFrame = sampleFrame;
}
public void windowClosing(WindowEvent we) {
sampleFrame.setVisible(false);
}

}
public class AppletFrame extends Applet {
Frame f;
public void init() {
f = new SampleFrame("A Frame Window");
f.setSize(250, 250);
f.setVisible(true);
}
public void start() {
f.setVisible(true);
}
public void stop() {
f.setVisible(false);
}
public void paint(Graphics g) {
g.drawString("This is in applet window", 10, 20);
}
}
//Appletframe.html

<html>
<p> This file launches the 'AppletFrame' applet: AppletFrame.class!
</p>
<applet code="AppletFrame.class" height=200 width=320>
No Java?!
</applet>
</html>
OUTPUT

C:/javac Appletframe.java

C:/appletviewer Appletframe.html
//Program to call two main in a class
public class Main {
private double main= 54.42;
public Main() { }
public static void main(String[] args) {
Main main= new Main();
main.main();
main.main(42);
main("can");
}
private void main() { System.out.print("yes "); }
private void main(int i) { System.out.print("you "); }
private static void main(String s) { System.out.println(s); }
}
OUTPUT

C:/javac Main.java

C:/java Main
yes you can

You might also like