Thursday, October 30, 2014

Need help with calling methods


I have a driver and a main program.


How would I go along with calling the encode method to the driver class that I made so I can have the user inputs affected by the encode method?



Java Code:



public class ShiftEncoderDecoder
{

private int shift;

public ShiftEncoderDecoder(int shift)
{
setShift(shift);
}

public int getShift()
{
return shift;
}
private String prepareString(String plainText)
{
String preparedString = "";
for(int i = 0 ; i < plainText.length();i++)
if(Character.isAlphabetic(plainText.charAt(i)))
{
preparedString = preparedString+Character.toUpperCase(plainText.charAt(i));
}
return preparedString;
}
public String encode(String plainText)
{
int prepareString;
int shiftChar;
String preparedString2 = prepareString(plainText);

String cipherText = "";
for(int c = 0 ; c < preparedString2.length();c++)
if(Character.isAlphabetic(preparedString2.charAt(c)))
{
cipherText = cipherText+shiftChar(preparedString2.charAt(c),shift);
}
return cipherText;
}
public String decode(String cipherText)
{
String decodedText ="";
for(int z = 0 ; z < cipherText.length();z++)
if(Character.isAlphabetic(cipherText.charAt(z)))
{
decodedText = decodedText+shiftChar(cipherText.charAt(z),-shift);
}
return decodedText;
}
}


Java Code:



import java.lang.*;
import java.util.*;
import java.util.Scanner;

public class ShiftEncoderDecoderDriver
{
public static void main(String[] args)
{
Scanner uInput = new Scanner(System.in);
System.out.print("Please enter the code that you wish to encode...");
String plainText = uInput.nextLine();
int shift;
do
{
System.out.println("Please enter the amount of spaces the letters will shift... ");
shift = uInput.nextInt();
if(shift <= 25 && shift >= -25)
{
System.out.println("The letters will shift "+shift+" places.");
break;
}
else if(shift > 25)
{
System.out.println("Your input is larger then how far the letters are allowed to shift, try again.");
}
else if(shift < -25)
{
System.out.println("Your input is smaller then how far the letters are allowed to shift, try again.");
}
}
while(shift <= 25 || shift >= -25);
{
System.out.println("The input you wish to encode... "+plainText);
System.out.println("How many spaces the letters will shift... "+shift);
}
ShiftEncoderDecoder cipher = new ShiftEncoderDecoder();
plaintText.encode();
}
}


No comments:

Post a Comment