INDEX
Sr. | DEFINITION |
1 | Create an array that stores 20 prices, such as $2.34, $7.89, $1.34, and so on. Display sum of All the prices. Display all values less than $5.Calculate the average of the prices and display all the values that are higher than calculated average value. |
2 | A set of 5 words is given. Write a program to reverse each word and arrange the resulting words in alphabetical order. |
3 | A set of 10 names are given. Write a program to delete the first three characters of the names and arrange the resulting names in alphabetical order and print them out. |
4 | Find the minimum and maximum values in an array. |
5 | Demonstrate the use of stack class for characters. Hint(Use array of Characters) |
6 | Menu driven application - Addition -Substraction - multiplication -division Using matrix multiplication in 2D |
7 | Menu driven application - Addition -Substraction - multiplication - division Using matrix multiplication in 3D |
8 | Write a Menu driven application in java that demonstrate a use of Stack and Queue. (Hint : Use Different class for Stack and Queue) 1) Stack a. Push b. Pop c. Display Stack d. Quit from stack 2) 2) Exit |
Program: - 1
/* Create an array that stores 20 prices, such as $2.34, $7.89, $1.34, and so on. Display sum of All the prices. Display all values less than $5.Calculate the average of the prices and display all the values that are higher than calculated average value. */
class cas
{
double a[] = {2.34, 7.89, 1.34, 1.20, 2.22, 1.40, 5.10, 2.1, 3.2, 4.7, 4.6, 2.7,5.6,3.6,7.2,7.5,8.9,5.3,3.2,2.4};
int n = a.length;
double b = 0,c=0;
double d=0;
int e=0;
public void printprices()
{
System.out.println(" ==> Floting point variables are : \n");
System.out.print(" ");
for(int i=0;i<n;i++)
{
System.out.print(""+a[i]+",");
}
System.out.println("\n\n ==> Total no of prices are : "+n);
}
public void less()
{
System.out.println(" ==> less than $5 values are: ");
System.out.print("\n ");
int ctr=0;
for(int i=0;i<n;i++)
{
if (a[i]<5)
{
System.out.print(""+a[i]+",");
ctr++;
b += a[i];
}
}
c=b/ctr;
System.out.print("\n\n ==> Sum of less than $5 prices are: "+b);
System.out.println("\n\n ==> Total no of less than $5 prices are: "+ctr);
System.out.println("\n ==> The Average of less than $5 prices is: "+c);
}
public void gtr()
{
System.out.print(" ==> Greater than $5 values are: ");
System.out.print(" ");
int ct=0;
for(int i=0;i<n;i++)
{
if (a[i]>5)
{
System.out.print(""+a[i]+",");
ct++;
b += a[i];
}
}
//d=b/ct;
//System.out.print("\n\n ==> Sum of greater than $5 prices are: "+b);
//System.out.println("\n\n ==> Total no of greater than $5 prices are: "+ct);
//System.out.println("\n ==> The Average of greater than $5 prices is: "+d);
}
public void printd( int n)
{
System.out.println();
for (int i=0;i<n;i++)
{
System.out.print("~~");
}
System.out.println();
}
}
class casting
{
public static void main (String args[])
{
cas c = new cas();
c.printd(45);
c.printprices();
c.printd(45);
c.less();
c.printd(45);
c.gtr();
c.printd(45);
}
}
Output:-
F:\>javac casting.java
F:\>java casting
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
==> Floting point variables are :
2.34,7.89,1.34,1.2,2.22,1.4,5.1,2.1,3.2,4.7,4.6,2.7,5.6,3.6,7.2,7.5,8.9,5.3,3.2,2.4,
==> Total no of prices are : 20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
==> less than $5 values are:
2.34,1.34,1.2,2.22,1.4,2.1,3.2,4.7,4.6,2.7,3.6,3.2,2.4,
==> Sum of less than $5 prices are: 35.0
==> Total no of less than $5 prices are: 13
==> The Average of less than $5 prices is: 2.6923076923076925
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ==> Greater than $5 values are: 7.89,5.1,5.6,7.2,7.5,8.9,5.3,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Program: - 2
/* A set of 5 words is given. Write a program to reverse each word and arrange the resulting words in alphabetical order. */
class wor
{
String a[] ={"Apple","Mengo","Banana","Strobary","Orange"};
int n = a.length;
public void printd()
{
System.out.println(" \n ==> The Entered Arguments are : \n" );
for(int i = 0; i < n; i++)
{
System.out.println(" ==> "+a[i]);
}
}
public void rev()
{
System.out.println(" \n ==> The revers is : \n" );
for(int i = 0; i < n; i++)
{
StringBuffer p = new StringBuffer(a[i]);
p.reverse();
System.out.println(" ==> " +p);
}
}
public void asort()
{
System.out.println(" \n ==> In the assending order : \n ");
// Here is using bubble sort.
for(int j = 0; j < n; j++)
{
for(int i = j + 1; i <n; i++)
{
if(a[i].compareTo(a[j]) < 0)
{
String t = a[j];
a[j] = a[i];
a[i] = t;
}
}
System.out.println(" ==> " +a[j]);
}
}
public void prints( int n)
{
System.out.println();
for (int i=0;i<n;i++)
{
System.out.print("*");
}
System.out.println();
}
}
class word
{
public static void main (String a[])
{
wor w =new wor();
w.prints(35);
w.printd();
w.prints(35);
w.asort();
w.prints(35);
w.rev();
w.prints(35);
}
}
Output:-
F:\>javac word.java
F:\>java word
***********************************
==> The Entered Arguments are :
==> Apple
==> Mengo
==> Banana
==> Strobary
==> Orange
***********************************
==> In the assending order :
==> Apple
==> Banana
==> Mengo
==> Orange
==> Strobary
***********************************
==> The revers is :
==> elppA
==> ananaB
==> ogneM
==> egnarO
==> yrabortS
***********************************
Program: - 3
/* A set of 10 names are given. Write a program to delete the first three characters of the names and arrange the resulting names in alphabetical order and print them out.*/
class wod
{
String a[] ={" Database", "CoreJava", "StatesticalMaths", "NumericalMaths", "Advancejava"};
int n = a.length;
public void printd()
{
System.out.println(" \n ==> The Entered Arguments are : \n" );
for(int i = 0; i < n; i++)
{
System.out.println(" ==> "+a[i]);
}
}
public void rem()
{
System.out.println(" \n ==> The remove first 3 charecter is : \n" );
for(int i = 0; i < n; i++)
{
StringBuffer p = new StringBuffer(a[i]);
p.deleteCharAt(0);
p.deleteCharAt(0);
p.deleteCharAt(0);
System.out.println(" ==> " +p);
}
}
public void asort()
{
System.out.println(" \n ==> In the assending order : \n ");
// Here is using bubble sort.
for(int j = 0; j < n; j++)
{
for(int i = j + 1; i <n; i++)
{
if(a[i].compareTo(a[j]) < 0)
{
String t = a[j];
a[j] = a[i];
a[i] = t;
}
}
System.out.println(" ==> " +a[j]);
}
}
public void prints( int n)
{
System.out.println();
for (int i=0;i<n;i++)
{
System.out.print("*");
}
System.out.println();
}
}
class wd
{
public static void main (String a[])
{
wod w =new wod();
w.prints(35);
w.printd();
w.prints(35);
w.asort();
w.prints(35);
w.rem();
w.prints(35);
}
}
Output:-
F:\>javac wd.java
F:\>java wd
***********************************
==> The Entered Arguments are :
==> Database
==> CoreJava
==> StatesticalMaths
==> NumericalMaths
==> Advancejava
***********************************
==> In the assending order :
==> Advancejava
==> CoreJava
==> Database
==> NumericalMaths
==> StatesticalMaths
***********************************
==> The remove first 3 charecter is :
==> ancejava
==> eJava
==> abase
==> ericalMaths
==> testicalMaths
***********************************
Program: - 4
/* Find the minimum and maximum values in an array.*/
import java.util.Arrays;
public class min
{
public static void main (String args[])
{
int numbers[]= {1,5,-9,12,-3,89, 18,23,4,-6};
int n = numbers.length;
System.out.print("\n ==> Entered arry is : ");
for(int i=0;i<n;i++)
{
System.out.print(""+numbers[i]+",");
}
//Find minimum (lowest) value in array using loop
System.out.println("\n\n Minimum Value = " + getMinValue(numbers));
//Find maximum (largest) value in array using loop
System.out.println("\n Maximum Value = " + getMaxValue(numbers));
}
//Find maximum (largest) value in array using loop
public static int getMaxValue(int[] numbers)
{
int maxValue = numbers[0];
for(int i=1;i<numbers.length;i++)
{
if(numbers[i] > maxValue)
{
maxValue = numbers[i];
}
}
return maxValue;
}
//Find minimum (lowest) value in array using loop
public static int getMinValue(int[] numbers)
{
int minValue = numbers[0];
for(int i=1;i<numbers.length;i++)
{
if(numbers[i] < minValue)
{
minValue = numbers[i];
}
}
return minValue;
}
}
Output:-
F:\>javac min.java
F:\>java min
==> Entered arry is : 1,5,-9,12,-3,89,18,23,4,-6,
Minimum Value = -9
Maximum Value = 89
Program: - 5
/* Demonstrate the use of stack class for characters. Hint(Use array of Characters)*/
import java.util.Stack;
public class Stk
{
public static void main(String[] args)
{
Stack stack = new Stack();
for (int i = 0; i < 10; i++)
stack.push(new Integer(i));
System.out.println("stack = " + stack);
// Treating a stack as a Vector:
stack.addElement("The last line");
System.out.println("element 5th = " + stack.elementAt(5));
System.out.println("popping elements:");
while (!stack.empty())
System.out.println(stack.pop());
}
}
Output:-
F:\>javac Stk.java
F:\>java Stk
stack = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
element 5th = 5
popping elements:
The last line
9
8
7
6
5
4
3
2
1
0
Program: - 6
/*Menu driven application
- Addition
-Substraction
- multiplication
-division
Using matrix multiplication in 2D*/
import java.io.*;
class tmat
{
int n1[][]={{4,6},{8,5}};
int n2[][]={{2,2},{4,3}};
int total=0;
public void printdata()
{
System.out.println("\n ==> First Matrics is : \n");
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
System.out.print(" "+n1[i][j]);
}
System.out.println();
}
System.out.println("\n ==> Second Matrics is : \n");
for (int i=0;i<2 ;i++)
{
for (int j=0;j<2;j++)
{
System.out.print(" "+n2[i][j]);
}
System.out.println();
}
}
public void addition()
{
System.out.println("\n ==> Addition is : \n");
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
if((n1[i][j]+n2[i][j])<10)
{
System.out.print(" ");
System.out.print("0");
System.out.print(+ (n1[i][j]+n2[i][j]));
}
else
{
System.out.print(" ");
System.out.print(+ (n1[i][j]+n2[i][j]));
}
}
System.out.println();
}
}
public void substraction()
{
System.out.println("\n ==> Substraction is : \n");
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
if((n1[i][j]-n2[i][j])<10)
{
System.out.print(" ");
System.out.print("0");
System.out.print(+ (n1[i][j]-n2[i][j]));
}
else
{
System.out.print(" ");
System.out.print(+ (n1[i][j]-n2[i][j]));
}
}
System.out.println();
}
}
public void multiplication()
{
System.out.println("\n ==> Multiplication is : \n");
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
if((n1[i][j]*n2[i][j])<10)
{
System.out.print(" ");
System.out.print("0");
System.out.print(+ (n1[i][j]*n2[i][j]));
}
else
{
System.out.print(" ");
System.out.print(+ (n1[i][j]*n2[i][j]));
}
}
System.out.println();
}
}
}
class tdA
{
public static void main(String args[])
{
tmat m = new tmat();
int ch;
try
{
System.out.println(" 1. for Print the two matrics.");
System.out.println(" 2. for Addition of two matrics.");
System.out.println(" 3. for Substraction of two matrics.");
System.out.println(" 4. for Multiplication of two matrics.");
System.out.println(" 5. for Exit.");
DataInputStream ds = new DataInputStream(System.in);
System.out.print(" Enter your choise : ");
ch = Integer.parseInt(ds.readLine());
switch(ch)
{
case 1 :
m.printdata();
break;
case 2 :
m.addition();
break;
case 3 :
m.substraction();
break;
case 4 :
m.multiplication();
break;
case 5 :
System.exit(0);
default : System.out.println("Wrong choise.");
}
}
catch (Exception e)
{
System.out.println(" Exception is : "+e);
}
}
}
Output:-
F:\>javac tdA.java
Note: tdA.java uses or overrides a depreca
Note: Recompile with -Xlint:deprecation fo
F:\>java tdA
1. for Print the two matrics.
2. for Addition of two matrics.
3. for Substraction of two matrics.
4. for Multiplication of two matrics.
5. for Exit.
Enter your choise : 1
==> First Matrics is :
4 6
8 5
==> Second Matrics is :
2 2
4 3
F:\>java tdA
1. for Print the two matrics.
2. for Addition of two matrics.
3. for Substraction of two matrics.
4. for Multiplication of two matrics.
5. for Exit.
Enter your choise : 2
==> Addition is :
06 08
12 08
F:\>java tdA
1. for Print the two matrics.
2. for Addition of two matrics.
3. for Substraction of two matrics.
4. for Multiplication of two matrics.
5. for Exit.
Enter your choise : 3
==> Substraction is :
02 04
04 02
F:\>java tdA
1. for Print the two matrics.
2. for Addition of two matrics.
3. for Substraction of two matrics.
4. for Multiplication of two matrics.
5. for Exit.
Enter your choise : 4
==> Multiplication is :
08 12
32 15
F:\>java tdA
1. for Print the two matrics.
2. for Addition of two matrics.
3. for Substraction of two matrics.
4. for Multiplication of two matrics.
5. for Exit.
Enter your choise : 5
Program: - 7
/*Menu driven application
- Addition
-Substraction
- multiplication
- division
Using matrix multiplication in 3D*/
import java.io.*;
class matt
{
int n1[][]={{4,6,6},{8,5,4},{9,8,6}};
int n2[][]={{2,2,1},{4,3,1},{3,4,1}};
public void printdata()
{
System.out.println("\n ==> First Matrics is : \n");
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
//n1[i][j] = i+j;
System.out.print(" "+n1[i][j]);
}
System.out.println();
}
System.out.println("\n ==> Second Matrics is : \n");
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
System.out.print(" "+n2[i][j]);
}
System.out.println();
}
}
public void addition()
{
System.out.println("\n ==> Addition is : \n");
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
if((n1[i][j]+n2[i][j])<10)
{
System.out.print(" ");
System.out.print("0");
System.out.print(+ (n1[i][j]+n2[i][j]));
}
else
{
System.out.print(" ");
System.out.print(+ (n1[i][j]+n2[i][j]));
}
}
System.out.println();
}
}
public void substraction()
{
System.out.println("\n ==> Substraction is : \n");
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
if((n1[i][j]-n2[i][j])<10)
{
System.out.print(" ");
System.out.print("0");
System.out.print(+ (n1[i][j]-n2[i][j]));
}
else
{
System.out.print(" ");
System.out.print(+ (n1[i][j]-n2[i][j]));
}
}
System.out.println();
}
}
public void multiplication()
{
System.out.println("\n ==> Multiplication is : \n");
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
if((n1[i][j]*n2[i][j])<10)
{
System.out.print(" ");
System.out.print("0");
System.out.print(+ (n1[i][j]*n2[i][j]));
}
else
{
System.out.print(" ");
System.out.print(+ (n1[i][j]*n2[i][j]));
}
}
System.out.println();
}
}
}
class dA
{
public static void main(String args[])
{
matt m =new matt();
int ch;
try
{
System.out.println(" 1. for Print the two matrics.");
System.out.println(" 2. for Addition of two matrics.");
System.out.println(" 3. for Substraction of two matrics.");
System.out.println(" 4. for Multiplication of two matrics.");
System.out.println(" 5. for Exit.");
DataInputStream ds = new DataInputStream(System.in);
System.out.print(" Enter your choise : ");
ch = Integer.parseInt(ds.readLine());
switch(ch)
{
case 1 :
m.printdata();
break;
case 2 :
m.addition();
break;
case 3 :
m.substraction();
break;
case 4 :
m.multiplication();
break;
case 5 :
System.exit(0);
default : System.out.println("Wrong choise.");
}
}
catch (Exception e)
{
System.out.println(" Exception is : "+e);
}
}
}
Output:-
F:\>javac dA.java
Note: dA.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details
F:\>java dA
1. for Print the two matrics.
2. for Addition of two matrics.
3. for Substraction of two matrics.
4. for Multiplication of two matrics.
5. for Exit.
Enter your choise : 1
==> First Matrics is :
4 6 6
8 5 4
9 8 6
==> Second Matrics is :
2 2 1
4 3 1
3 4 1
F:\>java dA
1. for Print the two matrics.
2. for Addition of two matrics.
3. for Substraction of two matrics.
4. for Multiplication of two matrics.
5. for Exit.
Enter your choise : 2
==> Addition is :
06 08 07
12 08 05
12 12 07
F:\>java dA
1. for Print the two matrics.
2. for Addition of two matrics.
3. for Substraction of two matrics.
4. for Multiplication of two matrics.
5. for Exit.
Enter your choise : 3
==> Substraction is :
02 04 05
04 02 03
06 04 05
F:\>java dA
1. for Print the two matrics.
2. for Addition of two matrics.
3. for Substraction of two matrics.
4. for Multiplication of two matrics.
5. for Exit.
Enter your choise : 4
==> Multiplication is :
08 12 06
32 15 04
27 32 06
F:\>java dA
1. for Print the two matrics.
2. for Addition of two matrics.
3. for Substraction of two matrics.
4. for Multiplication of two matrics.
5. for Exit.
Enter your choise : 5
Program: - 8
/* Write a Menu driven application in java that demonstrate a use of Stack and Queue. (Hint : Use Different class for Stack)
1) Stack
e. Push
f. Pop
g. Display Stack
h. Quit from stack
2) Exit*/
import java.io.*;
import java.util.*;
class Stack
{
int top=-1;
int max=5;
char stack[]=new char[5];
Stack()
{
}
public void menu()
{
System.out.println(" ==> Enter 1 to push");
System.out.println(" ==> Enter 2 to pop");
System.out.println(" ==> Enter 3 to print");
System.out.println(" ==> Enter 4 to exit");
}
int push(int top,int max,int u)
{
if(top==max)
{
System.out.println("stack over flow");
return -1;
}
else
{
System.out.println("enter element");
top=top+1;
try
{
stack[top]=(char)System.in.read();
}
catch(Exception e)
{
}
return 0;
}
}
void pop(int item)
{
if(top==-1)
{
System.out.println(" ==> stack is empty.");
}
else
{
item=stack[top];
top=top-1;
}
}
void print()
{
if(top==-1)
{
System.out.println(" ==> Stack is EMPTY.");
}
else
{
for(int i=0;i<=top;i++)
{
System.out.println(stack[i]);
}
}
}
}
//end class
class sq
{
public static void main(String args[])
{
Stack s = new Stack();
int ch;
Stack std=new Stack();
int max=5,top=-1,item=2,i;
int stack[]=new int [5];
char ch1;
System.out.println(std.top);
try
{
System.out.println(" 1. for Stack Operation.");
System.out.println(" 2. for Queue Operation.");
System.out.println(" 3. for Exit.");
DataInputStream ds = new DataInputStream(System.in);
System.out.print("\n ==> Enter your choise : ");
ch = Integer.parseInt(ds.readLine());
switch(ch)
{
case 1 :
s.menu();
System.out.println("\n ==> Enter the choice");
System.out.flush();
try
{
switch(ch=(char)System.in.read())
{
case '1':
System.out.println(" ==> push the element : ");
//std.push(top,max,5);
break;
case '2':
System.out.println(" ==> pop the element : ");
std.pop(3);
break;
case '3':
System.out.println(" ==> Print the elements : ");
std.print();
break;
case '4':
System.exit(0);
default :
System.out.println(" ==> Wrong choise.");
}
}
catch(Exception e)
{
}
break;
case 2 :
//m.addition();
break;
case 3 :
System.exit(0);
default : System.out.println("Wrong choise.");
}
}
catch (Exception e)
{
System.out.println(" Exception is : "+e);
}
}
}
Output:-
F:\>javac sq.java
Note: sq.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
F:\>java sq
1. for Stack Operation.
2. for Queue Operation.
3. for Exit.
Enter your choise : 1
-1
Enter the choice
Enter 1 to push
Enter 2 to pop
Enter 3 to print
Enter 4 to exit
Enter the choice
1
==> push the element :
2
4
5
6
7
8
9
1
==> Enter your choise :
4
No comments:
Post a Comment