diff --git a/ListsAndArrays/src/AccountBook.java b/ListsAndArrays/src/AccountBook.java index 0920c65..5c98912 100644 --- a/ListsAndArrays/src/AccountBook.java +++ b/ListsAndArrays/src/AccountBook.java @@ -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[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 diff --git a/Methods/src/Circle.java b/Methods/src/Circle.java index 76aa810..d8a0fbb 100644 --- a/Methods/src/Circle.java +++ b/Methods/src/Circle.java @@ -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)); + } } diff --git a/Methods/src/Prime.java b/Methods/src/Prime.java index 15ab306..6f44a9b 100644 --- a/Methods/src/Prime.java +++ b/Methods/src/Prime.java @@ -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 } diff --git a/Recursion/src/Factorial.java b/Recursion/src/Factorial.java index bb8fb09..7d8ae26 100644 --- a/Recursion/src/Factorial.java +++ b/Recursion/src/Factorial.java @@ -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)); + } } + diff --git a/Recursion/src/Fibonacci.java b/Recursion/src/Fibonacci.java index bd04c78..8b15c67 100644 --- a/Recursion/src/Fibonacci.java +++ b/Recursion/src/Fibonacci.java @@ -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)); } + +} + diff --git a/Structures/src/AssignGrade.java b/Structures/src/AssignGrade.java index 8b8e871..5440627 100644 --- a/Structures/src/AssignGrade.java +++ b/Structures/src/AssignGrade.java @@ -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) diff --git a/Structures/src/EvenOdd.java b/Structures/src/EvenOdd.java index 8a382c3..db1fbae 100644 --- a/Structures/src/EvenOdd.java +++ b/Structures/src/EvenOdd.java @@ -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.