Methods and Control Structures

public class LogMessage{
    private String machineId;
    private String description;
// a log message is formatted as message: description
    public LogMessage(String message)
{
    int colonIndex = message.indexOf(":"); //finding the index of the colon
    machineId = message.substring(0, colonIndex); //substring from the beginning to the colon index
    description = message.substring(colonIndex + 1); //from the colon to the end
    System.out.println(machineId); 
    System.out.println(description);
    //printing the message and description. 

}



public boolean containsWord(String keyword)
{
    int keywordIndex = description.indexOf(keyword);

    while(keywordIndex != -1) // making sure the substring is in the description
    {
        int beforeIndex = keywordIndex - 1;
        int afterIndex = keywordIndex + keyword.length();

        if((beforeIndex == -1 || description.substring(beforeIndex, beforeIndex + 1).equals(" ")) // if the str is at the beginning OR is immedietly preceeded by a space
                && (afterIndex == description.length() ||
                description.substring(afterIndex, afterIndex + 1).equals(" "))) // AND str is at the end, and is immeditely followed by a space
            return true;

        keywordIndex = description.indexOf(keyword, keywordIndex + 1); 
    }

    return false; // if all that is not true, return false
}
    public static void main(String[] args){
        LogMessage obj = new LogMessage("aadya:daita");
        System.out.println(obj.containsWord("yd"));

    }
}


LogMessage.main(null);
aadya
daita
false

Classes and Objects + Inheritence

public class RandomStringChooser
{
    private ArrayList<String> valuesRemaining;

    public RandomStringChooser(String[] values)
    {
        valuesRemaining = new ArrayList<String>(); //initialize arraylist

        for(String value : values)
            valuesRemaining.add(value); //add all the strings from values
    }

    public String getString(){
        int index = (int) (Math.random() * valuesRemaining.size()); //picking a random index
        return valuesRemaining.get(index);   //getting the string at that random index
    }
      public String getNext()
    {
        if(valuesRemaining.size() == 0)
            return "NONE"; //if the size is 0, then return none

        int index = (int) (Math.random() * valuesRemaining.size());
        return valuesRemaining.remove(index); //keep getting the random indeces, and them removes those string from the arraylist
    }
    public static void main(String[] args){
        String[] strings = {"hello", "my", "name", "is", "aadya"};
        RandomStringChooser obj = new RandomStringChooser(strings);
        for(int i=0; i<5; i++){
            System.out.println(obj.getNext());
        }
    }
}
RandomStringChooser.main(null);
hello
is
my
name
aadya
public class RandomLetterChooser extends RandomStringChooser{ //extends keyword
    private String result = "";
    
    public RandomLetterChooser(){
            result = super.getString(); //getting the random substring from the parent class, and then getting a single letter from it
        }
        public String getResult(){
            return result; //and thnen printing that

        } //esssentially making sure super works
    
        public static void main(String[] args){
            RandomLetterChooser obj = new RandomLetterChooser();
            obj.getResult();
            obj.getResult();//ran four times for random letters
            obj.getResult();
            obj.getResult();
        }        
    }
    RandomLetterChooser.main(null);
a
d
a
y

ArrayLists

public class numberFour{
    public static int totalLetters(List<String> wordList)
{
    int letters = 0; //making all the words in a array list into letters

    for(String word : wordList)
        letters += word.length();

    return letters;
}

public static int basicGapWidth(List<String> wordList, int formattedLen)
{// finding the basic Gap width
    int gaps = wordList.size() - 1; //finding the numebr of spaces 
    int spaces = formattedLen - totalLetters(wordList); //spaces left 
    return spaces / gaps; // the gap width is spaces left divided by te number of spaces
}

public static int leftoverSpaces(){
    return 4; // static value
}

public static String format(List<String> wordList, int formattedLen)
{
    int width = basicGapWidth(wordList, formattedLen);
    int leftoverRemaining = leftoverSpaces();

    String formatted = "";
    for(int i = 0; i < wordList.size() - 1; i++)
    {
        formatted += wordList.get(i); //adding the word into the main message

        for(int s = 1; s <= width; s++)
            formatted += " "; // add however many spaces there are in the gap width. 
//in this case this is 2
        if(leftoverRemaining > 0)
        {
            formatted += " "; //adding spaces at the end of the springs as well
            leftoverRemaining--; //decrementing the size of the formatted length
        }
    }

    formatted += wordList.get(wordList.size() - 1);

    return formatted; //return string formatted
}

public static void main(String[] args){
    ArrayList<String> string = new ArrayList<String>();
    string.add("ap");
    string.add("comp");
    string.add("sci");
    string.add("is");
    string.add("fun");
    numberFour obj = new numberFour();
    System.out.println("total letters: " + obj.totalLetters(string));
    System.out.println("gap width: " + obj.basicGapWidth(string, 20));
    System.out.println("message: [" + obj.format(string,20) + "]");

}


}
numberFour.main(null);
total letters: 14
gap width: 2
message: [ap  comp  sci  is  fun]

2D Arrays

public class 2DArr{ //combination of array and 2D array
    
    public static int arraySum(int[] arr){
    int sum = 0;

    for(int n : arr)
        sum += n;
        return sum; //find the sum of all the values in a given array
    }

    public static int[] rowSums(int[][] arr2D){
    int[] sums = new int[arr2D.length]; //create new array

    for(int i = 0; i < sums.length; i++){
        sums[i] = arraySum(arr2D[i]); //summing all the values in reach row and putting them in a seperate array
    }

     return sums; //return the sum aaray
    }

    public static boolean isDiverse(int[][] arr2D){
    int[] sums = rowSums(arr2D);

    for(int i = 0; i < sums.length; i++)
        for(int j = i+1; j < sums.length; j++) //used a nested for loop to check if all the valuesin the sum array are unique
            if(sums[i] == sums[j]) // if any of them are , then return false
                return false;

    return true;// else, return true
    }

    public static void main(String[] args){
        int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
        2DArr obj = new 2DArr();

        int [] arr2 = obj.rowSums(arr);

        System.out.println("is the sum of each row diver? : " + obj.isDiverse(arr));
        for(int i=0; i<arr2.length; i++){
            System.out.print(arr2[i] + " "); //return the array of all the sums
        }
        
    }

}

2DArr.main(null);
is the sum of each row diver? : true
6 15 24