Markou why must you make agonizing assignments

This commit is contained in:
2023-10-18 23:12:48 -05:00
parent f9babdde19
commit 95546d4a94
74 changed files with 3581 additions and 22 deletions

View File

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

View File

@@ -0,0 +1,60 @@
/*
* 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) {
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;
}
};
Comparator<A2> c2 = new Comparator<A2>() {
@Override
public int compare(A2 o1, A2 o2) {
int returnVal = 0;
if(o1.x > o2.x) {
returnVal = -1;
} else {
returnVal = 0;
}
return returnVal;
}
};
System.out.println("Sorting in ascending order");
Collections.sort(list1, c);
System.out.println(list1);
System.out.println("Sorting in desending order");
Collections.sort(list1,c2);
System.out.println(list1);
}
}

View File

@@ -0,0 +1,63 @@
/*
* 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 A3 {
int x;
public A3() {
}
public A3(int x) {
this.x = x;
}
@Override
public String toString() {
return "A2{" + "x=" + x + '}';
}
public static Comparator<A3> comparator() {
Comparator<A3> c = new Comparator<A3>() {
@Override
public int compare(A3 o1, A3 o2) {
return o1.x - o2.x;
}
};
return c;
}
public static Comparator<A3> comparatorReverse() {
Comparator<A3> c = new Comparator<A3>() {
@Override
public int compare(A3 o1, A3 o2) {
return o1.x > o2.x ? -1 : 0;
}
};
return c;
}
public static void main(String[] args) {
List<A3> list1 = Arrays.asList(new A3(4), new A3(), new A3(2));
System.out.println("Sorting in ascending order");
Collections.sort(list1, A3.comparator());
System.out.println(list1);
System.out.println("Sorting in desending order");
Collections.sort(list1, A3.comparatorReverse());
System.out.println(list1);
}
}

View File

@@ -0,0 +1,80 @@
/*
* 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 A4<E extends Comparable<E>> {
E x;
public A4() {}
public A4(E x) {this.x = x;}
public static Comparator<A4> comparator() {
Comparator<A4> c = new Comparator<A4>() {
@Override
public int compare(A4 o1, A4 o2) {
return o1.x.compareTo(o2.x);
}
};
return c;
}
public static Comparator<A4> comparatorReverse() {
Comparator<A4> c = new Comparator<A4>() {
@Override
public int compare(A4 o1, A4 o2) {
switch (o1.x.compareTo(o2.x)) {
case -1:
return 1;
case 0:
return 0;
case 1:
return -1;
}
return -112315234;
}
};
return c;
}
@Override
public String toString() {
return "A4{" + "x=" + x + '}';
}
public static void main(String[] args) {
System.out.println("Sorting in ascending order");
List<A4> list1 = Arrays.asList(
new A4(new Integer(4)),
new A4(new Integer(1)),
new A4(new Integer(2))
);
Collections.sort(list1, A4.comparator());
List<A4> list2 = Arrays.asList(
new A4(new String("once")),
new A4(new String("upon")),
new A4(new String("a")),
new A4(new String("time")),
new A4(new String("in")),
new A4(new String("America"))
);
Collections.sort(list2, A4.comparator());
System.out.println(list1);
System.out.println(list2);
System.out.println("Now, in descending order:");
Collections.sort(list2, A4.comparatorReverse());
System.out.println(list2);
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,40 @@
/*
* 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.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
*
* @author caleb
*/
public class TestPriorityQueue {
public static void main(String[] args) {
PriorityQueue<String> queue1 = new PriorityQueue<>();
queue1.offer("Oklahoma");
queue1.offer("Indiana");
queue1.offer("Georgia");
queue1.offer("Texas");
System.out.println("Priority queue using Comparable:");
while (queue1.size() > 0) {
System.out.print(queue1.remove() + " ");
}
Comparator<String> c = Collections.reverseOrder();
PriorityQueue<String> queue2 = new PriorityQueue<>(4, c);
queue2.offer("Oklahoma");
queue2.offer("Indiana");
queue2.offer("Georgia");
queue2.offer("Texas");
System.out.println("\n\nPriority queue is using Comparator: ");
while (queue2.size() > 0) {
System.out.print(queue2.remove() + " ");
}
}
}

View File

@@ -0,0 +1,100 @@
/*
* 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.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
*
* @author caleb
*/
public class TestTheCollections {
public static void main(String[] args) {
System.out.println("Sorting in ascending order");
List<String> list1 = Arrays.asList("red", "green", "blue");
Collections.sort(list1);
System.out.println(list1);
System.out.println("Sorting in descending order");
List<String> list2 = Arrays.asList("yellow", "red", "green", "blue");
Collections.sort(list2);
System.out.println(list2);
System.out.println("\nBinary Search");
List<Integer> list3
= Arrays.asList(2, 4, 7, 10, 11, 45, 50, 59, 60, 66);
System.out.println(list3 + " 7 is at index: " + Collections.binarySearch(list3, 7));
System.out.println(list3 + " 9 is at index: " + Collections.binarySearch(list3, 9));
System.out.println(list3 + " 100 is at index: " + Collections.binarySearch(list3, 100));
List<String> list4 = new ArrayList<>();
list4.addAll(list1);
System.out.println(list4 + " red is at index: " + Collections.binarySearch(list4, "red"));
System.out.println(list4 + " amber is at index: " + Collections.binarySearch(list4, "amber"));
System.out.println(list4 + " brown is at index: " + Collections.binarySearch(list4, "brown"));
System.out.println("\nReverse the list");
List<String> list5 = new ArrayList<>();
list5.addAll(list2);
System.out.println("Original list: " + list5);
Collections.reverse(list5);
System.out.println("Reversed list: " + list5);
System.out.println("\nShuffle lists");
List<String> list6 = new ArrayList<>();
List<String> list7 = new ArrayList<>();
list6.addAll(list2);
list7.addAll(list2);
System.out.println("Original list: " + list6);
Collections.shuffle(list6, new Random(20));
System.out.println("Shuffled list: " + list6);
System.out.println("Original list: " + list7);
Collections.shuffle(list7, new Random(20));
System.out.println("Shuffled list: " + list7);
List<String> list8 = new ArrayList<>();
list8.addAll(list2);
List<String> list9 = Arrays.asList("white", "black");
System.out.println("\nCopy into " + list8 + " the list " + list9);
Collections.copy(list8, list9);
System.out.println(list8);
System.out.println("The output for list 8 is [white, black, green, blue].\n"
+ "The copy method performs a \n"
+ "shallow copy: only the references of the elements fromt he source list are copied."
);
List<String> list10 = new ArrayList<>();
list10.addAll(list1);
System.out.println("\nFill the list " + list10 + " with \'black\'.");
Collections.fill(list10, "black");
System.out.println(list10);
/*
The disjoint(collection1, collection2) method returns true if the two collections
have no elements in common. For example, in the following code, the disjoint(collection1,
collectio2) returns false, but the disjoint(collection1, collection3) returns true.
*/
System.out.println("\nCollections.disjoints()");
Collection<String> collection1 = Arrays.asList("red", "cyan");
Collection<String> collection2 = Arrays.asList("red", "blue");
Collection<String> collection3 = Arrays.asList("pink", "tan");
System.out.println(Collections.disjoint(collection1, collection2));
System.out.println(Collections.disjoint(collection1, collection3));
System.out.println("\nFrequency");
Collection<String> collection = Arrays.asList("red", "cyan", "red");
System.out.println(collection + " red occurs " + Collections.frequency(collection, "red") + " times.");
}
}

View File

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