Dharmsinh Desai University, Nadiad Department of Information Technology Core Java Technology, IT510 B.Tech. IT, Sem: V
Dharmsinh Desai University, Nadiad Department of Information Technology Core Java Technology, IT510 B.Tech. IT, Sem: V
Experiment – 01 (Problem-1)
Submitted By
Roll No.: IT124
Name: Yasvi Patel
Aim: Write the programs using the concept of nested for loops and recursion.
Note: Write source code, input and output for below programs.
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
//lower half
for(int i=0;i<2;i++)
{
//print blank spaces
for(int j=0;j<i;j++)
{
System.out.print(" ");
}
//print stars
for(int k=2;k>i;k--)
{
System.out.print(" *");
}
System.out.println();
}
}
}
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Submitted By
Roll No.: IT124
Name: Yasvi Patel
Aim: Write the programs using the concept of nested for loops and recursion.
Note: Write source code, input and output for below programs.
class Fibonacci
{
static int n1=1,n2=2,n3=0;
public static void main(String[] s)
{
//Taking input
System.out.print("Enter Number of terms in Fibonacci Series : ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
//First 2 elements
System.out.println("1");
System.out.println("1");
//Calling Method fib()
fib(num-1);
}
//Recursive method
public static void fib(int n)
{
if(n>1) //To terminate loop
{
n3 = n1+n2;
n1 = n2;
n2 = n3;
System.out.println(n3);
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
class SumOfDigit
{
static int sum=0;
public static void main(String[] s)
{
System.out.print("Enter the Number : ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int sum_of_digit = 0;
//calling method
sum_of_digit = sod(num);
System.out.print("Sum of Digits : "+sum_of_digit);
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Experiment – 02
Submitted By:
Roll No.: IT124
Name: Yasvi Patel
Aim: Write the programs using the concept of command line argument.
Note: Write source code, input and output for below programs.
Code:
// Java source code
class argclc {
public static void main(String[] args){
if(args.length == 3){
int x = Integer.parseInt(args[0]); //Converting 1nd arg to Integer
String op = args[1]; //take operator
int y = Integer.parseInt(args[2]); //Converting 2nd arg to Integer
int r=0;
if (op.equals("+")) {
r = x+y;
}
else if (op.equals("-")){
r = x-y;
}
else if (op.equals("x")){
r = x*y;
}
else if (op.equals("/")){
r = x/y;
}
else{
System.out.println("Operator Invalid");
}
System.out.println("result is: "+r); //Result as output
}
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
}
}
Input/Output:
Input:
6+2
6–2
6x2
6/2
6$2
Output:
Code:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
class iacalc {
public static void main(String[] args) {
int num1, num2;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter 1st number:");
num1 = scanner.nextInt();
System.out.print("Enter 2nd number:");
num2 = scanner.nextInt();
switch(operator)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
System.out.printf("Invalid Operator");
return;
}
Input/Output:
Input:
1st time input
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
6
2
*
2nd time input
6
2
$
Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
2.3 Write a program to print all combinations of four digit number. A four digit number is
generated using only four digits {1, 2, 3, 4} and the number has second digit greater
than first digit and fourth digit is less than third digit.
Case 1: Duplication of digit is allowed.
Case 2: Duplication of digit is not allowed.
Code:
// Java source code
import java.util.*;
class Combination
{
public static void main(String[] s)
{
System.out.print("Enter 1 for Printing with Duplication and 2 for without Duplication : ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.println();
System.out.println();
int i,j,k,l;
switch(num){
case 1: //For printing with Duplication
for(i=1;i<5;i++){
for(j=1;j<5;j++){
for(k=1;k<5;k++){
for(l=1;l<5;l++){
if(i<j && k>l){
System.out.print(i);
System.out.print(j);
System.out.print(k);
System.out.print(l);
System.out.print(" ");
}
}
}
}
}
System.out.println();
System.out.println();
break;
case 2: //For printing without Duplication
for(i=1;i<5;i++){
for(j=1;j<5;j++){
for(k=1;k<5;k++){
for(l=1;l<5;l++){
if(i<j && k>l){
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
if(i!=j && i!=k && i!=l && j!=k && j!=l && k!=l){
System.out.print(i);
System.out.print(j);
System.out.print(k);
System.out.print(l);
System.out.print(" ");
}
}
}
}
}
}
System.out.println();
System.out.println();
break;
}
}
}
Input/Output:
Input:
Enter 1 for Printing with Duplication and 2 for without Duplication : 1
Enter 1 for Printing with Duplication and 2 for without Duplication : 2
Output:
2.4 Write a program that prints multiplication table in a matrix format from the number 1
to 10.
Code:
// Java source code
class Multiplication {
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Code:
// Java source code
import java.util.*;
class Factorial {
public static void main(String[] args) {
long num;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number : ");
num = input.nextLong(); //Taking input
int facto = 1;
for(long i = num;i > 0;i--) {
facto *= i;
}
System.out.println("factorial of "+num+" is: "+facto); //output
}
}
Input/Output:
Input:
10
Output:
Code:
// Java source code
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
import java.util.*;
class fibonacci1 {
//main Function
public static void main(String args[]) {
int m; //m is maximum number
System.out.print("enter max no: ");
Scanner s=new Scanner(System.in);
m=s.nextInt(); //take input from user
for(int i=1;i<=m;i++)
System.out.print(fibonaccirecursion(i) +" "); //Calling Recursive Function
}
//Recursive Function
public static int fibonaccirecursion(int n) {
if(n==1 || n==2)
return 1;
return fibonaccirecursion(n-2)+fibonaccirecursion(n-1);
}
}
Input/Output:
Input:
10
Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Experiment – 03
Submitted By:
Roll No.: IT124
Name: Yasvi Patel
Aim: Write the programs using the concept of arrays and StringBuffer class.
Note: Write source code, input and output for below programs.
3.1 Write a program to merge two arrays in third array. Also sort the third array in
ascending order.
Code:
// Java source code
import java.util.*;
}
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Code:
// Java source code
import java.util.*;
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
3.3 Write a program that reads email address from user and check whether email address
is valid or not and separate out email id from email server name.
If input is [email protected]
Output: It is valid address
Email id: abc
Email server address: xyz.com
Code:
// Java source code
import java.util.*;
class Email {
public static void main(String[] s) {
//Define variable input for scanning input
Scanner input = new Scanner(System.in);
System.out.print("Enter the Email : ");
//Take input
String email = input.next();
//Using Regression Expression
if(email.matches("(.*)@(.*)\\.(.*)") == true) {
//If the email obey [email protected] format
//then print valid message
System.out.println("It is valid address");
//Using split function to split string before and after "@"
String[] parts = email.split("[@]");
//First part is Email
System.out.println("Email id : "+parts[0]);
//Second part is email server address
System.out.println("Email server address : "+parts[1]);
}
else {
//If the email does not obey [email protected] format
//then print invalid message
System.out.println("Invalid Address");
}
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
3.4 Write a program that converts all characters of a string in capital letters. (Use
Code:
// Java source code
import java.util.*;
class CapitalLetters {
public static void main(String[] s) {
//Define variable input for scanning input
Scanner input = new Scanner(System.in);
System.out.print("Enter the string : ");
//Take input
String str = input.next();
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Experiment – 04
Submitted By:
Roll No.:IT124
Name: Yasvi Patel
Aim: Write the programs using the concept of Generic class, Inheritance, Interface and
Package.
Note: Write source code, input and output for below programs.
4.1 Write a program to add and to multiply two matrices using Generic class concept.
Code:
// Java source code
abstract class GenericMatrix{
Object[][] matrix;
GenericMatrix(Object[][] matrix){
this.matrix=matrix;
}
public Object[][] addMatrix(Object[][] matrix){
Object[][] result=new
Object[matrix.length][matrix[0].length];
//check size
if((this.matrix.length!=matrix.length) || (this.matrix[0].length!=matrix[0].length)){
System.out.println("Error: matrices do not have the same size");
System.exit(0);
}
//perform addition
for(int i=0;i<result.length;i++)
for(int j=0;j<result[i].length;j++)
result[i][j]=add(this.matrix[i][j], matrix[i][j]);
return result;
}
public Object[][] multiplyMatrix(Object[][] matrix){
Object[][] result=new
Object[this.matrix.length][matrix[0].length];
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
//check sizes
if(this.matrix[0].length!=matrix.length){
System.out.println("Error: matrices do not have valid size");
System.exit(0);
}
//perform multiplication
for(int i=0;i<result.length;i++)
for(int j=0;j<result[i].length;j++){
result[i][j]=zero();
for(int k=0;k<this.matrix[0].length;k++){
result[i][j]=add(result[i][j],multiply(this.matrix[i][k], matrix[k][j]));
}
}
return result;
}
}
}
class TestIntegerMatrix{
public static void main(String[] args){
Integer[][] m1=new Integer[3][3];
Integer[][] m2=new Integer[3][3];
//initialize two matrices
for(int i=0;i<m1.length;i++)
for(int j=0;j<m1[0].length;j++){
m1[i][j]=new Integer(i);
m2[i][j]=new Integer(i+j);
}
//create an instance of IntegerMatrix
IntegerMatrix im=new IntegerMatrix(m1);
//perform integer matrix addition and multiplication
Object[][] addResult=im.addMatrix(m2);
Object[][] mulResult=im.multiplyMatrix(m2);
//display m1, m2, addResult, and mulResult
System.out.println("m1=");
IntegerMatrix.displayMatrix(m1);
System.out.println("m2=");
IntegerMatrix.displayMatrix(m2);
System.out.println("addResult=");
IntegerMatrix.displayMatrix(addResult);
System.out.println("mulResult=");
IntegerMatrix.displayMatrix(mulResult);
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
4.2 Create an abstract class Shape and derived classes Rectangle and Circle from Shape
class. Implement abstract method of shape class in Rectangle and Circle class. Shape class
contains:
origin (x,y) as data member.
display() and area() as abstract methods.
Circle class contains: radius as data member.
Rectangle class contains: length and width
(Use Inheritance, overloading, and overriding concept)
Code:
// Java source code
import java.lang.Math;
area = w*h;
}
//Overriding the abstract method display()
void display()
{
//Display Output
System.out.println("area of Rectangle -->"+area);
}
}
class AbstractShape
{
public static void main(String [] args)
{
//Creating object of Rectangle
Rectangle r =new Rectangle();
//Creating object of Circle
Circle c =new Circle();
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Code:
// Java source code
//Creating 2 folder :
public class a {
public static void main(String[] s)
{
//To Display it is in same file
System.out.println("Accessing Default print");
//Creating the object from imported pacakage
Display dobj = new Display();
//Calling Display Method
dobj.display();
}
}
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Input/Output:
Code:
// Java source code
class InterfaceImply
{
{
//1st interface
System.out.println("This is Interface Number 1");
}
{
//2st interface
System.out.println("This is Interface Number 2");
}
public static void main(String[] args)
{
//Creating Object to call the Interfaces
InterfaceImply in = new InterfaceImply();
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Experiment – 05
Submitted By:
Roll No.: IT124
Name: Yasvi Patel
Aim: Write the program which creates the Frame and implements MouseListener.
Note: Write source code, input and output for below programs.
5.1 Write a program to display mouse position when the mouse is pressed.
Code:
// Java source code
import java.awt.*;
import java.awt.event.*;
class MousePressedPosition_ex5 extends Frame implements MouseListener
{
int cx = 0, cy = 0;
public MousePressedPosition_ex5(String title)
{
super(title);
addMouseListener(this);
setVisible(true);
setSize(300,400);
}
public void mousePressed(MouseEvent e)
{
cx = e.getX();
cy = e.getY();
repaint();
}
public void paint(Graphics g)
{
g.drawString("Mouse at X = " + cx,20,100);
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Code:
// Java source code
import java.awt.*;
import java.awt.event.*;
public class MatrixMulFrame extends Frame{
public static void main(String[] args){
MatrixMulFrame f = new MatrixMulFrame();
f.setSize(600,400);
f.setVisible(true);
f.repaint();
}
MatrixMulFrame(){}
public void paint(Graphics g){
for(int i=1;i<=10;i++){
for(int j=1;j<=10;j++){
String str = Integer.toString(i*j);
g.drawString(str,(i*30),30+(j*30));
}
}
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Experiment – 06
Submitted By:
Roll No.:IT124
Name:Yasvi Patel
Canvas.
Note: Write source code, input and output for below programs.
6.1 Implement a GUI based calculator application. It has two TextFields for two
input numbers, one TextField for result and four Buttons named Add, Sub, Mul and Div
for addition, subtraction, multiplication and division respectively.
Code:
// Java source code
import java.awt.*;
import
java.awt.event.*;
MyFrame2(String title)
{
super(title);
setVisible(true);
setSize(300,30
0);
setLayout(new FlowLayout());
l1 = new Label("First");
l2 = new
Label("second"); t1 =
new TextField();
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
t2 = new
TextField(); t3 =
new TextField();
add = new
Button("Add"); sub =
new Button("Sub"); mul
= new Button("Mul"); div
= new Button("Div");
add(l1);
add(t1);
add(l2);
add(t2);
add(t3);
add(add);
add(sub);
add(mul);
add(div);
//here no adapter because ActionListener has only one method to
override add.addActionListener(new Addition());
sub.addActionListener(new
Subtraction());
mul.addActionListener(new
Multiplication());
div.addActionListener(new Division());
//Adapter is used if you want to not override all listeners
Interfaces addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent
we){ System.exit(0);
}
});
}
n2 =
Integer.parseInt(t2.getText());
n3 = n1-n2;
text =
Integer.toString(n3);
t3.setText(text);
}
}
class Multiplication implements ActionListener
{
public void actionPerformed(ActionEvent
ae){ int n1,n2,n3;
String text;
n1 =
Integer.parseInt(t1.getText());
n2 =
Integer.parseInt(t2.getText());
n3 = n1*n2;
text =
Integer.toString(n3);
t3.setText(text);
}
}
class Division implements ActionListener
{
public void actionPerformed(ActionEvent
ae){ int n1,n2,n3;
String text;
n1 =
Integer.parseInt(t1.getText());
n2 =
Integer.parseInt(t2.getText());
n3 = n1/n2;
text =
Integer.toString(n3);
t3.setText(text);
}
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
6.2 Write a program to draw various figures on a Canvas. The user selects figure
from a CheckboxGroup, the selected figure is then displayed in the Canvas.
Code:
// Java source code
import java.awt.*;
import
java.awt.event.*;
else if(i==2)
{
g.drawRect(50,50,100,150);
}
else if(i==3)
{
g.drawOval(70,70,75,75);
}
}
public void square()
{
i=1;
repaint();
}
public void rect()
{
i=2;
repaint();
}
public void circle()
{
i=3;
repaint();
}
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
p1.add(cb1);
p1.add(cb2);
p1.add(cb3);
add(p1);
cb1.addItemListener(thi
s);
cb2.addItemListener(thi
s);
cb3.addItemListener(thi
s);
addWindowListener(new WindowAdapter(){public void
windowClosing(WindowEvent e){System.exit(0);}});
}
public void itemStateChanged(ItemEvent e)
{
if(e.getItem().equals("Square"))
{
mc.square();
}
else if(e.getItem().equals("Rectangle"))
{
mc.rect();
}
els
e
{ mc.circle();
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
}
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Experiment – 07
Submitted By:
Roll No.:IT124
Name:Yasvi Patel
Note: Write source code, input and output for below programs.
7.1 Write an application to simulate traffic lights. The program lets the user select one of
the three lights red, yellow and green. Upon selecting a menu item, the light is turned on
and there is only one light on at a time.
Code:
// Java source code
import java.awt.*;
import java.awt.event.*;
if(i==1)
{
g.setColor(Color.red);
g.fillOval(80,85,75,75);
}
else if(i==2)
{
g.setColor(Color.yellow);
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
g.fillOval(160,85,75,75);
}
else if(i==3)
{
g.setColor(Color.green);
g.fillOval(240,85,75,75);
}
}
public void re()
{
i=1;
repaint();
}
public void ye()
{
i=2;
repaint();
}
public void gr()
{
i=3;
repaint();
}
}
p1.add(cb1);
p1.add(cb2);
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
p1.add(cb3);
add(p1, BorderLayout.SOUTH);
cb1.addItemListener(this);
cb2.addItemListener(this);
cb3.addItemListener(this);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent
e){System.exit(0);}});
}
public void itemStateChanged(ItemEvent e)
{
if(e.getItem().equals("Stop"))
{
dk.re();
}
else if(e.getItem().equals("Wait"))
{
dk.ye();
}
else
{
dk.gr();
}
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Code:
// Java source code
import java.awt.*;
import java.awt.event.*;
private int a;
private Double n;
private String s1,s2,s3,s4,s5;
gbl.setConstraints(c,gbcn);
add(c);
}
public Cal()
{
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent
e){System.exit(0);}});
tf=new TextField("",15);
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
b0=new Button("0");
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b10=new Button("+");
b11=new Button("-");
b12=new Button("*");
b13=new Button("/");
b14=new Button(".");
b15=new Button("=");
b16=new Button("C");
gbl=new GridBagLayout();
gbcn=new GridBagConstraints();
setLayout(gbl);
gbcn.fill=GridBagConstraints.BOTH;
gbcn.anchor=GridBagConstraints.WEST;
addComp(tf,gbl,gbcn,0,0,1,5,0,0);
addComp(b16,gbl,gbcn,1,4,4,1,0,0);
addComp(b7,gbl,gbcn,1,0,1,1,0,0);
addComp(b8,gbl,gbcn,1,1,1,1,0,0);
addComp(b9,gbl,gbcn,1,2,1,1,0,0);
addComp(b10,gbl,gbcn,1,3,1,1,0,0);
addComp(b4,gbl,gbcn,2,0,1,1,0,0);
addComp(b5,gbl,gbcn,2,1,1,1,0,0);
addComp(b6,gbl,gbcn,2,2,1,1,0,0);
addComp(b11,gbl,gbcn,2,3,1,1,0,0);
addComp(b1,gbl,gbcn,3,0,1,1,0,0);
addComp(b2,gbl,gbcn,3,1,1,1,0,0);
addComp(b3,gbl,gbcn,3,2,1,1,0,0);
addComp(b12,gbl,gbcn,3,3,1,1,0,0);
addComp(b0,gbl,gbcn,4,0,1,1,0,0);
addComp(b14,gbl,gbcn,4,1,1,1,0,0);
addComp(b15,gbl,gbcn,4,2,1,1,0,0);
addComp(b13,gbl,gbcn,4,3,1,1,0,0);
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0)
{
s1=tf.getText();
s2="0";
s3=s1+s2;
tf.setText(s3);
}
if(e.getSource()==b1)
{
s1=tf.getText();
s2="1";
s3=s1+s2;
tf.setText(s3);
}
if(e.getSource()==b2)
{
s1=tf.getText();
s2="2";
s3=s1+s2;
tf.setText(s3);
}
if(e.getSource()==b3)
{
s1=tf.getText();
s2="3";
s3=s1+s2;
tf.setText(s3);
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
}
if(e.getSource()==b4)
{
s1=tf.getText();
s2="4";
s3=s1+s2;
tf.setText(s3);
}
if(e.getSource()==b5)
{
s1=tf.getText();
s2="5";
s3=s1+s2;
tf.setText(s3);
}
if(e.getSource()==b6)
{
s1=tf.getText();
s2="6";
s3=s1+s2;
tf.setText(s3);
}
if(e.getSource()==b7)
{
s1=tf.getText();
s2="7";
s3=s1+s2;
tf.setText(s3);
}
if(e.getSource()==b8)
{
s1=tf.getText();
s2="8";
s3=s1+s2;
tf.setText(s3);
}
if(e.getSource()==b9)
{
s1=tf.getText();
s2="9";
s3=s1+s2;
tf.setText(s3);
}
if(e.getSource()==b14)
{
s1=tf.getText();
s2=".";
s3=s1+s2;
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
tf.setText(s3);
}
if(e.getSource()==b10)
{
s4=tf.getText();
tf.setText("");
a=1;
}
if(e.getSource()==b11)
{
s4=tf.getText();
tf.setText("");
a=2;
}
if(e.getSource()==b12)
{
s4=tf.getText();
tf.setText("");
a=3;
}
if(e.getSource()==b13)
{
s4=tf.getText();
tf.setText("");
a=4;
}
if(e.getSource()==b15)
{
s5=tf.getText();
if(a==1)
{
n=Double.parseDouble(s4)+Double.parseDouble(s5);
tf.setText(String.valueOf(n));
}
if(a==2)
{
n=Double.parseDouble(s4)-Double.parseDouble(s5);
tf.setText(String.valueOf(n));
}
if(a==3)
{
n=Double.parseDouble(s4)*Double.parseDouble(s5);
tf.setText(String.valueOf(n));
}
if(a==4)
{
if((Double.parseDouble(s5))==0)
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
{
tf.setText("Cannot Divide By Zero");
}
else
{
n=Double.parseDouble(s4)/Double.parseDouble(s5);
tf.setText(String.valueOf(n));
}
}
}
if(e.getSource()==b16)
{
tf.setText("");
}
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Experiment – 08
Submitted By:
Roll No.:IT124
Name:Yasvi Patel
Write applications that use the concept of Applet and Exception Handling.
Note: Write source code, input and output for below programs.
8.1 Write an Applet that allows free hand drawing. (Pencil tool of Paint Brush
Application).
Code:
// Java source code
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="FreeHand" width="400" height="400">
</applet>
*/
public class FreeHand extends Applet
{
public static void main(String[] s)
{
FreeHand f=new FreeHand();
FreeHand fhd=new FreeHand();
fhd.init();
f.add("center",fhd);
f.setSize(400,400);
f.setVisible(true);
}
public void init()
{
Canvas c=new Paintc();
setLayout(new BorderLayout());
add("Center",c);
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
}
}
public Paintc()
{
g=getGraphics();
addMouseListener(this);
addMouseMotionListener(this);
}
public void mousePressed(MouseEvent e)
{
if(e.isMetaDown())
{
setForeground(getBackground());
}
linestart.move(e.getX(),e.getY());
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
g=getGraphics();
if(e.isMetaDown())
{
g.fillOval(e.getX()-(csize/2),e.getY()-(csize/2),csize,csize);
}
else
{
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
g.drawLine(linestart.x,linestart.y,e.getX(),e.getY());
}
linestart.move(e.getX(),e.getY());
}
public void mouseMoved(MouseEvent e)
{
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Code:
// Java source code
import java.awt.*;
import java.awt.event.*;
import java.util.*;
try {
int value = list[index];
String txt = Integer.toString(value);
tf2.setText(txt);
}
catch(ArrayIndexOutOfBoundsException aie) {
tf2.setText("Out of Bound");
}
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Experiment – 09
Submitted By:
Roll No.:IT124
Name:Yasvi Patel
Note: Write source code, input and output for below programs.
9.1 Write an application to display moving banner. Enter message to be moved, color and
size through three different TextFields. Select direction (left or right) using Choice. On
pressing start Button message should start moving.
Code:
// Java source code
import java.awt.*;
import java.awt.event.*;
import java.util.*;
{
MovingBanner f=new MovingBanner("MOVING BANNER");
}
MovingBanner(String title)
{
super(title);
t=new Thread(this,title);
//container containing components
Panel p=new Panel();
tf1=new TextField(20);
tf2=new TextField(10);
tf3=new TextField(3);
l1=new Label("Enter the String");
l2=new Label("Color");
l3=new Label("Size");
Panel p2=new Panel();
b=new Button("Start");
c=new Choice();
c.add("Right");
c.add("Left");
setLayout(new BorderLayout());
p.add(l1);
p.add(tf1);
p.add(l2);
p.add(tf2);
p.add(l3);
p.add(tf3);
add("North",p);
p2.add(b);
p2.add(c);
add("South",p2);
setSize(800,400);
setVisible(true);
//setBackground(Color.white);
b.setBackground(Color.gray);
b.setForeground(Color.white);
b.addActionListener(this);
//closing the window
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void run()
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
{
//Proper formatting of the movement of the banner.Handling exception
try
{
while(true)
{
Thread.sleep(200);
msg=tf1.getText();
z=Integer.parseInt(tf3.getText());
col=tf2.getText();
if(c.getSelectedItem().equals("Left"))
{
x=x-20;
if(x<(-100))
x=800;
repaint();
}
else
{
x=x+20;
if(x>(800))
x=0;
repaint();
}
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public void paint(Graphics g)
{
//Changing the colour of the text as per user requirement
Font f=new Font("Serif",Font.BOLD,z);
g.setFont(f);
if(col.equals("red"))
setForeground(Color.red);
else if(col.equalsIgnoreCase("blue"))
setForeground(Color.blue);
else if(col.equalsIgnoreCase("orange"))
setForeground(Color.orange);
else if(col.equalsIgnoreCase("cyan"))
setForeground(Color.cyan);
else if(col.equalsIgnoreCase("magenta"))
setForeground(Color.magenta);
else if(col.equalsIgnoreCase("yellow"))
setForeground(Color.yellow);
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
else
setForeground(Color.black);
g.drawString(msg,x,y);
}
public void actionPerformed(ActionEvent ae)
{
//Start button is pressed - start moving the banner
String s=ae.getActionCommand();
if(ae.getSource() instanceof Button) //if source of event is button the do the following
{
if(s.equals("Start"))
t.start();
}
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
9.2 Write an applet that shows four moving objects (balls) as shown below:
Four directions are used. On touching boundary, the object (ball) changes its direction
and start moving in that changed direction.
Code:
// Java source code
import java.awt.*;
import java.applet.*;
import java.awt.*;
/*
<applet code="screen.class" width="300" height="200">
</applet>
*/
class circle extends Thread
{
public int x,y,r;
public int inc;
public int dir;
public screen s1;
public void draw(Graphics g)
{
g.fillOval(x,y,r,r);
}
public circle(int xx,int yy,int rr,int d,int l,screen s)
{
x=xx;y=yy;r=rr;
dir=d;
s1=s;
inc=l;
start();
}
public void run()
{
for(;;)
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
{
if(y>=300)
{
if(dir==1)
{
dir=2;
}
if(dir==-2)
{
dir=-1;
}
}
if(y<=0)
{
if(dir==-1)
{
dir=-2;
}
if(dir==2)
{
dir=1;
}
}
if(x>=600)
{
if(dir==1)
{
dir=2;
}
if(dir==2)
{
dir=-1;
}
}
if(x<0)
{
if(dir==-1)
{
dir=2;
}
if(dir==-2)
{
dir=1;
}
}
if(dir==1)
{
x=x+inc;
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
y=y+inc;
}
else if(dir==2)
{
y=y-inc;
x=x+inc;
}
else if(dir==-1)
{
x=x-inc;
y=y-inc;
}
else if(dir==-2)
{
y=y+inc;
x=x-inc;
}
s1.repaint();
try
{
sleep(50);
}
catch(Exception e){
}
}
}
}
public class screen extends Applet
{
public circle c1,c2,c3,c4;
public screen(){}
public void init()
{
c1=new circle(0,300,20,1,10,this);
c2=new circle(400,300,20,2,5,this);
c3=new circle(400,0,20,-1,10,this);
c4=new circle(400,300,20,-2,5,this);
}
public void start(){}
public void stop(){}
public void paint(Graphics g)
{
setBackground(Color.white);
c2.draw(g);
c1.draw(g);
c3.draw(g);
c4.draw(g);
}
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Experiment – 10
Submitted By:
Roll No.:IT124
Name:Yasvi Patel
Note: Write source code, input and output for below programs.
Code:
// Java source code
import java.io.*;
import java.awt.*;
import java.awt.event.*;
m.add(n);
m.add(o);
m.add(s);
m.add(e);
mb.add(m);
add(ta);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
public void windowClosed(WindowEvent e){
System.exit(0);
}
});
n.addActionListener(this);
o.addActionListener(this);
s.addActionListener(this);
e.addActionListener(this);
}
else if(str.equals("Open"))
{
FileDialog fd=new FileDialog(this,"Open",FileDialog.LOAD);
fd.setVisible(true);
String fn=fd.getFile();
BufferedReader br=null;
try
{
br=new BufferedReader(new FileReader(fn));
String path=br.readLine();
boolean firstLine=true;
while(br!=null)
{
if(firstLine)
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
{
firstLine=false;
ta.append(path);
}
else
{
ta.append("\n"+path);
}
path=br.readLine();
if(path==null)
break;
}
}
catch(FileNotFoundException fnf)
{
System.out.println("File Not Found.");
}
catch(IOException fnf)
{
System.out.println("IO Exception!");
}
}
else if(str.equals("Save"))
{
try
{
FileDialog fd=new FileDialog(this,"Save",FileDialog.SAVE);
fd.setVisible(true);
String txt=ta.getText();
String dir=fd.getDirectory();
String fname=fd.getFile();
FileOutputStream fos=new FileOutputStream(dir+fname);
DataOutputStream dos=new DataOutputStream(fos);
dos.writeBytes(txt);
dos.close();
}
catch(Exception e){}
}
else if(str.equals("Exit"))
System.exit(0);
}
}
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
10.2 Write a program that will count number of characters, words and lines in a file. The
name should be passes as command line argument.
Code:
// Java source code
import java.io.*;
if(args.length!=1)
{
System.out.println("Enter Filename as one argument");
System.exit(0);
}
File file = new File(args[0]);
FileInputStream fileStream = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fileStream);
BufferedReader reader = new BufferedReader(input);
String line;
// Initializing counters
int characterCount = 0;
int countWord = 0;
int lineCount = 0;
int whitespaceCount = 0;
countWord += wordList.length;
whitespaceCount += countWord -1;
lineCount += sentenceList.length;
}
}
System.out.println("Total number of characters = " + characterCount);
System.out.println("Total word count = " + countWord);
System.out.println("Total number of lines = " + lineCount);
}
}
Input/Output:
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Experiment – 11
Submitted By:
Roll No.:IT124
Name:Yasvi Patel
Note: Write source code, input and output for below program.
Code:
// Java source code
ServerDemo.java
import java.net.*;
import java.io.*;
ClientDemo.java
import java.net.*;
import java.io.*;
{
String serverName = args[0];
int port = Integer.parseInt(args[1]);
try
{
System.out.println("Connecting to " + serverName +
" on port " + port);
Socket ClientDemo = new Socket(serverName, port);
System.out.println("Just connected to "
+ ClientDemo.getRemoteSocketAddress());
OutputStream outToServer = ClientDemo.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello from "
+ ClientDemo.getLocalSocketAddress());
InputStream inFromServer = ClientDemo.getInputStream();
DataInputStream in =
new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
ClientDemo.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
BTech-IT, Sem-5, Term Work, Core Java Technology, IT510
Input/Output: