Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Showing posts with label IA-1. Show all posts
Showing posts with label IA-1. Show all posts

Thursday, 7 March 2019

IA -1 Q 1 B)

Question:
Explain different ways to create a thread in java with a relevant example.


Solution:
Java lets you create a thread in the following two ways:- 
By implementing the Runnable interface.
By extending the Thread
Runnable Interface
The easiest way to create a thread is to create a class that implements the Runnable interface.
To implement Runnable interface, a class need only implement a single method called run( ), which is declared like this:
public void run( )
Inside run( ), we will define the code that constitutes the new thread
Example:
public class MyClass implements Runnable {
public void run(){
System.out.println("MyClass running");
   } 
}
To execute the run() method by a thread, pass an instance of MyClass to a Thread in its constructor(A constructor in Java is a block of code similar to a method that’s called when an instance of an object is created). Here is how that is done:
Thread t1 = new Thread(new MyClass ());
t1.start();
When the thread is started it will call the run() method of the MyClass instance instead of executing its own run() method. The above example would print out the text “MyClass running“.
Extending Java Thread
The second way to create a thread is to create a new class that extends Thread, then override the run() method and then to create an instance of that class. The run() method is what is executed by the thread after you call start(). 
Example:
public class MyClass extends Thread {
     public void run(){
     System.out.println("MyClass running");
         }
     }
To create and start the above thread you can do like this:
 MyClass t1 = new MyClass ();
 T1.start();
When the run() method executes it will print out the text “MyClass running“.

Sixth Week Term Work Program 3 [ IA-1 2 B) ]

Question:
Write a Java program the implements a generic sort method using any of the sorting techniques.

public class GenericSorting {
//Modification: Sorting Wrt To String Length.
public static <T extends Comparable<T>> void sortingMethod1(T array[])
   {
   int i,j,n;
   n=array.length;
   for(i=0;i<n;i++)
   for(j=0;j<n-i-1;j++)
   if((((String) array[j]).length())>((String) array[j+1]).length())
   {
   T temp;
   temp=array[j];
   array[j]=array[j+1];
   array[j+1]=temp;
   }
   
   for(T element: array)
  System.out.printf("%s ", element);
   System.out.println();
   }

   public static <T extends Comparable<T>> void sortingMethod(T array[])
   {
   int i,j,n;
   n=array.length;
   for(i=0;i<n;i++)
   for(j=0;j<n-i-1;j++)
   if(array[j].compareTo(array[j+1])>0)
   {
   T temp;
   temp=array[j];
   array[j]=array[j+1];
   array[j+1]=temp;
   }
   
   for(T element: array)
  System.out.printf("%s ", element);
   System.out.println();
   }
   public static void main(String args[])
   {
   Integer i[]={3,2,1,7,6};
           String s[]={"pear","orange","apple"};
   sortingMethod(i);
 sortingMethod(s);
 
 System.out.println("\nSorting As per String Length");
 sortingMethod1(s);
 
   }
}

Output:
1 2 3 6 7 
apple orange pear 

Sorting As per String Length

pear apple orange 

Blue Marked Are Modification Asked By Ma'am In B1 Batch.

Sixth Week Term Work Program 1 [ IA-1 3 A) ]

Question:
 Write a Java program to create the below-mentioned Student record using appropriate
Collection class to perform the following operations on a single student:
a. Add a student
b. Display all the details of the student
c. Delete/remove student record
Members of the class Student is given below:

Student
name
USN
division
CGPA


Solution:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class SingleStudentRecord {
 ArrayList<Comparable> stud=new   ArrayList<Comparable>(4);
static Scanner in=new Scanner(System.in);
public void EnterRecord() {
System.out.print("Enter The Name:");
stud.add(in.next());
System.out.println();
System.out.print("Enter The USN:");
stud.add(in.next());
System.out.println();
System.out.print("Enter The Division:");
stud.add(in.next());
System.out.println();
System.out.print("Enter The CGPA:");
stud.add(in.nextFloat());
System.out.println();
}
public void DisplayRecord() {
Iterator<Comparable> itr=stud.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}        

}
public void DeleteRecord() {
System.out.println("Deleted The Student Record");
stud.removeAll(stud);
}
public static void main(String[] args) {
SingleStudentRecord s=new SingleStudentRecord();
int choice;
int i=0;
    while (true) {
        System.out.println("\n\n*******************STUDENT RECORD*******************\n");
        System.out.println("1. Enter Student Record");
        System.out.println("2. Display Record");
        System.out.println("3. Delete Record");
        System.out.println("4. Exit");
        System.out.println("Enter choice: ");
        choice = in.nextInt();
        switch (choice) {
        case 1: if(i==0)
        {
        s.EnterRecord();
        i++;
        }
        else {
        System.out.println("U Can Store Only One Student Record");
        }
                break;
        case 2:
        if(i==1)
        {
        s.DisplayRecord();
        }
        else {
        System.out.println("No Record Exists");
        }
        break;
        case 3:if(i==1) {
        s.DeleteRecord();
        i--;
        }
        else {
        System.out.println("No Record To delete");
        }
        break;
        case 4:
        System.out.println("THANKS FOR VISTING");
        System.exit(0);
        break;
        default:
        System.out.println("Enter A Valid Option");

}
}
}
}

Output:

**********STUDENT RECORD**********

1. Enter Student Record
2. Display Record
3. Delete Record
4. Exit
Enter choice: 
1
Enter The Name:Amit

Enter The USN:2Sd16cs012

Enter The Division:B

Enter The CGPA:8.4


Enter choice: 
2
Amit
2Sd16cs012
B
8.4


Enter choice: 
3
Deleted The Student Record


Enter choice: 
4
THANKS FOR VISTING


Saturday, 23 February 2019

Extra Program #1(Awt) [ IA-1 3 B) ]

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);
*/
}
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

Saturday, 2 February 2019

SECOND WEEK TERM WORK Program 4 [ IA-1 1 A) ]

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