Created
June 25, 2021 00:00
-
-
Save SoPat712/54c7587b0d7f54ddc2e88ec3ece6b6ee to your computer and use it in GitHub Desktop.
Java Intermediate: 6/24/2021
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javafx.test; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
selectionSort(); | |
interest(); | |
randomNumbers(); | |
power(); | |
} | |
public static void selectionSort(){ | |
int[] list = new int[10]; | |
for (int i =0; i < list.length; i++){ | |
list[i] = (int) (Math.random()*100)+1; | |
} | |
//print the unsorted array | |
for (int n : list) { | |
System.out.print(n + " "); | |
} | |
System.out.println(); // move cursor to next line | |
// goes up to index list.length - 1 | |
// because you don't need to check last element | |
// since it will be sorted already | |
for (int i = 0; i < list.length - 1; i++) { | |
int min = list[i]; | |
int minIndex = i; | |
// find the current smallest element | |
for (int j = i + 1; j < list.length; j++) { | |
if (list[j] < min) { | |
min = list[j]; | |
minIndex = j; | |
} | |
} | |
// swap it with whatever is in the | |
// first position of unsorted array if needed | |
if (minIndex != i) { | |
int temp = list[i]; | |
list[i] = min; | |
list[minIndex] = temp; | |
} | |
} | |
// print the sorted array | |
for (int n : list) { | |
System.out.print(n + " "); | |
} | |
System.out.println(); // move cursor to next line | |
} | |
public static void interest(){ | |
Scanner input = new Scanner(System.in); | |
// prompt user for principal, rate, and time | |
System.out.print("Enter the principal amount: $"); | |
double principal = input.nextDouble(); | |
System.out.print("Enter the annual interest rate (in percentage points): "); | |
double rate = input.nextDouble()/100; | |
System.out.print("Enter the period of time (in years): "); | |
int time = input.nextInt(); | |
input.close(); | |
// calculate the final amount using A = Pe^(rt) | |
double finalAmount = principal * Math.exp(rate * time); | |
// display the final amount (2 decimals precision) | |
System.out.printf("The final amount is $%,.2f\n", finalAmount); // added newline to move cursor to next line | |
} | |
public static void randomNumbers() { | |
int[][] numbers = new int[4][6]; // create 4x6 array | |
int sum = 0; // sum of all elements in array | |
int max = Integer.MIN_VALUE; // max element in array | |
int min = Integer.MAX_VALUE; // min element in array | |
// iterate through each element in a | |
for (int row = 0; row < 4; row++) { | |
for (int col = 0; col < 6; col++) { | |
// generate a random number from 7 to 77 | |
// and store it in the array | |
numbers[row][col] = randInt(7, 77); | |
// print that element | |
System.out.print(numbers[row][col] + "\t"); | |
// keep a running total of all elements | |
sum += numbers[row][col]; | |
// update min or max as needed | |
if (numbers[row][col] < min) { | |
min = numbers[row][col]; | |
} | |
if (numbers[row][col] > max) { | |
max = numbers[row][col]; | |
} | |
} | |
System.out.println(); // move to next row | |
} | |
// display the sum of all elements | |
// and the max and min element | |
System.out.println("Sum: " + sum); | |
System.out.println("Max: " + max); | |
System.out.println("Min: " + min); | |
} | |
/** | |
* Returns a random integer | |
* in the range [min, max] | |
* @param min minimum random integer | |
* @param max maximum random integer | |
* @return a random integer in the range [min, max] | |
*/ | |
public static int randInt(int min, int max) { | |
return min + (int) (Math.random() * ((max - min) + 1)); | |
} | |
public static void power(){ | |
// create a 5x4 array | |
int[][] array = new int[5][4]; | |
for (int power = 2; power < 7; power++) { | |
for (int base = 2; base < 6; base++) { | |
// assign the correct element based on the pattern | |
array[power - 2][base - 2] = (int) (Math.pow(base, power)); | |
// print the current element | |
System.out.print(array[power - 2][base - 2] + "\t\t"); | |
} | |
System.out.println(); // move to next row | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment