Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added BigDecimalTasks/BigDecimalTask.class
Binary file not shown.
65 changes: 65 additions & 0 deletions BigDecimalTasks/BigDecimalTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
* Utility class providing precise rounding operations using BigDecimal.
* All methods handle null input safely by throwing IllegalArgumentException.
*/
public class BigDecimalTask {

/**
* Rounds the given BigDecimal to 2 decimal places (nearest hundredth)
* using HALF_UP rounding mode and returns the result as a double.
*
* @param value the BigDecimal value to round
* @return the rounded value as a primitive double
* @throws IllegalArgumentException if value is null
*/
public static double roundToNearestHundredth(BigDecimal value) {
if (value == null) {
throw new IllegalArgumentException("Value cannot be null");
}
return value.setScale(2, RoundingMode.HALF_UP).doubleValue();
}

/**
* Negates the given BigDecimal (changes its sign) and rounds the result
* to 1 decimal place (nearest tenth) using HALF_UP rounding mode.
*
* Examples:
* 1.2345 → -1.2
* -45.67 → 45.7
*
* @param value the BigDecimal value to negate and round
* @return a new BigDecimal with negated sign and rounded to 1 decimal place
* @throws IllegalArgumentException if value is null
*/
public static BigDecimal negateAndRoundToTenth(BigDecimal value) {
if (value == null) {
throw new IllegalArgumentException("Value cannot be null");
}
return value.negate().setScale(1, RoundingMode.HALF_UP);
}

/**
* Main method containing demonstration/test cases.
* Can be safely removed before final submission if not required.
*/
public static void main(String[] args) {
// Test case 1: rounding to hundredth
BigDecimal a = new BigDecimal("4.2545");
System.out.println("4.2545 rounded to 2 decimals → " + roundToNearestHundredth(a)); // Expected: 4.25

// Test case 2: positive → negative, round to tenth
BigDecimal b = new BigDecimal("1.2345");
System.out.println("1.2345 negated & rounded to 1 decimal → " + negateAndRoundToTenth(b)); // Expected: -1.2

// Test case 3: negative → positive, round to tenth
BigDecimal c = new BigDecimal("-45.67");
System.out.println("-45.67 negated & rounded to 1 decimal → " + negateAndRoundToTenth(c)); // Expected: 45.7

// Additional edge case: exact value (no rounding needed)
BigDecimal d = new BigDecimal("10.0");
System.out.println("10.0 negated & rounded → " + negateAndRoundToTenth(d)); // Expected: -10.0
}
}
Binary file added CarInventorySystem/Car.class
Binary file not shown.
Binary file added CarInventorySystem/CarHierarchy.class
Binary file not shown.
171 changes: 171 additions & 0 deletions CarInventorySystem/CarHierarchy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/**
* Abstract base class representing any type of car in the inventory system.
* All cars share common properties: VIN number, make, model, and mileage.
* Concrete subclasses (Sedan, UtilityVehicle, Truck) extend this class
* and may add type-specific properties.
*/
abstract class Car {

private final String vinNumber;
private final String make;
private final String model;
private final int mileage;

/**
* Constructs a new Car with the specified properties.
* All fields are final and immutable after construction.
*
* @param vinNumber unique Vehicle Identification Number (17 characters typically)
* @param make manufacturer of the vehicle (e.g., Toyota, Ford)
* @param model model name of the vehicle (e.g., Civic, F-150)
* @param mileage current mileage in kilometers
*/
public Car(String vinNumber, String make, String model, int mileage) {
this.vinNumber = vinNumber;
this.make = make;
this.model = model;
this.mileage = mileage;
}

/**
* Returns a formatted string containing all common car properties.
* Subclasses should override this method to include type-specific information.
*
* @return a readable string representation of the car's common properties
*/
public String getInfo() {
return String.format("VIN: %s | Make: %s | Model: %s | Mileage: %d km",
vinNumber, make, model, mileage);
}

// Getters – useful for future extensions (search, filtering, etc.)
public String getVinNumber() { return vinNumber; }
public String getMake() { return make; }
public String getModel() { return model; }
public int getMileage() { return mileage; }
}

/**
* Represents a standard passenger Sedan.
* Inherits all properties from Car; no additional fields.
*/
class Sedan extends Car {

public Sedan(String vinNumber, String make, String model, int mileage) {
super(vinNumber, make, model, mileage);
}

/**
* Returns car information including the specific type "Sedan".
*
* @return formatted string with all properties + vehicle type
*/
@Override
public String getInfo() {
return super.getInfo() + " | Type: Sedan";
}
}

/**
* Represents a Utility Vehicle (SUV, crossover, etc.).
* Adds a four-wheel-drive indicator.
*/
class UtilityVehicle extends Car {

private final boolean fourWheelDrive;

/**
* Constructs a UtilityVehicle with four-wheel-drive specification.
*
* @param vinNumber unique VIN
* @param make manufacturer
* @param model model name
* @param mileage current mileage in km
* @param fourWheelDrive true if the vehicle has 4WD / AWD
*/
public UtilityVehicle(String vinNumber, String make, String model,
int mileage, boolean fourWheelDrive) {
super(vinNumber, make, model, mileage);
this.fourWheelDrive = fourWheelDrive;
}

/**
* Returns car information including type and 4WD status.
*
* @return formatted string with all properties + type + 4WD info
*/
@Override
public String getInfo() {
return super.getInfo() +
" | Type: Utility Vehicle | 4WD: " + (fourWheelDrive ? "Yes" : "No");
}

public boolean hasFourWheelDrive() {
return fourWheelDrive;
}
}

/**
* Represents a pickup Truck.
* Adds towing capacity in kilograms or pounds (depending on context).
*/
class Truck extends Car {

private final double towingCapacity;

/**
* Constructs a Truck with specified towing capacity.
*
* @param vinNumber unique VIN
* @param make manufacturer
* @param model model name
* @param mileage current mileage in km
* @param towingCapacity maximum towing capacity (suggested unit: kg or lbs)
*/
public Truck(String vinNumber, String make, String model,
int mileage, double towingCapacity) {
super(vinNumber, make, model, mileage);
this.towingCapacity = towingCapacity;
}

/**
* Returns car information including type and towing capacity.
* Capacity is shown with one decimal place.
*
* @return formatted string with all properties + type + towing info
*/
@Override
public String getInfo() {
return super.getInfo() +
String.format(" | Type: Truck | Towing: %.1f kg", towingCapacity);
}

public double getTowingCapacity() {
return towingCapacity;
}
}

/**
* Demonstration of the car hierarchy and polymorphic behavior.
*/
public class CarHierarchy {

public static void main(String[] args) {
Car sedan = new Sedan("1HGCM82633A004352", "Honda", "Civic", 85000);
Car suv = new UtilityVehicle("5XYZG6789K1234567", "Toyota", "RAV4", 45000, true);
Car truck = new Truck("1FTFW1ETXEKD98765", "Ford", "F-150", 120000, 12000.5);

// Polymorphic calls – all use the overridden getInfo()
System.out.println(sedan.getInfo());
System.out.println(suv.getInfo());
System.out.println(truck.getInfo());

// Optional: showing access to subclass-specific properties
if (suv instanceof UtilityVehicle uv) {
System.out.println(" → Has 4WD: " + uv.hasFourWheelDrive());
}
if (truck instanceof Truck t) {
System.out.println(" → Towing capacity: " + t.getTowingCapacity() + " kg");
}
}
}
Binary file added CarInventorySystem/Sedan.class
Binary file not shown.
Binary file added CarInventorySystem/Truck.class
Binary file not shown.
Binary file added CarInventorySystem/UtilityVehicle.class
Binary file not shown.
Binary file added IntListTask/IntArrayList.class
Binary file not shown.
Binary file added IntListTask/IntList.class
Binary file not shown.
Binary file added IntListTask/IntVector.class
Binary file not shown.
Binary file added IntListTask/Main.class
Binary file not shown.
Loading