adding files

This commit is contained in:
2024-11-13 12:25:44 -08:00
parent 3c020ad218
commit 37b3c0216a
16 changed files with 526 additions and 0 deletions

4
.idea/modules.xml generated
View File

@@ -4,7 +4,11 @@
<modules>
<module fileurl="file://$PROJECT_DIR$/CS2012.iml" filepath="$PROJECT_DIR$/CS2012.iml" />
<module fileurl="file://$PROJECT_DIR$/IO/IO.iml" filepath="$PROJECT_DIR$/IO/IO.iml" />
<module fileurl="file://$PROJECT_DIR$/Scaleconverter/Scaleconverter.iml" filepath="$PROJECT_DIR$/Scaleconverter/Scaleconverter.iml" />
<module fileurl="file://$PROJECT_DIR$/checkpoint1/checkpoint1.iml" filepath="$PROJECT_DIR$/checkpoint1/checkpoint1.iml" />
<module fileurl="file://$PROJECT_DIR$/hw08/hw08.iml" filepath="$PROJECT_DIR$/hw08/hw08.iml" />
<module fileurl="file://$PROJECT_DIR$/monsterattack/monsterattack.iml" filepath="$PROJECT_DIR$/monsterattack/monsterattack.iml" />
<module fileurl="file://$PROJECT_DIR$/textfileio/textfileio.iml" filepath="$PROJECT_DIR$/textfileio/textfileio.iml" />
</modules>
</component>
</project>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -0,0 +1,16 @@
public class ConverterTest {
public static void main(String[] args) {
ScaleConverter englishToMetric = new EnglishToMetricConverter();
ScaleConverter metricToEnglish = new MetricToEnglishConverter();
System.out.println("English to Metric:");
System.out.println("104F to C: " + englishToMetric.convertTemperature(104));
System.out.println("10mi to M: " + englishToMetric.convertDistance(10));
System.out.println("25lbs to Kg : " + englishToMetric.convertWeight(25));
System.out.println("Metric to English");
System.out.println("32C to F: " + metricToEnglish.convertTemperature(32));
System.out.println("45M to Mi: " + metricToEnglish.convertDistance(45));
System.out.println("10Kg to lbs : " + metricToEnglish.convertWeight(10));
}
}

View File

@@ -0,0 +1,12 @@
class EnglishToMetricConverter implements ScaleConverter {
public double convertTemperature(double tempIn) {
return (tempIn - 32) * 5/9;
}
public double convertDistance(double distanceIn) {
return distanceIn * 1.609;
}
public double convertWeight (double weightIn) {
return weightIn / 2.2;
}
}

View File

@@ -0,0 +1,12 @@
class MetricToEnglishConverter implements ScaleConverter {
public double convertTemperature(double tempIn) {
return tempIn * 9/5 + 32;
}
public double convertDistance(double distanceIn) {
return distanceIn * 0.621371;
}
public double convertWeight (double weightIn) {
return weightIn * 2.2;
}
}

View File

@@ -0,0 +1,5 @@
public interface ScaleConverter{
public double convertTemperature(double tempIn);
public double convertDistance(double distanceIn);
public double convertWeight(double weightIn);
}

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -0,0 +1,72 @@
public class RectangleShape {
private double w;
private double h;
public RectangleShape() {
w = 1.0;
h = 1.0;
}
public RectangleShape(double w, double h) {
if (h <= 0) {
this.h = 1;
}
else {
this.h = h;
}
if (w <= 0) {
this.w = 1;
}
else {
this.w = w;
}
}
public double getW() {
return w;
}
public double getH() {
return h;
}
public void setW(double w) {
if (w <= 0) {
this.w = 1;
}
else {
this.w = w;
}
}
public void setH(double h) {
if (h <= 0) {
this.h = 1;
}
else {
this.h = h;
}
}
public double getArea() {
return h * w;
}
public String toString() {
return "RectangleShape("+ w + ", " + h + ")";
}
public double compareRectangles(RectangleShape rs) {
double d1 = getArea();
double d2 = rs.getArea();
if (Double.compare(d1, d2) == 0) {
return 0;
} else if (Double.compare(d1, d2) < 0) {
return -1;
} else {
return 1;
}
}
}

View File

@@ -0,0 +1,69 @@
public class RectangleShapeTest {
public static void main(String[] args) {
int totalPoints = 55; //just for not crashing
RectangleShape rect1 = new RectangleShape();
RectangleShape rect2 = new RectangleShape(3.4, 2.7);
RectangleShape rect3 = new RectangleShape(0.0, -2.4);
RectangleShape rect4 = new RectangleShape(9.2, 1.3);
// test 1
if (Double.compare(rect4.getW(), 9.2) == 0) {
System.out.println("passed test 1");
totalPoints += 5;
}
// test 2
if (Double.compare(rect4.getH(), 1.3) == 0) {
System.out.println("passed test 2");
totalPoints += 5;
}
// test 3
if (rect1.toString().equals("RectangleShape(1.0, 1.0)")) {
System.out.println("passed test 3");
totalPoints += 5;
}
// test 4
if (rect2.toString().equals("RectangleShape(3.4, 2.7)")) {
System.out.println("passed test 4");
totalPoints += 5;
}
// test 5
if (rect3.toString().equals("RectangleShape(1.0, 1.0)")) {
System.out.println("passed test 5");
totalPoints += 5;
}
// test 6
if (rect1.compareRectangles(rect2) == -1) {
System.out.println("passed test 6");
totalPoints += 5;
}
// test 7
if (rect2.compareRectangles(rect1) == 1) {
System.out.println("passed test 7");
totalPoints += 5;
}
// test 8
if (rect1.compareRectangles(rect3) == 0) {
System.out.println("passed test 8");
totalPoints += 5;
}
// test 9
rect4.setW(3.4);
rect4.setH(2.7);
if (rect2.compareRectangles(rect4) == 0) {
System.out.println("passed test 9");
totalPoints += 5;
}
System.out.println("Score is " + totalPoints + " out of possible 100");
}
}

11
hw08/hw08.iml Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -0,0 +1,33 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class A_WriteRandomNumbers {
public static void main(String[] args) {
// Declare Variables
// WARNING: THESE FILE PATHS ARE FOR LINUX, PLEASE CHANGE TO APPROPRIATE PATH IF RUNNING ON WINDOWS
// OR THE PROGRAM MAY NOT RUN.
String filename = "/home/system32/IdeaProjects/CS2012/textfileio/src/randomNumbers.txt";
int numberOfRandomNumbers = 100;
int lowerBound = 1000;
int upperBound = 9999;
// Tries Writing to File
try(FileWriter writer = new FileWriter(filename)) {
// sets var rand to new random number
Random rand = new Random();
// for the # in var numberOfRandomNumbers generate a random number within the bounds of the vars specified above
for (int i = 0; i < numberOfRandomNumbers; i++) {
int randomNumber = rand.nextInt(upperBound - lowerBound) + lowerBound;
// Writes ths number
writer.write(randomNumber + "\n");
}
// Informs user that the write was successful
System.out.println("Random numbers written to " + filename);
// Catches exception and informs user of the exception and its nature
} catch (IOException e) {
System.out.println("Error while writing to file " + filename);
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,60 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class B_OddlySortedNumbers {
public static void main(String[] args) {
// declares file paths
// WARNING: THESE FILE PATHS ARE FOR LINUX, PLEASE CHANGE TO APPROPRIATE PATH IF RUNNING ON WINDOWS
// OR THE PROGRAM MAY NOT RUN.
String inputFileName = "/home/system32/IdeaProjects/CS2012/textfileio/src/randomNumbers.txt";
String outputFileName = "/home/system32/IdeaProjects/CS2012/textfileio/src/oddSortedNumbers.txt";
// Creates new Variable array named numbers
ArrayList<Integer> numbers = new ArrayList<>();
// Tries to read the file specified in var inputFileName
try (BufferedReader reader = new BufferedReader(new FileReader((inputFileName)))) {
String line = null;
// adds numbers from file to variable array numbers
while ((line = reader.readLine()) != null) {
numbers.add(Integer.parseInt(line.trim()));
}
} catch (IOException e) {
// catches exceptions and prints them
System.out.println("Error reading file");
e.printStackTrace();
return;
}
// Sorts the numbers based on the output of the method compare();
Collections.sort(numbers, new Comparator<Integer>() {
// Takes 2 Integers from the Array, invokes the private method reverseNumber(), and compares the 2 numbers.
public int compare(Integer num1, Integer num2) {
int reversed1 = reverseNumber(num1);
int reversed2 = reverseNumber(num2);
return Integer.compare(reversed1, reversed2);
}
});
try (FileWriter writer = new FileWriter(outputFileName)) {
for (Integer number : numbers) {
writer.write(number + "\n");
}
System.out.println("Sorted numbers written to file" + outputFileName);
} catch (IOException e) {
System.out.println("Error writing file");
e.printStackTrace();
}
}
private static int reverseNumber(int number) {
int reversed = 0;
while (number != 0) {
int digit = number % 10;
reversed = reversed * 10 + digit;
number /= 10;
}
return reversed;
}
}

View File

@@ -0,0 +1,62 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;
public class C_WriteNumberListStats {
public static void main(String[] args) {
String inputFileName = "/home/system32/IdeaProjects/CS2012/textfileio/src/randomNumbers.txt";
String outputFileName = "/home/system32/IdeaProjects/CS2012/textfileio/src/numberListStats.txt";
int grandTotal = 0;
int totalFirstDigit = 0;
int totalSecondDigit = 0;
int totalThirdDigit = 0;
int totalFourthDigit = 0;
int countOf17= 0;
int count = 0;
try (BufferedReader br = new BufferedReader(new FileReader(inputFileName))) {
String line;
while ((line = br.readLine()) != null) {
int number = Integer.parseInt(line.trim());
grandTotal += number;
count++;
int firstDigit = number / 1000;
int secondDigit = (number / 100) % 10;
int thirdDigit = (number / 10) % 10;
int fourthDigit = number % 10;
totalFirstDigit += firstDigit;
totalSecondDigit += secondDigit;
totalThirdDigit += thirdDigit;
totalFourthDigit += fourthDigit;
if(number == 17){
countOf17++;
}
}
} catch (IOException e) {
System.out.println(" Error reading " + inputFileName);
e.printStackTrace();
return;
}
double average = (count > 0) ? grandTotal / count : 0;
try (FileWriter writer = new FileWriter(outputFileName)) {
writer.write( "Grand Total = " + grandTotal + "\n");
writer.write("1st column = " + totalFirstDigit + "\n");
writer.write("2nd column = " + totalSecondDigit + "\n");
writer.write("3rd column = " + totalThirdDigit + "\n");
writer.write("4th column = " + totalFourthDigit + "\n");
writer.write("average = " + (int) average + "\n");
writer.write("count of 17's = " + countOf17 + "\n");
System.out.println( outputFileName + "Has bee written to.");
} catch (IOException e) {
System.out.println(" Error writing " + outputFileName);
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,37 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class D_ReadFileStats {
public static void main(String[] args) {
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the file name/path: ");
String fileName = sc.nextLine();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line = "";
while ((line = reader.readLine()) != null) {
lineCount++;
charCount += line.length();
String[] words = line.split("\\s+");
if (!line.trim().isEmpty()) {
wordCount += words.length;
}
}
} catch (IOException e) {
System.out.println("Error reading file: " + fileName);
e.printStackTrace();
}
System.out.println("Number of Characters:" + charCount);
System.out.println("Number of Words:" + wordCount);
System.out.println("Number of Lines:" + lineCount);
}
}

View File

@@ -0,0 +1,100 @@
8874
8517
5953
5137
4840
2616
7814
6457
3689
1890
2118
7944
7164
2855
7031
8518
9039
5339
5156
1811
7310
4018
4159
6494
7824
4181
3192
4714
3444
8996
3324
3544
1162
6893
2529
1010
7712
2175
4504
9938
2054
4744
2821
2378
9087
8253
7444
4726
1578
7368
5381
6740
9108
8645
2148
5308
1389
6715
9438
6012
2180
3494
5405
4689
8209
8734
9411
2816
1543
1545
4439
6701
4624
4614
1747
1289
5066
1824
1396
7015
7525
9716
5066
8006
4472
9682
6809
7846
3348
1792
3791
2422
1798
1139
5331
4358
5709
7398
2845
4177

11
textfileio/textfileio.iml Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>