0% found this document useful (0 votes)
15 views23 pages

Java Programs for Beginners

This document contains summaries of multiple Java programs: 1. A program that accepts user input for two numbers and an operation, performs the calculation, and prints the result. It demonstrates taking input and if/else statements. 2. A program that accepts a character from the user and checks if it is a vowel or consonant. It uses if/else statements to classify the input. 3. A program that demonstrates method overloading by defining multiple sum methods that can handle a different number of integer arguments. It calls each method to show overloading works.

Uploaded by

shubham ganguly
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)
15 views23 pages

Java Programs for Beginners

This document contains summaries of multiple Java programs: 1. A program that accepts user input for two numbers and an operation, performs the calculation, and prints the result. It demonstrates taking input and if/else statements. 2. A program that accepts a character from the user and checks if it is a vowel or consonant. It uses if/else statements to classify the input. 3. A program that demonstrates method overloading by defining multiple sum methods that can handle a different number of integer arguments. It calls each method to show overloading works.

Uploaded by

shubham ganguly
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

//Program to print hello

class Hello
{
public static void main(String args[])
{
[Link]("Hello");
}
}
OUTPUT

C:/javac [Link]

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= [Link](h[0]);
a= [Link](h[1]);
b= [Link](h[2]);

[Link]("The first integer is " +a);


[Link]("The second integer is " +b);
[Link]("enter your choice");
[Link]("1. Addition");
[Link]("2. Subtraction");
[Link]("3. multiplication");
[Link]("[Link]");
if(choice == 1)
[Link]( "Sum is:"+ " " + (a+b));
else if(choice == 2)
[Link]("Difference is:"+" "+(a-b));

else if(choice == 3)
[Link]("Multiplication is:"+" "+(a*b));
else
[Link]("Division is:"+ " "+(a/b));

}
}
OUTPUT

C:/javac [Link]

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
[Link]
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 [Link]{
char ch;
[Link]("enter the character");
ch = (char) [Link]();
if(ch == 'a' || ch == 'A' || ch == 'e' || ch =='E' || ch =='i'
||ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
[Link]("The character is a vowel");
else
[Link]("The character is not a vowel");
}
}
OUTPUT

C:/javac [Link]

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=[Link](h[0]);
try
{
if(w>bal)
{
throw new MyEx();
}
else
{
[Link]("success");
}
}
catch(MyEx e)
{
[Link]([Link]());
}
}
}
OUTPUT

C:/javac [Link]

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

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

C:/javac [Link]

C:/java Cla chandan 1


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

class Hello1
{
static
{
[Link]("Hello");
}
}
OUTPUT

C:/javac [Link]

C:/java Hello1
Hello
Exception in thread “main“ [Link] : main
//program to demonstrate method overloading

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

C:/javac [Link]

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

import [Link].*;
import [Link].*;//applet package

public class Face extends Applet


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

C:/javac [Link]

C:/appletviewer [Link]
//Program to create child frame window within a
applet

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
class SampleFrame extends Frame {
SampleFrame(String title) {
super(title);
setLocation(100, 100);
MyFrameWindowAdapter adapter = new
MyFrameWindowAdapter(this);
addWindowListener(adapter);
}
public void paint(Graphics g) {
[Link]("This is in frame window", 10, 40);
}
}
class MyFrameWindowAdapter extends WindowAdapter {
SampleFrame sampleFrame;
public MyFrameWindowAdapter(SampleFrame sampleFrame) {
[Link] = sampleFrame;
}
public void windowClosing(WindowEvent we) {
[Link](false);
}

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

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

C:/javac [Link]

C:/appletviewer [Link]
//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();
[Link]();
[Link](42);
main("can");
}
private void main() { [Link]("yes "); }
private void main(int i) { [Link]("you "); }
private static void main(String s) { [Link](s); }
}
OUTPUT

C:/javac [Link]

C:/java Main
yes you can

You might also like