You are on page 1of 6

Q1:-Write a Java Program to accept parameters on the Command Line.

If there are no Command Line arguments


entered, the program should print an error message and exit else the arguments should be printed.
Sol:class argerror
{
public static void main(String args[])
{
int i=args.length;
if(i==0)
{
System.out.println("No value entered!! Exiting");
}
else
for(i=0;i<args.length;i++)
{
System.out.println(args[i]);
}
}
}
Output:-

Q2:-Write a Java program to accept a String from the user as Command Line Argument and display the total
number of characters in it.
Sol:class CharCount
{
public static void main(String args[])
{
System.out.println("The total number of characters in "+args[0]+" is :"+args[0].length());
}
}
Output:-

Q3:-Create a simple class with three instance variables named width, height and depth. Create another class which
will provide the values to those instance variables and compute the volume of the same.

Sol:class simple
{
int width;
int height;
int depth;
}
public class volume
{
public static void main(String args[])
{ sample w=new sample();
sample h=new sample();
sample d=new sample();
w.width=10;
h.height=20;
d.depth=30;
System.out.println("The Volume is: "+(w.width*h.height*d.depth));
}
}
Output:-

Q4:- Write a Java program to accept two integers at the time of initialization of an object and find out the smallest
between them using the concept of Nesting of Methods.
Sol:public class nestingmethod
{
static int i,j;
nestingmethod()
{
i=3;
j=2;
}
static void small()
{
if(i<j)
display(i);
else
display(j);
}
static void display(int a)
{
System.out.println("Smallest is: "+a);
}
public static void main(String args[])

{
new nestingmethod();
small();
}
}
Output:-

Q5:- Write a program to find the number of and sum of all integers greater than 100 and less than 200 that are
divisible by 7. Write a method for the same.
Sol:class divide7
{
public static void main(String s[])
{
int i ,sum=0;
System.out.println("Number that are divisible by 7 are :");
for(i=101;i<200;i++)
{
if(i%7==0)
{
sum = sum + i;
System.out.print(" "+i);
}
}System.out.println();
System.out.println("The total is :"+sum);
}
}
Output:-

Q6:- Write a program to sort the elements in descending order.


Sol:class SortOrder
{
public static void main(String[] args)

{
int[] arr={3,4,6,1,2,10,8,7,9,5};
System.out.print("Before Sorting: ");
for(int i=0;i<10;i++)
System.out.print(" "+arr[i]);
for(int i=0;i<10;i++)
{
int temp;
for(int j=i+1;j<10;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println();
System.out.print("After Sorting in descending order: ");
for(int i=9;i>=0;i--)
{
System.out.print(" "+arr[i]);
} }}
Output:-

Q7:- Write a program to identify the minimum value from the elements of a 4*3 matrix.
Sol:class MinMatrix {
public int minimum(int[][] matrix) {
int min = matrix[0][0];
for (int col = 0; col < matrix.length; col++) {
for (int row = 0; row < matrix[col].length; row++) {
if (min > matrix[col][row]) {
min = matrix[col][row];
}
}
}
return min; }
public static void main(String[] args) {
int[][] matrix = {{59, 4, 33}, {11, 13, 85}, {5, 25, 30}, {7, 1, 27}};
MinMatrix min = new MinMatrix();
System.out.println("The values of the matrix are :");
for(int i=0;i<matrix.length;i++)
{
for(int j=0;j<matrix.length-1;j++)

{
System.out.print(matrix[i][j]+"\t");
}
System.out.println();
}
System.out.println("The min value is: " + min.minimum(matrix));
} }
Output:-

Q8:-Write a java program to calculate the multiplication table where rows and columns will start from 10.
Sol:class Multi
{
public static void main(String args[])
{
int i,j;
for(i=10;i<20;i++)
{
for(j=10;j<20;j++)
{
System.out.print(i*j+" ");
}
System.out.println();
} }}
Output:-

Q9:- Write a program in java to create a string variable using StringBuffer class and print the length and capacity of
the buffer.
Sol:-

class Capacit
{
public static void main(String args[])
{
StringBuffer sbuffer=new StringBuffer(20);
sbuffer.insert(0,"java");
System.out.println("Length of the buffer "+sbuffer.length());
System.out.println("Capacity of the Buffer: "+sbuffer.capacity());
}}
Output:-

You might also like