Java moment

master
Caleb Fontenot 2023-10-18 13:38:56 +07:00
parent 7da3542ede
commit f9babdde19
42 changed files with 1616 additions and 0 deletions

4
.gitignore vendored

@ -177,3 +177,7 @@
/BigIntegerFibonacci/build/
/Semester 3/GenericRules/target/
/Semester 3/MP4_Generics_CalebFontenot/target/
/Semester 3/Assignments/FileStream/target/
/Semester 3/Assignments/DBConnectionTest/target/
/Semester 3/Assignments/mavenproject1/target/
/Semester 3/Assignments/JavaFXBallsWithComparator/target/

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>DBConnectionTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.dbconnectiontest.DBConnectionTest</exec.mainClass>
</properties>
</project>

@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.dbconnectiontest;
/**
*
* @author caleb
*/
public class DBConnectionTest {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

@ -0,0 +1,129 @@
package edu.slcc.asdv.caleb.dbconnectiontest;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
* @author a. v. markou
*/
public class Database
{
static String result = "";
private static Connection connection() //throws InstantiationException, IllegalAccessException
{
String databaseName = "suppliers_parts_23";
String userName = "admin";
String password = "RangerDog01!";
String URL2 = "com.mysql.jdbc.Driver";
//String URL2 = "com.mysql.cj.jdbc.Driver";
Connection con = null;
try
{// Load Sun's jdbc driver
Class.forName(URL2).newInstance();
System.out.println("JDBC Driver loaded!");
}
catch (Exception e) // driver not found
{
System.err.println("Unable to load database driver");
System.err.println("Details : " + e);
return null;
}
String ip = "localhost"; //internet connection
String url = "jdbc:mysql://" + ip + ":3306/" + databaseName;
try
{
con = DriverManager.getConnection(url, userName, password);
con.setReadOnly(false);
}
catch (Exception e)
{
System.err.println(e.toString());
return null;
}
System.out.println("connection successfull");
return con;
}
public String getResult()
{
return "<p style=\"color:green\">Suppliers <br />" + result;
}
public static void closeDatabaseConnection( Connection con)
{
try
{
if (con != null)
{
con.close();
}
}
catch (SQLException e)
{
result = e.toString();
e.printStackTrace();
}
}
public static void listAllSuppliers()
{
Connection con = connection();
if (con == null)
{
result = "cannot connect to database" ;
return ;
}
String table = "";
PreparedStatement ps = null;
ResultSet rs = null;
String sqlStr = "SELECT * FROM supplier";
try
{
//prepare statement
ps = con.prepareStatement(sqlStr);
//execute
rs = ps.executeQuery();
System.out.println("before entering while");
while (rs.next())
{
//System.out.println("inside while");
String sNumber = rs.getString(1) + " ";
String sName = rs.getString(2) + " ";
String status = rs.getDate(3) + " ";
String city = rs.getString(4) + " ";
table += sNumber + sName + status + city + "\n";
}
System.out.println(table);
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
closeDatabaseConnection( con);
// close the resources
if (ps != null)
{
ps.close();
}
}
catch (SQLException sqle){ sqle.printStackTrace(); }
}
result = table;
}
public static void main(String[] args)
{
listAllSuppliers();
}
}

@ -0,0 +1,22 @@
/*
* 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 edu.slcc.asdv.caleb.dbconnectiontest;
/**
*
* @author caleb
*/
public class Scroller {
public static void main(String[] args) throws InterruptedException {
String string = "Java moment";
while (true) {
System.out.println(string);
System.out.print("\033[H\033[2J");
System.out.flush();
string = string.charAt(string.length() - 1) + string.substring(0, string.length() - 1);
Thread.sleep(100);
}
}
}

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>FileStream</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.filestream.FileStream</exec.mainClass>
</properties>
</project>

@ -0,0 +1,62 @@
/*
* 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 edu.slcc.asdv.caleb.filestream;
/**
*
* @author caleb
*/
import java.io.*;
public class Copy {
/** Main method
@param args[0] for sourcefile
@param args[1] for target file
*/
public static void main(String[] args) throws IOException {
// Check command-line parameter usage
if (args.length != 2) {
System.out.println(
"Usage: java Copy sourceFile targetfile");
System.exit(1);
}
// Check if source file exists
File sourceFile = new File(args[0]);
if (!sourceFile.exists()) {
System.out.println("Source file " + args[0]
+ " does not exist");
System.exit(2);
}
// Check if target file exists
File targetFile = new File(args[1]);
if (targetFile.exists()) {
System.out.println("Target file " + args[1]
+ " already exists");
System.exit(3);
}
try (
// Create an input stream
BufferedInputStream input =
new BufferedInputStream(new FileInputStream(sourceFile));
// Create an output stream
BufferedOutputStream output =
new BufferedOutputStream(new FileOutputStream(targetFile));
) {
// Continuously read a byte from input and write it to output
int r, numberOfBytesCopied = 0;
while ((r = input.read()) != -1) {
output.write((byte)r);
numberOfBytesCopied++;
}
// Display the file size
System.out.println(numberOfBytesCopied + " bytes copied");
}
}
}

@ -0,0 +1,28 @@
/*
* 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 edu.slcc.asdv.caleb.filestream;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author caleb
*/
public class FileChooser {
public static void main(String[] args)
{
// Using this process to invoke the constructor,
// JFileChooser points to user's default directory
JFileChooser j = new JFileChooser();
// Open the save dialog
j.showSaveDialog(null);
}
}

@ -0,0 +1,33 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.filestream;
import java.io.*;
/**
*
* @author caleb
*/
public class FileStream {
public static void main(String[] args) throws IOException {
try (
// Create an output stream to the file
FileOutputStream output = new FileOutputStream("temp.dat");
) {
// Output values to the file
for (int i = 1; i <= 10; i++)
output.write(i);
}
try (
// Create an input stream for the file
FileInputStream input = new FileInputStream("temp.dat");
) {
// Read values from the file
int value;
while ((value = input.read()) != -1)
System.out.print(value + " ");
}
}
}

@ -0,0 +1,44 @@
/*
* 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 edu.slcc.asdv.caleb.filestream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
//import java.lang.Byte;
/**
*
* @author caleb
*/
public class ReadRawBytes {
public static void main(String[] args)
{
ArrayList<Byte> byteArrayList = new ArrayList<Byte>();
// Read raw bytes from file until 0xFF
try ( RandomAccessFile inout = new RandomAccessFile("test.dat", "r");) {
byte currentByte = 0x00;
for (int i = 0; i < inout.length(); i++) {
currentByte = inout.readByte();
if (currentByte != -1) {
byteArrayList.add(currentByte);
} else {
break;
}
}
} catch (IOException ex) {
System.out.println(ex);
}
byte[] byteArray = new byte[byteArrayList.size()];
for (int i = 0; i < byteArrayList.size(); i++) {
byteArray[i] = byteArrayList.get(i);
}
System.out.println("Data read back from file was: ");
String fileString = new String(byteArray, StandardCharsets.UTF_8);
System.out.println(fileString);
}
}

@ -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 edu.slcc.asdv.caleb.filestream;
import java.io.Serializable;
/**
*
* @author caleb
*/
public class Test implements Serializable {
private int x;
public Test(int x)
{
this.x = x;
}
/**
* Get the value of x
*
* @return the value of x
*/
public int getX()
{
return x;
}
/**
* Set the value of x
*
* @param x new value of x
*/
public void setX(int x)
{
this.x = x;
}
}

@ -0,0 +1,38 @@
/*
* 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 edu.slcc.asdv.caleb.filestream;
/**
*
* @author caleb
*/
import java.io.*;
public class TestDataStream {
public static void main(String[] args) throws IOException {
try ( // Create an output stream for file temp.dat
DataOutputStream output =
new DataOutputStream(new FileOutputStream("temp.dat"));
) {
// Write student test scores to the file
output.writeUTF("John");
output.writeDouble(85.5);
output.writeUTF("Jim");
output.writeDouble(185.5);
output.writeUTF("George");
output.writeDouble(105.25);
}
try ( // Create an input stream for file temp.dat
DataInputStream input =
new DataInputStream(new FileInputStream("temp.dat"));
) {
// Read student test scores from the file
System.out.println(input.readUTF() + " " + input.readDouble());
System.out.println(input.readUTF() + " " + input.readDouble());
System.out.println(input.readUTF() + " " + input.readDouble());
}
}
}

@ -0,0 +1,55 @@
/*
* 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 edu.slcc.asdv.caleb.filestream;
/**
*
* @author caleb
*/
import java.io.*;
public class TestObjectStreamForArray {
public static void main(String[] args)
throws ClassNotFoundException, IOException
{
int[] numbers = {1, 2, 3, 4, 5};
Test[] arTest = new Test[2];
arTest[0] = new Test(10);
arTest[0] = new Test(20);
String[] strings = {"John", "Susan", "Kim"};
try ( // Create an output stream for file array.dat
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("array.dat", true));) {
// Write arrays to the object output stream
output.writeObject(numbers);
output.writeObject(strings);
output.writeObject(arTest);
}
try ( // Create an input stream for file array.dat
ObjectInputStream input
= new ObjectInputStream(new FileInputStream("array.dat"));) {
int[] newNumbers = (int[]) (input.readObject());
String[] newStrings = (String[]) (input.readObject());
// Display arrays
for (int i = 0; i < newNumbers.length; i++) {
System.out.print(newNumbers[i] + " ");
}
System.out.println();
for (int i = 0; i < newStrings.length; i++) {
System.out.print(newStrings[i] + " ");
}
System.out.println();
for (int i = 0; i < arTest.length; i++) {
System.out.print(arTest[i] + " ");
}
}
}
}

@ -0,0 +1,75 @@
/*
* 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 edu.slcc.asdv.caleb.filestream;
/**
*
* @author caleb
*/
import java.io.*;
public class TestRandomAccessFile {
public static void main(String[] args) throws IOException
{
try ( // Create a random access file
RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw");) {
// Clear the file to destroy the old contents if exists
inout.setLength(0);
// Write new integers to the file
for (int i = 0; i < 200; i++) {
inout.writeInt(i);
}
// Display the current length of the file
System.out.println("Current file length is " + inout.length());
// Retrieve the first number
inout.seek(0); // Move the file pointer to the beginning
System.out.println("The first number is " + inout.readInt());
// Retrieve the second number
inout.seek(1 * 4); // Move the file pointer to the second number
System.out.println("The second number is " + inout.readInt());
// Retrieve the tenth number
inout.seek(9 * 4); // Move the file pointer to the tenth number
System.out.println("The tenth number is " + inout.readInt());
// Modify the eleventh number
inout.writeInt(555);
// Append a new number
inout.seek(inout.length()); // Move the file pointer to the end
inout.writeInt(999);
// Display the new length
System.out.println("The new length is " + inout.length());
// Retrieve the new eleventh number
inout.seek(10 * 4); // Move the file pointer to the eleventh number
System.out.println("The eleventh number is " + inout.readInt());
inout.seek(0); // Move the pointer to the beginning of the file
int intOut;
int counter = 0;
while (true) {
try {
counter++;
intOut = inout.readInt();
System.out.print(intOut + " ");
if ((counter + 1) % 5 == 0) {
System.out.println();
}
} catch (EOFException ex) {
System.out.println();
System.out.println("We reached the end of the file!");
break;
}
}
}
}
}

@ -0,0 +1 @@
This is another abritrary string of charactersÿ

File diff suppressed because one or more lines are too long

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>clean</goal>
<goal>javafx:run</goal>
</goals>
</action>
<action>
<actionName>debug</actionName>
<goals>
<goal>clean</goal>
<goal>javafx:run@ide-debug</goal>
</goals>
<properties>
<jpda.listen>true</jpda.listen>
</properties>
</action>
<action>
<actionName>profile</actionName>
<goals>
<goal>clean</goal>
<goal>javafx:run@ide-profile</goal>
</goals>
</action>
<action>
<actionName>CUSTOM-jlink</actionName>
<displayName>jlink</displayName>
<goals>
<goal>clean</goal>
<!-- compile not needed with javafx-maven-plugin v0.0.5 -->
<goal>compile</goal>
<goal>javafx:jlink</goal>
</goals>
</action>
</actions>

@ -0,0 +1,76 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>JavaFXBallsWithComparator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>20</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>edu.slcc.asdv.caleb.javafxballswithcomparator.App</mainClass>
</configuration>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
</execution>
<execution>
<!-- Configuration for manual attach debugging -->
<!-- Usage: mvn clean javafx:run@debug -->
<id>debug</id>
<configuration>
<options>
<option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000</option>
</options>
</configuration>
</execution>
<execution>
<!-- Configuration for automatic IDE debugging -->
<id>ide-debug</id>
<configuration>
<options>
<option>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</option>
</options>
</configuration>
</execution>
<execution>
<!-- Configuration for automatic IDE profiling -->
<id>ide-profile</id>
<configuration>
<options>
<option>${profiler.jvmargs.arg1}</option>
<option>${profiler.jvmargs.arg2}</option>
<option>${profiler.jvmargs.arg3}</option>
<option>${profiler.jvmargs.arg4}</option>
<option>${profiler.jvmargs.arg5}</option>
</options>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,42 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
*
* @author caleb
*/
public class A1 implements Comparable<A1> {
int x;
public A1() {}
public A1(int x) {this.x = x;}
@Override
public int compareTo(A1 o)
{
return this.x - o.x;
}
@Override
public String toString()
{
return "A1{" + "x=" + x + '}';
}
public static void main(String[] args)
{
System.out.println("Sorting in ascending order");
List<A1> list1 = Arrays.asList(new A1(3), new A1(), new A1(2));
Collections.sort(list1);
System.out.println(list1);
System.out.println("Sorting in descending order");
Collections.sort(list1, Collections.reverseOrder());
System.out.println(list1);
}
}

@ -0,0 +1,41 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
*
* @author caleb
*/
public class A2 {
int x;
public A2() {}
public A2(int x) {this.x = x;}
@Override
public String toString()
{
return "A2{" + "x=" + x + '}';
}
public static void main(String[] args)
{
System.out.println("Sorting in ascending order");
List<A2> list1 = Arrays.asList(new A2(4), new A2(), new A2(2));
Comparator<A2> c = new Comparator<A2>() {
@Override
public int compare(A2 o1, A2 o2)
{
return o1.x - o2.x;
}
};
Collections.sort(list1, c);
System.out.println(list1);
}
}

@ -0,0 +1,30 @@
package edu.slcc.asdv.caleb.javafxballswithcomparator;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* JavaFX App
*/
public class App extends Application {
@Override
public void start(Stage stage) {
var javaVersion = SystemInfo.javaVersion();
var javafxVersion = SystemInfo.javafxVersion();
var label = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
var scene = new Scene(new StackPane(label), 640, 480);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}

@ -0,0 +1,51 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
/**
*
* @author caleb
*/
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
@Override /** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
@Override /** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}

@ -0,0 +1,66 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
/**
*
* @author caleb
*/
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}

@ -0,0 +1,19 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
/**
*
* @author caleb
*/
import java.util.Comparator;
public class GeometricObjectComparator
implements Comparator<GeometricObject>, java.io.Serializable {
public int compare(GeometricObject o1, GeometricObject o2) {
return o1.getArea() > o2.getArea() ?
1 : o1.getArea() == o2.getArea() ? 0 : -1;
}
}

@ -0,0 +1,185 @@
package edu.slcc.asdv.caleb.javafxballswithcomparator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
public class MultipleBallsWithComparator extends Application
{
@Override // Override the start method in the Application class
public void start(Stage primaryStage)
{
MultipleBallPane ballPane = new MultipleBallPane();
Button btAdd = new Button("+");
Button btSubtract = new Button("-");
HBox hBox = new HBox(10);
hBox.getChildren().addAll(btAdd, btSubtract);
hBox.setAlignment(Pos.CENTER);
// Add or remove a ball
btAdd.setOnAction(e -> ballPane.add());
btSubtract.setOnAction(e -> ballPane.subtract());
// Pause and resume animation
ballPane.setOnMousePressed(e -> ballPane.pause());
ballPane.setOnMouseReleased(e -> ballPane.play());
// Use a scroll bar to control animation speed
ScrollBar sbSpeed = new ScrollBar();
sbSpeed.setMax(20);
sbSpeed.setValue(10);
ballPane.rateProperty().bind(sbSpeed.valueProperty());
BorderPane pane = new BorderPane();
pane.setCenter(ballPane);
pane.setTop(sbSpeed);
pane.setBottom(hBox);
// Create a scene and place the pane in the stage
Scene scene = new Scene(pane, 250, 150);
primaryStage.setTitle("Multiple Bouncing Balls"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
private class MultipleBallPane extends Pane
{
private Timeline animation;
public MultipleBallPane()
{
// Create an animation for moving the ball
animation = new Timeline(
new KeyFrame(Duration.millis(50), e -> moveBall()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play(); // Start animation
}
public void add()
{
Color color = new Color(Math.random(),
Math.random(), Math.random(), 0.5);
getChildren().add(new Ball(30, 30, Math.random() * 16 + 5, color));
}
public void subtract()
{
if (getChildren().size() > 0)
{
//> Locate the ball with the largest radius
Ball ball = (Ball) (getChildren().get(0));
for (Node node : getChildren())
{
if (((Ball) node).getRadius() > ball.getRadius())
{
ball = (Ball) node;
}
}
getChildren().remove(ball);
}
}
public void play()
{
animation.play();
}
public void pause()
{
animation.pause();
}
public void increaseSpeed()
{
animation.setRate(animation.getRate() + 0.1);
}
public void decreaseSpeed()
{
animation.setRate(
animation.getRate() > 0 ? animation.getRate() - 0.1 : 0);
}
public DoubleProperty rateProperty()
{
return animation.rateProperty();
}
protected void moveBall()
{
for (Node node : this.getChildren())
{
Ball ball = (Ball) node;
// Check boundaries
if (ball.getCenterX() < ball.getRadius()
|| ball.getCenterX() > getWidth() - ball.getRadius())
{
ball.dx *= -1; // Change ball move direction
}
if (ball.getCenterY() < ball.getRadius()
|| ball.getCenterY() > getHeight() - ball.getRadius())
{
ball.dy *= -1; // Change ball move direction
}
// Adjust ball position
ball.setCenterX(ball.dx + ball.getCenterX());
ball.setCenterY(ball.dy + ball.getCenterY());
}
}
}
class Ball extends Circle implements Comparable<Ball>
{
private double dx = 1, dy = 1;
Ball(double x, double y, double radius, Color color)
{
super(x, y, radius);
setFill(color); // Set ball color
}
public int compareTo(Ball b)
{
if (this.getRadius() - b.getRadius() < 0)
{
return -1;
}
else if (this.getRadius() - b.getRadius() == 0)
{
return 0;
}
else
{
return 1;
}
}
}
/**
* The main method is only needed for the IDE with limited JavaFX support.
* Not needed for running from the command line.
*/
public static void main(String[] args)
{
launch(args);
}
}

@ -0,0 +1,53 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
/**
*
* @author caleb
*/
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
@Override /** Return area */
public double getArea() {
return width * height;
}
@Override /** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
}

@ -0,0 +1,13 @@
package edu.slcc.asdv.caleb.javafxballswithcomparator;
public class SystemInfo {
public static String javaVersion() {
return System.getProperty("java.version");
}
public static String javafxVersion() {
return System.getProperty("javafx.version");
}
}

@ -0,0 +1,45 @@
package edu.slcc.asdv.caleb.javafxballswithcomparator;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class TestArrayAndLinkedList
{
public static void main(String[] args)
{
List<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(1);
arrayList.add(4);
arrayList.add(0, 10);
arrayList.add(3, 30);
System.out.println("A list of integers in the array list:");
System.out.println(arrayList);
LinkedList<Object> linkedList = new LinkedList<>(arrayList);
linkedList.add(1, "red");
linkedList.removeLast();
linkedList.addFirst("green");
System.out.println("Display the linked list backward:");
ListIterator<Object> listIterator = linkedList.listIterator();
while (listIterator.hasNext())
{
System.out.print(listIterator.next() + " ");
}
System.out.println();
System.out.println("Display the linked list backward:");
listIterator = linkedList.listIterator(linkedList.size());
while (listIterator.hasPrevious())
{
System.out.print(listIterator.previous() + " ");
}
}
}

@ -0,0 +1,29 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
/**
*
* @author caleb
*/
import java.util.Comparator;
public class TestComparator {
public static void main(String[] args) {
GeometricObject g1 = new Rectangle(5, 5);
GeometricObject g2 = new Circle(5);
GeometricObject g =
max(g1, g2, new GeometricObjectComparator());
System.out.println("The area of the larger object is " +
g.getArea());
}
public static GeometricObject max(GeometricObject g1,
GeometricObject g2, Comparator<GeometricObject> c) {
return c.compare(g1, g2) > 0 ? g1 : g2;
}
}

@ -0,0 +1,4 @@
module edu.slcc.asdv.caleb.javafxballswithcomparator {
requires javafx.controls;
exports edu.slcc.asdv.caleb.javafxballswithcomparator;
}

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.mavenproject1.Mavenproject1</exec.mainClass>
</properties>
</project>

@ -0,0 +1,51 @@
/*
* 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 edu.slcc.asdv.caleb.mavenproject1;
/**
*
* @author caleb
*/
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
@Override /** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
@Override /** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}

@ -0,0 +1,66 @@
/*
* 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 edu.slcc.asdv.caleb.mavenproject1;
/**
*
* @author caleb
*/
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}

@ -0,0 +1,19 @@
/*
* 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 edu.slcc.asdv.caleb.mavenproject1;
/**
*
* @author caleb
*/
import java.util.Comparator;
public class GeometricObjectComparator
implements Comparator<GeometricObject>, java.io.Serializable {
public int compare(GeometricObject o1, GeometricObject o2) {
return o1.getArea() > o2.getArea() ?
1 : o1.getArea() == o2.getArea() ? 0 : -1;
}
}

@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.mavenproject1;
/**
*
* @author caleb
*/
public class Mavenproject1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

@ -0,0 +1,53 @@
/*
* 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 edu.slcc.asdv.caleb.mavenproject1;
/**
*
* @author caleb
*/
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
@Override /** Return area */
public double getArea() {
return width * height;
}
@Override /** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
}

@ -0,0 +1,45 @@
package edu.slcc.asdv.caleb.mavenproject1;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class TestArrayAndLinkedList
{
public static void main(String[] args)
{
List<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(1);
arrayList.add(4);
arrayList.add(0, 10);
arrayList.add(3, 30);
System.out.println("A list of integers in the array list:");
System.out.println(arrayList);
LinkedList<Object> linkedList = new LinkedList<>(arrayList);
linkedList.add(1, "red");
linkedList.removeLast();
linkedList.addFirst("green");
System.out.println("Display the linked list backward:");
ListIterator<Object> listIterator = linkedList.listIterator();
while (listIterator.hasNext())
{
System.out.print(listIterator.next() + " ");
}
System.out.println();
System.out.println("Display the linked list backward:");
listIterator = linkedList.listIterator(linkedList.size());
while (listIterator.hasPrevious())
{
System.out.print(listIterator.previous() + " ");
}
}
}

@ -0,0 +1,51 @@
/*
* 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 edu.slcc.asdv.caleb.mavenproject1;
/**
*
* @author caleb
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestComparator {
public static void main(String[] args) {
GeometricObject g1 = new Rectangle(5, 5);
GeometricObject g2 = new Circle(5);
List<GeometricObject> list = new ArrayList<GeometricObject>();
list.add(g1);
list.add(g2);
System.out.println("min is: " + Collections.min(list, new GeometricObjectComparator()));
GeometricObject g = max(g1, g2, new GeometricObjectComparator());
GeometricObject g3 = max(g1, g2, new Comparator<GeometricObject>() {
@Override
public int compare(GeometricObject o1, GeometricObject o2) {
if (o1.equals(o2)) {
return 0;
}
return o1.getArea() > o2.getArea() ? 1 : -1;
}
});
System.out.println("The area of the larger object is " +
g.getArea());
}
public static GeometricObject max(GeometricObject g1,
GeometricObject g2, Comparator<GeometricObject> c) {
return c.compare(g1, g2) > 0 ? g1 : g2;
}
@Override
public String toString()
{
return "TestComparator{" + '}';
}
}