Java moment

This commit is contained in:
2023-10-18 13:38:56 -05:00
parent 7da3542ede
commit f9babdde19
42 changed files with 1616 additions and 0 deletions

View File

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

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

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

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

View File

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

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

View File

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

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.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{" + '}';
}
}