Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion ListsAndArrays/src/AccountBook.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@

public class AccountBook {
//intermediate (multidimensional arrays)

//todo: a bank will give you 2% interest on your savings stored on an account book.
//for a certain amount of money you want to store in the account book create a multidimensional array where
//the interest of the next 10 years is stored.
//For that save the year and the amount of money. Write a method which takes a certain year as input (e.g. 2019) and an amount of money you would like to store
//and returns the saving you would have for this year.

public static double[][] accountBook(double y) {
double x[][] = new double[11][2];
double year = 2017.0;
x[0][0] = year;
x[0][1] = y;
for (int i=1; i<11; i++) {
year = year + 1;
x[i][0] = year;
for (int j=1; j<2; j++ ) {
x[i][j] = x[i -1][j] + x[i -1][j]*0.02;
}
}
return x;
}

public static void money(double x[][], double y) {
for (int i=0; i<x.length; i++) {
for (int j= 0; j<x[i].length; j++) {
if (x[i][j] == y) {
System.out.println("The ammount of money in year " + x[i][j] + " will be " + x[i][(j + 1)]);
}
}
}
}

public static void main(String[] args) {
double x[][] = accountBook(100.0);
for (int i=0; i<x.length; i++) {
for (int j=0; j<x[i].length; j++) {
System.out.println(x[i][j]);
}
}
money(x, 2026);
}
}
39 changes: 39 additions & 0 deletions ListsAndArrays/src/Basics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,45 @@
public class Basics {
//beginner

public static void length(int[] x) {
System.out.println("The length of the array is: " + x.length);

}

public static void dropElement(int[] x) {
int y[] = new int[x.length -1];
for (int i = 0; i < y.length; i++) {
y[i] = x[i];
}
for (int i = 0; i < y.length; i++) {
System.out.println("Array is now x[" + i + "]: " + y[i]);
}

}

public static void sum(int[] x) {
int sum = 0;
for (int i = 0; i < x.length; i++) {
sum += x[i];
}
System.out.println("The sum of of all elements of the array is: " + sum);
}

public static void check(int x[], int element) {
for (int i = 0; i < x.length; i++) {
if (x[i] == element) {
System.out.println("The " + element + " is in row " + i);
}
}
}

public static void main(String[] args) {
int[] x = {2, 5, 7, 8, 9, 25, 55};
length(x);
dropElement(x);
sum(x);
check(x, 9);
}
//todo: write some methods for some basic operations with arrays, like:
//-return the length of an array
//-drop the last element of an array
Expand Down
23 changes: 22 additions & 1 deletion ListsAndArrays/src/BasicsArrayList.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@

import java.util.*;
public class BasicsArrayList {
//beginner

//todo: write some methods for basic operations with ArrayLists:
//-write a method where you can add a certain value to an ArrayList
//-write a method which prints all the values in an ArrayList

public static List addlist(List l, int x) {
Integer y = new Integer(x);
l.add(y);
return l;
}

public static void prlist(List l) {
for (Object o: l) {
System.out.println(o);
}
}

public static void main(String[] args) {
List list = new ArrayList();
Integer i = new Integer(5);
Integer j = new Integer(8);
list.add(i);
list.add(j);
addlist(list, 10);
prlist(list);
}
}
19 changes: 19 additions & 0 deletions ListsAndArrays/src/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@

public class BubbleSort {
//intermediate
public static int[] bubbleSort(int x[]) {
for (int i = 0; i < x.length; i++) {
for (int j = 1; j < (x.length - 1); j++) {
if (x[j - 1] > x[j]) {
int a = x[j];
x[j] = x[j - 1];
x[j - 1] = a;
}
}
}
return x;
}

public static void main(String[] args) {
int[] x = {2, 15, 4, 8, 22, 30};
bubbleSort(x);
for (int i = 0; i < x.length; i++) {
System.out.print(x[i] + " ");
}
}
//todo: sort an array using bubblesort.
//if you don't know how bubblesort works you can read it here: http://www.pkirs.utep.edu/CIS3355/Tutorials/chapter9/tutorial9A/bubblesort.htm

Expand Down
28 changes: 28 additions & 0 deletions Methods/src/Circle.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import java.util.Scanner;

public class Circle {
//todo: add some methods to compute a circle's diameter, circumference and area.
//print some outputs to confirm you methods are working right.
public static double diameter(double r) {
double d = 2 * r;
return d;
}

public static double circ(double r) {
double pi = Math.PI ;
double c = 2 * pi * r ;
return c ;
}

public static double area(double r) {
double pi = Math.PI ;
double a = pi * r * r ;
return a ;
}

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
System.out.println("Radius: ");
Double r = scan.nextDouble();

System.out.println(diameter(r));
System.out.println(circ(r));
System.out.println(area(r));
}
}
21 changes: 21 additions & 0 deletions Methods/src/Prime.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,30 @@

public class Prime {

public static boolean prime(int n) {

if (n <= 1) {
return false;
} else if (n <= 3 ) {
return true;
} else if (n % 2 == 0 || n % 3 == 0) {
return false;
} else {
for (int i = 5; i * i <= n; i = i + 6 ) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
return true;
}
} return true;
}

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
int n = scan.nextInt();

System.out.println("The number " + n + " is prime: " + prime(n));
//todo: write a method which tells you if a given number is a prime or not

}
Expand Down
17 changes: 17 additions & 0 deletions Recursion/src/Factorial.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import java.util.Scanner;

public class Factorial {
//todo: implement a recursive functions whicht gives you the factorial value of a given number
public static int factorial(int x) {
if (x <= 1) {
return 1;
} else {
return x * factorial(x -1);

}
}

public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();

System.out.println("The factorial of number " + x + " is: " + factorial(x));
}
}

18 changes: 17 additions & 1 deletion Recursion/src/Fibonacci.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@

import java.util.Scanner;
public class Fibonacci {
//todo: implement a recursive function which takes an integer value as inputs and outputs the corresponding Fibonacci number.
//if you don't know what Fibonacci numbers are you can read it here: https://en.wikipedia.org/wiki/Fibonacci_number
public static int placeFibonacci(int n) {
if (n == 0 || n == 1) {
return n;
} else {
return placeFibonacci(n - 2) + placeFibonacci(n - 1);
}
}

public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();

System.out.println("At the place" + n + " of the Fibonacci list, is the number: " + placeFibonacci(n));
}

}

9 changes: 9 additions & 0 deletions Structures/src/AssignGrade.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ public class AssignGrade {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Score: ");
int score = scan.nextInt();

double grade = ((100 - score)/50.0) * 3 + 1;

if (grade <= 4.0) {
System.out.println("Pass with grade: " + grade);
} else {
System.out.println("Fail with grade: " + grade);
}
//todo: when executing the program you will be ask to write a score.
//The program converts the score then into a grade and prints that grade (1.0, 1.3, 1.7, ...)
//Note that the student passed when the student gets a score of 50.0 points (4.0)
Expand Down
10 changes: 10 additions & 0 deletions Structures/src/EvenOdd.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ public class EvenOdd {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Number: ");
int number = scan.nextInt();


for (int count = 0; count < number; count ++) {
if (count % 2 != 0) {
System.out.println(count);
}
}


//todo: The program takes a number as input.
//It then goes through all integer values till reaching that value and prints all the odd ones.
Expand Down