Witshaper is the Professional (Computing Skills, English Learning and Soft Skills) Skill Development Training Center. We provide Professional IT Courses and Soft Skill Training in Dehradun to Students, Employees and organization. Who wish to pursue a career in IT Technology. Witshaper is led by a motivated team of IT experts and Soft Skill Professionals. We provide high quality trainings. Our Emphasis is on giving the practical knowledge to the students, so that they will get to know in depth and never forget what they opt, we provide to the students real learning environment. Witshaper prepares students and professionals to be the part of this growing industry. Be a part of Witshaper and get your dreams successful

Saturday 21 May 2016

Patterns questions and answers in java

Ques1.)       1
                 121
                12321
              1234321
            123454321
          12345654321

        1234567654321     

Ques2.   123456787654321
                 1234567654321
                   12345654321
                     123454321
                       1234321
                         12321
                           121
                             1

Ques3.                                1                 1
                                           12              21
                                           123          321
                                           1234      4321
                                           12345  54321
                                           12345654321


Ques 4.)  12345654321
 12345  54321
 1234      4321
             123           321
             12                21
             1                    1

Ques 5.)  
        1
      232
    34543
  4567654
567898765



Ques 6.)   
    1
                       1   2   1
                   1   2   4   2   1
               1   2   4   8   4   2   1
           1   2   4   8  16   8   4   2   1
       1   2   4   8  16  32  16   8   4   2   1
   1   2   4   8  16  32  64  32  16   8   4   2   1


Ques 7.)  
1
11
121
1331
14641
161051
1771561
19487171

Sunday 15 May 2016

console based Quiz application java using file handling

QuizTest.java 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author rajani sharma
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class QuizTest {
public static void main (String[] args) throws IOException{

    String question = "";
    String choiceA = "";
    String choiceB = "";
    String choiceC = "";
    String choiceD = "";
    String answer = "";
    String line;
    String userAnswer;
    int i = 0;
    BufferedReader reader = null;          

        reader = new BufferedReader(new FileReader("Caitto.txt"));
        Scanner scan = new Scanner(System.in);

        QuizClass run = new QuizClass(i, question, choiceA, choiceB, choiceC, choiceD, answer);

        while((line = reader.readLine()) != null){

            //Finds the question
            if(line.contains("?")){
                run.question = line;
                System.out.println(run.getQuestion() + "\n");
            }

            //Finds answer "A"
            if(line.contains("A.")){
                run.choiceA = line;
                System.out.println(run.getChoiceA());
            }

            //Finds answer "B"
            if(line.contains("B.")){
                run.choiceB = line;
                System.out.println(run.getChoiceB());
            }

            //Finds answer "C"
            if(line.contains("C.")){
                run.choiceC = line;
                System.out.println(run.getChoiceC());
            }
            //Finds answer "D"
            if(line.contains("D.")){
                run.choiceD = line;
                System.out.println(run.getChoiceD() + "\n");
            }

            //Finds the correct answer for the question
            if(line.contains("Correct Answer:"))
            {
                //System.out.println("correct answer block");
                String[] a = line.split(":");
                answer = a[a.length - 1];
                run.answer = answer;
               // System.out.print(" Answer:"+answer);
                System.out.print("Your Answer:");
                userAnswer = scan.next();

                    //Checks if the user's input matches the correct answer from the file
                    if(userAnswer.equalsIgnoreCase(answer)){
                        System.out.println("Correct!\n");
                        i++;
                    }

                    //Checks if the user's input doesn't match the correct answer from the file
                    else if (!userAnswer.equalsIgnoreCase(answer)) {
                        System.out.println("Wrong, the correct answer was: " + run.getAnswer());
                        i++;
                    }

            }


        }

                if(i == 274){
                    reader.close();
                    scan.close();
                }
}
}




QuizClass.java 


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author rajani sharma
 */
public class QuizClass {

    String question, answer, choiceA, choiceB, choiceC, choiceD;
    private int questionNum;

    public QuizClass(int questionNum, String question, String choiceA, String choiceB, String choiceC, String choiceD, String answer){
        this.question = question;
        this.answer = answer;
        this.choiceA = choiceA;
        this.choiceB = choiceB;
        this.choiceC = choiceC;
        this.choiceD = choiceD;
    }

    public String getQuestion(){
        return question;
    }

    public String getAnswer(){
        return answer;
    }

    public String getChoiceA(){
        return choiceA;
    }

    public String getChoiceB(){
        return choiceB;
    }

    public String getChoiceC(){
        return choiceC;
    }

    public String getChoiceD(){
        return choiceD;
    }

    public int getQuestionNum(){
        return questionNum;
    }


}

Math class methods for finding power, absolute value, min, max and rounding number etc in java

import java.lang.Math;

public class MathDemo {

   public static void main(String[] args) {

   // get some doubles to find their absolute values
   double x = 4876.1874d;
 
   //syntax is static double abs(double a)
   // get and print their absolute values
   System.out.println("Math.abs(" + x + ")=" + Math.abs(x));//abs method returns the absolute value of the argument.
 
    // get a variable x which is equal to PI/2
   double x = Math.PI / 2;

   // convert x to radians
   x = Math.toRadians(x);

   // get the arc cosine of x
   System.out.println("Math.acos(" + x + ")=" + Math.acos(x));

 // get a variable x which is equal to PI/2
   double x = Math.PI / 2;

   // convert x to radians
   x = Math.toRadians(x);

   // get the arc sine of x
   System.out.println("Math.asin(" + x + ")=" + Math.asin(x));



   // get a variable x which is equal to PI/2
   double x = Math.PI / 2;

   // convert x to radians
   x = Math.toRadians(x);

   // get the arc tangent of x
   System.out.println("Math.atan(" + x + ")" + Math.atan(x));

// get two double numbers
   double x = 45.0;
   double y = 180.0;

   // convert them to radians
   x = Math.toRadians(x);
   y = Math.toRadians(y);

   // print their cosine
   System.out.println("Math.cos(" + x + ")=" + Math.cos(x));
   System.out.println("Math.cos(" + y + ")=" + Math.cos(y));

 // get two double numbers
   double x = 5;
   double y = 0.5;

   // print e raised at x and y
   System.out.println("Math.exp(" + x + ")=" + Math.exp(x));
   System.out.println("Math.exp(" + y + ")=" + Math.exp(y));

// get two double numbers
   double x = 60984.1;
   double y = -497.99;

   // call floor and print the result
   System.out.println("Math.floor(" + x + ")=" + Math.floor(x));
   System.out.println("Math.floor(" + y + ")=" + Math.floor(y));
   System.out.println("Math.floor(0)=" + Math.floor(0));


   // get two double numbers
   double x = 60984.1;
   double y = -497.99;

   // get the natural logarithm for x
   System.out.println("Math.log(" + x + ")=" + Math.log(x));

   // get the natural logarithm for y
   System.out.println("Math.log(" + y + ")=" + Math.log(y));


   // get two integer numbers
   int x = 60984;
   int y = 1000;

   // print the larger number between x and y
   System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y));

 // get two random double numbers
   double x = Math.random();
   double y = Math.random();

   // print the numbers and print the higher one
   System.out.println("Random number 1:" + x);
   System.out.println("Random number 2:" + y);
   System.out.println("Highest number:" + Math.max(x, y));
   }
}



2 .program  

/*
 * Program to demonstrate the use of Math class methods
 */

import java.io.*;
public class Mathstest {


public static void main(String[] args)
{
int num1,num2,x,y,z;
double l1,l2,l3;
String str;
BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter a number");
str=bf.readLine();
num1=Integer.parseInt(str);
x=Math.abs(num1);
System.out.println("Absolute value of "+num1+" is ="+x);

System.out.println("Enter first number");
str=bf.readLine();
num1=Integer.parseInt(str);
System.out.println("Enter second number");
str=bf.readLine();

num2=Integer.parseInt(str);
y=Math.max(num1, num2);
System.out.println("Largest number is ="+y);
y=Math.min(num1, num2);
System.out.println("Smallest number is ="+y);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
l2=Math.log(l1);
System.out.println("Log of "+l1+" is ="+l2);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
l2=Math.sqrt(l1);
System.out.println("Square root of "+l1+" is ="+l2);
l3=Math.floor(l2);
System.out.println("Floor value of "+Math.sqrt(l1)+" is ="+l3);
l2=Math.ceil(l2);
System.out.println("Ceil value of "+Math.sqrt(l1)+" is ="+l2);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
l2=Math.cbrt(l1);
System.out.println("Cube root of "+l1+" is ="+l2);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
System.out.println("Enter the power to raise ");
str=bf.readLine();

l2=Float.valueOf(str);
l3=Math.pow(l1, l2);
System.out.println(l1+"raised to the power "+l2+ " = "+l3);
}
catch(Exception e){}


}


}

console application in java : Student Management System In java Project (Mini project)

First way : student management system

package java_trainkj;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.Scanner;

/**
 *
 * @author user
 */
public class Stud {

    private int rollno;
    String name;
    static String School_name;
    String address;
    String course;

    static {

        School_name = "Witshaper";
    }

    Stud() {

    }

    Stud Add(int l) {
        Stud o = new Stud();
        if (l > 1000) {
            System.out.println(" over flow");
        } else {
            try {System.out.println("--------------------------");
                Scanner s = new Scanner(System.in);
                System.out.println(" Enter name :");
                o.name = s.next();
                System.out.println(" Enter Address :");
                o.address = s.next();
                System.out.println(" Enter Course :");
                o.course = s.next();
                System.out.println(" Enter rollno. :");
                o.rollno = s.nextInt();

            } catch (Exception e) {
                System.out.println(e);
                e.getMessage();

            }
        }
        return (o);
    }

    void search(int rollno, int l, Stud s[]) {
        for (int i = 0; i < l; i++) {
            if (s[i].rollno == rollno) {
                s[i].display(s[i]);
            }
        }
    }

    boolean delete(int rollno, Stud o[], int l) {
        boolean t = false;
        for (int i = 0; i < l; i++) {
            if (rollno == o[i].rollno) {
                for (int j = i; j < l - 1; j++) {
                    o[j] = o[j + 1];
                }
               
                t = true;
                return (t);
            }

        }

        return (t);
    }

    boolean update(int rollno, Stud obj[], int l) {
        boolean t = false;

        for (int i = 0; i < l; i++) {
            if (rollno == obj[i].rollno) {
                try {System.out.println("--------------------------");
                    Scanner s = new Scanner(System.in);
                    System.out.println(" Enter name :");
                    obj[i].name = s.next();
                    System.out.println(" Enter Address :");
                    obj[i].address = s.next();
                    System.out.println(" Enter Course :");
                    obj[i].course = s.next();

                    t = true;
                    return (t);
                } catch (Exception e) {
                    System.out.println(e);
                    e.getMessage();

                }
            }

        }

        return (t);

    }

    void display(Stud s) {
        System.out.println("--------------------------");
        System.out.println("  name :" + s.name);
        System.out.println(" Address :" + s.address);
        System.out.println(" course :" + s.course);
        System.out.println(" rollno. :" + s.rollno);

    }

    void show(int l,Stud s[]) {
        for(int i=0;i<l;i++){
         System.out.println("--------------------------");
        System.out.println(" Name       :" + s[i].name);
        System.out.println(" Address    :" + s[i].address);
        System.out.println(" course     :" + s[i].course);
        System.out.println(" rollno.    :" + s[i].rollno);
        }
    }

    public static void main(String args[]) {
        Stud obj[] = new Stud[1000];
        Scanner s = new Scanner(System.in);
        boolean f = true;
        int choice;
        int l = 0;
        int r;
        Stud o = new Stud();

        do {
            try {System.out.println("#### WELCOME ####");
                System.out.println(" Enter your choice ");
                System.out.println("1) ......Add");
                System.out.println("2).......Delete");
                System.out.println("3).......Search");
                System.out.println("4).......Update");
                 System.out.println("5).......Display");
                choice = s.nextInt();
                try {
                    switch (choice) {
                        case 1:
                            obj[l] = o.Add(l);

                            l++;
                            break;
                        case 2:
                            System.out.println(" Enter rollno.for deletion ");
                            r = s.nextInt();
                            boolean b = o.delete(r, obj, l);
                            if (b = true) {l--;
                                System.out.println("Successfully deleted");
                            } else {
                                System.out.println(" rollno. not found");
                            }
                            break;
                        case 3:
                            System.out.println(" Enter rollno. to search student ");
                            r = s.nextInt();
                            o.search(r, l, obj);
                            break;
                        case 4:
                            System.out.println(" Enter rollno. for update");
                            r = s.nextInt();
                            boolean c = o.update(r, obj, l);
                            if (c = true) {
                                System.out.println("Successfully updated");
                            } else {
                                System.out.println(" rollno. not found");
                            }
                            break;
                        case 5:
                            o.show(l,obj);
                        default:
                            System.out.println(" Enter right choice");

                    }
                } catch (Exception m) {
                    System.out.println(m);
                }

            } catch (Exception e) {
                System.out.println(e);
            }

            System.out.println(" want to do more ... true/false");
            f = s.nextBoolean();

        } while (f);

    }

}



// second way : student management system 

package myproj.exceptionhandling;

import java.util.Scanner;

public class studentmanagement {

    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);

        int c, usr;
        store ob = new store();

        do {
            System.out.println("::::MENU::::");
            System.out.println("ENTER 1 FOR ADDING DETAIL");
            System.out.println("ENTER 2 FOR SEARCHING");
            System.out.println("ENTER 3 FOR DELETION");
            System.out.println("ENTER 4 FOR UPDATION");
            System.out.println("ENTER 5 TO EXIT");
            int ch = s.nextInt();

            switch (ch) {
                case 1:
                    System.out.println("enter the no of entries");
                    usr = s.nextInt();
                    ob.add(usr);
                    break;
                case 2:
                    ob.search();
                    break;
                case 3:
                    ob.delete();
                    break;
                case 4:
                    ob.update();
                    break;
                case 5:
                    System.exit(0);
            }

            System.out.println("press any key other than 5 to do more operations else press 5 to exit");
            c = s.nextInt();
        } while (c != 5);

    }
}

abstract class student {

    protected String name[];
    protected int roll[];

    abstract void display();
}

class store extends student {

   int usr;

    Scanner s = new Scanner(System.in);
    int i = 0, c = 0, r = 0;

    store() {
        roll = new int[10];
        name = new String[10];
    }

    public void add(int usr) {
        this.usr = usr;
        for (i = 0; i < usr; i++) {
            System.out.println("Enter name and roll no ");
            name[i] = s.next();
            roll[i] = s.nextInt();

        }
        display();

    }

    public void search() {

        System.out.println("Enter the roll no you want to search");
        r = s.nextInt();
        for (i = 0; i < roll.length; i++) {
            if (roll[i] == r) {
                break;
            }
        }
        System.out.println(" ");
        System.out.println(" ");
        System.out.println("---DETAILS OF THE SPECIFIED STUDENT IS---");
        System.out.println("NAME        ROLL NO");
        System.out.print(name[i]+"         ");
        System.out.print(roll[i]);
        System.out.println(" ");
        System.out.println(" ");
        System.out.println(" ");    
       

    }

    public void delete() {
        System.out.println("Enter the rollno to be deleted");
        r = s.nextInt();

        for (i = 0; i < roll.length; i++) {
            if (roll[i] == r) {
                roll[i] = 0;
                name[i] = " ";
            }
        }
        display();

    }

    public void update() {
        System.out.println("Enter the rollno to be updated");
        r = s.nextInt();

        for (i = 0; i < roll.length; i++) {
            if (roll[i] == r) {
                System.out.println("Enter the new name and roll no");
                name[i] = s.next();
                roll[i] = s.nextInt();
            }
        }
        display();
    }

    @Override
    void display() {
        System.out.println("----THE UPDATED RECORDS OF STUDENTS ARE----");
        System.out.println("NAME        ROLL NO");
        for (i = 0; i < roll.length; i++) {
            if(roll[i]!=0)
            {              
            System.out.print(name[i]+"         ");
            System.out.print(roll[i]);
            System.out.println(" ");
            }
        }
    }
}


Applet program to implement basic arithmetic operations( Add,multiply);

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Applet67 extends Applet implements Action Listener
{
Label l1,l2,l3;
Text Field t1,t2,t3,;
Button  b1,b2,b3,b4,b5;
public void init()
{
l1=new Label ("number 1");
l2=new Label("number 2");
l3=new Label("Result");
b1=new button ("Add");
b2=new button("multiply");
b3=new Button("Clear");
b4=new Button("Red");
b5=new Button("blue");
t1=new TextField("15");
t2=new TextField("15");
t3=new TextFieldss("15");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
ad(b2);
add(b3);
add(l3);
add(t3);
add(b4);
add(b5);
b1.addActionListener(this);
b2.addActionlistener(this);
b3.addActionlistener(this);
b4.addActionListener(this);
b5.addActionListener(this);
SetBackground(Color.green);
}
public void actionPerformed(Action Event ae)
{
int n1,n2,n3;
if(ae.getSource()=b1)
{
n1=Integer.parseInt(t1.getText());
n2=Integer.parseInt(t2.getText());
n3=n1+n2
t3.SetText(String.value Of(n3));
}
if(ae.getSource()=b2)
{
n1=Integer.parseInt(t1.getText());
n2=Integer.parseInt(t2.getText());
n3=n1*n2;
t3.SetText(String value of(n3));
}
if(ae.getsource()==b3)
{
t1.SetText("");
t2.SetText("");
t3.SetText("");
}
if(ae.getSource()==b4)
{
SetBackground(Color.red);
}
if(ae.getSource()==b5)
{
SetBackground(Color.Blue);
}
}
}
/*<applet code=Applet67
 height=20 width=220>
</applet>
*/









































applet program: Implementing Card layout and panel

import java.awt.*;
import java.awt.event.*;
public class Card1 extends Frame implements ActionListener
{
CardLayout cl;
Panel p1,p2,p3,p4,p5;
Button b1,b2,b3,b4,b5,b6;
Card1()
{
b1=new Button("Next");
b2=new Button("Previous");
b3=new Button("Next");
b4=new Button("Previous");
b5=new Button("Next");
b6=new Button("End");

p1=new Panel();
p1.setLayout(new FlowLayout());
p1.setBackground(Color.yellow);
p1.add(b1);

p2=new Panel();
p2.setLayout(new FlowLayout());
p2.setBackground(Color.pink);
p2.add(b2);
p2.add(b3);

p3=new Panel();
p3.setLayout(new FlowLayout());
p3.setBackground(Color.green);
p3.add(b4);
p3.add(b5);

p4=new Panel();
p4.setLayout(new FlowLayout());
p4.setBackground(Color.cyan);
p4.add(b6);

p5=new Panel();
cl=new CardLayout();
p5.setLayout(cl);
p5.add(p1,"First");
p5.add(p2,"Second");
p5.add(p3,"Third");
p5.add(p4,"Fourth");

add(p5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
cl.next(p5);
if(ae.getSource()==b2)
cl.previous(p5);
if(ae.getSource()==b3)
cl.next(p5);
if(ae.getSource()==b4)
cl.previous(p5);
if(ae.getSource()==b5)
cl.next(p5);
if(ae.getSource()==b6)
cl.first(p5);

}
public static void main(String ar[])
{
Card1 c1=new Card1();
c1.setSize(400,200);
c1.setVisible(true);
}
}

Applet program : implement KeyListener and ActionListener

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class KeyLis extends Applet implements KeyListener,ActionListener
{
Label l1,l2;
TextField t1,t2;
Button b1;
public void init()
{
l1=new Label("Name  ");
l2=new Label("Age     ");
t1=new TextField(15);
t2=new TextField(15);
b1=new Button("Clear");
setBackground(Color.cyan);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
b1.addActionListener(this);
t1.addKeyListener(this);
t2.addKeyListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
t1.setText("");
t2.setText("");
}
}
public void keyPressed(KeyEvent ke)
{}
public void keyReleased(KeyEvent ke)
{
int ch;
if(ke.getSource()==t1)
{
ch=ke.getKeyChar();
//System.out.println(ch);
if(( ch < 65 || ch >90) &&(ch<97 ||ch >122 )&&(ch!=32))
{
t1.setText("");
showStatus("Only alphabets and spaces allowed ... ");
}
else
{
showStatus("");
}
}
if(ke.getSource()==t2)
{
ch=ke.getKeyChar();
if(ch<48 || ch >57)
{
t2.setText("");
showStatus("Only numbers allowed .. ");
}
else
{
showStatus("");
}
}
}
public void keyTyped(KeyEvent ke)
{}
}
/*
<applet code=KeyLis.java height=200 width=200></applet>
*/