Notices
Results 1 to 6 of 6

Thread: Source Code

  1. #1 Source Code 
    Forum Freshman
    Join Date
    Oct 2007
    Posts
    8
    I couldnt Find a topic like this in here so, Post your source code that only you have made dont steal anyone elses stuff. Say what language you did it in and what it dose.

    Language: Java
    Purpose: Simple 8 ball program

    Code:
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.util.Arrays;
    import java.util.List;
    
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    import com.jgoodies.forms.layout.CellConstraints;
    import com.jgoodies.forms.layout.FormLayout;
    
    
    public class CopyOfEightBall {
    	public static void main(String[] args) {
    		
    		final JTextField question = new JTextField();
    		final List<String> list = Arrays.asList(
    				"Yes",
    				"My sources say no.",
    				"Yes, in due time.",
    				"Definitely not.",
    				"You will have to wait.",
    				"I have my doubts.",
    				"Outlook so so.",
    				"Looks good to me!",
    				"Who knows?",
    				"Looking good!",
    				"Probably.",
    				"Are you kidding?",
    				"Go for it!",
    				"Don't bet on it."
    				);
    		
    		Action askAction = new AbstractAction("Ask") {
    			public void actionPerformed(ActionEvent e) {
    				final int num = (int)(Math.random()*list.size());
    				question.setText(list.get(num));
    			}
    			
    		};
    		
    		
    		JButton ask = new JButton (askAction);
    		
    		
    		FormLayout layout = new FormLayout("30dlu, 4dlu, 30dlu, 30dlu, min",
    		"pref, 2dlu, pref, 2dlu, pref, 2dlu, pref");
    		
    
    		layout.setRowGroups(new int[][] { { 1, 3, 5 } });
    
    		JPanel panel = new JPanel(layout);
    		
    		CellConstraints cc = new CellConstraints();
    		panel.add(new JLabel("Ask a Question"), cc.xy(1, 1));
    		panel.add(question, cc.xyw(3, 1, 3));
    		panel.add(ask, cc.xy(3,3 )  );
    		
    		
    		JFrame frame = new JFrame();
    		frame.setContentPane(panel);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setTitle("Finding Slope");
    		frame.pack();
    		frame.setBackground(Color.black);
    		frame.setVisible(true);
    	}
    
    	}
    Language: Java
    Purpose: Do my some math homework and show the work

    Code:
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    import com.jgoodies.forms.layout.CellConstraints;
    import com.jgoodies.forms.layout.FormLayout;
    
    
    public class Coordinates {
    	public static void main(String[] args) {
    		
    		final JTextField coordinateOneField = new JTextField();
    		final JTextField coordinateTwoField = new JTextField();
    		final JTextField answerField = new JTextField();
    		final JTextField step1 = new JTextField();
    		JTextField step2 = new JTextField();
    		step1.setEnabled(false);
    		step2.setEnabled(false);
    		answerField.setEnabled(false);
    
    		Action sumAction = new AbstractAction("sum") {
    			private static final long serialVersionUID = 0L;
    
    			public void actionPerformed(ActionEvent e) {
    				int[] coordinate1 = extractCoordinateFrom(coordinateOneField);
    				int[] coordinate2 = extractCoordinateFrom(coordinateTwoField);
    				step1.setText("m = " + "(" + coordinate2[1] + "-" +
    					coordinate1[1] + ")" + "/" + "(" + coordinate2[0] + "-" + 
    					coordinate1[0] + ")");
    				answerField.setText("m = " +
    						(coordinate2[1] - coordinate1[1])
    						+ "/" + 
    						(coordinate2[0] - coordinate1[0]));
    			}
    
    			private int[] extractCoordinateFrom(JTextField textField) {
    				String text = textField.getText().replaceAll("\\(|\\)", "");
    				String[] values = text.split(",");
    				int[] numbers = new int[values.length];
    				for(int i = 0; i < values.length; ++i) {
    					numbers[i] = Integer.parseInt(values[i]);
    				}
    				return numbers;
    			}
    		};
    		
    		Action clearAction = new AbstractAction("Clear") {
    			public void actionPerformed(ActionEvent e) {
    				coordinateOneField.setText("");
    				coordinateTwoField.setText("");
    				step1.setText("");
    				answerField.setText("");
    				
    			}
    			
    		};
    
    		JButton detailsButton = new JButton(sumAction);
    		JButton clear = new JButton(clearAction);
    		
    		FormLayout layout = new FormLayout("pref, 4dlu, 50dlu, 4dlu, min", // columns
    		"pref, 2dlu, pref, 2dlu, pref, 2dlu, pref, 2dlu, pref");
    		
    
    		layout.setRowGroups(new int[][] { { 1, 3, 5, 7, 9 } });
    
    		JPanel panel = new JPanel(layout);
    		
    		CellConstraints cc = new CellConstraints();
    		panel.add(new JLabel("Coordinate 1"), cc.xy(1, 1));
    		panel.add(coordinateOneField, cc.xyw(3, 1, 2));
    		panel.add(new JLabel("Coordinate 2"), cc.xy(1, 3));
    		panel.add(coordinateTwoField, cc.xyw(3, 3, 2));
    		panel.add(new JLabel("Step 1"), cc.xy(1,5));
    		panel.add(step1, cc.xyw(3,5,2));
    		panel.add(new JLabel("Answer"), cc.xy(1, 7));
    		panel.add(answerField, cc.xyw(3, 7, 2));
    		panel.add(detailsButton, cc.xy(1, 9));
    		panel.add(clear, cc.xy(3,9 ));
    		
    		JFrame frame = new JFrame();
    		frame.setContentPane(panel);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setTitle("Finding Slope");
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    }


    Reply With Quote  
     

  2.  
     

  3. #2  
    Forum Freshman
    Join Date
    Oct 2007
    Posts
    8
    Language: Java
    purpose: Lets you type text and then lets you type in a new file name
    that it will create and put the text in.

    Code:
    import java.awt.event.ActionEvent;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    import com.jgoodies.forms.layout.CellConstraints;
    import com.jgoodies.forms.layout.FormLayout;
    
    
    public class Dspbb {
    public static void main(String[] args) {
    	
    	final JTextArea numField = new JTextArea("");
    	final JTextField nameField = new JTextField("");
    	
    	Action SubmitAction = new AbstractAction("Submit") {
    		public void actionPerformed(ActionEvent e) {
    			final String file2 = nameField.getText();
    			final String sum = numField.getText();
    			String[] lines = sum.split("\n");
    			
    			FileOutputStream fout;
    			 try {
    				fout = new FileOutputStream (file2);
    				new PrintStream(fout).println(sum);
    				fout.close();
    			 }
    				catch(IOException e1){
    					System.err.println("Unalbe to complete");
    			}
    				}
    		};
    
    	
    	JButton submit = new JButton(SubmitAction);
    	
    	
    	
    	FormLayout layout = new FormLayout("pref, 4dlu, 50dlu, 4dlu, min", // columns
    	"pref, 2dlu, pref, 2dlu, pref, 2dlu, pref, 2dlu, pref");
    	
    
    	layout.setRowGroups(new int[][] { { 1, 3, 5, 7, 9 } });
    
    	JPanel panel = new JPanel(layout);
    	
    	CellConstraints cc = new CellConstraints();
    	panel.add(submit, cc.xy(1, 1));
    	panel.add(nameField, cc.xy(3, 1));
    	panel.add(numField, cc.xy(1, 3));
    
    	
    	JFrame frame = new JFrame();
    	frame.setContentPane(panel);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setTitle("Finding Slope");
    	frame.pack();
    	frame.setVisible(true);
    }
    
    	
    }


    Reply With Quote  
     

  4. #3  
    Forum Freshman
    Join Date
    Oct 2007
    Posts
    8
    Language: C
    Purpose: Tells about How old you are!

    Code:
    #include <stdio.h>
    
    main() {
      int years days;
    
      printf("How old are you?");
      scanf("%i", &years);
      days = years * 356;
      printf("You are about %i days old", days);
    
    }
    Reply With Quote  
     

  5. #4  
    Forum Freshman
    Join Date
    Oct 2007
    Posts
    8
    Language: Java
    Purpose: Simple PortScanner

    Code:
         import java.net.Socket;
      public Class PortScanner {
         public static void main(String[] args){
         PortScanner portScanner = new PortScanner;
         for( int 1 = 10; i < 9000; ++i) {
         boolean portFound = portScanner.Scan("localhost", + i);
         if(portFound) {
         System.out.println("Port foune on" + i);
                }
             }
          }
          Static Class PortScanner {
             public boolean Scan(String host, int port) {
           try {
             Socket socket = new Socket (host, port);
             Socket.close;
             return true;
             } Catch(exception e) {
                   return false;
                            }
                          }
                     }
              }
    Reply With Quote  
     

  6. #5  
    New Member
    Join Date
    Oct 2007
    Posts
    3
    Quote Originally Posted by blacksage
    Language: C
    Purpose: Tells about How old you are!

    Code:
    #include <stdio.h>
    
    main() {
      int years days;
    
      printf("How old are you?");
      scanf("%i", &years);
      days = years * 356;
      printf("You are about %i days old", days);
    
    }
    356 should be 365, right?

    What's the reason for this topic anyways? Why do you want us to post the code we've written?
    Reply With Quote  
     

  7. #6  
    Forum Freshman
    Join Date
    Oct 2007
    Posts
    8
    I think its a good topic to learn from other peoples stuff and get ideas for your own programs or to just show off something you wrote.
    Reply With Quote  
     

Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •