0% found this document useful (0 votes)
28 views

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)
28 views

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
You are on page 1/ 17

1.

APPLET SKELETON TO PRINT A STRING


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

int sum = a + b;
System.out.println("Sum: " + sum);

int difference = a - b;
System.out.println("Difference: " + difference);

int product = a * b;
System.out.println("Product: " + product);

int quotient = a / b;
System.out.println("Quotient: " + quotient);

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

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

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

5.FACTORIAL
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;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}

6.POSITIVE OR NEGATIVE
import java.io.*;
public class number
{
public static void main(String[] args)throws IOException
{
BufferedReaderbr=newBufferedReader(newInputStreamReader(Syst
em.in));
int number;
System.out.print("Enter a number: ");
number=Integer.parseInt(br.readLine());
if (number > 0)
{
System.out.println("The number is positive.");
}
else if (number < 0)
{
System.out.println("The number is negative.");
}
else
{
System.out.println("The number is zero.");
}
}
}
7.GREATEST OF NUMBERS
import java.io.*;
public class greatnumber
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int a,b,c;
System.out.print("Enter a number1: ");
a=Integer.parseInt(br.readLine());
System.out.print("Enter a number2: ");
b=Integer.parseInt(br.readLine());
System.out.print("Enter a number3: ");
c=Integer.parseInt(br.readLine());
if (a>b && a>c)
{
System.out.println("The greatest number is "+a);
}
else if (b>a && b>c)
{
System.out.println("The greatest number is "+b);
}
else
{
System.out.println("The greatest number is "+c);
}
}
}
8.FIRST 10 NATURAL & ODD & EVEN NUMBERS
public class numbers {
public static void main(String[] args)
{
System.out.println("First 10 Natural Numbers:");
for (int i = 1; i <= 10; i++)
{
System.out.print(i + " ");
}

System.out.println("\nFirst 10 Odd Numbers:");


for (int i = 1; i <= 20; i += 2)
{
System.out.print(i + " ");
}

System.out.println("\nFirst 10 Even Numbers:");


for (int i = 2; i <= 20; i += 2)
{
System.out.print(i + " ");
}
}
}

9.DISPLAY N TERMS AND SUM


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

10.MULTIPLICATION TABLE
import java .io.*;
public class mul
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,num;
System.out.print("Enter a number ");
num=Integer.parseInt(br.readLine());
for(i=1; i <= 10; i++)
{
System.out.println(num+" * "+i+" = "+num*i);
}
}
}
11.SUM OF ODD NUMBERS

import java .io.*;


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

System.out.println("Relational Operators:");
System.out.println("a == b: " + (a == b)); // Equal to
System.out.println("a != b: " + (a != b)); // Not equal to
System.out.println("a < b: " + (a < b)); // Less than
System.out.println("a > b: " + (a > b)); // Greater than
System.out.println("a <= b: " + (a <= b)); // Less than or equal to
System.out.println("a >= b: " + (a >= b)); // Greater than or equal to

System.out.println("\nLogical Operators:");
System.out.println("(a < b) && (b < c): " + ((a < b) && (b < c)));
System.out.println("(a < b) || (b > c): " + ((a < b) || (b > c)));
System.out.println("!(a < b): " + !(a < b));
}
}

13.BORDER LAYOUT

/*<applet code=buttonDir.class height=400 width=200>


</applet>
*/
import java.awt.*;
import java.applet.Applet;
public class buttonDir extends Applet {
public void init() {
setLayout(new BorderLayout());
add(new Button("North"), BorderLayout.NORTH);
add(new Button("South"), BorderLayout.SOUTH);
add(new Button("East"), BorderLayout.EAST);
add(new Button("West"), BorderLayout.WEST);
add(new Button("Center"), BorderLayout.CENTER);
}
}

14.FLOW LAYOUT
*<applet code=myButtons.class height=400 width=200>
</applet>
*/
import java.awt.*;
import java.applet.Applet;

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);
}
}

15.CARD LAYOUT
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class CardLayoutExample1 extends JFrame implements ActionListener


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

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

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

18.MULTIPLE INHERITANCE
import java.io.*;
import java.lang.*;
import java.util.*;

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()
{
System.out.println("Geeks");
}

public void print_for() { System.out.println("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