Sie sind auf Seite 1von 14

PRACTICAL- Programming in JAVA Lab Total Time – 60 hours

1. To find the sum of any number of integers entered as command line arguments.
class Sum{
public static void main(String args[])
{
int sum=0;
for(int i=0; i < args.length; i++)
{
sum=sum+Integer.parseInt(args[i]);
}
System.out.println(" Sum of natural numbers" + sum);
}
}

2. Write a Java Program to accept Integers from the user, ask for Lower and
Higher integer limits, and then Compute the following:
a) Sum of Integers that are Inside the Range
b) Sum of Integers that are Outside the Range
import java.util.Scanner;
class sum
{
public static void main(String args[])
{
int low=0,high=0,i,n,num,suminside=0,sumoutside=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the lower and higher limit:");
low=in.nextInt();
high=in.nextInt();
System.out.println("Enter the number of elements to be accepted:");
n=in.nextInt();
System.out.println("Enter the elements:");
for(i=0;i<n;i++)
{
num=in.nextInt();
if(num>=low&&num<=high)
suminside=suminside+num;
else
sumoutside=sumoutside+num;
}
System.out.println("Suminside:"+suminside);
System.out.println("Sumoutside:"+sumoutside);
}
}

3. To learn use of single dimensional array by defining the array dynamically.

class OnedimensionalScanner
{
   public static void main(String args[])
   {    
int len;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Array length : ");
len=sc.nextInt();
int a[]=new int[len];//declaration    
System.out.print("Enter " + len + " Element to Store in Array :\n");
        for(int i=0; i<len; i++)
        {
           a[i] = sc.nextInt();
        }  
        System.out.print("Elements in Array are :\n");
        for(int i=0; i<len; i++)
        {
           System.out.print(a[i] + "  ");
        }  
   }
}

4. To convert a decimal to binary number.

import java.util.Scanner;
public class DecToBin {
public static void main(String args[])
{
int dec_num, quot, i=1, j;
int bin_num[] = new int[100];
Scanner scan = new Scanner(System.in);

System.out.print("Input a Decimal Number : ");


dec_num = scan.nextInt();

quot = dec_num;

while(quot != 0)
{
bin_num[i++] = quot%2;
quot = quot/2;
}

System.out.print("Binary number is: ");


for(j=i-1; j>0; j--)
{
System.out.print(bin_num[j]);
}
System.out.print("\n");
}
}

5. To check if a number is prime or not, by taking the number as input from the
keyboard.
import java.util.Scanner;
class PrimeCheck
{
public static void main(String args[])
{
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println("Enter any number:");
//capture the input in an integer
int num=scan.nextInt();
scan.close();
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
//If isPrime is true then the number is prime else not
if(isPrime)
System.out.println(num + " is a Prime Number");
else
System.out.println(num + " is not a Prime Number");
}
}

6. Write a Menu Driven Java Program to Demonstrate Mathematical Operations:


addition, subtraction, multiplication, division, remainder using static methods.
import java.util.*;
class static1
{
static float add(float x,float y)
{
return(x+y);
}
static float sub(float x,float y)
{
return(x-y);
}
static float mul(float x,float y)
{
return(x*y);
}
static float div(float x,float y)
{
return(x/y);
}
static float rem(float x,float y)
{
return(x%y);
}
}
class staticmain
{
public static void main(String args[])
{
float n1,n2;
int ch;
static1 s=new static1();
Scanner in=new Scanner(System.in);
System.out.println("Enter any 2 numbers:");
n1=in.nextInt();
n2=in.nextInt();
do
{

System.out.println("1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n5.Remai
nder\n6.Exit");
System.out.println("Enter your choice:");
ch=in.nextInt();
switch(ch)
{
case 1: float a=s.add(n1,n2);
System.out.println("The Addition is:"+a);
break;
case 2: float b=s.sub(n1,n2);
System.out.println("The Difference is:"+b);
break;
case 3: float c=s.mul(n1,n2);
System.out.println("The Product is:"+c);
break;
case 4: float d=s.div(n1,n2);
System.out.println("The Division is:"+d);
break;
case 5: float e=s.rem(n1,n2);
System.out.println("The Remainder is:"+e);
break;
case 6: System.exit(0);
}
}while(ch<=5);
}
}

7. Define a superclass ‘Cuboid’ with member’s length, breadth, height and all
possible Constructors. Derive a subclass ‘SpecialCube’ with a member weight
and all possible Constructors. Write a main class to Create Objects of
superclass and subclass using various Constructors and Display those Objects.
class cuboid
{
int length,breadth,height;
cuboid()
{
length=0;
breadth=0;
height=0;
}
cuboid(int x,int y,int z)
{
length=x;
breadth=y;
height=z;
}
}
class specialcube extends cuboid
{
int weight;
specialcube()
{
weight=0;
}
specialcube(int a,int b,int c,int d)
{
super(a,b,c);
weight=d;
}
void display()
{
System.out.println("The length is:"+length);
System.out.println("The breadth is:"+breadth);
System.out.println("The height is:"+height);
System.out.println("The weight is:"+weight);
}
}
class cube
{
public static void main(String args[])
{
specialcube s=new specialcube(10,20,30,40);
s.display();
}
}

8. Write a Java program to implement multiplication of two matrices.

import java.util.Scanner;
class MultiplyTwoMatrices
{
public static void main(String args[])
{
int r1, r2,c1,c2,i,j,k,sum;
Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows of matrix1");


r1 = in.nextInt();

System.out.println("Enter the number columns of matrix 1");


c1 = in.nextInt();

System.out.println("Enter the number of rows of matrix2");


r2 = in.nextInt();

System.out.println("Enter the number of columns of matrix 2");


c2 = in.nextInt();

if(c1==r2)
{

int mat1[][] = new int[r1][c1];


int mat2[][] = new int[r2][c2];
int res[][] = new int[r1][c2];

System.out.println("Enter the elements of matrix1");

for ( i= 0 ; i < r1 ; i++ )


{

for ( j= 0 ; j < c1 ;j++ )


mat1[i][j] = in.nextInt();

}
System.out.println("Enter the elements of matrix2");

for ( i= 0 ; i < r2 ; i++ )


{

for ( j= 0 ; j < c2 ;j++ )


mat2[i][j] = in.nextInt();

System.out.println("\n\noutput matrix:-");
for ( i= 0 ; i < r1 ; i++ )
for ( j= 0 ; j <c2;j++)
{
sum=0;
for ( k= 0 ; k <r2;k++ )
{
sum +=mat1[i][k]*mat2[k][j] ;
}
res[i][j]=sum;
}
for ( i= 0 ; i < r1; i++ )
{
for ( j=0 ; j < c2;j++ )
System.out.print(res[i][j]+" ");

System.out.println();
}
}
else
System.out.println("multiplication does not exist ");
}

9. Write a Java program to practice using String class and its methods.

import
java.lang.String; class
stringdemo
{
public static void main(String arg[])
{
String s1=new String("gpt
gulbarga"); String s2="GPT
GULBARGA";
System.out.println(" The string s1 is : " +s1);
System.out.println(" The string s1 is : " +s2);
System.out.println(" Length of the string s1 is : "
+s1.length());
System.out.println(" The first accurence of r is at the position : "
+s1.indexOf('r')); System.out.println(" The String in Upper Case : "
+s1.toUpperCase()); System.out.println(" The String in Lower Case : "
+s1.toLowerCase()); System.out.println(" s1 equals to s2 : " +s1.equals(s2));
System.out.println(" s1 equals ignore case to s2 : "
+s1.equalsIgnoreCase(s2)); int result=s1.compareTo(s2);
System.out.println("After
compareTo()"); if(result==0)
System.out.println( s1 + " is equal to
"+s2); else if(result>0)
System.out.println( s1 + " is greather than to
"+s2); else
System.out.println( s1 + " is smaller than to "+s2);
System.out.println(" Character at an index of 6 is :"
+s1.charAt(6)); String s3=s1.substring(4,12);
System.out.println(" Extracted substring is :"+s3);
System.out.println(" After Replacing g with a in s1 : " +
s1.replace('g','a')); String s4=" This is a book ";
System.out.println(" The string s4 is :"+s4);
System.out.println(" After trim()
:"+s4.trim
());
}
}
10. Write a Java program to practice using String Buffer class and its methods.
import java.lang.String;
class stringbufferdemo
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("This is my college");
System.out.println("This string sb is : " +sb);
System.out.println("The length of the string sb is : "
+sb.length());
System.out.println("The capacity of the string sb is : "
+sb.capacity()); System.out.println("The character at an index of
6 is : " +sb.charAt(6)); sb.setCharAt(3,'x');
System.out.println("After setting char x at position 3 : " +sb);
System.out.println("After appending : " +sb.append(" in
gulbarga ")); System.out.println("After inserting : "
+sb.insert(19,"gpt ")); System.out.println("After deleting : "
+sb.delete(19,22));
}
}

11. Write a Java Program to define a class, describe its constructor, overload the
constructors and instantiate its object.

import java.lang.*; class student


{
String name; int regno;
int marks1,marks2,marks3;
// null constructor student()
{
name="raju"; regno=12345; marks1=56; marks2=47; marks3=78;
}
// parameterized constructor
student(String n,int r,int m1,int m2,int m3)
{
name=n; regno=r; marks1=m1; marks2=m2; marks3=m3;
}
// copy constructor student(student s)
{
name=s.name; regno=s.regno; marks1=s.marks1; marks2=s.marks2; marks3=s.marks3;
}
void display()
{
System.out.println(name + "\t" +regno+ "\t" +marks1+ "\t" +marks2+ "\t" + marks3);
}
}

class studentdemo
{
public static void main(String arg[])
{
student s1=new student();
student s2=new student("john",34266,58,96,84); student s3=new student(s1);
s1.display();
s2.display();
s3.display();
}
}

12. Write a Java Program to implement Vector class and its methods.
import java.lang.*;
import java.util.Vector;
import java.util.Enumeration;
class vectordemo
{
public static void main(String arg[])
{
Vector v=new Vector();
v.addElement("one");
v.addElement("two");
v.addElement("three");
v.insertElementAt("zero",0);
v.insertElementAt("oops",3);
v.insertElementAt("four",5);
System.out.println("Vector Size :"+v.size());
System.out.println("Vector apacity :"+v.capacity());
System.out.println(" The elements of a vector are :");
Enumeration e=v.elements(); while(e.hasMoreElements())
System.out.println(e.nextElement() +" ");
System.out.println();
System.out.println("The first element is : " +v.firstElement());
System.out.println("The last element is : " +v.lastElement());
System.out.println("The object oops is found at position : "+v.indexOf("oops"));
v.removeElement("oops");
v.removeElementAt(1);
System.out.println("After removing 2 elements ");
System.out.println("Vector Size :"+v.size());
System.out.println("The elements of vector are :");
for(int i=0;i<v.size();i++)
System.out.println(v.elementAt(i)+" ");
}
}
13. Write any Java Program of your own to Demonstrate Multi-Level Inheritance.
import java.util.*;
class coluniversity
{
Scanner in=new Scanner(System.in);
String uname,naac_acer,loc;
void read1()
{
System.out.println("Enter University Name:");
uname=in.nextLine();
System.out.println("Enter NAAC Accreditation:");
naac_acer=in.nextLine();
System.out.println("Enter location:");
loc=in.nextLine();
}
void display1()
{
System.out.println("__________________________________");
System.out.println("University:"+uname);
System.out.println("NAA Accreditation:"+naac_acer);
System.out.println("Location:"+loc);
}
}
class college extends coluniversity
{
Scanner in=new Scanner(System.in);
String cname,naac,loc2;
void read2()
{
read1();
System.out.println("Enter the college name:");
cname=in.nextLine();
System.out.println("Enter NAAC Accreditation:");
naac=in.nextLine();
System.out.println("Enter the location:");
loc2=in.nextLine();
}
void display2()
{
display1();
System.out.println("College:"+cname);
System.out.println("NAAC Accreditation:"+naac);
System.out.println("Location:"+loc2);
}
}
class colcourse extends college
{
Scanner in=new Scanner(System.in);
String coursename,cduration;
int fees;
void read3()
{
read2();
System.out.println("Enter the course name:");
coursename=in.nextLine();
System.out.println("Enter course duration");
cduration=in.nextLine();
System.out.println("Enter fees details:");
fees=in.nextInt();
}
void display3()
{
display2();
System.out.println("Course Name:"+coursename);
System.out.println("Course Duration:"+cduration);
System.out.println("Fees:"+fees);
}
}
class coldetails
{
public static void main(String args[])
{
colcourse c=new colcourse();
c.read3();
c.display3();
}
}

14. Write a Java Program to implement inheritance and demonstrate use of method
overriding.
import java.lang.*;
class A
{
void display()
{
System.out.println("This is from class A ");
}
}
class B extends A
{
void display()
{
System.out.println("This is from class B ");
}
}

class AB
{
public static void main(String arg[])
{
B obj=new B(); obj.display();
}
}

15. Write a program to demonstrate the concept of boxing and unboxing / Write a
Java Program to implement Wrapper classes and their methods.
class wrapperdemo
{
public static void main(String args[])
{
Float P=new Float(0);
Float I=new Float(0);
int y=0;
try
{
DataInputStream ds=new DataInputStream(System.in);
System.out.println("ENTER THE PRINCIPAL AMOUNT");
System.out.flush();
String sp=ds.readLine();
P=Float.valueOf(sp);
System.out.println("ENTER THE INTEREST RATE");
System.out.flush();
String SI=ds.readLine();
I=Float.valueOf(SI);
System.out.println("ENTER THE NUMBER OF YEARS");
System.out.flush();
String sy=ds.readLine();
y=Integer.parseInt(sy);
}
catch(Exception e)
{
System.out.println("INPUT OUTPUT ERROR");
System.exit(1);
}
float value=loan(P.floatValue(),I.floatValue(),y);

System.out.println("FINAL VALUE IS:"+value);

}
static float loan(float P,float I,int y)
{
int year=1;
float sum=P;
while(year<=
y)
{
sum=sum+(P*I)/100;
year++;
}
return sum;
}
}

16. Define an abstract class ‘Shape’ with abstract methods readShape, findArea.
Derive classes ‘Triangle’ and ‘Rectangle’ from the abstract class and define the
methods. Write a main class to Create Objects of the classes and call the
methods.
import java.util.*;
abstract class Shape
{
int x,y;
double area;
abstract void readShape();
abstract void findArea();
}
class Rectangle extends Shape
{
Scanner in=new Scanner(System.in);
void readShape()
{
System.out.println("\nEnter rectangle length:");
x=in.nextInt();
System.out.println("Enter rectangle width:");
y=in.nextInt();
}
void findArea()
{
area=x*y;
}
void display()
{
System.out.println("\nRectangle Details:");
System.out.println("Length:"+x);
System.out.println("Width:"+y);
System.out.println("Area:"+area);
}
}
class Triangle extends Shape
{
Scanner in=new Scanner(System.in);
void readShape()
{
System.out.println("\nEnter triangle base:");
x=in.nextInt();
System.out.println("Enter triangle height:");
y=in.nextInt();
}
void findArea()
{
area=0.5*x*y;
}
void display()
{
System.out.println("\nTriangle Details:");
System.out.println("Base:"+x);
System.out.println("height:"+y);
System.out.println("Area:"+area);
}
}
class shapemain
{
public static void main(String args[])
{
Rectangle r=new Rectangle();
Triangle t=new Triangle();
t.readShape();
t.findArea();
t.display();
r.readShape();
r.findArea();
r.display();
}
}

Das könnte Ihnen auch gefallen