diff --git a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/BikeStores.java b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/BikeStores.java index 11329c2..75d3bcd 100644 --- a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/BikeStores.java +++ b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/BikeStores.java @@ -14,16 +14,37 @@ import java.util.Collection; public class BikeStores { ArrayList> storesOfBikes = new ArrayList>(); - /**Add a bike at storeNumber ( i.e. the storeNumber indicates the store) + /** + * Add a bike at storeNumber ( i.e. the storeNumber indicates the store) * * @param storeNumber which store * @param b the bike to add * @return true if the bike added, false if the storeNumber is invalid or b is null. */ + public BikeStores() { + for (int i = 0; i < 3; i++) { + storesOfBikes.add(new ArrayList()); + } + } + public boolean addBike( int storeNumber, Bicycle b) { - //to be implemented - return false; + + try { + System.out.println("Attempting to add " + b + " to store at index " + storeNumber); + //Unpack array + ArrayList bikesInInventory = storesOfBikes.get(storeNumber); + bikesInInventory.add(b); + + // Repack array + storesOfBikes.set(storeNumber, bikesInInventory); + System.out.println("success!"); + return true; + } catch (Exception ex){ + System.err.println(ex); + return false; + } + } /** Removes a bike at a specific store. @@ -35,21 +56,35 @@ public class BikeStores public boolean removeBike( int store , Bicycle b) { - //to be implemented - return false; + try{ + System.out.println("Attempting to remove " + b + " to store at index " + store); + // Remove bike from array. + ArrayList bikesInInventory = storesOfBikes.get(store); + bikesInInventory.remove(b); //YEET + System.out.println("success!"); + return true; + } catch (Exception ex) { + System.err.println(ex); + return false; + } + + + } /**Prints the indexes before each bike , one store per line * * @param stores stores of bikes */ - public static void printAllStores( ArrayList> stores) + public static void print(ArrayList> stores) { - - for ( int i=0; i < stores.size(); ++i) + for ( int i=0; i < stores.size(); ++i) { + System.out.println("---------- " + "printing store " + (i + 1) + " ----------"); for ( int j=0; j < stores.get(i).size(); ++j) { + System.out.print(stores.get(i).get(j) + " "); + } - + } } /** Groups bikes by type ans sorts them by the ranking of Details. * There will be three groups and each group stored in decending order by rank. @@ -62,20 +97,39 @@ public class BikeStores return newStore; } + + public static void print(Object[] arr) { + for (Object o: arr) { + System.out.println(o); + } + } + + public static void print(Object[][] arr) { + for (int i = 0; i < arr.length; i++) { + System.out.println("---------- " + "printing row " + (i + 1) + " ----------"); + print(arr[i]); + + } + } + /** * Returns a 2D array containing all the bikes in the store in proper sequence (from first to last element). Store 0 with all its bikes, store 1 with all its bikes, and so on. * @return a 2D array of all stores with bikes. */ public Object[][] toArray() { - //a.toArray(); + Object[][] arr = new Object[storesOfBikes.size()][]; + for (int i = 0; i < storesOfBikes.size(); i++) { + arr[i] = storesOfBikes.toArray(); + } + return arr; } /** * Retains only the Bicycles in the stores that are contained in the specified collection. In other words, removes from this list all of bikes that are not contained in the specified location. * @param c the bikes to be removed * @return true if any store changed as a result of the call. */ - public boolean retainAll (Collection c) { - + public boolean retainAll (Collection c) { + return false; } public static void main(String[] args) { @@ -100,7 +154,9 @@ public class BikeStores bikes.addBike(2, new ChildBike(true, 1, 2, 20)); bikes.addBike(2, new ChildBike(false, 1, 1, 18)); //remove one child bike from store 2 + bikes.removeBike(2, new ChildBike(true, 1, 2, 20)); // Java reuses the object created earlier since it has the same values. //PRINT + print(bikes); //SORT //PRINT what the method return //PRINT the original store diff --git a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/ChildBike.java b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/ChildBike.java index 3794fe4..01bbb60 100644 --- a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/ChildBike.java +++ b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/ChildBike.java @@ -8,7 +8,7 @@ package com.calebfontenot.mp4_calebfontenot; * * @author caleb */ -public class ChildBike extends Bicycle //remove comment in front or extends +public class ChildBike extends Bicycle //remove comment in front of extends { private boolean helpWheels; diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/CircleWithUncheckedException.java b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/CircleWithUncheckedException.java new file mode 100644 index 0000000..921f083 --- /dev/null +++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/CircleWithUncheckedException.java @@ -0,0 +1,50 @@ +/* + * 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.lab6.exceptions_calebfontenot; + +/** + * + * @author calebfontenot + */ +public class CircleWithUncheckedException { + private double radius; + + public CircleWithUncheckedException() + { + this(1.0); + } + public CircleWithUncheckedException(double radius) + { + if (radius < 0) { + throw new IllegalArgumentException("Radius cannot be negative."); + } + this.radius = radius; + } + + public double getRadius() { + return radius; + } + public void setRadius(double radius) { + if (radius < 0) { + throw new IllegalArgumentException("Radius cannot be negative."); + } + this.radius = radius; + } + + @Override + public String toString() { + return "CircleWithUnheckedException{" + "radius=" + radius + '}'; + } + public static void main(String[] args) { + try { + System.out.println(new CircleWithUncheckedException(5)); + System.out.println(new CircleWithUncheckedException(-5)); + System.out.println(new CircleWithUncheckedException(10)); + } catch (Exception ex) { + System.err.println(ex.getMessage()); + } + + } +} \ No newline at end of file diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/TestFileClass.java b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/TestFileClass.java new file mode 100644 index 0000000..b8a96ff --- /dev/null +++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/TestFileClass.java @@ -0,0 +1,33 @@ +/* + * 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.lab6.exceptions_calebfontenot; + +import java.util.Scanner; + +/** + * + * @author caleb + */ +public class TestFileClass { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter a file path or directory: "); + String filePath = input.nextLine(); + testFile(filePath); + } + public static void testFile(String filePath) { + java.io.File file = new java.io.File(filePath); + System.out.println("Does it exist? " + file.exists()); + System.out.println("The file has " + file.length() + " bytes."); + System.out.println("Can it be read? " + file.canRead()); + System.out.println("Can it be written? " + file.canWrite()); + System.out.println("Is it a directory? " + file.isDirectory()); + System.out.println("Is it a file? " + file.isFile()); + System.out.println("Is it absolute? " + file.isAbsolute()); + System.out.println("Is it hidden? " + file.isHidden()); + System.out.println("The absolute path to this file is " + file.getAbsolutePath()); + System.out.println("Last modified on " + new java.util.Date(file.lastModified())); + } +} diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ToDecimal.java b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ToDecimal.java new file mode 100644 index 0000000..438c0db --- /dev/null +++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ToDecimal.java @@ -0,0 +1,43 @@ +/* + * 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.lab6.exceptions_calebfontenot; + +import java.util.Scanner; + +/** + * + * @author caleb + */ +public class ToDecimal { + public static int hexToDecimal(String hex) { + int decimalValue =0; + for (int i = 0; i < hex.length(); ++i) { + char hexChar = hex.charAt(i); + decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar); + } + return decimalValue; + } + public static int hexCharToDecimal(char ch) { + ch = Character.toUpperCase(ch); + if (ch >= 'A' && ch <= 'F') { + return 10 + ch - 'A'; + } else if (ch >= '0' && ch <= '9'){ // ch is '0', '1', or '9' + return ch - '0'; + } else { + throw new NumberFormatException("Invalid hex character"); + } + } + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter a hex number or q/Q to quit: "); + String hex = input.nextLine(); + while ("q".equals(hex.toLowerCase()) == false) { + System.out.println("The decimal value for hex number " + hex + " is " + hexToDecimal(hex.toUpperCase())); + System.out.print("Emter a hex number: "); + hex = input.nextLine(); + } + + } +} diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/Triangle.java b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/Triangle.java new file mode 100644 index 0000000..7446be6 --- /dev/null +++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/Triangle.java @@ -0,0 +1,50 @@ +/* + * 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.lab6.exceptions_calebfontenot; + +import java.util.Scanner; + +/** + * + * @author caleb + */ +public class Triangle implements Geo{ + private double side1 = 1.0, side2 = 1.0, side3 = 1.0; + + public Triangle(double side1, double side2, double side3) { + this.side1 = side1; + this.side2 = side2; + this.side3 = side3; + } + + @Override + public double getArea() { + double s = (side1 + side2 + side3) / 2; + return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); + } + @Override + public double getPerimeter() { + return side1 + side2 + side3; + } + @Override + public String toString() { + return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3; + } + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.println("Enter the three sides: "); + double side1 = input.nextDouble(); + double side2 = input.nextDouble(); + double side3 = input.nextDouble(); + Triangle triangle = new Triangle(side1, side2, side3); + System.out.println("The area is " + triangle.getArea()); + System.out.println("The perimeter is " + triangle.getPerimeter()); + System.out.println(triangle); + } +} +interface Geo { + double getArea(); + public double getPerimeter(); +} diff --git a/Semester 2/Exams/Exam1_CalebFontenot/src/main/java/com/calebfontenot/exam1_calebfontenot/MyStringBuilder.java b/Semester 2/Exams/Exam1_CalebFontenot/src/main/java/com/calebfontenot/exam1_calebfontenot/MyStringBuilder.java index f5cab87..412afb9 100644 --- a/Semester 2/Exams/Exam1_CalebFontenot/src/main/java/com/calebfontenot/exam1_calebfontenot/MyStringBuilder.java +++ b/Semester 2/Exams/Exam1_CalebFontenot/src/main/java/com/calebfontenot/exam1_calebfontenot/MyStringBuilder.java @@ -8,7 +8,7 @@ import java.util.Arrays; /** * - * @author ar114 + * @author caleb */ public class MyStringBuilder {