Monday, 25 February 2019
Sunday, 24 February 2019
Saturday, 23 February 2019
Extra Program #1(Awt) [ IA-1 3 B) ]
February 23, 2019Extra Question, IA-1, Internal Lab IA, Lab Internals 3, SDM JAVA LAB TERM WORKNo comments
Question:
The program should display the message "Successfully Submitted" on the console when clicked on the "Submit" button and the text should get cleared when the user clicks on "Clear" button.
Program:
import java.awt.*;
import java.awt.event.*;
public class extraRegistrationForm {
public static void main(String[] args) {
Frame f = new newRegistrationFrame("Registration Form");
f.setSize(250, 300);
f.setVisible(true);
}
}
//Frame class
class newRegistrationFrame extends Frame {
TextField t1,t2;
public newRegistrationFrame(String title) {
super(title);
setLayout(null);
setBackground(Color.white);
Label l1=new Label("Enter your name:");
l1.setBounds(10, 40, 120, 50);
Label l2=new Label("Enter your Mobile no:");
l2.setBounds(10, 70, 120, 50);
add(l1);
add(l2);
t1=new TextField();
t1.setEditable(true);
t1.setBounds(130, 55, 100, 20);
t2=new TextField();
t2.setBounds(130, 85, 100, 20);
t2.setEditable(true);
add(t1);
add(t2);
//Add button to frame and attach listener
Button b = new Button("Submit");
b.setBounds(30, 150, 100, 30);
add(b);
b.addActionListener(new ButtonListener());
Button b1 = new Button("Clear");
b1.setBounds(140, 150, 100, 30);
add(b1);
b1.addActionListener(new ButtonListener());
//Attach window listener
addWindowListener(new WindowCloser());
}
//Listener for button
class ButtonListener implements ActionListener {
Label t=new Label();
public void actionPerformed(ActionEvent evt) {
if(evt.getActionCommand().equals("Submit")) {
//If we Have Print It On Console
System.out.println("Successfully Submitted");
/*
If we have print It on The Awt Window
t.setText("Successfully Submitted");
t.setBounds(100, 210, 150, 30);
add(t);
*/
System.out.println("Successfully Submitted");
/*
If we have print It on The Awt Window
t.setText("Successfully Submitted");
t.setBounds(100, 210, 150, 30);
add(t);
*/
}
else if(evt.getActionCommand().equals("Clear")) {
t1.setText("");
t2.setText("");
}
}
}
//Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
}
Credits: Arjun
Fifth Week Term Work Program 4
Question
Program:
import java.awt.*;
import java.awt.event.*;
public class Registrationform {
public static void main(String[] args) {
Frame f = new RegistrationFrame("Registration Form");
f.setSize(260, 200);
f.setVisible(true);
}
}
//Frame class
class RegistrationFrame extends Frame {
public RegistrationFrame(String title) {
super(title);
setLayout(null);
setBackground(Color.white);
Label l1=new Label("Enter your name:");
l1.setBounds(10, 40, 120, 50);
Label l2=new Label("Enter your Mobile no:");
l2.setBounds(10, 70, 120, 50);
add(l1);
add(l2);
TextField t1=new TextField();
t1.setEditable(true);
t1.setBounds(130, 55, 100, 20);
TextField t2=new TextField();
t2.setBounds(130, 85, 100, 20);
t2.setEditable(true);
add(t1);
add(t2);
//Add button to frame
Button b = new Button("Submit");
b.setBounds(30, 150, 100, 30);
add(b);
Button b1 = new Button("Clear");
b1.setBounds(140, 150, 100, 30);
add(b1);
//Attach window listener
addWindowListener(new WindowCloser());
}
//Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
}
Fifth Week Term Work Program 3
Question
Program:
//Displays a frame containing a three CheckBox. Tick the
//CheckBox causes the background of the frame to change from
//white to define color.
import java.awt.*;
import java.awt.event.*;
import javafx.scene.control.CheckBox;
public class checkboxcolor {
public static void main(String[] args) {
Frame f = new ChangecheckboxColorFrame("Check Box");
f.setSize(220, 200);
f.setVisible(true);
}
}
//Frame class
class ChangecheckboxColorFrame extends Frame {
Checkbox checkBox1 = new Checkbox("Red",false);
Checkbox checkBox2 = new Checkbox("Green",false);
Checkbox checkBox3 = new Checkbox("Blue",false);
public ChangecheckboxColorFrame(String title) {
super(title);
setLayout(null);
setBackground(Color.white);
//Add Checkbox to frame and attach listener
checkBox1.setBounds(10,150, 80,50);
checkBox2.setBounds(90,150, 80,50);
checkBox3.setBounds(170,150, 80,50);
add(checkBox1);
checkBox1.addItemListener(new ColorItemListener());
add(checkBox2);
checkBox2.addItemListener(new ColorItemListener());
add(checkBox3);
checkBox3.addItemListener(new ColorItemListener());//Attach //window listener
addWindowListener(new WindowCloser());
}
//Listener for checkbox
class ColorItemListener implements ItemListener{
public void itemStateChanged(ItemEvent e) {
boolean a1=checkBox1.getState();
boolean a2=checkBox2.getState();
boolean a3=checkBox3.getState();
if(a1==true && a2==true && a3==true) {
setBackground(Color.WHITE);
}
else if(a1==true) {
if(a2==true) {
setBackground(Color.YELLOW);
}
else if(a3==true) {
setBackground(Color.MAGENTA);
}
else {
setBackground(Color.RED);
}
}
else if(a2==true) {
if(a3==true) {
setBackground(Color.CYAN);
}
else{
setBackground(Color.GREEN);
}
}
else if(a3==true) {
setBackground(Color.BLUE);
}
else {
setBackground(Color.BLACK);
}
}
}
//Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
}
Fifth Week Term Work Program 2
February 23, 2019FIFTH WEEK LAB PROGRAM, Internal Lab IA, Lab Internals 2, SDM JAVA LAB TERM WORKNo comments
Question
Program:
//Displays a frame containing a three button. Pressing the
//button causes the background of the frame to change from
//white to define color.
import java.awt.*;
import java.awt.event.*;
import java.awt.Button;
public class threecolor {
public static void main(String[] args) {
Frame f = new ChangethreeColorFrame("Colors");
f.setSize(210, 200);
f.setVisible(true);
}
}
//Frame class
class ChangethreeColorFrame extends Frame {
public ChangethreeColorFrame(String title) {
super(title);
setLayout(null);
setBackground(Color.white);
//Add button to frame and attach listener
Button b1 = new Button("Red");
b1.setBounds(20, 150, 50, 30);
add(b1);
b1.addActionListener(new ButtonListener());
Button b2 = new Button("Blue");
b2.setBounds(80, 150, 50, 30);
add(b2);
b2.addActionListener(new ButtonListener());
Button b3 = new Button("Green");
b3.setBounds(140, 150, 50, 30);
add(b3);
b3.addActionListener(new ButtonListener());
//Attach window listener
addWindowListener(new WindowCloser());
}
//Listener for button
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("Red"))
setBackground(Color.RED);
else if (evt.getActionCommand().equals("Blue"))
setBackground(Color.BLUE);
else if (evt.getActionCommand().equals("Green"))
setBackground(Color.GREEN);
else
setBackground(Color.white);
}
}
//Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
}
Fifth Week Term Work Program 1
February 23, 2019FIFTH WEEK LAB PROGRAM, Java Lab Sem End, Java Part A 6, SDM JAVA LAB TERM WORKNo comments
Question
Program:
//Displays a frame containing a single button. Pressing the
//button causes the background of the frame to change Color Randomly
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
//Driver class
public class ChangingColor {
public static void main(String[] args) {
Frame f = new ChangeColorFrame("Random Colors");
f.setSize(160, 100);
f.setVisible(true);
}
}
//Frame class
class ChangeColorFrame extends Frame {
public ChangeColorFrame(String title) {
// Set title, layout, and background color
super(title);
setLayout(null);
setBackground(Color.white);
// Add "Change color" button to frame and attach listener
Button b = new Button("Random");
b.setBounds(60, 60, 60, 30);;
add(b);
b.addActionListener(new ButtonListener());
// Attach window listener
addWindowListener(new WindowCloser());
}
//Listener for button
class ButtonListener implements ActionListener {
Random rand=new Random();
public void actionPerformed(ActionEvent evt) {
int i=rand.nextInt(256);
int j=rand.nextInt(256);
int k=rand.nextInt(256);
Color c = new Color(i, j, k);
setBackground(c);
}
}
//Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
}
Sunday, 17 February 2019
Introduction
This Blog has been created keeping in mind all the difficulties faced by me and my friends during Practical Sessions. So, considering all the aspects this Blog Java Term work: I will share the best possible solution of Advance Java Lab Term Work and Other important possible java Program as per the interview point of view.
I hope this Blog will be a Stepping Stone to help all the people to perform well in your Academics. ALL THE BEST and HAPPY LEARNING.
Contact Information is mentioned in the Blogger Profile or Provide Below. Send your views, Questions or Solution To given email: csamitsharma1143@gmail.com, csamit1143@gmail.com and Don’t Forget Mention your Name.
Thanks for Visiting.
Fourth Week Term Work Program 3
February 17, 2019Awt Flow Layout Program, FOURTH WEEK LAB PROGRAM, Fourth Week Term Work, SDM JAVA LAB TERM WORKNo comments
3) Write a Java Program that creates an AWT window that should produce the following output when executed:
Program:
import java.awt.*;
import java.awt.event.*;
public class awtsecond extends Frame{
Button button1,button2,button3;
List l1;
TextField t1;
Label l;
Checkbox c1;
awtsecond(){
button1=new Button("RED");
button2=new Button("BLUE");
button3=new Button("WHITE");
button1.setBounds(20, 70, 70, 30);
button2.setBounds(95, 70, 70, 30);
button3.setBounds(170, 70, 70, 30);
add(button1);
add(button2);
add(button3);
l1=new List(7);
l1.add("Monday");
l1.add("Tuesday");
l1.add("Wednesday");
l1.add("Thursday");
l1.add("Friday");
l1.add("Saturday");
l1.add("Sunday");
l1.setBounds(250, 50, 100, 60);
add(l1); c1=new Checkbox("Pick Me");
c1.setBounds(360, 60, 60, 50);
add(c1);
l=new Label("Enter Name Here");
l.setBounds(420, 60, 100, 50);
add(l);
t1=new TextField();
t1.setBounds(505, 70, 200, 30);
add(t1);
setTitle("Flowlayout");
setSize(730,150);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new awtsecond();
}
}
Output:
Fourth week Term Work Program 2
February 17, 2019FOURTH WEEK LAB PROGRAM, Fourth Week Term Work, SDM JAVA LAB TERM WORK, SwingNo comments
2) Write a Java Program that creates a SWING window with the following attributes:
a. Dimensions of the window: 500X400
b. Initial position of the window: (150, 150)
c. Name of the window: My First SWING Frame
d. Add a button named “Cancel” to the window
e. On pressing “X” button, the window should hide
Program:
import java.awt.Frame;
import javax.swing.*;
public class swingfirst {
public static void main(String[] args) {
JFrame frame=new JFrame("My First Swing Frame");
frame.setSize(500, 400);
frame.setLocation(150, 150);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
JButton button=new JButton("Cancel");
frame.getContentPane().add(button);
frame.setVisible(true);
}
}
Output:
Fourth Week Term Work Program1
February 17, 2019Awt, FOURTH WEEK LAB PROGRAM, Fourth Week Term Work, SDM JAVA LAB TERM WORKNo comments
1) Write a Java Program that creates an AWT window with the following attributes:
a. Dimensions of the window: 500X400
b. Initial position of the window: (150, 150)
c. Name of the window: My First AWT Frame
d. Add a button named “Cancel” to the window
Wednesday, 6 February 2019
THIRD WEEK TERM WORK Program 4
February 06, 2019a circular queue using single producer and consumer, a circular queue using threads, Java program that implements bounded buffer problem, SDM JAVA LAB TERM WORK, THIRD WEEK LAB PROGRAMNo comments
4) Write a Java program that implements bounded buffer problem (consider a circular queue
and single producer and consumer threads).
import java.util.Scanner;
class Qb {
int n;
int a[] = new int[5];
boolean valueSet = false;
private Scanner s;
int front = -1;
int rear = -1;
int result;
synchronized int get() {
while (!valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println();
for (int i = 0; i <= 4; i++) {
result = deleteQ();
System.out.println("Consumed#" + (i + 1) + ": " + result);
valueSet = false;
}
notify();
return n;
}
synchronized void put(int n) {
while (valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
for (int i = 0; i <= 4; i++) {
this.n = n;
valueSet = true;
System.out.println("enter the value u want to push in ");
s = new Scanner(System.in);
result = s.nextInt();
System.out.println("Producer#" + (i + 1) + ": " + result);
insertQ(result);
}
notify();
}
void insertQ(int m) {
if ((front == 0 && rear == 4) || (front == rear + 1)) {
System.out.println("Production Queue is Full");
} else if (rear == -1) {
rear++;
front++;
a[rear] = m;
} else {
rear++;
a[rear] = m;
}
}
int deleteQ() {
int m;
if (front == -1 && rear == -1) {
System.out.println("NO More Produced Queue is empty");
return -1;
} else if (front == rear) {
if (front == 0) {
rear = -1;
front = -1;
System.out.println("NO More Produced Queue is empty");
return -1;
}
m = front;
rear = -1;
front = -1;
return a[m];
} else if (front == 5) {
front = 0;
return a[front];
} else {
m = a[front];
++front;
return m;
}
}
}
class Producers implements Runnable {
Qb q;
Producers(Qb q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
while (i == 0) {
q.put(i);
}
}
}
class Consumers implements Runnable {
Qb q;
Consumers(Qb q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
int i = 0;
while (i == 0) {
q.get();
}
}
}
class BoundBuffer {
public static void main(String args[]) {
Qb q = new Qb();
new Producers(q);
new Consumers(q);
System.out.println("Press Control-C to stop.");
}
}
THIRD WEEK TERM WORK Program 5
February 06, 2019Deadlock condition, Deadlock condition using threads, SDM JAVA LAB TERM WORK, THIRD WEEK LAB PROGRAM, Write a Java program that simulates deadlock condition using threads.No comments
5) Write a Java program that simulates deadlock condition using threads.
Program:
public class DeadLockThread {
public static Object L1 = new Object();
public static Object L2 = new Object();
public static void main(String args[]) {
ThreadDemo1 T1 = new ThreadDemo1();
ThreadDemo2 T2 = new ThreadDemo2();
T1.start();
T2.start();
}
private static class ThreadDemo1 extends Thread {
public void run() {
synchronized (L1) {
System.out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 1: Waiting for lock 2...");
synchronized (L2) {
System.out.println("Thread 1: Holding lock 1 & 2...");
}
}
}
}
private static class ThreadDemo2 extends Thread {
public void run() {
synchronized (L2) {
System.out.println("Thread 2: Holding lock 2...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 2: Waiting for lock 1...");
synchronized (L1) {
System.out.println("Thread 2: Holding lock 1 & 2...");
}
}
}
}
}
THIRD WEEK TERM WORK Program 3
February 06, 2019Java Program that simulates Client-Server Interaction using threads., Java simulates Client-Server Interaction, SDM JAVA LAB TERM WORK, THIRD WEEK LAB PROGRAMNo comments
3) Write a Java Program that simulates Client-Server Interaction using threads.
Methodology: Two threads should be created; one thread should act as Server, and the other
one should act as Client. The Server thread should accept a String request from Client thread,
parse that request and print the length of the string. It should also send back the
acknowledgement message “Message Received” to Client Thread. The Client thread should
terminate only when it receives acknowledgement from Server thread.
import java.util.Scanner;
class Qa {
int n;
String msg="a";
boolean valueSet = false;
synchronized String get() {
while (!valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
valueSet = false;
notify();
return msg;
}
synchronized void put(String msg) {
while (valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.msg=msg;
valueSet =true;
notify();
}
}
class Server implements Runnable {
Qa q;
String msg="a";
Server(Qa q) {
this.q = q;
new Thread(this, "Server").start();
}
public void run() {
msg=q.get();
System.out.println("Message:"+msg);
System.out.println("Message Length "+msg.length());
q.put("Message Received");
}
}
class Client implements Runnable {
Qa q;
Scanner input=new Scanner(System.in);
String msg;
Client(Qa q) {
this.q = q;
new Thread(this, "Client").start();
}
public void run() {
System.out.println("Enter the String");
msg=input.nextLine();
q.put(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(!(msg=q.get()).equals("Message Received"));
System.out.println("Message Received");
}
}
class clientserverthread {
public static void main(String args[]) {
Qa q = new Qa();
new Client(q);
new Server(q);
}
}
THIRD WEEK TERM WORK Program 2
February 06, 2019a Java program that implements producer consumer problem, a single resource slot and single producer and consumer threads, Producer Consumer Problem, SDM JAVA LAB TERM WORK, THIRD WEEK LAB PROGRAMNo comments
2) Write a Java program that implements producer consumer problem (consider a single
resource slot and single producer and consumer threads).
import java.util.Scanner;
//A correct implementation of a producer and consumer.
class Q {
int n;
int a;
boolean valueSet = false;
private Scanner s;
synchronized int get() {
while (!valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Consumed: " + a);
valueSet = false;
notify();
return n;
}
synchronized void put(int n) {
while (valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
valueSet = true;
System.out.println("enter the value u want to push in ");
s = new Scanner(System.in);
a = s.nextInt();
System.out.println("Produced#: " + a);
notify();
}
}
class Producer implements Runnable {
Q q;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
int i = 1;
q.put(i);
}
}
class Consumer implements Runnable {
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
q.get();
}
}
class producerconsumer {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-Z to stop.");
}
}
Tuesday, 5 February 2019
THIRD WEEK TERM WORK Program 1 (1b)
February 05, 2019Internal Lab IA, Java Part A 2, Java Lab Sem End, Lab Internals 1b, SDM JAVA LAB TERM WORK, THIRD WEEK LAB PROGRAMNo comments
Question:
Write a Java program that uses threads to compute multiplication of two matrices. The program should perform multiplication of matrices of arbitrary order. Also, proper error
handling mechanism should be used.
Methodology: Matrix multiplication is implemented using the formula: Cij = Aik * Bkj.
This formula is used for generating each element of the final matrix. The multiplication must be performed by separate threads.
Ex: If the order of the final matrix is 3X3, then a total of 9 threads should be created; each thread computing individual elements of the final matrix.
Solution:import java.util.*;
class MatrixMultiple extends Thread {
Mutiple obj;
public MatrixMultiple(Mutiple obj) {
// TODO Auto-generated constructor stub
this.obj = obj;
}
void run(int i, int j, int m) {
obj.multiple(i, j, m);
}
}
public class firstProgram {
static int[][] mat1;
static int[][] mat2;
static int[][] matResult;
public static void main(String[] args) throws InterruptedException {
Scanner input = new Scanner(System.in);
System.out.println("Enter first Matrix index nXm");
int n = input.nextInt();
int m = input.nextInt();
System.out.println("Enter second Matrix index pXq");
int p = input.nextInt();
int q = input.nextInt();
if (n!= q) {
System.out.println("m is not p matrix Multiplication is not possible");
System.exit(-1);
}
mat1 = new int[n][m];
mat2 = new int[p][q];
matResult = new int[n][q];
System.out.println("Enter the values for first matrix");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mat1[i][j] = input.nextInt();
}
}
System.out.println("Enter the values for Second matrix");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
mat2[i][j] = input.nextInt();
}
}
Mutiple mul = new Mutiple();
int nt = n * q, k = 0;
MatrixMultiple obj[] = new MatrixMultiple[nt];
try {
for (int i = 0; i < n; i++) {
for (int j = 0; j < q; j++, k++) {
obj[k] = new MatrixMultiple(mul);
obj[k].start();
obj[k].run(i, j, m);
obj[k].join();
}
}
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("RESULT MATRIX");
for (int i = 0; i < n; i++) {
for (int j = 0; j < q; j++, k++) {
System.out.print(matResult[i][j] + " ");
}
System.out.println();
}
}
}
class Mutiple extends firstProgram {
public void multiple(int i, int j, int m) {
int sum = 0;
for (int y = 0; y < m; y++) {
sum = sum + mat1[i][y] * mat2[y][j];
}
matResult[i][j] = sum;
}
}
Saturday, 2 February 2019
SECOND WEEK TERM WORK Program 4 [ IA-1 1 A) ]
February 02, 2019EXCEPTION, IA-1, SDM JAVA LAB TERM WORK, SECOND WEEK LAB PROGRAMS, USER DEFINE EXCEPTIONNo comments
4) A chemical company named “XYZ” manufactures various chemicals. To automate and
monitor the manufacturing process, they are planning to install an “Automated
Manufacturing and Maintenance System”. One important task of this system is to monitor
the temperature of the furnace. If the temperature of the furnace rises above 2000C, then it
should immediately generate an alarm. It should also generate a separate alarm if the
temperature of the furnace falls below 500C. You are hired as a Java Programmer to develop
this system. Write a Java Program which simulates the above scenario using exception
handling mechanism.
Create a class named Furnace having furnaceTemp as its attribute, and monitorTemp() &
displayTemp() as its methods. monitorTemp() throws TempTooHighException and
TempTooLowException. Program should first monitor the temperature, and if it is in the
permissible limit, display the temperature using displayTemp() method.
package exception4;
import java.util.*;
class TempTooHighException extends Exception{
public TempTooHighException(String message) {
super(message);
}
}
class TempTooLowException extends Exception{
public TempTooLowException(String message) {
super(message);
}
}
class Furnace{
double furnaceTemp=0;
Furnace(double temp)
{
furnaceTemp=temp;
}
void monitorTemp() throws TempTooHighException,TempTooLowException
{
if(furnaceTemp>200)
{
throw new TempTooHighException("Temperature Above 200'C");
}
else if(furnaceTemp<50)
{
throw new TempTooLowException("Temperature Below 50'C");
}
else
{
displayTemp();
}
}
void displayTemp()
{
System.out.println("Temperature of the Furnace=:"+furnaceTemp);
}
}
public class XYZ {
public static void main(String[] args) {
int n;
do{
Scanner input=new Scanner(System.in);
System.out.println("Enter the Furnace Temperature");
double temp=input.nextDouble();
Furnace obj=new Furnace(temp);
try {
obj.monitorTemp();
}
catch(Exception e)
{
System.out.println("Error");
System.out.println(e.getMessage());
}
System.out.println("Do u want to exit Press 0");
n=input.nextInt();
}while(n!=0);
}
}
SECOND WEEK TERM WORK Program 2
2) Write a Java Program that takes two strings as command-line arguments and checks whether
both command-line arguments are equal or not. If they are equal then print proper message;
otherwise generate a used-defined exception StringNotEqualException.
package exception2;
class StringNotEqualException extends Exception
{
public StringNotEqualException(String s)
{
super(s);
}
}
public class ExceptionTest {
public static void main(String[] args) {
if(args.length!=2)
{
System.out.println("Enter two strings only");
}
else {
try {
boolean num=args[0].equals(args[1]);
if(num!=true)
{
throw new StringNotEqualException("String Not Equal");
}
else {
System.out.println("Equal");
}
}
catch(Exception e)
{
System.out.println("Error");
System.out.println(e.getMessage());
}
}
}
}
FIRST WEEK TERM WORK Program 1
February 02, 2019FIRST WEEK LAB PROGRAM, Queue., SDM JAVA LAB TERM WORK, Write a Java Program that implements a Queue using OOP concepts.No comments
1) Write a Java Program that implements a Queue using OOP concepts.
package Program1;
import java.util.*;
class Queue {
public int Q[];
public int top;
public int rear = 0;
Queue(int size) {
Q = new int[size];
top = -1;
rear = 0;
}
public void EnQueue(int item) {
if (top == Q.length - 1) {
System.out.println("Queue overflow");
} else {
Q[++top] = item;
System.out.println("EnQueue");
}
}
public int DeQueue() {
if (top < 0) {
System.out.println("Queue underflow");
return 0;
} else {
int a = Q[rear];
for (int i = 0; i < Q.length - 1; i++) {
Q[i] = Q[i + 1];
}
top--;
return a;
}
}
public void display() {
if (top < 0) {
System.out.println("Queue underflow");
}
for (int i = 0; i <= top; i++) {
System.out.println(Q[i]);
}
}
}
public class QueueOperation {
public static void main(String args[]) {
int m = 1;
Queue obj = new Queue(5);
Scanner input = new Scanner(System.in);
do {
System.out.println("QUEUE MENU");
System.out.println("Press 1 For EnQueue");
System.out.println("Press 2 For DeQueue");
System.out.println("Press 3 For Display");
System.out.println("Press 4 For Exit");
System.out.println("Enter the operation");
int Op = input.nextInt();
switch (Op) {
case 1:
System.out.print("Enter the Element u want insert");
int n = input.nextInt();
obj.EnQueue(n);
break;
case 2:
n = obj.DeQueue();
System.out.println("Element DeQueue:" + n);
break;
case 3:
obj.display();
break;
case 4:
System.out.println("Exit");
m = 0;
break;
default:
System.out.println("Enter Proper Input");
}
} while (m == 1);
}
}
import java.util.*;
class Queue {
public int Q[];
public int top;
public int rear = 0;
Queue(int size) {
Q = new int[size];
top = -1;
rear = 0;
}
public void EnQueue(int item) {
if (top == Q.length - 1) {
System.out.println("Queue overflow");
} else {
Q[++top] = item;
System.out.println("EnQueue");
}
}
public int DeQueue() {
if (top < 0) {
System.out.println("Queue underflow");
return 0;
} else {
int a = Q[rear];
for (int i = 0; i < Q.length - 1; i++) {
Q[i] = Q[i + 1];
}
top--;
return a;
}
}
public void display() {
if (top < 0) {
System.out.println("Queue underflow");
}
for (int i = 0; i <= top; i++) {
System.out.println(Q[i]);
}
}
}
public class QueueOperation {
public static void main(String args[]) {
int m = 1;
Queue obj = new Queue(5);
Scanner input = new Scanner(System.in);
do {
System.out.println("QUEUE MENU");
System.out.println("Press 1 For EnQueue");
System.out.println("Press 2 For DeQueue");
System.out.println("Press 3 For Display");
System.out.println("Press 4 For Exit");
System.out.println("Enter the operation");
int Op = input.nextInt();
switch (Op) {
case 1:
System.out.print("Enter the Element u want insert");
int n = input.nextInt();
obj.EnQueue(n);
break;
case 2:
n = obj.DeQueue();
System.out.println("Element DeQueue:" + n);
break;
case 3:
obj.display();
break;
case 4:
System.out.println("Exit");
m = 0;
break;
default:
System.out.println("Enter Proper Input");
}
} while (m == 1);
}
}
FIRST WEEK TERM WORK Program 3
3) Using the concept of interface, write a Java Program that calculates and prints the areas of triangle, square and rectangle.
package Program3;
interface shapes {
abstract void print();
abstract void calc();
}
class triangle implements shapes {
public int a, b, area;
triangle(int p, int q) {
a = p;
b = q;
}
public void calc() {
area = a * b;
}
public void print() {
System.out.println("Triangle area" + area);
}
}
class rectangle implements shapes {
public int a, b, area;
rectangle(int p, int q) {
a = p;
b = q;
}
public void calc() {
area = a * b;
}
public void print() {
System.out.println("Rectangle area" + area);
}
}
class square implements shapes {
public int a, area;
square(int p) {
a = p;
}
public void calc() {
area = a * a;
}
public void print() {
System.out.println("Square area" + area);
}
}
public class ShapesInterface {
public static void main(String args[]) {
shapes obj = new triangle(5, 10);
obj.calc();
obj.print();
obj = new rectangle(5, 10);
obj.calc();
obj.print();
obj = new square(5);
obj.calc();
obj.print();
}
}