Java F
Java F
Submitted To: DR. NEHA VERMA MALHOTRA Submitted By: Neeraj Kumar
Professor MCA 1B
VSIT 06717704422
QUESTION 1: Write a Java program to print all odd numbers between 1 to 10.
QUESTION 2: Write a Java program to find out factorial of a number through recursion
QUESTION 3: Write a Java program to accept command line arguments & print them
QUESTION 4: Write a Java program to print fibonacci series.
QUESTION 5: Write a Java program that creates a class accounts with following details: Instance variables: ac_no., name,
ac_name, balance. Methods: withdrawal (), deposit (), display (). Use constructors to initialize members.
QUESTION 6: Write a Java program to implement constructor overloading.
QUESTION 7: Write a Java program to count the no. of objects created in a program.
QUESTION 8: Write a Java program to show call by value & call by reference.
QUESTION 9: Write a Java program to implement method over ridding & method overloading.
OVER RIDDING
class Add
{
void display(int a, int b)
{
System.out.println("SUM OF "+a+" AND "+b+": "+(a+b));
}
}
class Mul extends Add
{
void display(int a, int b)
{
System.out.println("PRODUCT OF "+a+" AND "+b+": "+(a*b));
}
}
}
}
OVER LOADING
QUESTION 10: Create a class box having height, width, depth as the instance variables & calculate its volume. Implement
constructor overloading in it. Create a subclass named box_new that has weight as an instance variable. Use super in the
box_new class to initialize members of the base class.
class box
{
int height, width,depth;
box()
{
height=width=depth=19;
}
box(int h,int w,int d)
{
height=h;
width=w;
depth=d;
}
void volume()
{
System.out.println("Volume of BOX: "+height*width*depth);
}
}
class box_new extends box
{
int weight;
box_new()
{
super();
weight=13;
}
box_new(int a,int b,int c,int d)
{
super(a,b,c);
weight=d;
}
void show()
{
volume();
System.out.println("Weight of BOXNEW: "+weight);
class Add
{
void display(int a, int b)
{
System.out.println("SUM OF "+a+" AND "+b+": "+(a+b));
}
}
class Mul extends Add
{
void display(int a, int b)
{
System.out.println("PRODUCT OF "+a+" AND "+b+": "+(a*b));
}
}
}
QUESTION 12: Write a Java program to implement interface. Create an interface named shape having area () & perimeter
() as its methods. Create three classes circle, rectangle & square that implement this interface. }
interface Shape
{
double area();
double perimeter();
}
class Rectangle implements Shape
{
private double length;
private double breadth;
public Rectangle(double length, double breadth) {
this.length = length;
this.breadth = breadth;
}
public double area()
{
return length * breadth;
}
public double perimeter()
{
return 2 * (length + breadth);
}
}
class Circle implements Shape
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public double area()
{
return Math.PI * radius * radius;
}
public double perimeter()
{
return 2 * Math.PI * radius;
}
}
class Square implements Shape
{private double side;
public Square(double side)
{
this.side = side;
}
public double area()
{
return side * side;
}
public double perimeter()
{
return 4 * side;
}
}
}
QUESTION 13: Write a Java program to show multiple inheritance.
interface Length
{
int l=10;
void displayLength();
}
interface Breadth
{
int b=5;
void displayBreadth();
}
class Rectangle implements Length, Breadth
{
public void displayLength()
{
System.out.println("LENGTH OF RECTANGLE:"+l);
}
public void displayBreadth()
{
System.out.println("BREADTH OF RECTANGLE:"+b);
}
QUESTION 14: Write a Java program to implement exception handling. Use try, catch & Finally.
}
}
QUESTION 15: Write a Java program to implement matrix multiplication by 2d array
int a[][]={{2,3,3},{3,2,2},{2,2,2}};
int b[][]={{4,3,2},{4,2,3},{3,4,3}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
QUESTION 17: Create a user defined exception named “nomatchexception” that is fired when the string entered by the
user is not “india”.
import java.util.Scanner;
class NoMatchException extends Exception
{
String s1;
NoMatchException(String s2)
{
this.s1 = s2;
}
public String toString()
{
return s1;
}
}
QUESTION 18: Write a Java program to show even & odd numbers by thread.
}
}
QUESTION 19: Write a Java program to iterate through all elements in an array list.
arr.add(15);
arr.add(25);
arr.add(35);
arr.add(45);
}
}
24. QUESTION 24: Write a Java program to demonstrate the use of equals(), trim() ,length() ,substring(),
compareto() of string class.
}
25.
public class file {
System.out.println(str1.equals(str2));
System.out.println(str1==str2);
System.out.println(str1==str3);
System.out.println(str1.equals(str3));
}
}
}
CODE:
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
}
}
OUTPUT:
QUESTION 28: Write a Java program using AWT to create a simple calculator.
CODE:
import java.awt.*;
import java.awt.event.*;
TextField tfInput;
Panel panel;
String btnString[] = {"7", "8", "9", "+","4", "5", "6", "-","1", "2", "3", "*","C", "0", "=", "/"};
Button btn[] = new Button[16];
int num1 = 0, num2 = 0, result = 0;
char op;
public Calculator()
{
add(tfInput, "North");
add(panel, "Center");
panel.setLayout(new GridLayout(4,4));
addWindowListener(new WindowAdapter()
{
if(str.equals("+"))
{
op = '+';
num1 = Integer.parseInt(tfInput.getText());
tfInput.setText("");
}
else if(str.equals("-"))
{
op = '-';
num1 = Integer.parseInt(tfInput.getText());
tfInput.setText("");
}
else if(str.equals("*"))
{
op = '*';
num1 = Integer.parseInt(tfInput.getText());
tfInput.setText("");
}
else if(str.equals("/"))
{
op = '/';
num1 = Integer.parseInt(tfInput.getText());
tfInput.setText("");
}
else if(str.equals("="))
{
num2 = Integer.parseInt(tfInput.getText());
switch(op)
{
tfInput.setText(result + "");
result = 0;
}
else if(str.equals("C"))
{
tfInput.setText("");
num1 = num2 = result = 0;
}
else
{
tfInput.setText(tfInput.getText() + str);
}
}
public static void main(String args[])
{
OUTPUT:
QUESTION 29: Create a Java applet with three buttons ‘red’,’green’,’blue’. Whenever user presses any
button the corresponding color should be seen as background color in an applet window.
CODE:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
</applet>
*/
add(Red);
Red.addActionListener(this);
add(Blue);
Blue.addActionListener(this);
Green = new Button("Green");
add(Green);
Green.addActionListener(this);
if (e.getSource() == Red)
{
setBackground(Color.red);
setBackground(Color.blue);
setBackground(Color.green);
OUTPUT:
QUESTION 30: Write a Java program to show all layout managers (4 layout managers).
CODE:
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="LayoutManagers.class" width=500 height=500>
</applet>
*/
public class LayoutManagers extends Applet implements ActionListener
{
CardLayout cl = new CardLayout();
JPanel Card;
CODE:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="ImageAndAudio.class" width=300 height=300>
<param name="msg" value="Hello World">
</applet>
*/
public class ImageAndAudio extends Applet implements ActionListener
{
Button b1, b2;
Image img;
public void init()
{
b1 = new Button("IMAGE");
b2 = new Button("AUDIO");
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
if (ae.getSource() == b2)
{
AudioClip ac = getAudioClip(getDocumentBase(),"Music.wav");
ac.play();
repaint();
}
}
super.finalize();
}
}
OUTPUT:
QUESTION 32: Create a Java applet with three buttons ‘red’,’green’,’blue’. Whenever user presses any
button the corresponding color should be seen as background color in an applet window.
CODE:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
</applet>
*/
add(Red);
Red.addActionListener(this);
add(Blue);
Blue.addActionListener(this);
add(Green);
Green.addActionListener(this);
setBackground(Color.red);
setBackground(Color.blue);
setBackground(Color.green);
OUTPUT:
QUESTION 36: Write a Java program in Java to create database table using Java.
CODE:
import java.sql.*;
public class Jdbc36
{
public static void main(String[] args)
{
Connection connection = null;
String query;
ResultSet result;
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3308/","12345", "12345");
}
}
OUTPUT:
QUESTION 37: Write a Java program in Java to insert, update, delete & select records.
CODE:
import java.sql.*;
public class Jdbc37
{
public static void main(String[] args)
{
Connection connection = null;
String query;
ResultSet result;
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3308/java_database","12345", "12345");
connection.close();
}
catch (Exception exception)
{
System.out.println(exception);
}
}
}
OUTPUT:
QUESTION 38: Write Java program to read input from java console.
}
}
QUESTION 39: Write a Java program to implement file handling(both reading & writing to a file).
CODE:
import java.io.*;
import java.util.Scanner;
class FileHandling
{
static String fileName = "file.txt";
catch (IOException e)
{
System.out.println("Error occurred: " + e);
}
}
}
OUTPUT:
class Demo
{
public void view()
{
System.out.println("Inside The Demo Class!");
}
}
class MyClass
{
public void myMethod()
{
}
}
QUESTION 41: Write a Java program to highlight the structure/syntax of a lambda expression.
interface Operation
{
int calculate(int a);
}
}
}
QUESTION 42: Write a Java program to check a word contains the character 'g' in a given string.
public class file {
if (s.contains("g"))
{
System.out.println("\n String \"" + s + "\" contains character \'g\'!!");
}
else
{
System.out.println("\n String \"" + s + "\" does not contains character \'g\'!!");
}
}
}
QUESTION 42: Write a Java program to find sequences of lowercase letters joined with an underscore.
else
{
System.out.println("\n Match not found!");
}
}
}