MP4 progress

master
Chloe Fontenot 🏳️‍⚧️ 2023-03-16 08:09:48 +07:00
parent e28eec5c61
commit 880852443a
7 changed files with 246 additions and 14 deletions

@ -14,16 +14,37 @@ import java.util.Collection;
public class BikeStores
{
ArrayList<ArrayList<Bicycle>> storesOfBikes = new ArrayList<ArrayList<Bicycle>>();
/**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<Bicycle>());
}
}
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<Bicycle> 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<Bicycle> 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<ArrayList<Bicycle>> stores)
public static void print(ArrayList<ArrayList<Bicycle>> 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<Bicycle> c) {
public boolean retainAll (Collection<Bicycle> 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

@ -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;

@ -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());
}
}
}

@ -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()));
}
}

@ -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();
}
}
}

@ -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();
}

@ -8,7 +8,7 @@ import java.util.Arrays;
/**
*
* @author ar114
* @author caleb
*/
public class MyStringBuilder {