jAVA LAB Programs
jAVA LAB Programs
3. Java program to find whether the person is eligible for voting or not.
4. Java program to find grade of the student using if else if
5. Java program to perform calculator program using switch
6. Java program to print number from 1 t 5 using for loop
7. To print pyramid using for loop
8. Java program using scanner class to accept name from the user and display it
9. Factorial of a number using scanner class
import java.util.*;
class Swap_With {
public static void main(String[] args) {
int x, y, t;// x and y are to swap
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of X and Y");
x = sc.nextInt();
y = sc.nextInt();
System.out.println("before swapping numbers: "+x +" "+ y);
/*swapping */
t = x;
x = y;
y = t;
System.out.println("After swapping: "+x +" " + y);
System.out.println( );
}
}
11. Java program for checking whether the number is greater than 2 by passing argument.
12. Java program to analyses the use of static in main function
13. Java program with the application of two classes.
14. Write a program to add ‘n’ numbers using command line argument.
class Addition
{
public static void main(String args[])
{
int sum=0;
for(int i=0;i<args.length;i++)
{
sum+=Integer.parseInt(args[i]);
}
System.out.println("Total:" +sum);
}
}
double total = 0;
total += myList[i];
}
}
class arrayargument{
//creating a method which receives an array as a parameter
static void display(int arr[]){
for(int i=1;i<arr.length;i++)
System.out.println(arr[i]);
}
public static void main(String args[]){
int a[]={33,3,4,5};//declaring and initializing an array
display(a);//passing array to method
}}
18.Write a java 2D array program to accept the values of rows and columns
from the user and display it.
TwoDimensionalScanner
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Row length of an array : ");
int row=sc.nextInt();
System.out.println("Enter column length of an array : ");
int column=sc.nextInt();
int a[][]=new int[row][column];//declaration
System.out.print("Enter " + row*column + " Elements to Store in Array :\n");
for (int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.print("Elements in Array are :\n");
for (int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
System.out.println("Row ["+i+"]: Column ["+j+"] :"+a[i][j]);
}
}
}
}
19. WAP to sort an array of integers accept the values of array
from user.
22.Asimple java program using class and methods for finding area of rectangle.
23. Write a separate Java program to explain the use of public, private
,protected and default access specifier Also display the output for errors.
24. Write functions get and print to read data of objects and to print
objects data.(Static Function)
class Student
{
int roll_num;
void getData(int n)
{
roll_num=n;
}
void printData()
{
System.out.println("Roll Number is"+roll_num);
}
}
class PrintData
{
public static void main(String args[])
{
}
}
Output:
Roll Number is 10
25. Write a java array list to add string values to the array and remove a[3]
value from the array. Also display total size of string array and clear all the
list of values from array and again display total size of array
}
//save as first.html
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>
Save as First.html
Execute as
27 . Create an applet program to draw various shapes using the following methods of paint class of awt
package.
• public abstract void drawString(String str, int x, int y): is used to draw the specified string.
• public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified
width and height.
• public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the
default color and specified width and height.
• public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the
specified width and height.
• public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the default
color and specified width and height.
• public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the
points(x1, y1) and (x2, y2).
• public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used
draw the specified image.
• public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is
used draw a circular or elliptical arc.
• public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used
to fill a circular or elliptical arc.
• public abstract void setColor(Color c): is used to set the graphics current color to the specified
color.
• public abstract void setFont(Font font): is used to set the graphics current font to the specified
font.
28. Write an applet program which shows the life cycle of applet.
import java.applet.*;
import java.awt.*;
setBackground(Color.YELLOW);
g.setColor(Color.RED);
g.drawString("welcome",100,100);
Output:
30)Write an applet event handling program to generate button click event
//myapplet.html
<html>
<body>
<applet code="EventApplet.class" width="300" height="300">
</applet>
</body>
</html>
Output:
Applet2.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
*lt;/applet>
*/
CheckboxGroup cb;
System.out.println("Initializing an applet");
public void actionPerformed(ActionEvent ae) //listening to textfields events when enter is pressed in a
textField
repaint(); //repaint() calls paint() method when a button click event is generated.
if(cb.getSelectedCheckbox()!=null)
}
32. Java program to create Package
//save as Simple1.java
1. package mypack;
2. public class Simple1{
3. public static void main(String args[]){
4. System.out.println("Welcome to package");
5. }
6. }
Execute:
C:\>javac -d . Simple1.java
C:\>java mypack.Simple1
33. Write a program to import packages.
// Filename : Balance.java
package core;
public class Balance
{
public String name;
public double bal;
public Balance(String n, double b)
{
name = n;
bal = b;
}
public void show()
{
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
//Filename: AccountBalance.java
import core.Balance;
class AccountBalance
{
public static void main(String args[])
{
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++)
current[i].show();
}
}
Execution:
34. Create character array. Check and print whether it is digit, letter,uppercase or
white space.
36. Write a program using Static member functions to display the details of
students such as name, age ,mobile,course where course is static data
member. Refer this program
import java.io.*; class GFG {
obj.asign1("mohit");
System.out.println(obj. Name);
}
import java.io.*;
class Complex
{
int Real,Imag;
Complex()
{}
Complex(int Real1,int Imag1)
{
Real=Real1;
Imag=Imag1;
}
Complex AddComplex(Complex C1,Complex C2)
{
Complex CSum=new Complex();
CSum.Real=C1.Real+C2.Real;
CSum.Imag=C1.Imag+C2.Imag;
return CSum;
}
Complex SubtractComplex(Complex C1,Complex C2)
{
Complex CSum=new Complex();
CSum.Real=C1.Real-C2.Real;
CSum.Imag=C1.Imag-C2.Imag;
return CSum;
}
Complex MultiplyComplex(Complex C1,Complex C2)
{
Complex CSum=new Complex();
CSum.Real=C1.Real*C2.Real;
CSum.Imag=C1.Imag*C2.Imag;
return CSum;
}
class Complexmain
{
public static void main(String[] a)
{
Complex C1=new Complex(4,8);
Complex C2=new Complex(5,7);
Complex C3=new Complex();
Complex C4=new Complex();
Complex C5=new Complex();
C3=C3.AddComplex(C1,C2);
C4=C4.SubtractComplex(C1,C2);
C5=C5.MultiplyComplex(C1,C2);
System.out.println("SUM:" + C3.Real +"+i" + C3.Imag);
System.out.println("subtraction:" + C3.Real +"-i" + C3.Imag);
System.out.println("subtraction:" + C3.Real +"*i" + C3.Imag);
}
}
class Demo{
int value1;
int value2;
Demo(){
value1 = 10;
value2 = 20;
System.out.println("Inside Constructor");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[]){
Demo d1 = new Demo();
d1.display();
}
}
39. Write a program to pass the values of rollno, name and age
using parameterized constructor and display it.
//constructor
exconstructor(String name, int age){
studentName = name;
studentAge = age;
}
void display(){
System.out.println(studentName+ " "+studentAge);
}
public static void main(String args[])
{
exconstructor myObj = new exconstructor("Manan" , 19);
myObj.display();
}
}
{ {
hourlyWage=h;
yearsWithCompany=q;
System.out.println(lastName);
System.out.println(firstName);
System.out.println(hourlyWage);
System.out.println(yearsWithCompany);
}
40. WAP to demonstrate the banking operations such as debit and
credit with the help of packages.
PACKAGE
package Bank;
import java.io.*;
Program
import Bank.PackBank;
import java.io.*;
Output:
Bank Transaction
Enter
1:Debit
2:Credit
2
Enter amount
1000
Your balance after credit transaction is 2000
41. Write a program to perform mathematical operations. Create a class
called AddSub with methods to add and subtract. Create another class
called MultDiv that extends from AddSub class to use the member data
of the superclass. MultDiv should have methods to multiply and divide.
A main method should access the methods and perform the
mathematical operations using Inheritance.
42. Create class A which accept 3 numbers from the user .Create
another class B which performs sum of the accepted numbers and
display it using single inheritance.
43. Create 2 classes A and B which has same method display. Display
the method of base class using method overriding.