Professional Documents
Culture Documents
Nitigya Singh
Nitigya Singh
Nitigya Singh
Vasudeva
https://skydrive.live.com/?sc=documents&cid=f56c331e15e7c334#cid=F56C331E15 E7C334&id=F56C331E15E7C334%21162&sc=documents
b. Math
Math
Grade Sorting: Bubble Sort: Methods to sort various inputsfor (int pass=1; pass<n; pass++){ for (int i=0; i<n-pass; i++){ if (a[i]>a[i+1]){ temp= a[i]; a[i]=a[i+1]; a[i+1] = temp;
The main method of this code uses the if else statement to sort out the grades into the different categories: Binary to Decimal: This code uses this method to convert binary to decimal while(num > 0) { rem = num % 10; num = num / 10; if(rem != 0 && rem != 1) } public String findGrade(int a){ String gradereturn = ""; if (a>=87){ gradereturn = "A+"; } else if (a>=82){ gradereturn = "A"; } else if (a>=80){ gradereturn = "A-"; } else if (a>=77){ gradereturn = "B+"; } else if (a>=73){ gradereturn = "B"; } else if (a>=70){ gradereturn = "B-"; } else if (a>=67){ gradereturn = "C+"; } else if (a>=63){ gradereturn = "C"; } else if (a>=60){ gradereturn = "C-"; } else if (a>=57){ gradereturn = "D+"; } else if (a>=53){ gradereturn = "D"; } else if (a>=50){ gradereturn = "D-"; } else{ gradereturn = "F"; } return gradereturn;
Different variables can be sorted by using the above code. To sort String, float, double and other variables, One must change the assignment. Eg: if the method is assigned as int I; And we want to sort a String: Change the int to String.
c. Algorithms
Algorithm
Reverse String: The following code takes the String input from the user. Then the String value is counted by a loop as characters.
for(int i=s.length()-1; i>=0; i--) { b+=s.charAt(i); }
Factorial: Methods to sort various inputsfor (int i = 1; i <= input; i++) { result *= ((double)i);
Harmonic: This code uses this method find the harmonic of the number for (int i = 1; i <= input; i++) { result += (1/(double) i); }
Finding the Power: The code below is used to find the power double ans= Math.pow(x,y);
} The above method is the main method that is used to find the factorial of the inputted number.
d. Drawing
JTextField Out 1-3
JTextField In 1-3
Bouncing Ball: Methods while(true) { try { Thread.sleep(speed); } catch(InterruptedException e) {} if(ball_x>= (start_x+ width10)) { delta_x=-delta_x; } else if(ball_x<=(start_x-10)) { delta_x=-delta_x; } if(ball_y>= (start_y+ height-10)) { delta_y=-delta_y; } else if(ball_y<=(start_y10)) { delta_y=-delta_y; } ball_x+=delta_x; ball_y+=delta_y; if(delta_y<20) { delta_y+=gravity; gravity+=0.001; } repaint(); Drawing: Methods setBackground(Color.orange); MousePainter m = new MousePainter(); addMouseMotionListener(m); JOptionPane.showMessageDial og(null,"This is not a binary number. /n Please Try again"); } } int i= Integer.parseInt(str,2); String s= "" +i; Mouse Motion Adapter: xval=m_event.getX(); yval=m_event.getY(); repaint();
3. Source Code: Main Frame: package gui_sem1showcase; import gui_sem1showcase.drawing.Mouse_Draw_Frame; import java.awt.*; import java.awt.event.*; import javax.swing.*;
/** * * @author raveenvasudeva * This is the main frame of the project which consist of the Menu * items whose methods are called by the ActionListener */ // Creates the Main Frame for the ShowCase public class MainFrame extends JFrame{ JMenuBar programMenu; JMenu mathMenu, algorithmMenu, graphicMenu; JMenuItem mouseDraw, bouncingBall, factorial, nHarmonic, bubbleSort, xpowery, letterGrade, reverseString, bin2Dec; //--------- Initial Classes--------// BouncingBall b; MouseDrawing mdraw; Grade g; menuHandler h; Factorial f; BubbleSort_GUI bs; HarmonicNumber hn; power p; ReverseString rs; BinaryToDecimal B; //---------------------------------// public MainFrame() { super("learning programs in sem1"); programMenu= new JMenuBar(); // Main Menu Bar add(programMenu, BorderLayout.NORTH); addMenuItems(); addAction(); setSize(500,500); setVisible(true); } // adding of sub menu items which when called functions the method written void addMenuItems() { //----------3 Core Menu Items-------------//
mathMenu= new JMenu("Math"); programMenu.add(mathMenu); algorithmMenu= new JMenu("Algorythms"); programMenu.add(algorithmMenu); graphicMenu= new JMenu("Graphics"); programMenu.add(graphicMenu); //--------------------------------------// //*************Math Menu Items****************// factorial= new JMenuItem("Factorial"); mathMenu.add(factorial); nHarmonic= new JMenuItem("N Harmonic"); mathMenu.add(nHarmonic); xpowery= new JMenuItem("X^Y"); mathMenu.add(xpowery); bin2Dec= new JMenuItem("Binary to Decimal"); mathMenu.add(bin2Dec); //*********************************************//
//----------Bubble Sort--------------// bubbleSort= new JMenuItem("Bubble Sort"); algorithmMenu.add(bubbleSort); //-------------------------------------------// //-----------------Other options--------------// letterGrade= new JMenuItem("Display Letter Grade"); algorithmMenu.add(letterGrade); reverseString= new JMenuItem("Reverse a String"); algorithmMenu.add(reverseString); //--------------------------------------------// //**************************************************//
//*************Graphics Menu***********************// mouseDraw = new JMenuItem("Mouse Drawing"); graphicMenu.add(mouseDraw); bouncingBall= new JMenuItem("Bouncing Ball"); graphicMenu.add(bouncingBall); //*************************************************// } // methods to handle actionevents void addAction() { h= new menuHandler(); //*************Add Action Listeners***************// mouseDraw.addActionListener(h); xpowery.addActionListener(h); letterGrade.addActionListener(h); bouncingBall.addActionListener(h); factorial.addActionListener(h); nHarmonic.addActionListener(h); bubbleSort.addActionListener(h); reverseString.addActionListener(h); bin2Dec.addActionListener(h); //************************************************// } class menuHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==mouseDraw) { mdraw= new MouseDrawing(); } else if(e.getSource()== xpowery) { power p= new power(); } else if(e.getSource()== letterGrade) { g= new Grade(); } else if(e.getSource()== factorial) { f= new Factorial(); } else if(e.getSource()== nHarmonic) { hn= new HarmonicNumber();
} else if(e.getSource()== xpowery) { p= new power(); } else if(e.getSource()== bin2Dec) { B= new BinaryToDecimal(); } else if(e.getSource()== reverseString) { rs= new ReverseString(); } else if(e.getSource()== mouseDraw) { mdraw= new MouseDrawing(); } else if(e.getSource()== bouncingBall) { b= new BouncingBall(); } else if(e.getSource()== bubbleSort) { bs= new BubbleSort_GUI(); } } } // implements exit window handling task public static void main(String args[]) { MainFrame M= new MainFrame(); M.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } (Math) Bubblesort: package JavaBasicGUI; import import import import import import javax.swing.JFrame; javax.swing.JButton; javax.swing.JTextField; javax.swing.JLabel; java.awt.FlowLayout; java.awt.event.ActionEvent;
import java.awt.event.ActionListener; /** * This code is written to sort out inputs in an ascending order * @author jonathanwu */ public class BubbleSort { public void bubbleSort(int[] a){ int n=a.length; int temp=a[0]; for (int pass=1; pass<n; pass++){ for (int i=0; i<n-pass; i++){ if (a[i]>a[i+1]){ temp= a[i]; a[i]=a[i+1]; a[i+1] = temp; } } } } public void bubbleSort(double[] a){ int n=a.length; double temp=a[0]; for (int pass=1; pass<n; pass++){ for (int i=0; i<n-pass; i++){ if (a[i]>a[i+1]){ temp= a[i]; a[i]=a[i+1]; a[i+1] = temp; } } } } public void bubbleSort(char[] a){ int n=a.length; char temp; for (int pass=1; pass<n; pass++){ for (int i=0; i<n-pass; i++){ if (a[i] >a[i+1]){ temp= a[i]; a[i]=a[i+1]; a[i+1] = temp; } } } } public void bubbleSort(String[] ss){ String temp=ss[0]; for (int pass=1; pass<ss.length; pass++){ for (int i=0; i<ss.length-pass; i++){ if ((ss[i].compareTo(ss[i+1]))>0){
temp= ss[i]; ss[i]=ss[i+1]; ss[i+1] = temp; } } } } } class BubbleSort_GUI extends JFrame { JButton Integer, Character, string, Double; JTextField Input; JLabel Output; BubbleSort b= new BubbleSort(); Parser p= new Parser(); String out="",s=""; BubbleSort_GUI() { super("Bubble Sort"); setLayout(new FlowLayout()); Input= new JTextField("Input List here"); Output= new JLabel("Sorted List will be displayed here"); Integer= new JButton("Sort Integer Values"); Character= new JButton("Sort Character Values"); string= new JButton("Sort String Values"); Double = new JButton("Sort Double Values"); add(Input); add(Integer); add(Character); add(string); add(Double); add(Output); Action a= new Action(); Integer.addActionListener(a); Character.addActionListener(a); string.addActionListener(a); Double.addActionListener(a); setSize(500,500); setVisible(true); } class Action implements ActionListener {
public void actionPerformed(ActionEvent e) { if(e.getSource()== Integer) { s=Input.getText(); p.parseIn(s); p.convertInt(p.values); b.bubbleSort(p.int_values); out= p.convertInt_String(p.int_values); Output.setText(out); } else if(e.getSource()== Character) { s=Input.getText(); p.parseIn(s); p.convertChar(p.values); b.bubbleSort(p.char_values); out= p.convertChar_String(p.char_values); Output.setText(out); } else if(e.getSource()==string) { s=Input.getText(); p.parseIn(s); for (int i=0; i<p.values.length; i++){ p.values[i].trim(); } b.bubbleSort(p.values); out= p.convertStringArray_String(p.values); Output.setText(out); } else if(e.getSource()== Double) { s=Input.getText(); p.parseIn(s); p.convertDouble(p.values); b.bubbleSort(p.Double_values); out= p.convertDouble_String(p.Double_values); Output.setText(out); } } } } Parser: package JavaBasicGUI; /**
* * @author nitigyasingh * The class below contains methods to sort out character, integer, float, double * and String values in an ascending order */ public class Parser_e { public int count=1; public String[] values; public int[] int_values; public double[] Double_values; public char[] char_values; public void parseIn(String s){ int element=0; for (int i=0; i<s.length(); i++)// counts number of commas { if (s.charAt(i) == ',') count++; //counts the comma } values = new String[count];// makes a new string array for(int i=0; i<count; i++){ String number_string = new String(); //catches error like "," so that there is no runtime error try{ while (s.charAt(element) !=','){ number_string +=s.charAt(element); if(element>=s.length()-1) break; element++; }} catch(StringIndexOutOfBoundsException e){} values[i]=number_string.toString(); element++; } } //sorting of Integer in ascending order public void convertInt(String[] ss){ int_values = new int[count]; for(int i=0; i<count; i++){ int_values[i] = Integer.parseInt(values[i]); } } //sorting of float values in ascending order public void convertDouble(String[] ss){
Double_values = new double[count]; for(int i=0; i<count; i++){ Double_values[i] = Double.parseDouble(values[i]); } } //sorting of Character public void convertChar(String[] ss){ char_values = new char[count]; for(int i=0; i<count; i++){ char_values[i] = values[i].charAt(0); } } /* public String convertInt_String(int[] int_vals){ String output=""; for(int i=0; i<count; i++){ output+=int_vals[i]+","; } return output; } */ //sorting of Double in ascending order public String convertDouble_String(double[] Double_vals){ String output=""; for(int i=0; i<count; i++){ output+=Double_vals[i]+","; } return output; } //cconverting of Character into String public String convertChar_String(char[] char_vals){ String output=""; for(int i=0; i<count; i++){ output+=char_vals[i]+","; } return output; } //sorting a list of Strings public String convertStringArray_String(String[] str_vals){ String output=""; for(int i=0; i<count; i++){ output+=str_vals[i]+","; } return output; } }// end class
Binary to decimal:
package JavaBasicGUI; import import import import import javax.swing.JFrame; javax.swing.JTextField; javax.swing.JButton; java.awt.FlowLayout; javax.swing.JOptionPane;
import java.awt.event.ActionListener; import java.awt.event.ActionEvent; /** * This program is written to convert binary numbers into decimal * @author jonathanwu */ public class BinaryToDecimal extends JFrame{ JTextField txt1, txt2; JButton btn1, btn2; BinaryToDecimal() { super("Binary To Decimal"); setLayout(new FlowLayout()); //----------Input and Output Text Fields--------------------// txt1= new JTextField("Enter Number Here"); txt2= new JTextField("The Result Value displayed here "); txt2.setEditable(false); add(txt1); add(txt2); //----------------------------------------------------------// //----------Decimal To Binary Components--------------------// btn1= new JButton("Decimal --> Binary"); btn2= new JButton("Binary --> Decimal"); add(btn1); add(btn2); //------------------------------------------------------// //-----------Adding Action Listeners-------------// Action a= new Action(); btn1.addActionListener(a); btn2.addActionListener(a); //-----------------------------------------------// setSize(500,500); setVisible(true); }
public String IntToBin(int i) { String result; result = Integer.toBinaryString(i);// method in the Integer class that converts Integer values to Binary return result; } public String BinToInt(String str) { long num = Long.parseLong(str); long rem; while(num > 0)// find the decimal value of the binary number { rem = num % 10; num = num / 10; if(rem != 0 && rem != 1)// checks to see if input is binary { JOptionPane.showMessageDialog(null,"This is not a binary number. /n Please Try again"); } } int i= Integer.parseInt(str,2); String s= "" +i; return s; } class Action implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==btn1) { txt2.setText(IntToBin(Integer.parseInt(txt1.getText()))); } else if(e.getSource()==btn2) { txt2.setText(BinToInt(txt1.getText())); } } } }
Grade SOrt: package JavaBasicGUI; import java.awt.event.*; import java.awt.*; import javax.swing.*; /** * This code is a simple code which is used to take in the grade and * categorize them. * @author nitigyasingh * This class is written to find the final grade with the marks obtained */ public class GradeInput extends JFrame{ private JLabel grade; private JLabel gradeOut; private JTextField input; private Container c; private ActionHandler action; private static myWinHandler w; public GradeInput(){ super("Grade Converter"); c = getContentPane(); action = new ActionHandler(); w = new myWinHandler(); grade = new JLabel("Grade:"); gradeOut = new JLabel(""); input = new JTextField("Enter the Numerical Grade Here."); input.addActionListener(action); //adjusting the frame c.add(grade, BorderLayout.WEST); c.add(gradeOut, BorderLayout.EAST); c.add(input, BorderLayout.SOUTH); setSize(200,100); setVisible(true); } //decision making begins here public String findGrade(int a){ String gradereturn = ""; if (a>=87){ gradereturn = "A+"; } else if (a>=82){
gradereturn = "A"; } else if (a>=80){ gradereturn = "A-"; } else if (a>=77){ gradereturn = "B+"; } else if (a>=73){ gradereturn = "B"; } else if (a>=70){ gradereturn = "B-"; } else if (a>=67){ gradereturn = "C+"; } else if (a>=63){ gradereturn = "C"; } else if (a>=60){ gradereturn = "C-"; } else if (a>=57){ gradereturn = "D+"; } else if (a>=53){ gradereturn = "D"; } else if (a>=50){ gradereturn = "D-"; } else{ gradereturn = "F"; } return gradereturn; } // class calls actionlistener to perform the method written above private class ActionHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ int q = 0; String s = ""; String r = ""; r = input.getText(); q = Integer.parseInt(r); s = findGrade(q); gradeOut.setText(s); } } public static void main(String[] args){
GradeInput app = new GradeInput(); app.addWindowListener(w); // implements exit window handling task } private static class myWinHandler extends WindowAdapter{ public void windowClosing (WindowEvent event){ System.exit(0); } //end of frame } }
(ALGORITHM) Factorial: package JavaBasicGUI; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /* * This program finds the factors of a number and adds them up * @author michaelrodda */ public class Factorial extends JFrame{ // declared variables for GUI private JLabel text; private JTextField input; //constuctor is made public Factorial(){ super ("Factorial of a Number"); text = new JLabel("Answer"); TextFieldHandler h = new TextFieldHandler(); input = new JTextField("Enter Number Here."); input.addActionListener(h); add(text, BorderLayout.NORTH); add(input, BorderLayout.SOUTH); setSize(200,100);
setVisible(true); } //The factorial for the number input is found public static double findFactorial(int input) { double result = 1; for (int i = 1; i <= input; i++) { result *= ((double)i); } return result; } // event handler is written here and handles the conversion // of the input to the factorial private class TextFieldHandler implements ActionListener { public void actionPerformed(ActionEvent event) { String s = ""; int q = 0; double r = 0; if (event.getSource()==input) { s = input.getText(); q = Integer.parseInt(s); r = findFactorial(q); s = String.valueOf(r); text.setText(s); } } } } Harmonic: package JavaBasicGUI; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * * @author nitigyasingh * This class is written to find the Harmonic after the user inputs * the number */
public class HarmonicNumber extends JFrame{ private JLabel text; private Container c; private JTextField input; public HarmonicNumber(){ super ("Harmonic Number"); text = new JLabel("Answer"); //text.setEditable(false); c = getContentPane(); TextFieldHandler h = new TextFieldHandler(); input = new JTextField("Enter Number Here."); input.addActionListener(h); c.add(text, BorderLayout.NORTH); c.add(input, BorderLayout.SOUTH); setSize(200,100); setVisible(true); } // finding the harmonic public static double findHarmonic(int input) { double result = 0; for (int i = 1; i <= input; i++) { result += (1/(double) i); } return result; } // class calls actionlistener to perform the method of finding harmonic private class TextFieldHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ String s = ""; int q = 0; double r = 0; if (event.getSource()==input){ s = input.getText(); q = Integer.parseInt(s); r = findHarmonic(q); s = String.valueOf(r); text.setText(s); }
} } public static void main(String[] args){ HarmonicNumber app = new HarmonicNumber(); windowHandler wl = new windowHandler(); app.addWindowListener(wl); // implements exit window handling task } private static class windowHandler extends WindowAdapter{ public void windowClosing(WindowEvent event){ System.exit(0); }//exit of frame } } Reverse String: package JavaBasicGUI; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * This code gets the input from the user and then reverses the input * eg: Dog (Input) --- Output : God * @author jonathanwu */ public class ReverseString extends JFrame { JTextField txt1, txt2; String h; ReverseString() { super("Reverse String"); setLayout(new FlowLayout()); txt1 = new JTextField("Enter String here"); add(txt1); txt2= new JTextField("Reversed String here"); add(txt2); txt2.setEditable(false); Action a= new Action(); txt1.addActionListener(a); setVisible(true); setSize(500,500); } class Action implements ActionListener { public void actionPerformed(ActionEvent e) {
h=reverseString(txt1.getText()); txt2.setText(h); } } String reverseString(String s) { String b=""; try{ for(int i=s.length()-1; i>=0; i--) { b+=s.charAt(i); }} catch(StringIndexOutOfBoundsException e){} return b; } } Power: package JavaBasicGUI; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * This code calculates the power of the inputted number * @author nitigyasingh */ public class power extends JFrame{ JTextField X, Y; JLabel answer; double ans; JButton calculate; power() { super("Calculate Power"); setLayout(new FlowLayout()); X= new JTextField("Enter X value"); Y= new JTextField("Enter Y value"); answer= new JLabel("Answer"); calculate= new JButton("Calculate"); add(X); add(Y);
add(calculate); add(answer); Power p= new Power(); calculate.addActionListener(p); setSize(300,300); setVisible(true); } public void paintComponent(Graphics g){ setBackground(Color.RED); } double calc(double x,double y) { double ans= Math.pow(x,y); return ans; } class Power implements ActionListener { public void actionPerformed(ActionEvent e) { ans=calc(Double.parseDouble(X.getText()),Double.parseDouble(Y.getText( ))); answer.setText("The answer is: "+ans); } } } (DRAWING) Mouse Draw: package JavaBasicGUI; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author raveenvasudeva */ public class MouseDrawing extends JFrame{ private int xval=-10,yval=-10;
int brushSize; JTextField txt; public MouseDrawing(){ super("Mouse Drawing"); txt= new JTextField("Enter Brush Size here"); add(txt, BorderLayout.NORTH); BrushSize b= new BrushSize(); txt.addActionListener(b);
setSize(500,500); setVisible(true); }
class MousePainter extends MouseMotionAdapter{ public void mouseDragged(MouseEvent m_event){ xval=m_event.getX(); yval=m_event.getY(); repaint(); } } private class BrushSize implements ActionListener { public void actionPerformed(ActionEvent e) { brushSize=Integer.parseInt(txt.getText()); } } public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(xval, yval, brushSize, brushSize); } }
Bouncing Ball: package JavaBasicGUI; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Graphics; import java.awt.Color; /** * This code is the first code which shows the ball bouncing on the * screen * @author nitigyasingh */ public class BouncingBall extends JFrame{ double delta_x=5,delta_y=5,gravity=0.7; JButton b; Ball B; Button btn; int ball_x=30, ball_y=300, radius=20, speed=20, start_x=30, start_y=30, width=500, height=500 ; BouncingBall() { super("Bouncing Ball"); b= new JButton("Bounce"); add(b,BorderLayout.SOUTH); B= new Ball(); add(B, BorderLayout.CENTER); setSize(612,612); setVisible(true); btn= new Button(); b.addActionListener(btn); }
class Ball extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.RED); g.drawRect(start_x, start_y, width, height); g.setColor(Color.RED); g.fillOval(ball_x, ball_y, radius, radius);
} } public void bounceBall() { while(true) { try { Thread.sleep(speed); } catch(InterruptedException e) {} if(ball_x>= (start_x+ width-10)) { delta_x=-delta_x; } else if(ball_x<=(start_x-10)) { delta_x=-delta_x; } if(ball_y>= (start_y+ height-10)) { delta_y=-delta_y; } else if(ball_y<=(start_y-10)) { delta_y=-delta_y; } ball_x+=delta_x; ball_y+=delta_y; if(delta_y<20) { delta_y+=gravity; gravity+=0.001; } repaint(); } } public class Button implements ActionListener { public void actionPerformed(ActionEvent e) { bounceBall(); } } }
Thank You!!