Page 1
Java Assignment
ASSIGMENT-1
//Write an Application program to print your name and
address.
class Ex1
{
public static void main(String args[])
{
System.out.println("Bhavin Modi");
System.out.print("Ankleshwar");
}
}
ASSIGMENT-2
/**
Write an Application program to find two matrix
multiplication.
1 2 3 1 2 3 = 1 4 9
4 5 0 * 1 2 3 = 4 10 0
6 7 8 1 2 3 = 6 14 24.
*/
Page 2
Java Assignment
import java.io.*;
class arr2mul
{
public static void main(String args[])
{
int i=0,j=0;
DataInputStream in = new
DataInputStream(System.in);
try
{
int n[][] = new int[3][3];
int m[][] = new int[3][3];
int mul[][] = new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
Page 3
Java Assignment
System.out.print("Enter n[" + i + "][" + j + "]: ");
n[i][j] = Integer.parseInt(in.readLine());
}
}
System.out.println();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print("Enter m[" + i + "][" + j + "]: ");
m[i][j] = Integer.parseInt(in.readLine());
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mul[i][j] = n[i][j] * m[i][j];
Page 4
Java Assignment
}
}
System.out.println();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.println(n[i][j] + " * " + m[i][j] + " = " +
mul[i][j]);
}
System.out.println();
}
}
catch (Exception e){}
}
}
ASSIGMENT-3
Page 5
Java Assignment
/*
Write an Application program to find area and perimeter
of a rectangle.
Note: define static methods.
Area Return l*b;
Perimeter Return 2*(l+b);
*/
import java.io.*;
import java.lang.*;
class fun
{
static float area(float x,float y)
{
return (x * y);
}
static float perimeter(float x,float y)
{
Page 6
Java Assignment
return (2 * (x + y));
}
}
class rect
{
public static void main(String args[])
{
DataInputStream in = new
DataInputStream(System.in);
float l=0,b=0;
try
{
System.out.flush();
System.out.print("Enter Length: ");
l = Float.valueOf(in.readLine());
System.out.print("Enter Breadth: ");
b = Float.valueOf(in.readLine());
Page 7
Java Assignment
}
catch (Exception e)
{
System.out.print("IO Error");
}
System.out.println();
System.out.println("Area of a rectangle = " +
fun.area(l,b));
System.out.println("Perimeter of a rectangle = " +
fun.perimeter(l,b));
}
}
ASSIGMENT-4
/*
Page 8
Java Assignment
Write a program for Hardware company. Ask the user to
choose F for Floppy, C for CD, P for Pen drive.
Show the price of each item.
Show price of a hardware manufactured with the chosen
wood.
Floppy price is 15 Rs., CD price is 20 Rs and Pen drive
price is 1250 Rs.
The class name is Hardware.
*/
class Hardware
{
public static void main(String args[])
{
char ch;
System.out.println("F for Floppy");
System.out.println("C for CD");
System.out.println("P for Pen Drive");
System.out.flush();
System.out.print("Select your choice: ");
Page 9
Java Assignment
try
{
switch(ch =(char)System.in.read())
{
case 'F':
case 'f':
System.out.print("Floppy price is 15 Rs.");
break;
case 'C':
case 'c':
System.out.print("CD price is 20 Rs.");
break;
case 'P':
case 'p':
System.out.print("Pen Drive price is 1250 Rs.");
break;
default:
System.out.print("Wrong choice");
}
Page 10
Java Assignment
}
catch (Exception e)
{
System.out.print("I/O Error");
}
}
}
ASSIGMENT-5
/*
Write a menu driven an Application program.
1.Check for positive or negative number
2.Check for odd or even number
3.Check for primary number
4.Check for Palindrome number
5.Check for Armstrong number
6.Check for number whether a member of fibonacci series
7.Exit
Page 11
Java Assignment
Create abstract methods for every check into abstract
Proto1 class and
define into Defi1 class and call from Menu1 class.
*/
import java.io.*;
abstract class Proto1
{
abstract void disp();
abstract void pn(int x);
abstract void oe(int x);
abstract void prime(int x);
// abstract void palin(int x);
abstract void arms(int x);
abstract void fibo(int x);
abstract void exit();
}
class Defi1 extends Proto1
Page 12
Java Assignment
{
int n = 0;
void disp()
{
System.out.println("1.Check for positive or negative
number ");
System.out.println("2.Check for odd or even
number");
System.out.println("3.Check for primary number");
System.out.println("4.Check for Palindrome
number");
System.out.println("5.Check for Armstrong
number");
System.out.println("6.Check for number whether a
member of fibonacci series ");
System.out.println("7.Exit");
System.out.println();
}
void pn(int x)
{
n = x;
Page 13
Java Assignment
if ( n > 0)
System.out.print("Number is Positive");
else
System.out.print("Number is Negative");
}
void oe(int x)
{
n = x;
if ((n%2) == 1)
System.out.print("Number is Odd");
else
System.out.print("Number is Even");
}
void prime(int x)
{
int i=0,c=0;
n = x;
Page 14
Java Assignment
for(i=1;i<=n;i++)
{
if (n%i == 0)
c++;
}
if (c==0)
System.out.print("primary no is " + n);
else
System.out.print("Not primary no");
}
void arms(int x)
{
n = x;
int a,b,c,sum=0;
a = n/100;
b = (n-(a*100))/10;
c = (n-(a*100))-(b*10);
Page 15
Java Assignment
sum = (a*a*a) + (b*b*b) + (c*c*c);
if (sum == n)
System.out.print("Armstrong no is " + sum);
else
System.out.print("Not Armstrong no");
}
void fibo(int x)
{
n = x;
int n1=1,n2=0,sum=0;
System.out.print("Fibonacci series is ");
while(n>0)
{
sum = n1+n2;
if (x==sum)
System.out.print("fibo");
n1 = n2;
n2 = sum;
Page 16
Java Assignment
n--;
System.out.print(sum + " ");
}
}
void exit()
{
System.out.print("Exit");
}
}
class Menu1
{
public static void main(String args[]) throws
IOException
{
DataInputStream in = new
DataInputStream(System.in);
Defi1 d1=new Defi1();
String s = new String();
Page 17
Java Assignment
do
{
d1.disp();
System.out.print("Select your choice: ");
int ch = Integer.parseInt(in.readLine());
try
{
System.out.print("Enter Number: ");
int n = Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
d1.pn(n);
break;
case 2:
d1.oe(n);
break;
Page 18
Java Assignment
case 3:
d1.prime(n);
break;
case 5:
d1.arms(n);
break;
case 6:
d1.fibo(n);
break;
case 7:
d1.exit();
break;
default:
System.out.print("Wrong choice");
}
}
catch (Exception e)
{
System.out.print("I/O Error");
Page 19
Java Assignment
}
System.out.println();
System.out.print("\t\tDo want to Continue (y/n)?: ");
s = in.readLine();
}while ((s.charAt(0) == 'Y') || (s.charAt(0) == 'y'));
}
}
ASSIGMENT-6
/**
Create an abstract class Auto with fields for the car maker
and price.
Include get and set methods for these fields;
the setPrice() method is an abstract.
Create two subclasses for individual automobile makers
and
include appropriate setPrice() methods in each subclass.
Finally, write a program that uses the Auto class and
subclasses
to display information about different cars.
Page 20
Java Assignment
*/
import java.io.*;
abstract class Auto
{
abstract void setprice(int x);
}
class maruti extends Auto
{
void get()
{
System.out.println("Car Maker is Maruti");
}
void setprice(int x)
{
System.out.println("Car Price is " + x);
}
}
class honda extends Auto
Page 21
Java Assignment
{
void get()
{
System.out.println("Car Maker is Honda");
}
void setprice(int x)
{
System.out.println("Car Price is " + x);
}
}
class car
{
public static void main(String args[]) throws Exception
{
DataInputStream in = new
DataInputStream(System.in);
System.out.print("Enter Car Price: ");
int cp = Integer.parseInt(in.readLine());
Page 22
Java Assignment
maruti m = new maruti();
m.get();
m.setprice(cp);
honda h = new honda();
h.get();
h.setprice(cp);
}
}
ASSIGNMENT-7
/**
Create a class named Square that contains data fields for
height, width,
and surfaceArea, and a method named
computeSurfaceArea().
Create child class Cube.
Cube contains an additional data field named depth,
and a computeSurfaceArea() method that overrides the
parent method.
Page 23
Java Assignment
Write a program that instantiate a Square object and a
Cube object and
display the surface areas of the objects.
*/
import java.io.*;
class square
{
int height=0,width=0,surfaceArea=0;
square(int x,int y)
{
height=x;
width=y;
}
int sfa()
{
surfaceArea=height * width;
return surfaceArea;
}
Page 24
Java Assignment
}
class Cube extends square
{
int depth=0;
Cube(int x,int y,int z)
{
super(x,y);
depth=z;
}
int sfa()
{
return (height * width * depth);
}
}
class surfacearea
{
public static void main(String args[])
{
Page 25
Java Assignment
int h=0,w=0,d=0;
DataInputStream in = new
DataInputStream(System.in);
try
{
System.out.print("Enter height: ");
h = Integer.parseInt(in.readLine());
System.out.print("Enter width: ");
w = Integer.parseInt(in.readLine());
System.out.print("Enter depth: ");
d = Integer.parseInt(in.readLine());
}
catch (Exception e){}
square s = new square(h,w);
Cube c = new Cube(h,w,d);
System.out.println("Square SurfaceArea is " + s.sfa());
System.out.println("Cube SurfaceArea is " + c.sfa());
Page 26
Java Assignment
}
}
ASSIGNMENT-8
/**
Write a simple java program that give the list of valid and
invalid numbers
using command line arguments.
Input : 112 java 23.2 3434
Output: valid numbers:2 invalid numbers :2
*/
class cmdvi
{
public static void main(String args[])
{
int invalid=0,valid=0,n;
for(int i=0;i<args.length;i++)
{
Page 27
Java Assignment
try
{
n = Integer.parseInt(args[i]);
}
catch(Exception e)
{
invalid++;
continue;
}
valid++;
}
System.out.println("Valid Number: " + valid);
System.out.println("Invalid Number: " + invalid);
}
}
ASSIGNMENT-9
/**
Create 1integer array of size 10 and initialize the array
with some values.
Page 28
Java Assignment
Write a program to arrange the entire array into its
ascending order and
display the sorted array.
*/
import java.io.*;
class ascending
{
public static void main(String args[])
{
int i=0,j=0,swap=0;
DataInputStream in = new
DataInputStream(System.in);
try
{
int n[] = new int[10];
for(i=0;i<10;i++)
{
System.out.print("Enter n[" + i + "]: ");
n[i] = Integer.parseInt(in.readLine());
Page 29
Java Assignment
}
System.out.println();
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if (n[i] < n[j])
{
swap = n[i];
n[i] = n[j];
n[j] = swap;
}
}
}
System.out.print("Ascending order is ");
for(i=0;i<10;i++)
{
Page 30
Java Assignment
System.out.print(n[i] + " ");
}
}
catch (Exception e){}
}
}
ASSIGNMENT-10
/**
Write an Application program to find matrix
multiplication table
for example.
1 2 3
------------
1 | 1 2 3
2 | 2 4 6
3 | 3 6 9
Note: use command-line arguments(R C)
*/
Page 31
Java Assignment
class multitable
{
public static void main(String args[])
{
int i=0,j=0;
int r[] = new int[3];
int c[] = new int[3];
int m[][] = new int[3][3];
for(i=0;i<3;i++)
{
r[i]=Integer.parseInt(args[i]);
}
for(j=0;i<6;i++,j++)
{
c[j]=Integer.parseInt(args[i]);
}
for(i=0;i<3;i++)
Page 32
Java Assignment
{
for(j=0;j<3;j++)
{
m[i][j] = r[i] * c[j];
}
}
System.out.print(" ");
for(i=0;i<3;i++)
{
System.out.print(r[i] + " ");
}
System.out.println();
System.out.print(" ------");
System.out.println();
for(i=0;i<3;i++)
{
System.out.print(c[i] + "| ");
for(j=0;j<3;j++)
{
Page 33
Java Assignment
System.out.print(m[i][j] + " ");
}
System.out.println();
}
}
}
ASSIGNMENT-11
/**
Write menu driven an Application program for following
string manipulation.
Reverse a string.
Sorting (using a string)
Conversion from upper to lower and vice-versa.
Exit.
*/
import java.io.*;
import java.lang.*;
Page 34
Java Assignment
class fun
{
String s,s1 = new String();
int i,j;
char temp;
void rev(String s)
{
System.out.print("Reverse String is ");
for(i=s.length()-1;i>=0;i--)
{
System.out.print(s.charAt(i));
}
}
void sort(String s)
{
char s2[] = new char[s.length()];
System.out.print("Sorted String is ");
Page 35
Java Assignment
for(i=0;i<s.length();i++)
{
s2[i] = s.charAt(i);
}
for(i=0;i<s.length();i++)
{
for(j=i+1;j<s.length();j++)
{
if(s2[j].compareTo(s2[i]) < 0)
{
temp = s2[i];
s2[i] = s2[j];
s2[j] = temp;
}
}
}
System.out.print(s2);
}
Page 36
Java Assignment
void uc(String s)
{
System.out.println("Upper String is " +
s.toUpperCase());
}
void lc(String s)
{
System.out.println("Lower String is " +
s.toLowerCase());
}
void exit()
{
System.out.println("Exit");
}
}
class strfun
{
Page 37
Java Assignment
public static void main(String args[]) throws
IOException
{
DataInputStream in = new
DataInputStream(System.in);
int ch;
fun f = new fun();
String s = new String();
System.out.println("1.Reverse a string");
System.out.println("2.Sorting a string");
System.out.println("3.Conversion from upper to
lower");
System.out.println("4.Conversion from lower to
upper");
System.out.println("5.Exit");
System.out.println();
System.out.print("Select your choice: ");
try
{
Page 38
Java Assignment
System.out.flush();
ch = Integer.parseInt(in.readLine());
System.out.print("Enter String: ");
s = in.readLine();
switch(ch)
{
case 1:
f.rev(s);
break;
case 2:
f.sort(s);
break;
case 3:
f.lc(s);
break;
case 4:
f.uc(s);
break;
Page 39
Java Assignment
case 5:
f.exit();
break;
default:
System.out.print("Wrong choice");
}
}
catch (Exception e)
{
System.out.print("I/O Error");
}
}
}
ASSIGNMENT-12
/**
You are given a sting str ="sardar patel university".
Page 40
Java Assignment
Perform the following operation on it.
a. find the length of string
b. replace the character p' by 'r'
c. convert all character in uppercase
extract and print "sardar" from given string.
*/
import java.io.*;
import java.lang.*;
class fun
{
String s = new String("sardar patel university");
void len()
{
System.out.print("Length of String is " + s.length());
}
void rep()
{
Page 41
Java Assignment
System.out.print("Replaced String is " +
s.replace('p','r'));
}
void uc()
{
System.out.println("Upper String is " +
s.toUpperCase());
}
void ext()
{
System.out.println("Extract String is " +
s.substring(0,6));
}
void exit()
{
System.out.println("Exit");
}
}
Page 42
Java Assignment
class strfun
{
public static void main(String args[]) throws
IOException
{
DataInputStream in = new
DataInputStream(System.in);
fun f = new fun();
System.out.println();
System.out.println("a. find the length of string");
System.out.println("b. replace the character p' by 'r'");
System.out.println("c. convert all character in
uppercase");
System.out.println("d. extract and print " + '\"' +
"sardar" + '\"' + " from given string.");
System.out.println("e. Exit");
System.out.println();
System.out.print("Select your choice: ");
Page 43
Java Assignment
try
{
char ch;
switch(ch = (char) System.in.read())
{
case 'a':
f.len();
break;
case 'b':
f.rep();
break;
case 'c':
f.uc();
break;
case 'd':
f.ext();
break;
case 'e':
f.exit();
Page 44
Java Assignment
break;
default:
System.out.print("Wrong choice");
}
}
catch (Exception e)
{
System.out.print("I/O Error");
}
}
}
ASSIGNMENT-13
/**
Write an Application program that search through its
command-line arguments
if an argument is found that does not begin with an
Uppercase letter
display an error message and terminate.
*/
Page 45
Java Assignment
class search
{
public static void main(String args[])
{
int c = args[0].length(),n;
String str = args[0];
char ch;
try
{
for( int i=0;i<c;i++)
{
ch = str.charAt(i);
if (ch >= 65 && ch <= 90)
break;
else
System.out.print (ch);
}
Page 46
Java Assignment
}
catch(Exception e)
{
System.out.print("IO Error");
}
}
}
ASSIGNMENT-14
/**
Write an Application program to find maximum number
from two numbers of
any data type using command-line arguments.Note:
Using the concept of method overloading.
*/
class fun
{
void max(int a,int b)
{
Page 47
Java Assignment
if(a>b)
System.out.println(a+ " is greater");
else
System.out.println(b+ " is greater");
}
void max(double a,double b)
{
if(a>b)
System.out.println(a+ " is greater");
else
System.out.println(b+ " is greater");
}
void max(char a,char b)
{
if(a>b)
System.out.println("\'"+a+"\'"+ " is greater");
else
System.out.println("\'"+b+"\'"+ " is greater");
Page 48
Java Assignment
}
}
class cmdmax
{
public static void main(String args[])
{
fun f = new fun();
try
{
if(args[0].indexOf('.')>=0 &&
args[0].indexOf('.')<=32000)
{
double t=Double.parseDouble(args[0]);
double t1=Double.parseDouble(args[1]);
f.max(t,t1);
}
else
{
Page 49
Java Assignment
int s=Integer.parseInt(args[0]);
int s1=Integer.parseInt(args[1]);
f.max(s,s1);
}
}
catch(NumberFormatException e)
{
char c=args[0].charAt(0);
char c2=args[1].charAt(0);
f.max(c,c2);
}
}
}
ASSIGNMENT-15
/**
Write an Application program and create one class that
accepts
Page 50
Java Assignment
an array of ten numbers
create one subclass which has following,
- Display numbers entered
- Sum of the numbers
- Average of numbers
- Maximum of numbers
- Minimum of numbers
- Exit
Create appropriate methods in the subclass to execute
operation
as per our choice.
Note: use super keyword.
*/
import java.io.*;
class operation
{
public static void main(String args[]) throws
IOException
{
Page 51
Java Assignment
DataInputStream in = new
DataInputStream(System.in);
System.out.println();
System.out.println("1. Display numbers entered");
System.out.println("2. Sum of the numbers");
System.out.println("3. Average of numbers");
System.out.println("4. Maximum of numbers");
System.out.println("5. Minimum of numbers");
System.out.println("6. Exit");
System.out.println();
System.out.print("Enter Choice: ");
int ch = Integer.parseInt(in.readLine());
fun f = new fun();
System.out.println();
switch(ch)
Page 52
Java Assignment
{
case 1:
f.disp();
break;
case 2:
f.sum();
break;
case 3:
f.avg();
break;
case 4:
f.max();
break;
case 5:
f.min();
break;
case 6:
f.exit();
break;
default:
Page 53
Java Assignment
System.out.print("Wrong choice");
}
}
}
class read
{
DataInputStream in = new
DataInputStream(System.in);
int a[] = new int[10];
read()
{
try
{
for(int i=0;i<10;i++)
{
System.out.print("Enter a[" + (i+1) + "]: ");
a[i] = Integer.parseInt(in.readLine());
}
Page 54
Java Assignment
}
catch(Exception e){}
}
}
class fun extends read
{
fun()
{
super();
}
int s=0;
void disp()
{
System.out.print("Array is ");
for (int i=0;i<10;i++)
{
Page 55
Java Assignment
System.out.print(a[i] + " ");
}
}
void sum()
{
for (int i=0;i<10;i++)
{
s += a[i];
}
System.out.print("Sum of Array is " + s);
}
void avg()
{
for (int i=0;i<10;i++)
{
s += a[i];
}
System.out.print("Average of Array is " + (s/10));
Page 56
Java Assignment
}
void max()
{
int m = a[0];
for (int i=1;i<10;i++)
{
if (m < a[i])
m = a[i];
}
System.out.print("Maximum Value of Array is " + m);
}
void min()
{
int m = a[9];
for (int i=0;i<10;i++)
{
if (m > a[i])
m = a[i];
Page 57
Java Assignment
}
System.out.print("Minimum Value of Array is " + m);
}
void exit()
{
System.out.print("Exit");
}
}
ASSIGNMENT-16
/**
5 candidates contest an election.
The candidates are numbered 1 to 5 and making the
candidate number on
the ballot paper does the voting.
Write a program to read the ballots and count the votes
cast candidate
using an array variable count.
In case, a number read is outside the range 1 to 5,
the ballot should be considered as a 'spoilt ballot' and
Page 58
Java Assignment
the program should also count the number of spoilt
ballots.
*/
import java.io.*;
class election
{
public static void main(String args[]) throws
IOException
{
DataInputStream in = new
DataInputStream(System.in);
int i,n,count=0,spoilt=0;
System.out.print("Enter number of ballots: ");
n = Integer.parseInt(in.readLine());
System.out.println();
int a[] = new int[n];
for(i=0;i<n;i++)
Page 59
Java Assignment
{
System.out.print("Enter Vote: ");
a[i] = Integer.parseInt(in.readLine());
if( a[i] == 1 || a[i] == 2 || a[i] == 3 || a[i] == 4 || a[i] ==
5)
count++;
else
{
System.out.println("\t\tOutside the range");
spoilt++;
}
}
System.out.println("bollte paper: " + count);
System.out.println("spoilt parep: " + spoilt);
}
}
ASSIENMENT-17
Page 60
Java Assignment
/**
Write an application program to count occurrence of
particular
character in entered String.
*/
import java.io.*;
import java.lang.*;
class count
{
public static void main(String args[]) throws
IOException
{
DataInputStream in = new
DataInputStream(System.in);
String s = new String();
char ch;
int c=0;
System.out.print("Enter String: ");
Page 61
Java Assignment
s = in.readLine();
System.out.print("Enter Character: ");
ch = (char) System.in.read();
for(int i=0;i<s.length();i++)
{
if (ch == s.charAt(i))
c++;
}
System.out.println();
System.out.println("occurrence of character is " + c);
}
}
ASSIGNMENT-18(1)
import java.io.*;
import java.lang.*;
class info
Page 62
Java Assignment
{
int no;
String name;
void getdata(int n,String nm)
{
no = n;
name = nm;
}
void display()
{
System.out.println("Student No: " + no );
System.out.println("Student Name: " + name);
}
}
class stud
{
public static void main(String args[]) throws
IOException
Page 63
Java Assignment
{
DataInputStream in = new
DataInputStream(System.in);
int std_no;
String std_name;
info st[] = new info[10];
try
{
for(int i=0;i<2;i++)
{
System.out.print("Student No: ");
std_no = Integer.parseInt(in.readLine());
System.out.print("Student Name: ");
std_name = in.readLine();
System.out.println();
st[i] = new info();
Page 64
Java Assignment
st[i].getdata(std_no,std_name);
}
}
catch (Exception e){}
for(int i=0;i<2;i++)
{
st[i].display();
}
}
}
ASSIGNMENT-18(2)
/**
Write a menu driven an Application program to do
following.
Member variable:
std_no,std_name,std_sub1,std_sub2,std_sub3,total,per
1. New Student Entry
2. Calculate student result
Page 65
Java Assignment
3. Display specified student's formatted Mark sheet
4. Delete Student Entry
5. Modify the Student info
6. Exit
Note: use array of objects and read appropriate value on
choice.
*/
import java.io.*;
import java.lang.*;
class info
{
int no,sub1,sub2,sub3;
float total,per;
String name;
void data(int n,String s,int s1,int s2,int s3)
{
no = n;
name = s;
Page 66
Java Assignment
sub1 = s1;
sub2 = s2;
sub3 = s3;
System.out.print("New Entry");
}
void result(int n)
{
if(no == n)
{
total = sub1 + sub2 + sub3;
per = total / 3;
System.out.print("Result calculated");
}
else
{
System.out.print("std_no not exist");
}
}
void display(int n)
Page 67
Java Assignment
{
if ( no == n)
{
System.out.println("Student No: " + no);
System.out.println("Student Name: " + name);
System.out.println("---------------------------");
System.out.println("Student Subject1: " + sub1);
System.out.println("Student Subject2: " + sub2);
System.out.println("Student Subject3: " + sub3);
System.out.println("---------------------------");
System.out.println("total: " + total);
System.out.println("per: " + per);
}
else
{
System.out.print("std_no not exist");
}
}
void del(int n)
{
Page 68
Java Assignment
if(no == n)
{
no = '\0';
System.out.print("Entry Deleted");
}
else
{
System.out.print("std_no not exist");
}
}
}
class student
{
public static void main(String args[]) throws
IOException
{
DataInputStream in = new
DataInputStream(System.in);
int std_no,std_sub1,std_sub2,std_sub3;
Page 69
Java Assignment
String std_name;
String s = new String();
int c = 0,i,j;
info st[] = new info[10];
do
{
System.out.println();
System.out.println("1. New Student Entry");
System.out.println("2. Calculate student result");
System.out.println("3. Display specified student's
formatted Mark sheet");
System.out.println("4. Delete Student Entry");
System.out.println("5. Modify the Student info");
System.out.println("6. Exit");
System.out.println();
System.out.print("Enter Choice: ");
Page 70
Java Assignment
int ch = Integer.parseInt(in.readLine());
System.out.println();
switch(ch)
{
case 1:
{
System.out.print("Student No: ");
std_no = Integer.parseInt(in.readLine());
System.out.print("Student Name: ");
std_name = in.readLine();
System.out.print("Student Subject1: ");
std_sub1 = Integer.parseInt(in.readLine());
System.out.print("Student Subject2: ");
std_sub2 = Integer.parseInt(in.readLine());
System.out.print("Student Subject3: ");
Page 71
Java Assignment
std_sub3 = Integer.parseInt(in.readLine());
st[c] = new info();
st[c].data(std_no,std_name,std_sub1,std_sub2,std_sub3)
;
c++;
break;
}
case 2:
{
System.out.print("Student No: ");
std_no = Integer.parseInt(in.readLine());
System.out.println();
for(i=0;i<c;i++)
{
st[i].result(std_no);
}
break;
}
case 3:
Page 72
Java Assignment
{
System.out.print("Student No: ");
std_no = Integer.parseInt(in.readLine());
System.out.println();
for(i=0;i<c;i++)
{
st[i].display(std_no);
}
break;
}
case 4:
{
System.out.print("Student No: ");
std_no = Integer.parseInt(in.readLine());
System.out.println();
for(i=0;i<c;i++)
{
st[i].del(std_no);
}
break;
Page 73
Java Assignment
}
default:
System.out.print("Wrong choice");
}
System.out.println("\n");
System.out.print("\t\tDo want to Continue (y/n)?:
");
s = in.readLine();
}while ((s.charAt(0) == 'Y') || (s.charAt(0) == 'y'));
}
}
/*
class info
{
int no;
String name;
Page 74
Java Assignment
void getdata(int n,String nm)
{
no = n;
name = nm;
}
void display()
{
System.out.println("Student No: " + no );
System.out.println("Student Name: " + name);
}
}
class stud
{
public static void main(String args[]) throws
IOException
{
DataInputStream in = new
DataInputStream(System.in);
Page 75
Java Assignment
int std_no;
String std_name;
info st[] = new info[10];
try
{
for(int i=0;i<2;i++)
{
System.out.print("Student No: ");
std_no = Integer.parseInt(in.readLine());
System.out.print("Student Name: ");
std_name = in.readLine();
System.out.println();
st[i] = new info();
st[i].getdata(std_no,std_name);
}
}
catch (Exception e){}
Page 76
Java Assignment
for(int i=0;i<2;i++)
{
st[i].display();
}
}
}
*/
ASSIGNMENT-19
/**
Define a class baby with the following attributes.
1. xname
2. date of birth
3. date on which beg injection has to be given (60 days
from date of birth)
4. xdate on which polio drops is to be given (45 days from
date of birth)
Write a constructor to construct the babyobject y.
The constructor must find out bcg and polio drops
dates from the date of birth.
Page 77
Java Assignment
In the main program define a baby and display its details.
*/
import java.io.*;
class babyobject
{
int d,m,y,beg,polio;
String xname = new String();
babyobject()
{
DataInputStream in = new
DataInputStream(System.in);
try
{
System.out.print("Enter Name: ");
xname = in.readLine();
System.out.println("Date of birth");
System.out.print("Enter Day: ");
Page 78
Java Assignment
d = Integer.parseInt(in.readLine());
System.out.print("Enter Month: ");
m = Integer.parseInt(in.readLine());
System.out.print("Enter Year: ");
y = Integer.parseInt(in.readLine());
}
catch (Exception e){}
beg = d + 60 ;
polio = d + 45;
if ( (beg >= 30) || (polio >= 30))
{
m = m + 1;
if(m == 2 && y%4 == 0)
{
beg = beg - 28;
polio = polio - 28;
Page 79
Java Assignment
m = m + 1;
}
else
{
beg = beg - 30;
polio = polio - 30;
}
if ( (beg >= 30) || (polio >= 30))
{
beg = (beg - d) - 30;
polio = polio - d;
}
}
if (m >= 12)
{
m = 1;
y = y + 1;
}
Page 80
Java Assignment
System.out.println();
System.out.println("date on " + beg + "/" + m + "/" + y +
" beg injection has to be given");
System.out.println("date on " + polio + "/"+ m + "/" + y +
" polio drops is to be given");
}
}
class baby
{
public static void main(String args[])
{
babyobject y = new babyobject();
}
}
ASSIGNMENT-20
/**
Write an Application program to generate Employee
Payslip.
Create following classes.
Employee - emp_no,emp_name,basic ,Des
Page 81
Java Assignment
InvalidBasicException - Class for user defined
Exception
(if basic is negative or non numeric).
Payslip - do all additions (da, hra) And
deductions (loan_ins , gpr)
If basic<=5000 Then
hra=5% of basic and da=3% of basic.
If basic>=15000 Then
hra=7.5% of basic and da=5% of basic
Else
hra=10% of basic and da=8% of basic.
Based on that calculate netpay for each employee in
Payslip class and
generate the formatted pay slip.
Note: emp_no should be generated automatically with
prefix EMP,
use array of objects.
*/
Page 82
Java Assignment
import java.io.*;
class employee
{
public static void main(String args[]) throws
IOException
{
DataInputStream in = new
DataInputStream(System.in);
int emp_no[] = new int[100];
String emp_name[] = new String[100];
float basic[] = new float[100];
String Des[] = new String[100];
int c = 0,i,j;
String s = new String();
Payslip p = new Payslip();
Page 83
Java Assignment
do
{
System.out.println();
System.out.print("emp_no: ");
emp_no[c] = Integer.parseInt(in.readLine());
System.out.print("emp_name: ");
emp_name[c] = in.readLine();
System.out.print("basic: ");
basic[c] = Integer.parseInt(in.readLine());
System.out.print("Des: ");
Des[c] = in.readLine();
c++;
System.out.println();
System.out.print("\t\tDo want to Continue (y/n)?: ");
s = in.readLine();
Page 84
Java Assignment
}while ((s.charAt(0) == 'Y') || (s.charAt(0) == 'y'));
do
{
System.out.println();
System.out.print("emp_no: ");
j = Integer.parseInt(in.readLine());
for(i=0;i<c;i++)
{
if( emp_no[i] == j)
{
System.out.println("emp_no: EMP" +
emp_no[i]);
System.out.println("emp_name: " +
emp_name[i]);
System.out.println(" basic: " + basic[i]);
p.netpay(basic[i]);
System.out.println("Des: " + Des[i]);
Page 85
Java Assignment
System.out.println();
}
else
{
System.out.println("emp_no not found");
}
}
System.out.println();
System.out.print("\t\tDo want to Continue (y/n)?: ");
s = in.readLine();
}while ((s.charAt(0) == 'Y') || (s.charAt(0) == 'y'));
}
}
class Payslip
{
void netpay(float x)
{
float basic,da,hra,net;
Page 86
Java Assignment
basic = x;
if (basic <= 5000)
{
hra = (basic * 5)/100;
da = (basic * 3)/100;
}
else if (basic >= 15000)
{
hra = (basic * 75)/1000;
da = (basic * 5)/100;
}
else
{
hra = (basic * 10)/100;
da= (basic * 8)/100;
}
System.out.println(" hra: " + hra);
System.out.println(" da: " + da);
Page 87
Java Assignment
System.out.println("netpay: " + (basic + hra + da));
//return (basic + hra + da);
}
}
ASSIGNMENT-21
/*
Write a simple java program that will catch exception
using multiple catch.
*/
import java.lang.*;
class mulce
{
public static void main(String args[])
{
int a[] = {5,10};
int b = 5;
try
{
Page 88
Java Assignment
int x = a[2] / b - a[1];
}
// catch (Exception e)
// {
// System.out.print("IOException");
// }
catch (ArithmeticException e)
{
System.out.print("Division by Zero");
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.print("Array index error");
}
int y = a[1] /a[0];
System.out.print("y = " + y);
}
}
ASSIGNMENT-22
Page 89
Java Assignment
/**
Write a program that displays an invoice of several items.
It should contain the item name, quantity, price, and total
cost on each
line for the quantity and item cost.
Use two classes. The first class contains the item data and
methods
to get an set the item name, quantity and price.
The other class creates objects for the items and uses the
objects
to call the set and get methods.
*/
import java.io.*;
import java.lang.*;
class item
{
public static void main(String args[]) throws
IOException
{
DataInputStream in = new
DataInputStream(System.in);
Page 90
Java Assignment
String name = new String();
int quantity = 0;
float price = 0.0f;
invoice i = new invoice();
System.out.print("Item Name : ");
name = in.readLine();
System.out.print("Quantity : ");
quantity = Integer.parseInt(in.readLine());
System.out.print("Price : ");
price = Float.valueOf(in.readLine()).floatValue();
i.get(name,quantity,price);
i.set();
}
}
class invoice
{
String n = new String();
int q;
Page 91
Java Assignment
float p,tc;
void get(String x, int y, float z)
{
n = x;
q = y;
p = z;
}
void set()
{
System.out.println();
System.out.println("Item Name : " + n);
System.out.println("Quantity : " + q);
System.out.println("Price : " + p);
System.out.println("Total Cost: " + (q*p));
}
}
ASSIGNMENT-23
/**
Page 92
Java Assignment
Design a class to represent a library account.
Include the following members :
Data members:
name, number, total no of books
Methods:
To assign an initial values.
To display total no of issue books and returned books.
To display the name and date of issue of the book.
*/
import java.io.*;
class book
{
String s = new String();
String no = new String();
int tno;
void get(String x, String y, int z)
{
no = x;
s = y;
Page 93
Java Assignment
tno = z;
}
void display()
{
System.out.println();
System.out.println("Book Number: " + no);
System.out.println("Book Name : " + s);
// System.out.println("Book date : " +
}
}
class library
{
public static void main(String args[]) throws
IOException
{
DataInputStream in = new
DataInputStream(System.in);
String bname = new String();
String bno = new String();
int btno;
book b = new book();
Page 94
Java Assignment
System.out.print("Book Number: ");
bno = in.readLine();
System.out.print("Book Name : ");
bname = in.readLine();
System.out.print("Total no of Books: ");
btno = Integer.parseInt(in.readLine());
b.get(bno,bname,btno);
b.display();
}
}