0% found this document useful (0 votes)
4 views13 pages

Aiml Java

The document lists a series of programming experiments primarily focused on Java, covering topics such as basic arithmetic operations, method overloading, inheritance, exception handling, multithreading, applets, and Swing applications. Each experiment includes a brief description and sample code demonstrating the concept. The aim is to provide practical experience in Java programming techniques and concepts.

Uploaded by

nepalmf01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Aiml Java

The document lists a series of programming experiments primarily focused on Java, covering topics such as basic arithmetic operations, method overloading, inheritance, exception handling, multithreading, applets, and Swing applications. Each experiment includes a brief description and sample code demonstrating the concept. The aim is to provide practical experience in Java programming techniques and concepts.

Uploaded by

nepalmf01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

List of Experiments:

1. Write a program that accepts two numbers from the user and print their sum.

2. Write a program to calculate addition of two number using prototyping of


methods.

3. Program to demonstrate function overloading for calculation of average.

4. Program to demonstrating overloaded constructor for calculating box volume.

5. Program to show the detail of students using concept of inheritance.

6. Program to demonstrate package concept.

7. Program to demonstrate implementation of an interface which contains two


methods declaration square and cube.

8. Program to demonstrate exception handling in case of division by zero error.

9. Program to demonstrate multithreading.

10. Program to display “Hello World” in web browser using applet.

11. Program to add user controls to applets.

12. Write a program to create an application using concept of swing.


1. Accept Two Numbers and Print Their Sum

import [Link];

class Sum {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);

[Link]("Enter First Number: ");


int a = [Link]();

[Link]("Enter Second Number: ");


int b = [Link]();

int sum = a + b;

[Link]("Sum = " + sum);


}
}

2. Addition of Two Numbers Using Method Prototyping


class Addition {

int add(int a, int b);

int add(int a, int b) {


return a + b;
}

public static void main(String args[]) {


Addition obj = new Addition();

[Link]("Sum = " + [Link](10,20));


}
}

Note: Java does not support separate method prototypes like C/C++. Normally use:

class Addition {
int add(int a, int b) {
return a + b;
}

public static void main(String args[]) {


Addition obj = new Addition();
[Link]([Link](10,20));
}
}

3. Function Overloading for Calculation of Average


class Average {

void avg(int a, int b) {


[Link]("Average = " + (a+b)/2.0);
}

void avg(int a, int b, int c) {


[Link]("Average = " + (a+b+c)/3.0);
}

public static void main(String args[]) {


Average obj = new Average();

[Link](10,20);
[Link](10,20,30);
}
}

4. Overloaded Constructor for Calculating Box Volume


class Box {
int l,b,h;

Box() {
l=b=h=1;
}

Box(int x) {
l=b=h=x;
}

Box(int x,int y,int z) {


l=x;
b=y;
h=z;
}

void volume() {
[Link]("Volume = " + (l*b*h));
}

public static void main(String args[]) {


Box b1 = new Box();
Box b2 = new Box(5);
Box b3 = new Box(2,3,4);

[Link]();
[Link]();
[Link]();
}
}
5. Student Details Using Inheritance

class Student {
int roll = 101;
String name = "Rahul";
}

class Result extends Student {


int marks = 85;

void display() {
[Link]("Roll No : " + roll);
[Link]("Name : " + name);
[Link]("Marks : " + marks);
}
}

class InheritanceDemo {
public static void main(String args[]) {
Result r = new Result();
[Link]();
}
}

6. Package Concept
Package File

package mypack;

public class Demo {


public void show() {
[Link]("Package Example");
}
}

Main File

import [Link];

class Test {
public static void main(String args[]) {
Demo d = new Demo();
[Link]();
}
}
7. Interface with Square and Cube Methods

interface Calculate {
void square(int n);
void cube(int n);
}

class Demo implements Calculate {

public void square(int n) {


[Link]("Square = " + (n*n));
}

public void cube(int n) {


[Link]("Cube = " + (n*n*n));
}

public static void main(String args[]) {


Demo d = new Demo();

[Link](5);
[Link](5);
}
}

8. Exception Handling (Division by Zero)


class ExceptionDemo {
public static void main(String args[]) {

try {
int a = 100;
int b = 0;

int c = a / b;

[Link](c);
}

catch(ArithmeticException e) {
[Link]("Division by Zero Not Allowed");
}

[Link]("Program Continues...");
}
}
9. Multithreading

class MyThread extends Thread {

public void run() {


for(int i=1;i<=5;i++) {
[Link]([Link]().getName()
+" : "+i);
}
}

public static void main(String args[]) {

MyThread t1 = new MyThread();


MyThread t2 = new MyThread();

[Link]();
[Link]();
}
}
10. Hello World Using Applet

import [Link];
import [Link];

public class HelloApplet extends Applet {

public void paint(Graphics g) {


[Link]("Hello World",100,100);
}
}

HTML File

<html>
<body>
<applet code="[Link]"
width="300" height="300">
</applet>
</body>
</html>
11. Applet with User Controls

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

public class ControlApplet extends Applet {

TextField t;

public void init() {

add(new Label("Enter Name"));

t = new TextField(20);

add(t);

add(new Button("Submit"));
}
}
12. Swing Application

import [Link].*;

class SwingDemo {

public static void main(String args[]) {

JFrame f = new JFrame("Swing Example");

JButton b =
new JButton("Click Here");

[Link](100,100,120,40);

[Link](b);

[Link](300,300);
[Link](null);
[Link](true);

[Link](
JFrame.EXIT_ON_CLOSE);
}
}

You might also like