Java Programs: Chapter 2:derived Syntactical Constructs in Java
Java Programs: Chapter 2:derived Syntactical Constructs in Java
Code:
import java.util.*;
class account {
int depAmo;
int tprice1=tb;
depAmo=obj.nextInt();
tprice1=tprice1+depAmo;
void withdrawl(int b) {
int wAmo;
int tprice=b;
wAmo=obj.nextInt();
tprice=tprice-wAmo;
System.out.print("\nTotal balance : "+tprice);
int accno;
String accname;
int ch;
int bal;
accno=obj1.nextInt();
accname=obj1.next();
bal=obj1.nextInt();
do {
ch=obj1.nextInt();
switch(ch) {
case 1:o.deposite(bal);
break;
case 2:o.withdrawl(bal);
break;
case 3:System.exit(0);
}while (ch<2);
Output:
2.Define a class item having data member’s code and price. Accept data from one object
and display it.
Code:
import java.util.*;
class item {
static int code,price;
void accept() {
code=obj.nextInt();
price=obj.nextInt();
void display() {
r.accept();
r.display();
Output:
3] Write a program to accept a number as command line argument and print the
number is even or odd.
Code :
import java.util.*;
class evenoddcmd {
int no=Integer.parseInt(arg[0]);
if(no%2==0) {
else {
}
}
Output:
4]Write a program to accept two numbers as command line arguments and print the
addition of those numbers.
Code:
import java.util.*;
class demo {
int no1=Integer.parseInt(arg[0]);
int no2=Integer.parseInt(arg[1]);
int no3=no1+no2;
System.out.print("Addition : "+no3);
}
Output:
5] Define a class Item having data members code and price. Accept the data for 5 objects
and display data in tabular form using array of object. Also display total price of all items.
Code:
import java.util.*;
class Itemobj {
int code,price;
static int p;
void accept() {
code=obj1.nextInt();
price=obj1.nextInt();
p=p+price;
void display() {
System.out.println(code+"\t"+price);
for(int i=0;i<5;i++) {
o[i]=new Itemobj();
o[i].accept();
System.out.println("Code\tPrice");
for(int i=0;i<5;i++) {
o[i].display();
Output:
6] WAP to add two 3X3 matrices.
Code:
import java.util.*;
class matrix {
int i,j;
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
a[i][j]=obj.nextInt();
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
b[i][j]=obj.nextInt();
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
System.out.print("\t"+a[i][j]);
System.out.print("\n");
for(j=0;j<3;j++) {
System.out.print("\t"+b[i][j]);
System.out.print("\n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
c[i][j]=a[i][j]+b[i][j];
System.out.print("\nAddition : \n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
System.out.print("\t"+c[i][j]);
System.out.print("\n");
Output:
7] WAP to Subtract two 3x3 matrices.
Code:
import java.util.*;
class submatrix {
public static void main(String args[]) {
int i,j;
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
a[i][j]=obj.nextInt();
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
b[i][j]=obj.nextInt();
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
System.out.print("\t"+a[i][j]);
}
System.out.print("\n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
System.out.print("\t"+b[i][j]);
System.out.print("\n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
c[i][j]=a[i][j]-b[i][j];
System.out.print("\nSubtraction : \n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
System.out.print("\t"+c[i][j]);
System.out.print("\n");
}
Output:
8] WAP to search element in unsorted array.
Code:
import java.util.*;
class binarysearch {
public static void main(String args[]) {
int a[]=new int[10];
int n,sele;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the number of elements in array : ");
n=obj.nextInt();
System.out.print("\nEnter the array elements : ");
for(int i=0;i<n;i++) {
a[i]=obj.nextInt();
}
System.out.print("\nEnter the element to be searched : ");
sele=obj.nextInt();
int val=Arrays.binarySearch(a,sele);
System.out.print("Element found at index : "+val);
val=val+1;
System.out.print("\tElement found at position : "+val);
}
}
Output:
Code:
import java.util.*;
class insert {
public static void main(String args[]) {
int a[]=new int[10];
int n,i,pos,ele,temp;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the number of array elemenet's in array : ");
n=obj.nextInt();
System.out.print("\nEnter the array elements : ");
for(i=0;i<n;i++) {
a[i]=obj.nextInt() ;
}
System.out.print("\nEnter the new element : ");
ele=obj.nextInt();
System.out.print("\nEnter the position of element to be inserted : ");
pos=obj.nextInt();
for(i=n-1;i>=pos-1;i--) {
temp=a[i];
a[i+1]=a[i];
a[i]=temp;
}
a[pos-1]=ele;
System.out.print("\nUpdated array : \n");
for(i=0;i<=n;i++) {
System.out.print("\t"+a[i]);
}
}
}
Output:
10. WAP to sort the array elements.
Code:
import java.util.*;
import java.lang.*;
class sort {
public static void main(String args[]) {
int a[]=new int[10];
int n,i;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the number of array elements : ");
n=obj.nextInt();
System.out.print("\nEnter the array elements : ");
for(i=0;i<n;i++) {
a[i]=obj.nextInt();
}
System.out.print("\nBefore sorting : ");
for(i=0;i<n;i++) {
System.out.print("\t"+a[i]);
}
Arrays.sort(a,0,n);
System.out.print("\nSorted array : ");
for(i=0;i<n;i++) {
System.out.print("\t"+a[i]);
}
}
}
Output:
11]WAP to delete
element from array.
Code:
import java.util.*;
class deleteele {
public static void main(String args[]) {
int pos,temp,no;
int a[]=new int[100];
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the number of elements in array : ");
no=obj.nextInt();
System.out.print("\nEnter the array elements : ");
for(int i=0;i<no;i++) {
a[i]=obj.nextInt();
}
System.out.print("\nEnter the position of element to be deleted : ");
pos=obj.nextInt();
for(int i=pos-1;i<no;i++) {
a[i]=a[i+1];
}
System.out.print("\nArray after deletion : ");
for(int i=0;i<no-1;i++) {
System.out.print("\t"+a[i]);
}
}
}
Output:
12]
Sort
the
13] Accept an array of 10 floats from user and find the largest number among them.
Code:
import java.util.*;
class floatlarge {
public static void main(String args[]) {
float temp;
float f[]=new float[10];
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the 10 float numbers : ");
for(int i=0;i<10;i++) {
f[i]=obj.nextFloat();
}
for(int i=0;i<10;i++) {
for(int j=i+1;j<10;j++) {
if(f[i]>f[j]) {
temp=f[i];
f[i]=f[j];
f[j]=temp;
}
}
}
System.out.print("\nLargest number : "+f[9]);
}
}
Output:
14]WAP to accept a number form user and convey it into binary, hexadecimal and octal
format and print it.
Code:
import java.util.*;
class bohequi {
public static void main(String args[]) {
int no;
Scanner obj = new Scanner(System.in);
System.out.print("\nEnter the number : ");
no=obj.nextInt();
System.out.print("\nBinary Number : "+Integer.toBinaryString(no));
System.out.print("\nOctal Number : "+Integer.toOctalString(no));
System.out.print("\nHexa-decimal Number : "+Integer.toHexString(no));
}
}
Output:
15]Accept first name, middle name and surname form keyboard form keyboard and
display the while in sequence as a single string.
Code:
import java.util.*;
class strcat {
public static void main(String args[]) {
String f;
String m;
String l;
String s1,s2;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the first name : ");
f=obj.next();
System.out.print("\nEnter the middle name : ");
m=obj.next();
System.out.print("\nEnter the last name : ");
l=obj.next();
s1=f.concat(m);
s2=s1.concat(l);
System.out.print("\nFull name : "+s2);
}
}
Output:
16] Accept the string containing all types of characters and symbols from user and count
only number of digits.
Code:
import java.util.*;
import java.io.*;
class digitinstr {
public static void main(String args[])throws Exception {
int count=0;
String s;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("\nEnter the String : ");
s=br.readLine();
StringBuffer sb=new StringBuffer(s);
int l=s.length();
char ch[]=new char[l];
ch=s.toCharArray();
for(int i=0;i<l;i++) {
if(Character.isDigit(ch[i])) {
count++;
}
}
System.out.print("\nTotal number of ditits in string : "+count);
}
}
Output:
17] Perform following string/string buffer operations, write Java program:
Code:
import java.lang.*;
import java.util.*;
import java.io.*;
class pass {
public static void main(String args[])throws Exception {
int id;
String p,p1;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the user id : ");
id=obj.nextInt();
System.out.print("\nEnter the password : ");
p=obj.next();
System.out.println("\n==========");
System.out.println("Checking password ");
System.out.println("==========");
System.out.print("\nEnter the correct password : ");
p1=obj.next();
if(p.equals(p1)) {
System.out.print("\nGood");
System.out.println("\n==========");
System.out.println("Revrsing Password");
System.out.println("==========");
StringBuffer br=new StringBuffer(p);
System.out.print(br.reverse()+"\n");
System.out.println("\n==========");
System.out.println("Appending password with welcome");
System.out.println("==========");
StringBuffer br1=new StringBuffer(" Welcome");
System.out.println(br1.append(p));
}
else {
System.out.print("Wrong");
}
}
}
Output:
18]Write a program to implement a vector class and its methods for adding and
removing elements. After removing display remaining list.
Code:
import java.lang.*;
import java.util.*;
class add_remove_vector1 {
public static void main(String args[]) {
Vector v=new Vector();
v.addElement(8);
v.addElement("raj debadwar");
v.addElement("SYCO");
System.out.println("Vector : "+v);
v.insertElementAt("SVERI",2);
System.out.println("Vector : "+v);
v.removeElementAt(3);
System.out.println("Vector : "+v);
}
}
Output:
19] Input a number from command line and find its binary, octal and hexadecimal
equivalent.
Code:
import java.util.*;
class bohequi1 {
public static void main(String args[]) {
int no=Integer.parseInt(args[0]);
System.out.print("\nBinary Number : "+Integer.toBinaryString(no));
System.out.print("\nOctal Number : "+Integer.toOctalString(no));
System.out.print("\nHexa-decimal Number : "+Integer.toHexString(no));
}
}
20]Write a program to create a vector with seven elements as (10, 30, 50, 20, 40, 10,
20). Remove element at 3rd and 4th position. Insert new element at 3rd position.
Display the original and current size of the vector.
Code:
import java.util.*;
import java.lang.*;
class vectoroperaton {
public static void main(String args[]) {
Vector v=new Vector();
v.addElement(10);
v.addElement(30);
v.addElement(50);
v.addElement(20);
v.addElement(40);
v.addElement(10);
v.addElement(20);
System.out.print("\nVector : "+v);
v.removeElementAt(3);
v.removeElementAt(4);
System.out.print("\nVector after deleting elements: "+v);
v.insertElementAt(10,3);
System.out.print("\nVector agter adding element at 3rd position : "+v);
}
}
Output:
Code:
import java.lang.*;
import java.util.*;
class palestr {
public static void main(String args[]) {
String s;
String r="";
Scanner obj=new Scanner(System.in);
System.out.print("Enter the string : ");
s=obj.next();
for(int i=s.length()-1;i>=0;i--)
{
r=r+s.charAt(i);
}
if(s.equals(r))
{
System.out.println(s+" string is palendrome");
}
else
{
System.out.println(s+" string is not palendrome");
}
}
}
Output:
22] Write a program to add 2 integer,2 string and 2 float objects top a vector. Remove
elements specified by user and display the list.
Code:
import java.lang.*;
import java.util.*;
class vectorrr {
public static void main(String args[]) {
int r;
Vector v=new Vector();
v.addElement(10);
v.addElement(20);
v.addElement("PICT");
v.addElement("COEP");
v.addElement(99.9f);
v.addElement(99.8f);
System.out.print("Vector : "+v);
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the position of element to be removed : ");
r=obj.nextInt();
v.removeElementAt(r);
System.out.print("\nVector after deleting element : "+v);
}
}
Output:
Code:
import java.lang.*;
import java.util.*;
class stringoper {
public static void main(String args[]) {
String s,s1;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the lower case string : ");
s=obj.next();
System.out.print("\nUpper case string : "+s.toUpperCase());
System.out.print("\nLength of given string : "+s.length());
System.out.print("\nEnter the second string for comparison : ");
s1=obj.next();
if(s.compareTo(s1)>0)
System.out.print("\nFirst string is greater");
else
System.out.print("\nSecond string is greater");
}
}
Output:
24] WAP in java to read 10 strings from console and then print the stored string on the
console.(Use string class)
Code:
import java.lang.*;
import java.util.*;
class cmdstr
{
public static void main(String args[])
{
String r[]=new String[10];
for(int i=0;i<10;i++)
r[i]=args[i];
System.out.println("10 Strings entered bye user are : ");
for(int i=0;i<10;i++)
System.out.println(r[i]);
}
}
Output:
25] Define a class employee with data members as emp_id and salary. Accept data for 5
objects and print it.
Code:
import java.lang.*;
import java.util.*;
class Employee1 {
int emp_id;
float salary;
void accept() {
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the employee id : ");
emp_id=obj.nextInt();
System.out.print("\nEnter the employee salary : ");
salary=obj.nextFloat();
}
void display() {
System.out.print("\nEmployee id : "+emp_id);
System.out.print("\nEmployee salary : "+salary);
}
public static void main(String args[]) {
Employee1 e[]=new Employee1[5];
for(int i=0;i<5;i++)
e[i]=new Employee1();
for(int i=0;i<5;i++)
e[i].accept();
for(int i=0;i<5;i++)
e[i].display();
}
}
Output:
26] WAP to transpose a matrix.
Code:
import java.lang.*;
import java.util.*;
class transpose_matrix {
public static void main(String args[]) {
int s[][]=new int[3][3];
int r[][]=new int[3][3];
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the matrix : ");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
s[i][j]=obj.nextInt();
}
}
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
r[j][i]=s[i][j];
}
}
System.out.print("\nOriginal matrix : \n");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
System.out.print("\t"+s[i][j]);
}
System.out.print("\n");
}
Code:
import java.lang.*;
import java.util.*;
class aplhaorder {
String s1;
s1=obj.next();
carr=s1.toCharArray();
Arrays.sort(carr);
Output: