96 lines
2.7 KiB
Java
96 lines
2.7 KiB
Java
import java.util.Arrays;
|
|
import java.util.Scanner;
|
|
|
|
public class Main {
|
|
public static void main(String[] args) {
|
|
/*Opgave1.printNumbers(15);
|
|
Opgave2.processName();
|
|
Opgave3.repl("Hello", 3);
|
|
Opgave4.printPowersOf2(10);
|
|
Opgave4.printPowersOf2NoMath(10);
|
|
Opgave5.Opgave5();*/
|
|
Opgave6.quadratic(1,3,2);
|
|
}
|
|
}
|
|
|
|
class Opgave1 {
|
|
public static void printNumbers(int n) {
|
|
for (int i = 1; i <= n; i++) {
|
|
System.out.print("["+i+"] ");
|
|
}
|
|
System.out.println();
|
|
}
|
|
}
|
|
|
|
class Opgave2 {
|
|
public static void processName() {
|
|
Scanner scanner = new Scanner(System.in);
|
|
System.out.print("\n Skriv dit navn: ");
|
|
String name = scanner.nextLine();
|
|
String[] splitName = name.split(" ");
|
|
System.out.print("Dit navn i omvendt rækkefølge er: "+splitName[splitName.length-1]+", ");
|
|
for (int i = 0; i <= splitName.length-2; i++) {
|
|
System.out.print(splitName[i]+" ");
|
|
}
|
|
System.out.println();
|
|
}
|
|
}
|
|
|
|
class Opgave3 {
|
|
public static void repl(String input, int n) {
|
|
for (int i = 1; i <= n; i++) {
|
|
System.out.print(input);
|
|
}
|
|
System.out.println();
|
|
}
|
|
}
|
|
|
|
class Opgave4 {
|
|
public static void printPowersOf2(int n) {
|
|
for (int i = 0; i <= n; i++) {
|
|
System.out.print((long)Math.pow(2, i)+" ");
|
|
}
|
|
System.out.println();
|
|
}
|
|
|
|
public static void printPowersOf2NoMath(int n) {
|
|
for (int i = 0; i <= n; i++) {
|
|
System.out.print(calculatePowerOf2(2, i)+" ");
|
|
}
|
|
System.out.println();
|
|
}
|
|
|
|
public static int calculatePowerOf2(int n, int repeat) {
|
|
if (repeat == 0) {
|
|
return 1;
|
|
} else {
|
|
return calculatePowerOf2(n, repeat - 1) * n;
|
|
}
|
|
}
|
|
}
|
|
|
|
class Opgave5 {
|
|
public static void Opgave5() {
|
|
System.out.printf("%-5s %-15s %-15s %-10s %-10s%n", "Year", "Balance", "Interest", "Deposit", "New Balance");
|
|
double balance = 1000.0;
|
|
double interest = 0.065;
|
|
double deposit = 100;
|
|
for (int i = 1; i <= 25; i++) {
|
|
double oldBalance = balance;
|
|
balance += balance * interest;
|
|
double newBalance = balance + deposit;
|
|
System.out.printf("%-5d $%-14.2f $%-9.2f $%-9.2f $%-14.2f%n", i, oldBalance, oldBalance * interest, deposit, newBalance);
|
|
balance = newBalance;
|
|
}
|
|
}
|
|
}
|
|
|
|
class Opgave6 {
|
|
public static void quadratic(double a, double b, double c) {
|
|
double[] x = {0.0,0.0};
|
|
for (int i = -1; i <= 1; i += 2) {
|
|
x[(i <= 0 ? 0 : 1)] = (-b-(Math.sqrt(Math.pow(b,2)-(4*a*c))*i))/(2*a);
|
|
}
|
|
System.out.println(Arrays.toString(x));
|
|
}
|
|
} |