add lab 6

This commit is contained in:
SowinskiBraeden committed 2025-02-15 15:23:03 -08:00
1 parent d47c34b49a
commit 8b942b5ddf
6 files changed
+489

No files matched your search

@@ -0,0 +1,70 @@
package ca.bcit.comp1510.lab06;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
/**
* Reads baseball data in from a comma delimited file. Each line
* of the file contains a name followed by a list of symbols
* indicating the result of each at bat: h for hit, o for out,
* w for walk, s for sacrifice. Statistics are computed and
* printed for each player.
* @author Braeden Sowinski
* @version 1.0.0
*/
public class BaseballStats {
/**
* main program entry.
* Reads baseball stats from a file and counts
* total hits, outs, walks, and sacrifice flies
* for each player.
* @param args unused.
*/
public static void main(String[] args) throws FileNotFoundException {
Scanner fileScan;
Scanner lineScan;
String fileName;
Scanner scan = new Scanner(System.in);
String basePath = "src/ca/bcit/comp1510/lab06/";
System.out.print("Enter the name of the input file: ");
fileName = scan.nextLine();
fileScan = new Scanner(new File(basePath + fileName));
while (fileScan.hasNext()) {
lineScan = new Scanner(fileScan.nextLine());
lineScan.useDelimiter(",");
System.out.print(lineScan.next() + " ");
int outs = 0;
int hits = 0;
int sacs = 0;
int walks = 0;
while (lineScan.hasNext()) {
String current = lineScan.next();
hits += current.equals("h") ? 1 : 0;
outs += current.equals("o") ? 1 : 0;
sacs += current.equals("s") ? 1 : 0;
walks += current.equals("w") ? 1 : 0;
}
System.out.print(hits + " hits, "
+ outs + " outs, "
+ sacs + " sacrifices, "
+ walks + " walks, ");
final double percent = 100.0;
double average = ((float) (hits) / (float) (hits + outs)) * percent;
System.out.printf("batting average: %.2f%s", average, "%");
System.out.println();
lineScan.close();
}
scan.close();
fileScan.close();
}
}
+62
View File
@@ -0,0 +1,62 @@
package ca.bcit.comp1510.lab06;
import java.util.Scanner;
/**
* Factorial calculates stuff.
* @author Braeden Sowinski
* @version 1.0.0
*/
public class Factorial {
/**
* readPositiveInt reads and validates an integer.
* @return validated int
*/
private static int readPositiveInt() {
Scanner scan = new Scanner(System.in);
int number = -1;
while (number < 0) {
System.out.print("Enter a non-negative integer: ");
if (!scan.hasNextInt()) {
System.out.println(scan.next() + " is not a number.");
continue;
}
number = scan.nextInt();
if (number < 0) {
System.out.println(number + " is a negative number.");
continue;
}
}
scan.close();
return number;
}
/**
* main program entry.
* @param args unused
*/
public static void main(String[] args) {
int number = readPositiveInt();
if (number == 0) {
System.out.println("The factorial of 0 is 1.");
} else {
int product = 1;
for (int i = 2; i <= number; i++) {
product *= i;
}
System.out.println("The factorial of "
+ number + " is "
+ product + "."
);
}
}
}
@@ -0,0 +1,123 @@
package ca.bcit.comp1510.lab06;
/**
* Mathematics2 does more math stuff.
* @author Braeden Sowinski
* @version 1.0.0
*/
public class Mathematics2 {
/** FOOT_TO_KILOMETRE_RATIO is important. */
public static final double FOOT_TO_KILOMETRE_RATIO = 0.0003048;
/**
* getSquareArea returns square of input.
* @param d input to get square.
* @return square area.
*/
public double getSquareArea(double d) {
return d * d;
}
/**
* add two floating point numbers.
* @param d first number
* @param e second number
* @return sum of d and e
*/
public double add(double d, double e) {
return d + e;
}
/**
* multiply two floating point numbers.
* @param d first number
* @param e second number
* @return product of d and e
*/
public double multiply(double d, double e) {
return d * e;
}
/**
* subtract floating point number from another.
* @param d number
* @param e number to subtract
* @return difference of d and e
*/
public double subtract(double d, double e) {
return d - e;
}
/**
* divide two floating point numbers.
* @param i first number
* @param j divide by
* @return quotiant of i and j
*/
public double divide(int i, int j) {
if (j == 0) {
return 0.0;
}
return i / j;
}
/**
* absoluteValue converts negative numbers to positive.
* @param i number
* @return i as a positive number
*/
public int absoluteValue(int i) {
if (i >= 0) {
return i;
} else {
return i * -1;
}
}
/**
* getRandomNumberBetweenTenAndTwentyButNotFifteen
* does as the name says.
* @return int
*/
public int getRandomNumberBetweenTenAndTwentyButNotFifteen() {
final int max = 21;
final int min = 10;
final int bad = 15;
int rand = bad;
while (rand == bad) {
rand = (int) (Math.random() * (max - min)) + min;
}
return rand;
}
/**
* convertFeetToKilometres.
* @param d number of feet
* @return kilometres.
*/
public double convertFeetToKilometres(double d) {
return d * FOOT_TO_KILOMETRE_RATIO;
}
/**
* sumOfProducts.
* @param i min/max
* @param j step
* @return sum
*/
public int sumOfProducts(int i, int j) {
int sum = 0;
if (i >= 0) {
for (int k = j; k <= i; k += j) {
sum += k;
}
} else {
for (int k = -j; k >= i; k -= j) {
sum += k;
}
}
return sum;
}
}
@@ -0,0 +1,222 @@
package ca.bcit.comp1510.lab06;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.util.HashSet;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* MathematicsTest.
*
* @author BCIT
* @version 1.0
*/
public class Mathematics2Test {
/**
* The Mathematics object to test.
*/
private Mathematics2 math;
/**
* Creates a new Mathematics objects before each test is executed.
*
* @throws Exception
* if a Mathematics object cannot be created.
*/
@BeforeEach
public void setUp() throws Exception {
math = new Mathematics2();
}
/**
* Test method for the value of ONE_FOOT_TO_KILOMETER_RATIO.
*/
@Test
public void testFeetToKMRatioValue() {
assertEquals(0.0003048, Mathematics2.FOOT_TO_KILOMETRE_RATIO, 0.0000001);
}
/**
* Tests getSquareArea method.
*/
@Test
public void testGetSquareArea1() {
double area = math.getSquareArea(2.0);
assertEquals(4.0, area, 0.1);
}
/**
* Tests getSquareArea method.
*/
@Test
public void testGetSquareArea2() {
double area = math.getSquareArea(32.0);
assertEquals(1024.0, area, 0.1);
}
/**
* Tests add method.
*/
@Test
public void testAdd1() {
double sum = math.add(6.0, 7.0);
assertEquals(13.0, sum, 0.1);
}
/**
* Tests add method.
*/
@Test
public void testAdd2() {
double sum = math.add(-6.0, 6.0);
assertEquals(0.0, sum, 0.1);
}
/**
* Tests multiply method.
*/
@Test
public void testMultiply1() {
double product = math.multiply(1.0, 5.0);
assertEquals(5.0, product, 0.1);
}
/**
* Tests multiply method.
*/
@Test
public void testMultiply2() {
double product = math.multiply(20.0, 20.0);
assertEquals(400.0, product, 0.1);
}
/**
* Tests subtract method.
*/
@Test
public void testSubtract1() {
double difference = math.subtract(0.0, 20.0);
assertEquals(-20.0, difference, 0.1);
}
/**
* Tests subtract method.
*/
@Test
public void testSubtract2() {
double difference = math.subtract(20.0, 20.0);
assertEquals(0.0, difference, 0.1);
}
/**
* Tests divide method.
*/
@Test
public void testDivide1() {
double division = math.divide(20, 10);
assertEquals(2.0, division, 0.00001);
}
/**
* Tests divide method.
*/
@Test
public void testDivide2() {
double division = math.divide(20, 0);
assertEquals(0.0, division, 0.00001);
}
/**
* Tests absolute value method.
*/
@Test
public void testAbsolute1() {
int abs = math.absoluteValue(1);
assertEquals(1, abs);
}
/**
* Tests absolute value method.
*/
@Test
public void testAbsolute2() {
int abs = math.absoluteValue(-5000);
assertEquals(5000, abs);
}
/**
* Tests the random number generator method.
*/
@Test
public void testRandomNumberBetweenTenAndTwentyButNotFifteen() {
HashSet<Integer> values = new HashSet<Integer>();
for (int i = 0; i < 10000; i++) {
int number = math.getRandomNumberBetweenTenAndTwentyButNotFifteen();
boolean lessThanTen = number < 10;
boolean greaterThanTwenty = number > 20;
boolean notFifteen = number == 15;
assertFalse(lessThanTen);
assertFalse(greaterThanTwenty);
assertFalse(notFifteen);
values.add(number);
}
assertEquals(10, values.size());
}
/**
* Tests the method that converts feet to kilometres.
*/
@Test
public void testFootToKM1() {
double km = math.convertFeetToKilometres(1.0);
assertEquals(0.0003048, km, 0.1);
}
/**
* Tests the method that converts feet to kilometres.
*/
@Test
public void testFootToKM2() {
double km = math.convertFeetToKilometres(892.0);
assertEquals(0.272186, km, 0.1);
}
/**
* Tests the method that returns the sum of the numbers
* between 0 and the first parameter that are divisible
* by the second parameter.
*/
@Test
public void testSumOfProducts() {
int sumOfProducts = math.sumOfProducts(10, 3);
assertEquals(18, sumOfProducts); // 3 + 6 + 9
}
/**
* Tests the method that returns the sum of the numbers
* between 0 and the first parameter that are divisible
* by the second parameter.
*/
@Test
public void testSumOfProducts2() {
int sumOfProducts = math.sumOfProducts(-10, 3);
assertEquals(-18, sumOfProducts); // -3 + -6 + -9
}
/**
* Tests the method that returns the sum of the numbers
* between 0 and the first parameter that are divisible
* by the second parameter.
*/
@Test
public void testSumOfProducts3() {
int sumOfProducts = math.sumOfProducts(40, 5);
assertEquals(180, sumOfProducts); // 5 + 10 + ... + 40
}
}
+11
View File
@@ -0,0 +1,11 @@
Willy Wonk,o,o,h,o,o,o,o,h,w,o,o,o,o,s,h,o,h
Shari Jones,h,o,o,s,s,h,o,o,o,h,o,o,o,o
Barry Bands,h,h,w,o,o,o,w,h,o,o,h,h,o,o,w,w,w,h,o,o
Sally Slugger,o,h,h,o,o,h,h,w
Missy Lots,o,o,s,o,o,w,o,o,o
Joe Jones,o,h,o,o,o,o,h,h,o,o,o,o,w,o,o,o,h,o,h,h
Larry Loop,w,s,o,o,o,h,o,o,h,s,o,o,o,h,h
Sarah Swift,o,o,o,o,h,h,w,o,o,o
Bill Bird,h,o,h,o,h,w,o,o,o,h,s,s,h,o,o,o,o,o,o
Don Daring,o,o,h,h,o,o,h,o,h,o,o,o,o,o,o,h
Jill Jet,o,s,s,h,o,o,h,h,o,o,o,h,o,h,w,o,o,h,h,o
+1
View File
@@ -0,0 +1 @@
Barry Bands,h,h,w,o,o,o,w,h,o,o,h,h,o,o,w,w,w,h,o,o