After 3 Days of coding

My brain is boiling.

Yes. Thats true. This is possible. When You starting such difficult thing You have to prepare for very intensive thinking. All the time.  When I gets up I am thinking about my homework, problem, solving this problems, thinking about every aspect of things I had learned.

Some times I have doubts and sometimes I am frustrated. But as Louis Zamperini said: Never give up, no matter what!  And I am holding this words very hard. I will not stop to learn.

Because there is something what I like in coding.

Do You know what is it?

Satisfaction! That feeling when You compile code and code is working with out mistakes and exceptions.

I have to work on my code. Sometimes when I code I want to open opened door. And my code is not clean as want to be.

My biggest problem is that I know what I have to do, how algorithm or program have to work but when I sit down and want to code it I have wall a front of me. I think every beginning coder have same problems. My coach says that solution will come with time. I hope so, I believe it will come!

And what is most important! I want to do this I like it and I can’t wait for weekend for new lessons!

Scope of knowledge after 3 days is:

  • types
  • methods
  • conditions
  • loops
  • tables
  • basics object programming //only theory
  • setters and getters

My homework:

  1. Draw 6 numbers from the table with range 1-49 //maybe some day I win some $$ 😉
  2. You have table with number and You ask user to choose table index and number You want to add to table

As always You can use those code if You want.

Link to Github: github.com/abramovvicz

So first exercise is:

package domowe;

import java.util.Random;

public class Lotto {
    int table[] = new int[6];
    int table2[] = new int[6];

    public static void main(String[] args) {
        Lotto lotto = new Lotto();
        lotto.chooseNumbers();
        lotto.checkNumbers();
    }

    private void chooseNumbers() {
        Random random = new Random();

        for (int i = 0; i < table.length; i++) {
            table[i] = random.nextInt(49) + 1;
            table2[i] = table[i];
        }
    }
    
    private void checkNumbers() {
        System.out.println("    ");
        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table2.length; j++) {
                if (i != j) {
                    if (table[i] == table2[j]) {
                        chooseNumbers();
                    }
                }
            }
            System.out.println("Prędzej wygram w totka niż zostanę programistą? Sprawdźmy to!: " + table[i]);
        }
    }
}

Second exercise:

package domowe;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class ArrayTest {
    private int[] table1 = new int[6];

    public static void main(String[] args) {

        ArrayTest arrayTest = new ArrayTest();
        arrayTest.fillTable();
        arrayTest.write();
        Arrays.sort(arrayTest.table1);
        arrayTest.write();
        arrayTest.addNumberToIndex();
        arrayTest.write();
    }

    private void addNumberToIndex() {
        Scanner giveIndex = new Scanner(System.in);
        Scanner giveNumber = new Scanner(System.in);
        System.out.println("Enter index of the array: ");
        int tableIndex = giveIndex.nextInt();


        if (tableIndex < table1.length) {
            System.out.println("Table length is: " + table1.length + " \n you have exceeded the length of the table, give another index");
            return;
        } else {
            System.out.println("Enter number to add to table");
            int number = giveNumber.nextInt();
            int[] table2 = new int[table1.length + 1];
            for (int i = tableIndex; i < table2.length; i++) {
                if (tableIndex == 0 && i < table1.length) {
                    table2[i + 1] = table1[i];
                } else {
                    table2[i] = table1[i - 1];
                }
                for (int j = 0; j &lt; table1.length; j++) {
                    if (table2[j] == 0) {
                        table2[j] = table1[j];
                    }

                }
            }
            table2[tableIndex] = number;
            table1 = table2;
        }

    }

    private void fillTable() {
        int max = 1000;
        Random random = new Random();
        for (int i = 0; i < table1.length; i++) {
            table1[i] = random.nextInt(max);

        }
    }

    private void write() {
        System.out.println("tablica: " + Arrays.toString(table1));
    }

    private void addNumber(int a) {
        int[] tablica2 = Arrays.copyOf(table1, table1.length + 1);
        tablica2[tablica2.length - 1] = a;
        table1 = tablica2;
    }
}

Lesson #1 – headache from Java Basics

So, I`m after first lesson of Java Basics. What can I say? It will be tuff road to achieve goal.

For now we have theory with some small coding. Coach introduced a lot of material that U can find in Udemy Java Basics. He explain everything from beginning. Said about basics type, methods, packages, basic math operations, declaration of class type, method type, differences of private, public, protected. But He always repeat – “You will learn about it later, we will talk about the details later!” So to be honest I cant wait for the weekend to go to school and learn.

Tip for beginner.  You should watch Udemy course before Your course. It will be much more easier to keep up with coach.

But I know theory is theory practice is practice! After 7 hours (sic) of lessons I get headache but I know  it`s worth it!

I write two tiny codes:

  1. Calculator:
  2. Homework

Calculator:

package day1;

public class Calculator {

    static int a = 5; //declaration class type
    static int b = 6; //declaration class type
    
    public static int sum() { //declaration method
    }

    public static int substraction() { // return method
        return a - b;
    }

    public static int multiplitaction() {
        return a * b;
    }

    public static double division() {
        double c = (double)a/b;  // variable projection 
        return c;
    }

    public static void main(String[] args) {
        int resultSum = sum();
        System.out.println("sum " + resultSum);
        int resultSubstraction = substraction();
        System.out.println("substraction " + resultSubstraction);
        int resultMultiplication = multiplitaction();
        System.out.println("multiplication " + resultMultiplication);
        double resultDivision = division();
        System.out.println("division " +  resultDivision);
    }
}

Homework – at first glance I thought that is easy to do. But after trying to write it I had to think a lot and I get headache 😀

Topic: Write method accepting two arguments int type and which returns result of divide those types in a fraction format. Example: 1 2/5. And You have to remember when You divide two int You get int and You cant division by zero. Catchy isn`t it?

Answer:

package homeworks;
public class Homework1 {
    public static void main(String[] args) {
        System.out.println(divide(7, 5));
        System.out.println(divide(10, 5));
        System.out.println(divide(10, 0));
    }
    static String divide(int i, int j) {
        if (j != 0) { // check denominator it cant be 0
            int div = i / j;
            if (i % j == 0) { // check modulo, when it is 0 return string whit out fraction
                return "" + div;
            } else { // else modulo is not 0 your result should be fraction
                int mod = i % j;
                return "" + div + " " + mod + "/" + j;
            }
        } else {
            System.out.println("You have been trying to divide by zero");
            return "";
        }
    }
}

Two difficult things. First. Method should returns String! Why? Because You have to return division in fraction as example is 1 2/5 not 1.4. Second I didn`t know that You can combine String with int(double etc).

Example:

 return "" + someNumber + "/" + someNumber2

Modulo is very ancillary calculation. Coach said that we are going to use it a lot of time.

Check this:  7%5 = 2. You know after that Your result is fraction. So You should return total number ( 1 because 5 in 7 is 1) + modulo + denominator.  Know You will ask what about result for ex.  4/8 ? You should correct to 1/2. Yes, You have right. Know I am going to correct this!  Stay tuned!