0% found this document useful (0 votes)
50 views17 pages

Programs

Uploaded by

vijay20071204
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)
50 views17 pages

Programs

Uploaded by

vijay20071204
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

1.

APPLET SKELETON TO PRINT A STRING


/*
<applet code="[Link]" width="300" height="300">
</applet>
*/
import [Link];
import [Link];
public class First extends Applet
{
public void paint(Graphics g)
{
[Link]("welcome to applet",150,150);
}
}
OUTPUT:
COMPILE :javac [Link]
RUN :Appletviewer [Link]
[Link] WITH TWO OBJECT
class MyClass
{
private int myNumber;
public MyClass(int num)
{
myNumber = num;
}
public void displayNumber()
{
[Link]("MyNumber: " + myNumber);
}
}
public class Main
{
public static void main(String[] args) {
MyClass obj1 = new MyClass(10);
MyClass obj2 = new MyClass(20);
[Link]("Object 1:");
[Link]();
[Link]("Object 2:");
[Link]();
}
}
[Link] AND TERNARY OPERATOR
public class operator{
public static void main(String[] args) {
int a = 10;
int b = 5;

int sum = a + b;
[Link]("Sum: " + sum);

int difference = a - b;
[Link]("Difference: " + difference);

int product = a * b;
[Link]("Product: " + product);

int quotient = a / b;
[Link]("Quotient: " + quotient);

int remainder = a % b;
[Link]("Remainder: " + remainder);
int x = 10;
int y = 5;
int max = (x > y) ? x : y;
[Link]("Max: " + max);
}
}
OUTPUT:
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
Max: 10

[Link] A STRING
public class ReverseString {
public static void main(String[] args) {
String str = "Hello, World!";
String reversed = reverseString(str);
[Link]("Original String: " + str);
[Link]("Reversed String: " + reversed);
}
public static String reverseString(String str)
{
char[] charArray = [Link]();
int a= 0;
int b = [Link] - 1;

while (a < b) {
char temp = charArray[a];
charArray[a] = charArray[b];
charArray[b] = temp;
a++;
b--;
}
return new String(charArray);
}
}

[Link]
class factorial{
public static void main(String args[])
{
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++)
{
fact=fact*i;
}
[Link]("Factorial of "+number+" is: "+fact);
}
}

[Link] OR NEGATIVE
import [Link].*;
public class number
{
public static void main(String[] args)throws IOException
{
BufferedReaderbr=newBufferedReader(newInputStreamReader(Syst
[Link]));
int number;
[Link]("Enter a number: ");
number=[Link]([Link]());
if (number > 0)
{
[Link]("The number is positive.");
}
else if (number < 0)
{
[Link]("The number is negative.");
}
else
{
[Link]("The number is zero.");
}
}
}
[Link] OF NUMBERS
import [Link].*;
public class greatnumber
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
int a,b,c;
[Link]("Enter a number1: ");
a=[Link]([Link]());
[Link]("Enter a number2: ");
b=[Link]([Link]());
[Link]("Enter a number3: ");
c=[Link]([Link]());
if (a>b && a>c)
{
[Link]("The greatest number is "+a);
}
else if (b>a && b>c)
{
[Link]("The greatest number is "+b);
}
else
{
[Link]("The greatest number is "+c);
}
}
}
[Link] 10 NATURAL & ODD & EVEN NUMBERS
public class numbers {
public static void main(String[] args)
{
[Link]("First 10 Natural Numbers:");
for (int i = 1; i <= 10; i++)
{
[Link](i + " ");
}

[Link]("\nFirst 10 Odd Numbers:");


for (int i = 1; i <= 20; i += 2)
{
[Link](i + " ");
}

[Link]("\nFirst 10 Even Numbers:");


for (int i = 2; i <= 20; i += 2)
{
[Link](i + " ");
}
}
}

[Link] N TERMS AND SUM


import java .io.*;
public class sum
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
int i,num,sum = 0;
[Link]("Enter a number ");
num=[Link]([Link]());
for(i = 1; i <= num; ++i)
{
sum = sum + i;
}
[Link]("Sum of First 10 Natural Numbers is = " + sum);
}
}

[Link] TABLE
import java .io.*;
public class mul
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
int i,num;
[Link]("Enter a number ");
num=[Link]([Link]());
for(i=1; i <= 10; i++)
{
[Link](num+" * "+i+" = "+num*i);
}
}
}
[Link] OF ODD NUMBERS

import java .io.*;


public class oddsum
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
int i,n,sum=0;
[Link]("Enter a number ");
n=[Link]([Link]());
for (i = 1; i <=n; i++)
{
if (i % 2 != 0)
{
sum = sum + i;
}
}
[Link]("Sum of first " +n+ " Odd Numbers is: " +sum);
}
}
[Link] AND LOGICAL
public class rloperator {
public static void main(String[] args) {
int a = 5;
int b = 10;
int c = 15;

[Link]("Relational Operators:");
[Link]("a == b: " + (a == b)); // Equal to
[Link]("a != b: " + (a != b)); // Not equal to
[Link]("a < b: " + (a < b)); // Less than
[Link]("a > b: " + (a > b)); // Greater than
[Link]("a <= b: " + (a <= b)); // Less than or equal to
[Link]("a >= b: " + (a >= b)); // Greater than or equal to

[Link]("\nLogical Operators:");
[Link]("(a < b) && (b < c): " + ((a < b) && (b < c)));
[Link]("(a < b) || (b > c): " + ((a < b) || (b > c)));
[Link]("!(a < b): " + !(a < b));
}
}

[Link] LAYOUT

/*<applet code=[Link] height=400 width=200>


</applet>
*/
import [Link].*;
import [Link];
public class buttonDir extends Applet {
public void init() {
setLayout(new BorderLayout());
add(new Button("North"), [Link]);
add(new Button("South"), [Link]);
add(new Button("East"), [Link]);
add(new Button("West"), [Link]);
add(new Button("Center"), [Link]);
}
}

[Link] LAYOUT
*<applet code=[Link] height=400 width=200>
</applet>
*/
import [Link].*;
import [Link];

public class myButtons extends Applet {


Button button1, button2, button3;
public void init() {
button1 = new Button("Ok");
button2 = new Button("Open");
button3 = new Button("Close");
add(button1);
add(button2);
add(button3);
}
}

[Link] LAYOUT
import [Link].*;
import [Link].*;
import [Link].*;

public class CardLayoutExample1 extends JFrame implements ActionListener


{
CardLayout crd;
JButton btn1, btn2, btn3;
Container cPane;
CardLayoutExample1()
{
cPane = getContentPane();
crd = new CardLayout();
[Link](crd);
btn1 = new JButton("Apple");
btn2 = new JButton("Boy");
btn3 = new JButton("Cat");
[Link](this);
[Link](this);
[Link](this);
[Link]("a", btn1);
[Link]("b", btn2);
[Link]("c", btn3);
}
public void actionPerformed(ActionEvent e)
{
[Link](cPane);
}
public static void main(String argvs[])
{
CardLayoutExample1 crdl = new CardLayoutExample1();
[Link](300, 300);
[Link](true);
}
}

[Link] LAYOUT
import [Link].*;
class GridLayoutEx extends Frame
{
GridLayoutEx()
{
Button[] button=new Button[12];
setLayout(new GridLayout(4,3));
for(int i=0;i<[Link];i++)
{
button[i]=new Button("Button"+(i+i));
add(button[i]);
}
}
}
class GridLayoutJavaExample
{
public static void main(String args[])
{
GridLayoutEx frame=new GridLayoutEx();
[Link]("GridLayout in Java EXample");
[Link](400,150);
[Link](true);
}
}

[Link] INHERITANCE
import [Link].*;
class Employee {
int salary = 60000;
}
class Engineer extends Employee {
int benefits = 10000;
}
class Gfg {
public static void main(String args[])
{
Engineer E1 = new Engineer();
[Link]("Salary : " + [Link]
+ "\nBenefits : " + [Link]);
}
}

[Link] INHERITANCE
import [Link].*;
import [Link].*;
import [Link].*;

interface One {
public void print_geek();
}

interface Two {
public void print_for();
}

interface Three extends One, Two {


public void print_geek();
}
class Child implements Three {
public void print_geek()
{
[Link]("Geeks");
}

public void print_for() { [Link]("for"); }


}
public class bfg {
public static void main(String[] args)
{
Child c = new Child();
c.print_geek();
c.print_for();
c.print_geek();
}
}

You might also like