0% found this document useful (0 votes)
82 views41 pages

Java Programs: Chapter 2:derived Syntactical Constructs in Java

The document contains 9 Java programs. Program 1 creates a class Account with methods to deposit and withdraw from an account. Program 2 defines a class item with data members code and price. Program 3 accepts a number as a command line argument and checks if it is even or odd. Program 4 accepts two numbers as command line arguments and prints their sum. Program 5 accepts data for 5 Item objects and displays them in a table along with the total price. Programs 6 and 7 add and subtract 3x3 matrices respectively. Program 8 searches for an element in an unsorted array. Program 9 inserts an element into an array at a given position.

Uploaded by

Raj Debadwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views41 pages

Java Programs: Chapter 2:derived Syntactical Constructs in Java

The document contains 9 Java programs. Program 1 creates a class Account with methods to deposit and withdraw from an account. Program 2 defines a class item with data members code and price. Program 3 accepts a number as a command line argument and checks if it is even or odd. Program 4 accepts two numbers as command line arguments and prints their sum. Program 5 accepts data for 5 Item objects and displays them in a table along with the total price. Programs 6 and 7 add and subtract 3x3 matrices respectively. Program 8 searches for an element in an unsorted array. Program 9 inserts an element into an array at a given position.

Uploaded by

Raj Debadwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 41

Java Programs

Chapter 2:Derived Syntactical Constructs in Java


1] Write a program to create a class Account having variable accno, accname and
balance. Define deposit () and withdraw() methods. Create one object of class and
perform the operation.

Code:

import java.util.*;

class account {

Scanner obj=new Scanner(System.in);

void deposite(int tb) {

int depAmo;

int tprice1=tb;

System.out.print("\nEnter the amount to be deposited : ");

depAmo=obj.nextInt();

tprice1=tprice1+depAmo;

System.out.print("Total balance = "+tprice1);

void withdrawl(int b) {

int wAmo;

int tprice=b;

System.out.print("\nEnter the amount to be withdrawl : ");

wAmo=obj.nextInt();

tprice=tprice-wAmo;
System.out.print("\nTotal balance : "+tprice);

public static void main(String arg[]) {

int accno;

String accname;

int ch;

int bal;

account o=new account();

Scanner obj1=new Scanner(System.in);

System.out.print("\nEnter the account no : ");

accno=obj1.nextInt();

System.out.print("\nEnter the account name : ");

accname=obj1.next();

System.out.print("\nEnter the account balance : ");

bal=obj1.nextInt();

do {

System.out.print("\nEnter your preference : \n1.Deposite\


n2.Withdrawl\n");

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() {

Scanner obj=new Scanner(System.in);

System.out.print("\nEnter the code of the item : ");

code=obj.nextInt();

System.out.print("\nEnter the price of the item : ");

price=obj.nextInt();

void display() {

System.out.print("\nItem code : "+code);

System.out.print("\nItem price : "+price);

public static void main(String arg[]) {

item r=new item();

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 {

public static void main(String arg[]) {

int no=Integer.parseInt(arg[0]);

if(no%2==0) {

System.out.print("\nEntered number "+no+" an even number");

else {

System.out.print("\nEntered number "+no+" an odd number");

}
}

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 {

public static void main(String arg[]) {

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;

Scanner obj1=new Scanner(System.in);

void accept() {

//Scanner obj=new Scanner(System.in);


System.out.print("Enter the item code : ");

code=obj1.nextInt();

System.out.print("\nEnter the item price : ");

price=obj1.nextInt();

p=p+price;

void display() {

System.out.println(code+"\t"+price);

public static void main(String arg[]) {

Itemobj o[]=new Itemobj[5];

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();

System.out.println("\nTotal price : "+p );

Output:
6] WAP to add two 3X3 matrices.

Code:

import java.util.*;

class matrix {

public static void main(String args[]) {

int i,j;

int a[][]=new int[3][3];


int b[][]=new int[3][3];

int c[][]=new int[3][3];

Scanner obj=new Scanner(System.in);

System.out.print("\nEnter the first matrix : ");

for(i=0;i<3;i++) {

for(j=0;j<3;j++) {

a[i][j]=obj.nextInt();

System.out.print("\nEnter the second matrix : ");

for(i=0;i<3;i++) {

for(j=0;j<3;j++) {

b[i][j]=obj.nextInt();

System.out.print("First matrix : \n");

for(i=0;i<3;i++) {

for(j=0;j<3;j++) {

System.out.print("\t"+a[i][j]);

System.out.print("\n");

System.out.print("Second matrix : \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("\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;

int a[][]=new int[3][3];

int b[][]=new int[3][3];

int c[][]=new int[3][3];

Scanner obj=new Scanner(System.in);

System.out.print("\nEnter the first matrix : ");

for(i=0;i<3;i++) {

for(j=0;j<3;j++) {

a[i][j]=obj.nextInt();

System.out.print("\nEnter the second matrix : ");

for(i=0;i<3;i++) {

for(j=0;j<3;j++) {

b[i][j]=obj.nextInt();

System.out.print("First matrix : \n");

for(i=0;i<3;i++) {

for(j=0;j<3;j++) {

System.out.print("\t"+a[i][j]);

}
System.out.print("\n");

System.out.print("Second matrix : \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:

9. WAP to Insert element into array.

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

characters from string inputted from user.


Code:
import java.util.*;
class strrev
{
public static void main(String args[])
{
String a;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the string to be reversed : ");
a=obj.next();
StringBuffer obj1=new StringBuffer(a);
System.out.print("\nReverse string : "+obj1.reverse());
}
}
Output:

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:

A) Accept a password from user.

B) Check if password id correct then display “Good ”    else, display “Wrong”.

C) Display the password to reverse order.

D) Append password with “Welcome”.

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:

21]WAP to check whether string is palindrome or not.

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:

23] Write a program to demonstrate the following:

A) To convert lowercase string to upper case

B) To calculate the length of given string

C) To compare two strings

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");
}

System.out.print("\nTranspose matrix    : \n");


for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
System.out.print("\t"+r[i][j]);
}
System.out.print("\n");
}
}
}
Output:
27] WAP which will read string and receive it in the alphabetical order for example the
word “Java” should be written as “aaJv”(Ignore the case of the letter).

Code:

import java.lang.*;

import java.util.*;

class aplhaorder {

public static void main(String args[]) {

String s1;

Scanner obj=new Scanner(System.in);

System.out.print("\nEnter the string : ");

s1=obj.next();

char carr[]=new char[50];

carr=s1.toCharArray();

Arrays.sort(carr);

String s2=new String(carr);

System.out.print("\nString in particular alphabetical order : "+s2);

Output:

You might also like