So I would be very grateful if you could check these four tasks - I'm a Java newbie and recently coded such things on a Introduction to CS test which I didn't score so well at and would love to know where my mistakes were, what I should do some other way and how can I get better at it to score higher again :)
1> Write a method that takes an array of integers (you can assume the amount is divisble by four) and decodes them in the said manner:
- add 15 to each integer unless it's equal to 0
- divide the numbers in four-integer sub-words
- let the first charater become the third, the fourth one be the first, the third - the second and the second be the last
For example, {97,90,82,0} should output " api".
public static String decipher(int[] specArray)
{
String finalWord = "";
for(int i=0; i<specArray.length; ++i)
{
if(specArray[i]!=0) specArray[i]+=15;
}
for(int i=0; i<specArray.length; i+=4)
{
finalWord+=(char)specArray[i+3];
finalWord+=(char)specArray[i+2];
finalWord+=(char)specArray[i];
finalWord+=(char)specArray[i+1];
}
return finalWord;
}
2> Write a method that takes a String as an argument and also returns a String and processes it from left to right as follows:
- if it finds a, it should add two a's to the result
- if it finds two adjacent b's or one b alone, it should add one b to the result
- if it finds two c's, it should add them both to the result. If it finds one c only, it should skip it.
For example: "abaabbcbaccb" returns "aabaaaabbaaccb" and "bbbbbbb" returns "bbbb".
public static String processWord(String inputWord)
{
String outputWord = "";
for(int i=0; i<inputWord.length(); ++i)
{
switch(inputWord.charAt(i)){
case 'a':
outputWord+="aa";
break;
case 'b':
if(((i+1)<inputWord.length())&&(inputWord.charAt(i+1)=='b')) // if there's at least one char left in the string and the next is b
{
i+=1;
}
outputWord += 'b';
break;
case 'c':
if(((i+1)<inputWord.length())&&(inputWord.charAt(i+1)=='c')) // if there's at least one char left in the string and the next is c
{
outputWord += "cc";
i+=1;
}
break;
}
}
return outputWord;
}
3> Create a class MyClass with two class variables - myNumber of type double and myString of type String. Also, add a default constructor giving myNumber a value of 13.13 and myString value "some string" and a constructor taking a double and String parameters, then assigning them to myNumber and myString respectively. Add appropriate methods for getting and setting the values of the class.
public class MyClass {
private double myNumber;
private String myString;
// CONSTRUCTORS
public MyClass(){
myNumber = 13.13;
myString = "some string";
}
public MyClass(double d, String s){
myNumber = d;
myString = s;
}
// METHODS
public double getNumber()
{
return myNumber;
}
public void setNumber(double newN)
{
myNumber = newN;
}
public String getString()
{
return myString;
}
public void setString(String newS)
{
myString = newS;
}
}
4> Create a method working similarly to String.indexOf() but taking as parameters a source String in which it should look and String containing the word it should look for in the source. It should return an integer array containing all the occurrences of said sub-word in a word denoted by indexes of the first letters.
For example, ("ThiDFUs iDFs a DF D DFU sample.", "DFU") should return an array containing 3 and 20.
public static int[] differentIndexOf(String source, String divisor)
{
ArrayList<Integer> occurrenceFound = new ArrayList<Integer>();
int suspectedStart=-1;
for(int i=0; i<source.length(); ++i) // check the whole word letter-by-letter
{
if(source.charAt(i)==divisor.charAt(0)) // if one of the letters corresponds the first one of the separator:
{
suspectedStart = i;
for(int j=1; j<divisor.length(); ++j) // check the ones after it until the separator is fully used
{
if(source.charAt(i+j) != divisor.charAt(j)) suspectedStart = -1; // (^) or until you find a difference
}
}
if(suspectedStart != -1) {occurrenceFound.add(suspectedStart);} // if no differences found in (^) => occurrence found
suspectedStart = -1;
}
int[] finalArray = new int[occurrenceFound.size()];
for(int i=0; i<finalArray.length; ++i)
{
finalArray[i] = occurrenceFound.get(i);
}
return finalArray;
}