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

JAVA RECORD-II BCA C

The document outlines various Java programming exercises, including converting Fahrenheit to Celsius, performing automatic type conversion, checking for equilateral triangles, handling operators, and implementing classes and objects. Each exercise includes an aim, algorithm, program code, output, and result indicating successful execution. The exercises cover topics such as method overloading, constructor overloading, string manipulation, abstract classes, packages, interfaces, exception handling, and multithreading.

Uploaded by

anithar2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JAVA RECORD-II BCA C

The document outlines various Java programming exercises, including converting Fahrenheit to Celsius, performing automatic type conversion, checking for equilateral triangles, handling operators, and implementing classes and objects. Each exercise includes an aim, algorithm, program code, output, and result indicating successful execution. The exercises cover topics such as method overloading, constructor overloading, string manipulation, abstract classes, packages, interfaces, exception handling, and multithreading.

Uploaded by

anithar2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Ex No :1(a)

Date :
PROGRAM TO CONVERT FAHRENHEIT TO CELSIUS

AIM:
To convert Fahrenheit to Celsius .

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable
Step 3: Read the temperature value using scanner object as sc.nextInt() and store it in thevariable
a. Step 4: In the main method, call the celsius method as celsius (a), then celsius(double f)method
returns the Celsius temperature.
Step 6: Print the output
Step 7: End the program

PROGRAM:
import java.util.Scanner;class
FahrenheittoCelsius
{
public static void main(String arg[])
{
double a,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Fahrenheit temperature");
a=sc.nextDouble();
System.out.println("Celsius temperature is = "+celsius(a));
}
static double celsius(double f)
{
return (f-32)*5/9;
}
}

1
OUTPUT:

RESULT:
Thus the required output has been acquired successfully.

1
Ex No :1(b)
Date :

PROGRAM TO PERFORM AUTOMATIC TYPE CONVERSION


AIM:
To convert Fahrenheit to Celsius .

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable a,d and initialize it.
Step 3: Perform type conversion of integer to long by assigning a to l , long to float byassigning l
to f, and float to double by assigning d1 = f.
Step 6: Print the output variables
Step 7: End the program

PROGRAM:

import java.util.*;public
class Main
{
public static void main(String[] args)
{
int a = 50; double d =
100;
// Automatic type conversionlong l = a;

// Automatic type conversionfloat f = l;


double d1 = f;
System.out.println("Int value "+a);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
System.out.println("Double value " +d1);
}
}
OUTPUT:

RESULT:
Thus the required output has been acquired successfully.
2
Ex No :1(c )

Date :
PROGRAM TO CHECK FOR EQUILATERAL TRIANGLE
AIM:
To check for Equilateral Triangle
ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable,b,c.
Step 3:Read the input for a,b,c using Scanner object sc.
Step 4: Check the condition if(a=b)&&(b==c)
Step 5: if true then the it forms a equilateral triangle else print not a equilateral triangle.
Step 6: End the program
PROGRAM:
import java.io.*;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{ int a,b,c;
Scanner sc= new Scanner (System.in);
System.out.println("Enter the angles of Triangle");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
sc.close();
if((a==b)&&(b==c))
System.out.println("Yes, equilateral Triangle");
else
System.out.println("Not a equilateral Traingle");
}
}
OUTPUT:

3
RESULT: Thus the required output has been acquired successfully.

4
EX.NO: 2
Date :
OPERATORS
AIM

To write a java program for handling Operators

PROGRAM :

import java.io. *;
public class operator
{
public static void main (String args[])
{
int a=12, b= 5;
System.out.println("Arithmetic operator");
System.out.println("a+b="+(a+b));
System.out.println("a - b = " + (a-b));
System.out.println("Assignment operator");
int var;
var=a;
System.out.println("var using=:"+var);
var+=a;
System.out.println("var using +=: " +
var); System.out.println("Relational
operator"); System.out.println(a==b);
System.out.println(a!=b);
System.out.println("Logical operator");
System.out.println((5>3)&&(8>5));
int result1,result2;
System.out.println("Value of a:"+a);
result1=++a;
System.out.println("After increment:"+result1);
System.out.println("Unary operator");
System.out.println("Value of b:"+b);
result2=--b;
System.out.println("After decrement:"+result2);
}
}

5
OUTPUT:

RESULT:

The Program has been executed and the output was verified

6
EX.NO: 3
Date :

ARRAY AND CONTROL STATEMENTS

AIM:

To find the largest and smallest among the given list

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the values
Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program

PROGRAM:

public class Main


{
public static void main(String[] args)
{
//array of 10 numbers
int numbers[] = new int[]{10,43,53,54,32,65,63,180,43,23};
//assign first element of an array to largest and smallestint
smallest = numbers[0];
int largest = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largest)largest
= numbers[i];
else if (numbers[i] < smallest)smallest =
numbers[i];
}
System.out.println("Largest Number is : " + largest);
System.out.println("Smallest Number is : " + smallest);
}
}

7
OUTPUT:

RESULT:

Thus the required output has been obtained successfully.

8
EX.NO: 4
Date : CLASSES AND OBJECTS
AIM:

To write a java program to handle with class and object.

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the values
Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program

PROGRAM:
public class Employee
{
String name;
int age;
String designation;
double salary;
public Employee(String name)
{
this.name=name;
}
public void empAge(int empAge)
{
age=empAge;
}
public void empDesignation(String empDesig)
{
designation=empDesig;
}
public void empSalary(double empSalary)
{
salary=empSalary;
}
public void printEmployee()
{
System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" + designation );
System.out.println("Salary:" + salary);
}
public static void main(String[] args)
9
{
Employee emp1=new Employee("Steve");

emp1.empAge(19);
emp1.empDesignation("Developer");
emp1.empSalary(80000);
emp1.printEmployee();
}
}

OUTPUT:

RESULT:
Thus the required output has been obtained successfully

10
EX.NO: 5(a)
Date :
METHOD OVERLOADING

AIM:

To write a java program to handle with Method Overloading

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the Method values
Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program

PROGRAM:
import java.io.*;
public class aaa
{
public int sum(int x, int y)
{
return (x + y);
}
public int sum(int x, int y, int z)
{
return (x + y + z);
}
public double sum(double x, double y)
{
return (x + y);
}
public static void main(String args[])
{
Sum s = new aaa();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
11
}
}

OUTPUT:
30
60
31.0

RESULT:
The Program has been executed and the output was verified.

12
EX.NO: 5(b)
Date :

CONSTRUCTOR OVERLOADING

AIM:

To write a java program to handle with constructor overloading

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the Method values
Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program

PROGRAM:

import java.io.*;
class add
{
int x,y,z;
add(int a, int b, int c)
{
x= a;
y= b;
z=c
int tot=x+y+z;
System.out .println(“ The ans”+tot);
}
add()
{
x = y = z = 3;
int tot1= x+y+z;
System.out .println(“ The ans”+tot1);
}
add(int m)
{
x = y = z = m;
int tot2= x+y+z;
System.out .println(“ The ans”+tot2);
13
}
public class Test
{
public static void main(String args[])
{
add a1= new add (10, 20, 15);
add a2 = new add ();
add a3 = new add (7);
}
}

OUTPUT:
The ans 45
The ans 9
The ans 21

RESULT:
The Program has been executed and the output was verified.

14
EX.NO: 6
Date :

STRING CLASS

AIM:
To write a java program to handle with string functions

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the string
functionsvalues Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program

PROGRAM:
import java .io.*;
import java.lang.String;
public class strMethod
{
static String str="object";
public static void main(String arg[])
{
System.out.println("original string: "+str);
int str1=str.length();
System.out.println("length of string: "+str1);
String str2=str+" Oriented";
System.out.println("Modified string: "+str2);
String str3=str2.toUpperCase();
System.out.println("String :"+str3);
char ch;
ch="abc".charAt(2);
System.out.println("CharAt :"+ch);
byte x[]={66,67,68,69,70};
String s4=new String(x);
System.out.println(s4);
String str4="Hello".replace('l','w');
System.out.println(str4);
String s1="BCA";
String s2="bca";
System.out.println(s1.equals(s2));

15
System.out.println(s1.equalsIgnoreCase(s2));
String s5= "Foot ball";
System.out.println(s5.startsWith("foot"));
System.out.println(s5.endsWith("ball"));
}
}

OUTPUT:
original string:
object length of
string: 6
Modified string: object Oriented
String :OBJECT ORIENTED
CharAt :c
BCDEF
Hewwo
false
true
false
true

RESULT:
The Program has been executed and the output was verified.

16
EX.NO: 7
Date :
ABSTRACT CLASS and METHOD

AIM :
To write a java program to handle Abstract class and Method

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the string
functionsvalues Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program

PROGRAM:

abstract class Animal {


// Abstract method (does not have a
body) public abstract void
animalSound();
// Regular method
public void sleep() {
System.out.println("Zzz");
}
}

// Subclass (inherit from


Animal) class Pig extends
Animal { public void
animalSound() {
// The body of animalSound() is provided
here System.out.println("The pig says: wee
wee");

17
}
}

18
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig
object myPig.animalSound();
myPig.sleep();
}
}

OUTPUT:

RESULT:
The Program has been executed and the output was verified.

19
EX.NO: 8(a)
Date :
PACKAGES

Aim :
To write a java program to handle package.

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the string
functionsvalues Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program

PROGRAM:

import java.util.Scanner; // import the Scanner class


class Main {
public static void main(String[] args) {
Scanner myObj = new
Scanner(System.in); String userName;

// Enter username and press Enter


System.out.println("Enter username");
userName = myObj.nextLine();

System.out.println("Username is: " + userName);


}
}

20
OUTPUT:

RESULT:
The Program has been executed and the output was verified.

21
EX.NO: 8(b)
Date :
INTERFACES

Aim :
To write a java program to handle Interface.

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the string
functionsvalues Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program

PROGRAM:
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
OUTPUT:

Result:The Program has been executed and the output was verified.

22
EX.NO: 9
Date :
EXCEPTION HANDLING

AIM
To write a java program to handle Exception handling.

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the string
functionsvalues Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program

PROGRAM:
public class MultipleCatchBlock1 {
public static void main(String[] args)
{
try{
int a[]=new
int[5]; a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}

23
OUTPUT:

RESULT:

The Program has been executed and the output was verified.

24
EX.NO: 10
Date :
MULTITHREADING

AIM :
To write a java program to handle Multithreading.

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the string
functionsvalues Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program
PROGRAM:

class Worker extends Thread

{ public void run() {

for (int i = 0; i <= 5; i++) {

System.out.println(Thread.currentThread().getName() + ": " + i);

}}}

public class ThreadClassDemo {

public static void main(String[] args)

{ Thread t1 = new Worker();

Thread t2 = new

Worker(); Thread t3 =

new Worker(); t1.start();

t2.start();
t3.start();

25
}

OUTPUT:

Result:
The Program has been executed and the output was verified.

26
EX.NO: 11
Date :
LEGACY CLASSES AND METHODS

AIM :
To write a java program to handle Legacy classes and Method.

PROGRAM:
import java.util.*;
public class VectorExample
{
public static void main(String[] args)
{
Vector<String> vec = new
Vector<String>(); vec.add("C");
vec.add("C++");
vec.add("Java");
vec.add("Python");
vec.add("C#");
vec.add("Vb.Net");
Enumeration<String> data =
vec.elements();
while(data.hasMoreElements())
{
System.out.println(data.nextElement());
} } }

OUTPUT:

Result:
The Program has been executed and the output was verified.

27
EX.NO: 12
Date :
UTILITY CLASSES AND METHODS

AIM :
To write a java program to handle Utility classes and Method.

PROGRAM:
import java.util.*;
class Test1{
public static void main(String[] args)
{
ArrayList al;
al = new
ArrayList<>();
al.add(11);
al.add(32);
al.add(28);
al.add(10);
System.out.println(al);
Collections.sort(al);
System.out.println(al);
Collections.reverse(al);
System.out.println(al);
Collections.shuffle(al);
System.out.println(al);
}
}

OUTPUT:

RESULT:
The Program has been executed and the output was verified.

28
EX.NO: 13
Date :
EVENT HANDLING
AIM :

To write a Java program for Event Handling.


PROGRAM:
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
b.addActionListener(this);//passing current instance
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
public static void main(String args[])
{ new AEvent();
}}

29
OUTPUT:

Result:
The Program has been executed and the output was verified.

30
EX.NO: 14
Date :
AWT CONTROLS
AIM :
To write a Java program for AWT Control.

PROGRAM:
import java.awt.*;
public class AWTExample1 extends Frame {
AWTExample1() {
Button b = new Button("Click Me!!");
b.setBounds(30,100,80,30);
add(b);
setSize(300,300);
setTitle("This is our basic AWT example");
setLayout(null);
setVisible(true);
}
public static void main(String args[]) {
AWTExample1 f = new AWTExample1();
} }

OUTPUT:

RESULT

The Program has been executed and the output was verified.

31
EX.NO: 15
Date :
BORDER LAYOUT
AIM :

To write a Java program for displaying Border Layout

PROGRAM:

import java.awt.*;
import javax.swing.*;

public class Border


{
JFrame f;
Border()
{
f = new JFrame();

// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER

f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction


f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction
f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center

f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}
}

32
OUTPUT:

RESULT

The Program has been executed and the output was verified.

33

You might also like