Reset author name to chosen name ✨
This commit is contained in:
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* 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.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Circle extends GeometricObject {
|
||||
private double diameter;
|
||||
|
||||
public Circle(double diameter) {
|
||||
this.diameter = diameter;
|
||||
System.out.println("Circle Diameter: " + diameter);
|
||||
}
|
||||
|
||||
public double getRadius() {
|
||||
return diameter / 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
double radius = diameter / 2;
|
||||
return Math.PI * Math.pow(radius, 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getPerimeter() {
|
||||
return Math.PI * diameter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Circle{" + "diameter=" + diameter + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(GeometricObject t) {
|
||||
System.out.println(this.getArea() + ", " + (t).getArea());
|
||||
if (this.getArea() < (t).getArea()) {
|
||||
return -1;
|
||||
} else if (this.getArea() > (t).getArea()) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Object t) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
/*
|
||||
* 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.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
class Combinations
|
||||
{
|
||||
|
||||
private static void findCombinations(String[] A, int i, int k,
|
||||
Set<List<String>> subarrays,
|
||||
List<String> out)
|
||||
{
|
||||
if (A.length == 0 || k > A.length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// base case: combination size is `k`
|
||||
if (k == 0)
|
||||
{
|
||||
subarrays.add(new ArrayList<>(out));
|
||||
return;
|
||||
}
|
||||
|
||||
// start from the next index till the last index
|
||||
for (int j = i; j < A.length; j++)
|
||||
{
|
||||
// add current element `A[j]` to the solution and recur for next index
|
||||
// `j+1` with one less element `k-1`
|
||||
out.add(A[j]);
|
||||
findCombinations(A, j + 1, k - 1, subarrays, out);
|
||||
out.remove(out.size() - 1); // backtrack
|
||||
}
|
||||
}
|
||||
|
||||
private static Set<List<String>> findCombinations(String[] A, int k)
|
||||
{
|
||||
Set<List<String>> subarrays = new HashSet<>();
|
||||
findCombinations(A, 0, k, subarrays, new ArrayList<>());
|
||||
return subarrays;
|
||||
}
|
||||
|
||||
private static Set<List<String>> findAllCombinations(String[] A)
|
||||
{
|
||||
Set<List<String>> subarrays = new HashSet<>();
|
||||
for (int k = 1; k <= A.length; ++k)
|
||||
{
|
||||
findCombinations(A, 0, k, subarrays, new ArrayList<>());
|
||||
}
|
||||
return subarrays;
|
||||
}
|
||||
/** Finds all distinct combinations of all sizes for elements of array.
|
||||
*
|
||||
* @param A the elements to find their combinations
|
||||
* @return all distinct combinations of the elements, sorted by length. ascending order.
|
||||
*/
|
||||
public static ArrayList<String> allCombinations(String[] A)
|
||||
{
|
||||
Set<List<String>> set = findAllCombinations(A);
|
||||
ArrayList<String> all = new ArrayList<String>();
|
||||
Iterator it = set.iterator();
|
||||
while (it.hasNext())
|
||||
{
|
||||
List<String> list = (List<String>) it.next();
|
||||
String s1 = "";
|
||||
for (String s2 : list)
|
||||
{
|
||||
s1 += s2;
|
||||
}
|
||||
all.add(s1);
|
||||
}
|
||||
Collections.sort(all, new Comparator<String>(){
|
||||
@Override
|
||||
public int compare(String o1, String o2)
|
||||
{
|
||||
return o1.length() - o2.length();
|
||||
}
|
||||
});
|
||||
return all;
|
||||
}
|
||||
/** Finds all distinct combinations of all sizes for chars of the String.
|
||||
*
|
||||
* @param A the characters to find their combinations.
|
||||
* @return all distinct combinations of the characters sorted by length, ascending order.
|
||||
*/
|
||||
public static ArrayList<String> allCombinations(String a)
|
||||
{
|
||||
String[] A = new String[a.length()];
|
||||
for (int i = 0; i < A.length; ++i)
|
||||
{
|
||||
A[i] = Character.toString(a.charAt(i));
|
||||
}
|
||||
Set<List<String>> set = findAllCombinations(A);
|
||||
ArrayList<String> all = new ArrayList<String>();
|
||||
Iterator it = set.iterator();
|
||||
while (it.hasNext())
|
||||
{
|
||||
List<String> list = (List<String>) it.next();
|
||||
String s1 = "";
|
||||
for (String s2 : list)
|
||||
{
|
||||
s1 += s2;
|
||||
}
|
||||
all.add(s1);
|
||||
}
|
||||
|
||||
Collections.sort(all, new Comparator<String>(){
|
||||
@Override
|
||||
public int compare(String o1, String o2)
|
||||
{
|
||||
return o1.length() - o2.length();
|
||||
}
|
||||
});
|
||||
return all;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
String[] A =
|
||||
{
|
||||
"1", "2", "3", "4"
|
||||
};
|
||||
int k = 2;
|
||||
|
||||
// process elements from left to right
|
||||
System.out.println(allCombinations(A));
|
||||
System.out.println(allCombinations("1234"));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
package com.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ASDV2
|
||||
*/
|
||||
public class FD
|
||||
{
|
||||
|
||||
private String lhs;
|
||||
|
||||
private String rhs;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param lhs the LHS of the FD
|
||||
* @param rhs the RHS of the FD
|
||||
* @throws IllegalArgumentException if the length of the LHS or the length
|
||||
* of the RHS are less than 1 or if they are null.
|
||||
*/
|
||||
public FD(String lhs, String rhs)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
if (lhs == null || rhs == null )
|
||||
throw new IllegalArgumentException( "the LHS and/or RHS cannot be null.");
|
||||
|
||||
if (lhs.length() < 1 || rhs.length() < 1 )
|
||||
throw new IllegalArgumentException( "the LHS and/or RHS cannot be of lenght less than 1.");
|
||||
|
||||
this.lhs = lhs;
|
||||
this.rhs = rhs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of rhs
|
||||
*
|
||||
* @return the value of rhs
|
||||
*/
|
||||
public String getRhs()
|
||||
{
|
||||
return rhs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of rhs
|
||||
*
|
||||
* @param rhs new value of rhs
|
||||
*/
|
||||
public void setRhs(String rhs)
|
||||
{
|
||||
this.rhs = rhs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of lhs
|
||||
*
|
||||
* @return the value of lhs
|
||||
*/
|
||||
public String getLhs()
|
||||
{
|
||||
return lhs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of lhs
|
||||
*
|
||||
* @param lhs new value of lhs
|
||||
*/
|
||||
public void setLhs(String lhs)
|
||||
{
|
||||
this.lhs = lhs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return lhs + " -> " + rhs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decomposes the RHS of the FD into singletons. where the LHS is the same
|
||||
* as this FD and the RHS is 1 character of each character of the FD.
|
||||
*
|
||||
* @return array of FD he
|
||||
*/
|
||||
public FD[] decomposeRightHandSide()
|
||||
{
|
||||
FD[] fdDecomosition = new FD[this.rhs.length()];
|
||||
|
||||
for (int i = 0; i < this.rhs.length(); ++i)
|
||||
{
|
||||
fdDecomosition[i] = new FD(this.lhs, Character.toString(rhs.charAt(i)));
|
||||
}
|
||||
return fdDecomosition;
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
/*
|
||||
* 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.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public abstract class GeometricObject implements Comparable {
|
||||
|
||||
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();
|
||||
|
||||
// Additional code below
|
||||
public abstract int compareTo(GeometricObject t);
|
||||
|
||||
public static GeometricObject max(GeometricObject o1, GeometricObject o2) {
|
||||
if (o1.getArea() >= o2.getArea()) {
|
||||
return o1;
|
||||
} else {
|
||||
return o2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* 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.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Large {
|
||||
public static void main(String[] args) {
|
||||
// Read data file
|
||||
// ArrayLists
|
||||
ArrayList firstNameArr = new ArrayList();
|
||||
ArrayList lastNameArr = new ArrayList();
|
||||
ArrayList jobTitleArr = new ArrayList();
|
||||
ArrayList salaryArr = new ArrayList();
|
||||
|
||||
File file = new File("Salary.txt");
|
||||
try (Scanner fileScanner = new Scanner(file)) {
|
||||
while (fileScanner.hasNext()) {
|
||||
firstNameArr.add(fileScanner.next());
|
||||
lastNameArr.add(fileScanner.next());
|
||||
jobTitleArr.add(fileScanner.next());
|
||||
salaryArr.add(fileScanner.next());
|
||||
fileScanner.nextLine(); // consume newline
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
System.out.println("Unable to read file");
|
||||
}
|
||||
for (int i = 0; i < firstNameArr.size(); ++i) {
|
||||
System.out.println("first name :" + firstNameArr.get(i));
|
||||
System.out.println("last name: " + lastNameArr.get(i));
|
||||
System.out.println("job title: " + jobTitleArr.get(i));
|
||||
System.out.println("salary: " + salaryArr.get(i));
|
||||
System.out.println("--------------------------------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package com.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class MP5_ChloeFontenot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* 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.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MyStack implements Cloneable {
|
||||
private ArrayList<Object> list = new ArrayList<>();
|
||||
|
||||
public boolean isEmpty() {
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
public Object peek() {
|
||||
return list.get(getSize() - 1);
|
||||
}
|
||||
|
||||
public Object pop() {
|
||||
Object o = list.get(getSize() - 1);
|
||||
list.remove(getSize() - 1);
|
||||
return o;
|
||||
}
|
||||
|
||||
public void push(Object o) {
|
||||
list.add(o);
|
||||
}
|
||||
|
||||
@Override /** Override the toString in the Object class */
|
||||
public String toString() {
|
||||
return "stack: " + list.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object clone() throws CloneNotSupportedException {
|
||||
MyStack clonedStack = (MyStack) super.clone();
|
||||
ArrayList<Object> newList = new ArrayList<>(list);
|
||||
clonedStack.list = newList;
|
||||
return clonedStack;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws CloneNotSupportedException {
|
||||
MyStack stack = new MyStack();
|
||||
final int STACK_SIZE = 50;
|
||||
for (int i = 0; i < STACK_SIZE; ++i) {
|
||||
stack.push(i + 1); // Fill Stack with sequential numbers.
|
||||
//System.out.println(stack.peek());
|
||||
}
|
||||
System.out.println(stack.peek());
|
||||
MyStack clonedStack = (MyStack) stack.clone();
|
||||
System.out.println(clonedStack.peek());
|
||||
}
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
/*
|
||||
* 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.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ASDV2
|
||||
*/
|
||||
public class NormalizeDatabase
|
||||
{
|
||||
/**Finds the closure of a set of attributes given a set of FDs of a relation R
|
||||
*
|
||||
* @param attributes attributes to find their closure
|
||||
* @param fds set of FDs of relation R
|
||||
* @return the closure of the parameter attributes.
|
||||
*/
|
||||
public static String closure(String attributes, ArrayList<FD> fds )
|
||||
{
|
||||
attributes = attributes.toUpperCase();
|
||||
for (int j = 0; j < fds.size(); ++j)
|
||||
{
|
||||
FD capitalsFD = new FD(fds.get(j).getLhs().toUpperCase(),
|
||||
fds.get(j).getRhs().toUpperCase());
|
||||
fds.set(j, capitalsFD);
|
||||
}
|
||||
// 1. Set x+ = x
|
||||
String closure = attributes;
|
||||
String closurePreveious = attributes;
|
||||
// 2. Starting with x+ apply each FD xF —> y in F where
|
||||
// xF belongs in closure x+ but but the rhs y is not already in x+, to find determined
|
||||
// attributes y
|
||||
// 3. x+ = x+ U y
|
||||
|
||||
while (true)
|
||||
{
|
||||
|
||||
for (int i = 0; i < fds.size(); ++i)
|
||||
{
|
||||
if (closure.contains(fds.get(i).getRhs()))
|
||||
continue;
|
||||
// if the left hand side of the FD is contained in the closure
|
||||
// then add to the closure the RHS of the FD
|
||||
if (closure.contains(fds.get(i).getLhs()))
|
||||
closure += fds.get(i).getRhs();
|
||||
}
|
||||
if (closurePreveious.equals(closure))
|
||||
break;
|
||||
else
|
||||
closurePreveious = closure;
|
||||
}
|
||||
|
||||
// 4, If y not empty goto (2)
|
||||
// 5. Return x+
|
||||
return closure;
|
||||
|
||||
}
|
||||
/**
|
||||
* Eliminates redundant attributes from the LHS of each FD of a set of FDs
|
||||
* given as parameters.
|
||||
*
|
||||
* @param fds the set of FDs to eliminate the redundancy
|
||||
* @return and ArrayList with no redundancy on LHS of each FD.
|
||||
*/
|
||||
public static ArrayList<FD> eliminateRedundantAttributes(ArrayList<FD> fds)
|
||||
{
|
||||
for (int j = 0; j < fds.size(); ++j)
|
||||
{
|
||||
int s = fds.get(j).getLhs().length();
|
||||
if (s < 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
String fl = fds.get(j).getLhs().substring(0, 1);
|
||||
ArrayList<FD> fFD = new ArrayList<FD>();
|
||||
String s1 = " ";
|
||||
if (fds.get(j).getLhs().length() == 2)
|
||||
{
|
||||
s1 = fds.get(j).getLhs().substring(1);
|
||||
if (closure(s1,fds).contains(fl))
|
||||
{
|
||||
|
||||
fds.add(new FD (fds.get(j).getLhs().substring(1, 2), fds.get(j).getRhs()));
|
||||
fds.remove(j);
|
||||
|
||||
}
|
||||
}
|
||||
else if (fds.get(j).getLhs().charAt(1) == 3)
|
||||
{
|
||||
s1 = fds.get(j).getLhs().substring(1);
|
||||
if (closure(s1,fds).contains(fl))
|
||||
{
|
||||
fds.add(new FD (fds.get(j).getLhs().substring(1, 2), fds.get(j).getRhs()));
|
||||
fds.add(new FD (fds.get(j).getLhs().substring(2, 3), fds.get(j).getRhs()));
|
||||
fds.remove(j);
|
||||
}
|
||||
}
|
||||
else if (fds.get(j).getLhs().charAt(1) == 4)
|
||||
{
|
||||
s1 = fds.get(j).getLhs().substring(1);
|
||||
if (closure(s1,fds).contains(fl))
|
||||
{
|
||||
fds.add(new FD (fds.get(j).getLhs().substring(1, 2), fds.get(j).getRhs()));
|
||||
fds.add(new FD (fds.get(j).getLhs().substring(2, 3), fds.get(j).getRhs()));
|
||||
fds.add(new FD (fds.get(j).getLhs().substring(3, 4), fds.get(j).getRhs()));
|
||||
fds.remove(j);
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
return fds;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return fds;
|
||||
}
|
||||
public static void main(String[] args)
|
||||
{
|
||||
ArrayList<FD> fds = new ArrayList<FD>();
|
||||
FD fd = new FD("a", "BC");
|
||||
FD[] fdDecomposed = fd.decomposeRightHandSide();
|
||||
for (int i = 0; i < fdDecomposed.length; ++i)
|
||||
{
|
||||
fds.add( new FD(fdDecomposed[i].getLhs(), fdDecomposed[i].getRhs()));
|
||||
}
|
||||
|
||||
fds.add(new FD("B", "C"));
|
||||
fds.add(new FD("AB", "B"));
|
||||
fds.add(new FD("C", "A"));
|
||||
System.out.println(fds);
|
||||
System.out.println(closure("b", fds));
|
||||
System.out.println(eliminateRedundantAttributes(fds));
|
||||
/* TEST it with
|
||||
Let F1 = {1. A -> BC
|
||||
2. B -> C,
|
||||
3. AB -> D }.
|
||||
Attribute B is extraneous in FD 3 AB -> D
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
F2 = { 1. AB -> C,
|
||||
2. C -> A,
|
||||
3. BC -> D,
|
||||
4. ACD -> B,
|
||||
5. D -> E,
|
||||
6. D -> G,
|
||||
7. BE -> C,
|
||||
8. CG -> B,
|
||||
9. CG -> D,
|
||||
10. CE -> A,
|
||||
11. CE -> G}
|
||||
*/
|
||||
}
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
/*
|
||||
* 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.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
import java.util.Calendar;
|
||||
import java.util.Scanner;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
public class PrintCalendar {
|
||||
/** Main method */
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
int year, month;
|
||||
String userInput;
|
||||
|
||||
do {
|
||||
System.out.println("What would you like to do?");
|
||||
System.out.println("1. Print today's date.");
|
||||
System.out.println("2. Print a specified date.");
|
||||
System.out.println("Q/q. Quit.");
|
||||
System.out.print("Respond with 1, 2 or Q/q: ");
|
||||
userInput = input.next();
|
||||
if (userInput.toLowerCase().charAt(0) == 'q') {
|
||||
System.exit(0);
|
||||
}
|
||||
if (Integer.parseInt(userInput) < 0 || Integer.parseInt(userInput) > 2) {
|
||||
System.out.println("Invalid input!");
|
||||
} else {
|
||||
if (Integer.parseInt(userInput) == 1) {
|
||||
Date date = new Date();
|
||||
printMonth((date.getYear() + 1900), date.getMonth());
|
||||
} else if (Integer.parseInt(userInput) == 2) {
|
||||
System.out.print("Enter a month (1-12): ");
|
||||
month = (input.nextInt() + 1);
|
||||
System.out.print("Enter a year: ");
|
||||
year = (input.nextInt());
|
||||
GregorianCalendar date = new GregorianCalendar(year, month, 0, 0, 0);
|
||||
printMonth(date.get(Calendar.YEAR), date.get(Calendar.MONTH));
|
||||
}
|
||||
}
|
||||
} while (true);
|
||||
|
||||
}
|
||||
|
||||
/** Print the calendar for a month in a year */
|
||||
public static void printMonth(int year, int month) {
|
||||
// Print the headings of the calendar
|
||||
printMonthTitle(year, month);
|
||||
|
||||
// Print the body of the calendar
|
||||
printMonthBody(year, month);
|
||||
}
|
||||
|
||||
/** Print the month title, e.g., May, 1999 */
|
||||
public static void printMonthTitle(int year, int month) {
|
||||
System.out.println(" " + getMonthName(month)
|
||||
+ " " + year);
|
||||
System.out.println("-----------------------------");
|
||||
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
|
||||
}
|
||||
|
||||
/** Get the English name for the month */
|
||||
public static String getMonthName(int month) {
|
||||
String monthName = "";
|
||||
switch (month) {
|
||||
case 1: monthName = "January"; break;
|
||||
case 2: monthName = "February"; break;
|
||||
case 3: monthName = "March"; break;
|
||||
case 4: monthName = "April"; break;
|
||||
case 5: monthName = "May"; break;
|
||||
case 6: monthName = "June"; break;
|
||||
case 7: monthName = "July"; break;
|
||||
case 8: monthName = "August"; break;
|
||||
case 9: monthName = "September"; break;
|
||||
case 10: monthName = "October"; break;
|
||||
case 11: monthName = "November"; break;
|
||||
case 12: monthName = "December";
|
||||
}
|
||||
|
||||
return monthName;
|
||||
}
|
||||
|
||||
/** Print month body */
|
||||
public static void printMonthBody(int year, int month) {
|
||||
// Get start day of the week for the first date in the month
|
||||
int startDay = getStartDay(year, month);
|
||||
|
||||
// Get number of days in the month
|
||||
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
|
||||
|
||||
// Pad space before the first day of the month
|
||||
int i = 0;
|
||||
for (i = 0; i < startDay; i++)
|
||||
System.out.print(" ");
|
||||
|
||||
for (i = 1; i <= numberOfDaysInMonth; i++) {
|
||||
System.out.printf("%4d", i);
|
||||
|
||||
if ((i + startDay) % 7 == 0)
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/** Get the start day of month/1/year */
|
||||
public static int getStartDay(int year, int month) {
|
||||
final int START_DAY_FOR_JAN_1_1800 = 3;
|
||||
// Get total number of days from 1/1/1800 to month/1/year
|
||||
int totalNumberOfDays = getTotalNumberOfDays(year, month);
|
||||
|
||||
// Return the start day for month/1/year
|
||||
return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
|
||||
}
|
||||
|
||||
/** Get the total number of days since January 1, 1800 */
|
||||
public static int getTotalNumberOfDays(int year, int month) {
|
||||
int total = 0;
|
||||
|
||||
// Get the total days from 1800 to 1/1/year
|
||||
for (int i = 1800; i < year; i++)
|
||||
if (isLeapYear(i))
|
||||
total = total + 366;
|
||||
else
|
||||
total = total + 365;
|
||||
|
||||
// Add days from Jan to the month prior to the calendar month
|
||||
for (int i = 1; i < month; i++)
|
||||
total = total + getNumberOfDaysInMonth(year, i);
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
/** Get the number of days in a month */
|
||||
public static int getNumberOfDaysInMonth(int year, int month) {
|
||||
if (month == 1 || month == 3 || month == 5 || month == 7 ||
|
||||
month == 8 || month == 10 || month == 12)
|
||||
return 31;
|
||||
|
||||
if (month == 4 || month == 6 || month == 9 || month == 11)
|
||||
return 30;
|
||||
|
||||
if (month == 2) return isLeapYear(year) ? 29 : 28;
|
||||
|
||||
return 0; // If month is incorrect
|
||||
}
|
||||
|
||||
/** Determine if it is a leap year */
|
||||
public static boolean isLeapYear(int year) {
|
||||
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* 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.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
public double getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setWidth(double width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public double getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(double height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return width * height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getPerimeter() {
|
||||
return 2 * (width + height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(GeometricObject t) {
|
||||
System.out.println(this.getArea() + ", " + (t).getArea());
|
||||
if (this.getArea() < (t).getArea()) {
|
||||
return -1;
|
||||
} else if (this.getArea() > (t).getArea()) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Object t) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* 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.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class ShuffleArrayList {
|
||||
|
||||
public static void shuffle(ArrayList<Number> list) {
|
||||
// Create a new Random object.
|
||||
Random rng = new Random();
|
||||
// Create an ArrayList to store the indices of the elements that have been selected.
|
||||
ArrayList<Integer> selectedIndices = new ArrayList<Integer>();
|
||||
|
||||
// Loop through each element in the list.
|
||||
for (int i = 0; i < list.size(); ++i) {
|
||||
// Generate a random index that has not been selected before.
|
||||
int randomIndex;
|
||||
do {
|
||||
randomIndex = rng.nextInt(list.size()); // Generate a random integer between 0 (inclusive) and the size of the list (exclusive).
|
||||
} while (selectedIndices.contains(randomIndex)); // Repeat until an unselected index is found.
|
||||
selectedIndices.add(randomIndex); // Add the selected index to the list of selected indices.
|
||||
//System.out.println(randomIndex + ", " + i);
|
||||
// Swap the element at the random index with the element at the current index of the loop.
|
||||
// This shuffles the list by randomly selecting an element to swap with the current element at each iteration.
|
||||
Number temp = list.get(randomIndex); // Save the element at the random index to a temporary variable.
|
||||
list.set(randomIndex, list.get(i)); // Overwrite the element at the random index with the element at the current index of the loop.
|
||||
list.set(i, temp); // Set the current index of the loop to the saved element, effectively swapping the two elements.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String checkForDuplicates(ArrayList<Number> list) {
|
||||
// Ensure Array does not include repeat numbers.
|
||||
boolean repeatNumber = false;
|
||||
for (int i = 0; i < list.size(); ++i) {
|
||||
for (int j = 0; j < list.size(); ++j) {
|
||||
if (i != j) {
|
||||
//System.out.println("Checking " + list.get(i) + " and " + list.get(j));
|
||||
if (list.get(i) == list.get(j)) {
|
||||
repeatNumber = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (repeatNumber) {
|
||||
return "Numbers repeat in ArrayList.";
|
||||
} else {
|
||||
return "Numbers do not repeat in ArrayList.";
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
final int ARRAY_SIZE = 50;
|
||||
ArrayList<Number> list = new ArrayList<>();
|
||||
for (int i = 0; i < ARRAY_SIZE; ++i) {
|
||||
list.add(i + 1); // Fill ArrayList with sequential numbers.
|
||||
}
|
||||
|
||||
System.out.println(list);
|
||||
System.out.println(checkForDuplicates(list));
|
||||
shuffle(list);
|
||||
System.out.println(list);
|
||||
System.out.println(checkForDuplicates(list));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* 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.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class SortArrayList {
|
||||
|
||||
public static void sort(ArrayList<Number> list) {
|
||||
// Selection sort implementation for ArrayLists.
|
||||
for (int i = 0; i < list.size(); ++i) {
|
||||
for (int j = i + 1; j < list.size(); ++j) {
|
||||
// BigDecimal should work for any type. Have not confirmed this.
|
||||
Number numI = list.get(i);
|
||||
Number numJ = list.get(j);
|
||||
BigDecimal bigNumI = new BigDecimal(list.get(i).toString());
|
||||
BigDecimal bigNumJ = new BigDecimal(list.get(j).toString());
|
||||
if (bigNumI.compareTo(bigNumJ) == 1) {
|
||||
Number tmp = numI;
|
||||
list.set(i, numJ);
|
||||
list.set(j, tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
final int ARRAY_SIZE = 50;
|
||||
ArrayList<Number> list = new ArrayList<>();
|
||||
for (int i = 0; i < ARRAY_SIZE; ++i) {
|
||||
list.add((i) + Math.random()); // Fill ArrayList with sequential numbers.
|
||||
}
|
||||
System.out.println(list);
|
||||
ShuffleArrayList.shuffle(list); // Use our shuffle method from earlier
|
||||
System.out.println(list);
|
||||
sort(list);
|
||||
System.out.println(list);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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.chloefontenot.mp5_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class TestGeometricObject {
|
||||
public static void main(String[] args) {
|
||||
// Create two comparable Circles
|
||||
Circle circle1 = new Circle(5);
|
||||
Circle circle2 = new Circle(4);
|
||||
|
||||
// Display the max Circle
|
||||
Circle circle = (Circle) GeometricObject.max(circle1, circle2);
|
||||
System.out.println("The max Circle's radius is " + circle.getRadius());
|
||||
System.out.println(circle);
|
||||
|
||||
// Create two comparable rectangles
|
||||
Rectangle r1 = new Rectangle(5, 4);
|
||||
Rectangle r2 = new Rectangle(4, 5);
|
||||
|
||||
System.out.println(r1.compareTo(r2));
|
||||
System.out.println("The max rectangle is " + (Rectangle) Rectangle.max(r1, r2));
|
||||
|
||||
System.out.println("The max geometric object is " + GeometricObject.max(circle1, r2));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user