More NetBeans moments

master
Chloe Fontenot 🏳️‍⚧️ 2022-10-18 09:54:27 +07:00
parent 6fdafdbaf6
commit a07ef7df60
3 changed files with 211 additions and 3 deletions

@ -4,13 +4,53 @@
*/
package mp3_calebfontenot;
import java.util.Scanner;
/**
*
* @author caleb
*/
public class Stats {
public static void main(String[] args)
{
public static void main(String[] args) {
// Create scanner
Scanner input = new Scanner(System.in);
// Define variables
double mean = 0; //It's twice as mean
double inputSum = 0, numberOfTimesRun = 0, inputPow = 0, deviation = 0;
String userInput = "";
// Prompt for input
do {
System.out.println("Press Y/y to enter a series of numbers or Q/q to quit.");
System.out.println("Current sum: " + inputSum);
userInput = input.next();
if (userInput.toLowerCase().equals("y")) {
try {
do {
System.out.print("Please enter a series of numbers; Type '-1' to quit (no quotes, you doofus!): ");
userInput = input.next();
if (Double.parseDouble(userInput) != -1) {
inputSum += Double.parseDouble(userInput);
inputPow += Math.pow(Double.parseDouble(userInput), 2);
numberOfTimesRun++; // counter to store number of times we've added to input sum
}
System.out.println("Current sum: " + inputSum);
System.out.println("Input sum^2: " + inputPow);
} while (Double.parseDouble(userInput) != -1);
} catch (Exception e) {
System.out.println("Invalid input!");
}
}
// Calculate mean
mean = inputSum / numberOfTimesRun;
// Calculate deviation
deviation = ((numberOfTimesRun - 1) / (inputPow - (Math.pow(inputSum, 2) / numberOfTimesRun)));
System.out.println();
System.out.println("The mean is: " + mean);
System.out.println("The deviation is: " + deviation);
} while (!(userInput.toLowerCase().equals("q")));
}
}

@ -0,0 +1,168 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.mavenproject1;
/**
*
* @author caleb
*/
import java.util.Scanner;
public class Tyler {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double percentage = 0.3; //initializing variables
System.out.print("Enter the number of players: ");
int size = in.nextInt();
System.out.println("Contest with percentage set to 0.3: "); //each contest with percentages assigned
System.out.println();
contest(size, percentage);
percentage = 0.2;
System.out.println("Contest with percentage set to 0.2: ");
System.out.println();
contest(size, percentage);
percentage = 0.1;
System.out.println("Contest with percentage set to 0.1: ");
System.out.println();
contest(size, percentage);
}
public static int getWinner(int one, int two) { //randomly decides winner, if loop says all
double random = Math.random();
if (random <= 0.5) {
return one;
}
else {
return two;
}
}
public static boolean contestRound(int[] array) { //changes the values in the array
int first = (int) Math.round(Math.random() * array.length); //randomly assigns variables for the two indecies of
if (first == 10) { //the values that we will change. None of the variables
first--; //will equal the length of the array
}
while (array[first] == 0) { //Finds an index of the array where the value is positive when the value is zero
first = (int) Math.round(Math.random() * array.length);
if (first == 10) {
first--;
}
}
int second = (int) Math.round(Math.random() * array.length );
if (second == 10) {
second--;
}
while (array[second] == 0 && second != first) {
second = (int) Math.round(Math.random() * array.length);
if (second == 10) {
second--;
}
}
int returned = getWinner(first, second); //sees who wins
if (returned == array[first]) { //changes index values based on who won
array[first]++;
array[second]--;
}
else {
array[second]++;
array[first]--;
}
if (array[first] == 0 || array[second] == 0) { //return statement
return true;
}
else {
return false;
}
}
public static int countPlayers(int[] array) { // counts all the non-zero numbers in the token array
int count = 0;
for (int element : array)
if (element != 0)
count++;
return count;
}
public static void printPlayers(int[] array) { // prints out the non-zero parts of the token array
int highest=0, highestAt=0, lowest=100, lowestAt=0; //initializes variables
System.out.print("Players "); //prints out the index of each player with a positive value
for (int i = 0; i < array.length; i++)
if (array[i] != 0)
System.out.print(i + " ");
System.out.println();
System.out.print("Values "); //prints the values of the indexes from before
for (int i = 0; i < array.length; i++) {
if (array[i] != 0) {
System.out.print(array[i] + " ");
}
}
int count = 0;
for (int i = 0; i < array.length; i++) { //Could not find a better way to figure out which is highest and which
if (array[i] > 0) { //is lowest, so created new array with only the positive values.
count++; //this loop calculates how many positive values there are to set the
} //length of the new index
}
int[] positives = new int[count];
int pos = 0;
for (int i = 0; i < array.length; i++) { //finds the values of the initialized variables
if (array[i] > 0) {
positives[pos] = array[i];
pos++;
}
if (array[i] > highest) {
highest = array[i];
highestAt = i;
}
if (array[i] < lowest && array[i] != 0){
lowest = array[i];
lowestAt = i;
}
}
System.out.println(); //prints out the variables
System.out.println("Highest Player Index: " + highestAt + ", Value : " + highest + "; Lowest Player Index: " + lowestAt + ", Value: " + lowest);
System.out.println();
}
public static void contest(int size, double percentage) { // calls other methods to do the contest
int square = (int) Math.ceil(Math.sqrt(size));
int[] array = new int[size]; //creates the array we'll use for the contest
for (int i = 0; i < array.length; i++) { //assigns each value of the array the same thing
array[i] = square;
}
printPlayers(array); //prints the first line
int perc = (int) Math.round(array.length * percentage); //calculates which is higher, 2 or the percentage of the length
if (perc < 2) {
perc = 2;
}
int count = countPlayers(array);
boolean contesting = contestRound(array); //loop that does the contests
while (count > perc) {
contesting = contestRound(array);
if (contesting == true) {
printPlayers(array);
count = countPlayers(array);
}
}
}
}