Java Manual
Java Manual
package lab_programs;
class Bank{
String name;
String address;
String phone;
String pan;
String aadhar;
public Bank() {
}
public Bank(String name, String address, String phone, String pan, String aadhar) {
this.name = name;
this.address = address;
this.phone = phone;
this.pan = pan;
this.aadhar = aadhar;
}
void calcInt(){}
void displayDetails(){
System.out.println("Name : "+name+" "+"Address : "+address+" "+"Phone No : "+phone+"
"+"PAN : "+pan+" "+"Aadhar : "+aadhar);
}
}
class SBAccount extends Bank{
String accNum;
double accBal;
double interest;
public SBAccount(String name, String address, String phone, String pan, String aadhar,String
num) {
super(name, address, phone, pan, aadhar);
accNum=num;
accBal=1000;
}
public SBAccount(String name, String address, String phone, String pan, String aadhar,String
num,double bal) {
super(name, address, phone, pan, aadhar);
accNum=num;
accBal=bal;
}
void calcInt(){
interest=accBal*0.04;
}
void displayDetails(){
super.displayDetails();
System.out.println("Account Type : S/B Account\n"+"Account No : "+accNum+"\n"+"Interest
paid per year : "+interest+"\nfor Amount : "+accBal);
}
}
Expected Output
Name : Mohan Address : Bangalore Phone No : 123456 PAN : ASTPM1000 Aadhar : 33334444
Account Type : S/B Account
Account No : 111
Interest paid per year : 40.0
for Amount : 1000.0
Name : Kiran Address : Mysore Phone No : 654321 PAN : BSTPM1000 Aadhar : 55556666
Account Type : Loan Account
Account No : 222
Interest demand for 1st year : 2000.0
for Amount : 20000.0
2. Consider a scenario of myself owning a car and a bike, which need fuel to run. The
maximum speed at which my car can run is 150 Km/Hr and bike is 100 Km/Hr. write a java
program to define an abstract class which contain methods for printing the status of fuel
(EMPTY, HALF or FULL) in the vehicle and top speed of it. Vehicles fuel status and top
speed is initialized at the time of object creation. (hint: use CONSTRUCTOR, SUPER,
ABSTRACT CLASS)
a. Identify the abstract class and normal class to implement above scenario.
b. Check weather constructors can be used in the abstract class.
c. Identify abstract and non-abstract methods in a abstract class.
d. Demonstrate how to pass values from derived class constructor to base class
constructor.
e. Display the attributes of the car and a bike using abstract method.
package lab_programs;
abstract class Vehicle{
String fuel;
@Override
void topSpeed() {
System.out.println("Car runs at "+speed+" Km/Hr top speed");
}
}
class Bike extends Vehicle{
int speed;
public Bike(String fuel,int bikeSpeed) {
super(fuel);
speed=bikeSpeed;
}
@Override
void topSpeed() {
System.out.println("Bike runs at "+speed+" Km/Hr top speed");
}
}
Expected Output
package lab_programs;
interface TourPlan
{
public abstract void chennaiToHyd(String mode,double fare);
public abstract void hydToIndore(String mode,double fare);
public abstract void indoreToDelhi(String mode,double fare);
}
void totalTravelExpenditure(){
System.out.println("Total Travell Expenditure to reach from chennai to Delhi is "+totalFare);
}
}
Expected Output
package lab_programs;
import java.util.Scanner;
}
public class prg4 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int choice=0;double amt=0;
AxisBank ab=new AxisBank("Mohan", "Bangalore", "123456", "ASTPM1000", "111222",
1000);
ab.display();
do{
System.out.println("1 : Deposit\n2 : Withdraw\n3 : Exit");
choice=scan.nextInt();
switch(choice){
case 1:System.out.println("Enter amount to be deposited : ");
amt=scan.nextDouble();
ab.depositAmount(amt);
ab.display();
break;
case 2:
System.out.println("Enter amount to withdraw : ");
amt=scan.nextDouble();
ab.withdrawAmount(amt);
ab.display();
break;
default:System.out.println("Enter Valid Option");
}
}while(choice!=3);
scan.close();
}
}
Expected Output
Name : Mohan Address : Bangalore Phone No : 123456 PAN : ASTPM1000 Aadhar : 111222
Available Balanc in the account is 1000.0
1 : Deposit
2 : Withdraw
3 : Exit
2
Enter amount to withdraw :
67
LessBalanceException = can't with this much amount due to insufficient balance, minimum 1000 has to be
maintained
Name : Mohan Address : Bangalore Phone No : 123456 PAN : ASTPM1000 Aadhar : 111222
Available Balanc in the account is 1000.0
1 : Deposit
2 : Withdraw
3 : Exit
1
Enter amount to be deposited :
6000
Name : Mohan Address : Bangalore Phone No : 123456 PAN : ASTPM1000 Aadhar : 111222
Available Balanc in the account is 7000.0
1 : Deposit
2 : Withdraw
3 : Exit
2
Enter amount to withdraw :
4501
TransactionNotAllowed = Exceeds per day limit (day limit is 4500)
Name : Mohan Address : Bangalore Phone No : 123456 PAN : ASTPM1000 Aadhar : 111222
Available Balanc in the account is 7000.0
5. Searching and sorting are the often used parts in any program. Create two Java packages
for searching and sorting. (hint: use PACKAGE & MULTI-THREAD)
a. Implement some basic searching and sorting methods in the packages.
b. Search for a particular student in a student list of CSE department. sort the
elements before performing the binary search. Use methods defined in the
packages. (Use sequential threads)
c. Sort same student list concurrently using different sorting methods available in the
packages. (use Concurrent threads)
package algorithms.search;
search = key;
first = 0;
last = n - 1;
middle = (first + last)/2;
package algorithms.sort;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Sorting using Bubble Sort");
}
return arr;
}
}
package algorithms.sort;
public class SelectionSort {
public int [] selectionSort(int arr[],int n){
int i = 0;
for (; i < n - 1; i++)
{
int index = i;
for (int j = i + 1; j < n; j++){
if (arr[j] < arr[index]){
index = j;//searching for lowest index
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
System.out.println("Sorting using Selection Sort");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Sorting using Selection Sort");
return arr;
}
}
package lab_programs;
import algorithms.search.*;
import algorithms.sort.*;
int array[];
int n=5;
if(str.equals("Bub"))
{
BubbleSort bubsort=new BubbleSort();
array=bubsort.bubbleSort(arr1, n);
//for(int i=0;i<array.length;i++)
//System.out.println(array[i]);
}
else
{
SelectionSort selsort=new SelectionSort();
array=selsort.selectionSort(arr,n);
//for(int i=0;i<array.length;i++)
//System.out.println(array[i]);
}
}
}
6. Consider a scenario similar to Producer/Consumer problem in day to day life where son is
studying engineering in other state, for his expenditure father will be depositing some
money to his account every month. Father will wait until his son notifies him to deposit
money else son will wait until he spends available amount in the account. In this example
depositing (put) and withdrawing (get) is considered as synchronized method executed by
father and son on a shared account.
class Q
{
int Balance_Amount;
boolean valueSet = false;
class PCFixed
{
public static void main(String args[])
{
Q que = new Q();
new Producer(que);
new Consumer(que);
System.out.println("Press Control-C to stop...");
}
}
7. Design a scrolling banner for our Institute, which should display collage name passed from
html page in the scrolling fashion. You should able to scroll the institute name either from
left to right or from right to left.
/*<applet code=”Banner” width=500 height=500>
<param name=”string1” value=”Nitte Meenakshi Institute of technology”>
</applet>*/
import java.applet.Applet;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Banner extends Applet implements Runnable{
String msg; int x;
public void init() {
msg=getparameter(“string1”);
x=500;
new Thread(this).start();
}
public void paint(Graphics g) {
g.drawString(msg, x, 100);
}
8. Traffic signal control is a vital problem and it matters our work life and business. Write a
java program that simulates a traffic signal light. ((hint: use SWING, APPLET, EVENT
HANDLING)
a. The program allows the user to select one of three color for the traffic light: red,
yellow, or green with radio buttons. (Manual signal control)
b. Modify the program to display appropriate messages like “stop” or “ready” or “go”
to appear above the buttons in a selected color. Initially there is no message
shown.
c. Automate the Traffic Signal to change Traffic Light periodically.
/*Manual Signal Control*/
package lab_programs;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
@Override
public void mouseClicked(MouseEvent e) {
if(color=="red"){
color="green";
}else{
color="red";
}
repaint();
}
@Override
public void paint(Graphics g) {
if(color=="red")
{
g.setColor(Color.red);
g.fillOval(100, 100, 100, 100);
}else{
g.setColor(Color.green);
g.fillOval(100, 100, 100, 100);
}
}
@Override
public void mousePressed(MouseEvent e) { }
@Override
public void mouseReleased(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
}
/*Automatic Signal Control*/
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
@Override
public void run() {
while(true){
if(x<=0 && color=="red")
{
color="green"; x=40;
}else{
color="red"; x=20;
}
while(x!=0){
x-=1;
repaint();
try {
Thread.sleep(500);
} catch (InterruptedException ex) { }
}
}
}
@Override
public void paint(Graphics g) {
g.drawString(x+"", 50, 50);
if(color=="red")
{
g.setColor(Color.red);
g.fillOval(100, 100, 100, 100);
}else{ g.setColor(Color.green);
g.fillOval(100, 100, 100, 100);
}
}
}
9. We need our class time tables to be prepared and displayed. Given Day, time and Subjects
of the classes in a flat files (filename.txt file), a Java program needs to be constructed for
displaying the time table for a section. (hint: use SWING CONTROLS, FILE OPERATION)
a. The elements in the flat files are separated by commas.
b. Modify the program to support elements in the file separated using single space
separator.
c. Display the data from flat file in the table control.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
EventQueue.invokeLater(r);
}
private void createUI() {
try {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JTable table = new JTable();
timeTableList.add(timetable);
}
tableModel.setList(timeTableList);
table.setModel(tableModel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.setTitle("File to JTable");
frame.pack();
frame.setVisible(true);
} catch(IOException ex) {}
class TimeTable {
@Override
public String getColumnName(int column) {
return columnNames[column];
}
TimeTable.txt
Expected Output:
10. Human resource management is a major challenge for any organization with thousands of
employees spread over many regions. Employee information, salary structure, service
details, promotion due date etc. have to be maintained and updated periodically. Web
based solution or a desktop solution will be of great help in such cases. (hint: use
COLLECTIONS, DATABASE & CLIENT SERVER)
a. Construct a Java program to create the employee database, display the information
and allow entries to be added or removed from the client side.
b. Provide methods to display Employee information, salary structure, service details,
promotion due date etc. in the web page.
c. Sort the employees based on Emp ID and Display
d. Demonstrate the use of iterator in collections