Im trying to program a Caeser cipher that will upload a file from documents and enrypt/decrypt it. i do not know how to implement one main class containing all that and different methods in one class.
i have different separate classes. can anyone pls put them together as one main class
here is an upload class code
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Uploadfile extends Frame implements ActionListener {
String directory; // The default directory to display in the FileDialog
TextArea textarea; // The area to display the file contents into
/** Convenience constructor: file viewer starts out blank */
public Uploadfile() { this(null, null); }
/** Convenience constructor: display file from current directory */
public Uploadfile(String filename) { this(null, filename); }
/**
* The real constructor. Create a FileViewer object to display the
* specified file from the specified directory
**/
public Uploadfile(String directory, String filename) {
super(); // Create the frame
// Destroy the window when the user requests it
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { dispose(); }
});
// Create a TextArea to display the contents of the file in
textarea = new TextArea("", 24, 80);
textarea.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
textarea.setEditable(false);
this.add("Center", textarea);
// Create a bottom panel to hold a couple of buttons in
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 8));
this.add(p, "South");
// Create the buttons and arrange to handle button clicks
Font font = new Font("SansSerif", Font.BOLD, 12);
Button openfile = new Button("Upload your file");
Button cancel = new Button("Cancel");
openfile.addActionListener(this);
openfile.setActionCommand("open");
openfile.setFont(font);
cancel.addActionListener(this);
cancel.setActionCommand("Cancel");
cancel.setFont(font);
p.add(openfile);
p.add(cancel);
Button send = new Button("send");
send.addActionListener(this);
send.setFont(font);
this.pack();
// Figure out the directory, from filename or current dir, if necessary
if (directory == null) {
File f;
if ((filename != null)&& (f = new File(filename)).isAbsolute()) {
directory = f.getParent();
filename = f.getName();
}
else directory = System.getProperty("user.dir");
}
this.directory = directory; // Remember the directory, for FileDialog
setFile(directory, filename); // Now load and display the file
}
/**
* Load and display the specified file from the specified directory
**/
public void setFile(String directory, String filename) {
if ((filename == null) || (filename.length() == 0)) return;
File f;
FileReader in = null;
// Read and display the file contents. Since we're reading text, we
// use a FileReader instead of a FileInputStream.
try {
f = new File(directory, filename); // Create a file object
in = new FileReader(f); // And a char stream to read it
char[] buffer = new char[4096]; // Read 4K characters at a time
int len; // How many chars read each time
textarea.setText(""); // Clear the text area
while((len = in.read(buffer)) != -1) { // Read a batch of chars
String s = new String(buffer, 0, len); // Convert to a string
textarea.append(s); // And display them
}
this.setTitle("Uploadfile: " + filename); // Set the window title
textarea.setCaretPosition(0);
this.setTitle("send: " + filename); // Set the window title
textarea.setCaretPosition(5);// Go to start of file
}
// Display messages if something goes wrong
catch (IOException e) {
textarea.setText(e.getClass().getName() + ": " + e.getMessage());
this.setTitle("Upload your file: " + filename + ": I/O Exception");
}
// Always be sure to close the input stream!
finally { try { if (in!=null) in.close(); } catch (IOException e) {} }
}
/**
* Handle button clicks
**/
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("open")) { // If user clicked "Open" button
// Create a file dialog box to prompt for a new file to display
FileDialog f = new FileDialog(this, "Uplaod File", FileDialog.LOAD);
f.setDirectory(directory); // Set the default directory
// Display the dialog and wait for the user's response
f.show();
directory = f.getDirectory(); // Remember new default directory
setFile(directory, f.getFile()); // Load and display selection
f.dispose(); // Get rid of the dialog box
}
else if (cmd.equals("Cancel")) // If user clicked "Close" button
this.dispose(); // then close the window
}
/**
* The FileViewer can be used by other classes, or it can be
* used standalone with this main() method.
**/
static public void main(String[] args) throws IOException {
// Create a FileViewer object
Frame f = new Uploadfile((args.length == 1)?args[0]:null);
// Arrange to exit when the FileViewer window closes
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) { System.exit(0); }
});
// And pop the window up
f.show();
}
}
here is anothey shift cipher class codes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class ShiftCipher extends JApplet implements ActionListener
{
public void init () {
initComponents();
setSize(250, 150);
}
private javax.swing.JTextField kTextField;
private javax.swing.JButton encryptButton;
private javax.swing.JButton decryptButton;
private javax.swing.JTextArea inputTextArea;
private javax.swing.JTextArea outputTextArea;
private javax.swing.JPanel parameterPanel;
private javax.swing.JPanel textPanel;
private javax.swing.JPanel inputPanel;
private javax.swing.JPanel outputPanel;
private javax.swing.JPanel topInputPanel;
private javax.swing.JPanel topOutputPanel;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private Container mainContainer;
private final int width=300, height=400;
private boolean casechange=false, clean=false;
private javax.swing.JButton attachfileButton;
private void initComponents() {
mainContainer = getContentPane();
mainContainer.setLayout(new BorderLayout(1,0));
jLabel2 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel2.setText("Encryp/Decryp key=");
jLabel4.setText("Enter plain or cipher text down here");
kTextField = new javax.swing.JTextField(3);
jLabel5.setText("OR");
parameterPanel = new javax.swing.JPanel();
parameterPanel.add(jLabel2);
parameterPanel.add(kTextField);
topInputPanel = new javax.swing.JPanel();
topInputPanel.add(jLabel4);
inputTextArea = new javax.swing.JTextArea();
inputTextArea.setLineWrap(true);
inputTextArea.setText("");
inputPanel = new javax.swing.JPanel();
inputPanel.setLayout(new BorderLayout());
inputPanel.add(topInputPanel,BorderLayout.NORTH);
inputPanel.add(inputTextArea,BorderLayout.CENTER);
attachfileButton = new javax.swing.JButton();
encryptButton = new javax.swing.JButton();
decryptButton = new javax.swing.JButton();
attachfileButton = new javax.swing.JButton();
encryptButton.setText("Encrypt");
encryptButton.addActionListener(this);
decryptButton.setText("Decrypt");
decryptButton.addActionListener(this);
attachfileButton.setText("Attach a file");
attachfileButton.addActionListener(this);
topOutputPanel = new javax.swing.JPanel();
topOutputPanel.add(encryptButton);
topOutputPanel.add(jLabel5);
topOutputPanel.add(decryptButton);
outputTextArea = new javax.swing.JTextArea();
outputTextArea.setText("");
outputTextArea.setLineWrap(true);
outputPanel = new javax.swing.JPanel();
outputPanel.setLayout(new BorderLayout(0,0));
outputPanel.add(topOutputPanel,BorderLayout.NORTH);
outputPanel.add(outputTextArea, BorderLayout.CENTER);
textPanel = new javax.swing.JPanel();
textPanel.setLayout(new GridLayout(2,2));
textPanel.add(inputPanel);
textPanel.add(outputPanel);
mainContainer.add(parameterPanel, BorderLayout.NORTH);
mainContainer.add(textPanel, BorderLayout.CENTER);
}
public String cleanString(String toClean)
{
String returnString = new String();
for(int i=0;i<toClean.length();i++)
{
if (Character.isLetter(toClean.charAt(i)) == true)
returnString = returnString + toClean.charAt(i);
}
return returnString;
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==encryptButton)
{
String toEncrypt = new String(inputTextArea.getText());
int k = Integer.parseInt(kTextField.getText()) % 26;
toEncrypt = toEncrypt.toUpperCase();
char charArray[] = toEncrypt.toCharArray();
for(int i=0;i<toEncrypt.length();i++)
{
if (Character.isLetter(charArray[i]))
if (Character.isUpperCase(charArray[i]))
charArray[i] = (char)(((int)charArray[i] - 65 - k + 26) % 26 + 65);
else
charArray[i] = (char)(((int)charArray[i] - 97 - k + 26) % 26 + 97);
}
toEncrypt = new String(charArray);
outputTextArea.setText(toEncrypt);
}
else if (e.getSource()==decryptButton)
{
String toDecrypt = new String(inputTextArea.getText());
int k = Integer.parseInt(kTextField.getText()) % 26;
toDecrypt = toDecrypt.toUpperCase();
char charArray[] = toDecrypt.toCharArray();
for(int i=0;i<toDecrypt.length();i++)
{
if (Character.isLetter(charArray[i]))
if (Character.isUpperCase(charArray[i]))
charArray[i] = (char)(((int)charArray[i] - 65 + k + 26) % 26 + 65);
else
charArray[i] = (char)(((int)charArray[i] - 97 + k + 26) % 26 + 97);
}
toDecrypt = new String(charArray);
outputTextArea.setText(toDecrypt);
}
}
}
can please anyone make the above classes as one main method in one class for me. i got stuck.
Thank u in advance!!