Reset author name to chosen name ✨
This commit is contained in:
@@ -1,30 +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 edu.slcc.asdv.chloe.lab_chloefontenot_maximumorderedstring;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class CompareSize implements Comparator {
|
||||
|
||||
@Override
|
||||
public int compare(Object o1, Object o2)
|
||||
{
|
||||
int str1 = ((String) o1).length();
|
||||
int str2 = ((String) o2).length();
|
||||
if (str1 > str2) {
|
||||
return -1;
|
||||
} else if(str1 == str2) {
|
||||
return 0;
|
||||
} else if (str1 < str2) {
|
||||
return 1;
|
||||
}
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 edu.slcc.asdv.chloe.lab_chloefontenot_maximumorderedstring;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class FindMax {
|
||||
public static String findMax(String input) {
|
||||
int iterationCount = 0;
|
||||
ArrayList<String> possibleSubStr = new ArrayList<>();
|
||||
String currentStr = "";
|
||||
for (int i = 0; i < input.length() -2; ++i) {
|
||||
currentStr = input.charAt(i) + "";
|
||||
for(int j = i + 1; j < input.length() -1; ++j) {
|
||||
iterationCount++;
|
||||
if (input.toLowerCase().charAt(i) < input.toLowerCase().charAt(j)) {
|
||||
currentStr += input.charAt(j);
|
||||
} else {
|
||||
possibleSubStr.add(currentStr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Iteration count: " + iterationCount);
|
||||
System.out.println(possibleSubStr);
|
||||
Collections.sort(possibleSubStr, new CompareSize());
|
||||
return possibleSubStr.get(0);
|
||||
}
|
||||
public static void main(String[] args)
|
||||
{
|
||||
//System.out.print("Enter a string: ");
|
||||
//Scanner input = new Scanner(System.in);
|
||||
String inputString = "abcdabcdefgzabcdefadcdefab";
|
||||
//input.nextLine();
|
||||
System.out.print("The maximum sorted subString is: ");
|
||||
System.out.println(findMax(inputString));
|
||||
}
|
||||
}
|
||||
@@ -1,38 +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 edu.slcc.asdv.chloe.lab_chloefontenot_maximumorderedstring;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class FindMaxOrderedSubstring {
|
||||
|
||||
public static String findMaxOrderedSubString(String input) {
|
||||
String outputString = "";
|
||||
int j = 0;
|
||||
for (int i = 0; i < input.length(); ++i) {
|
||||
if (i == 0) {
|
||||
outputString += input.charAt(i);
|
||||
}
|
||||
if (input.charAt(i) > outputString.charAt(j)) {
|
||||
System.out.println(++j);
|
||||
outputString += input.charAt(i);
|
||||
}
|
||||
}
|
||||
return outputString;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter a string: ");
|
||||
String inputStr = input.nextLine();
|
||||
System.out.println(findMaxOrderedSubString(inputStr));
|
||||
}
|
||||
}
|
||||
@@ -1,105 +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 edu.slcc.asdv.chloe.lab_chloefontenot_maximumorderedstring;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
import java.util.Comparator;
|
||||
|
||||
public class GenericMergeSort {
|
||||
|
||||
public static <E extends Comparable<E>> void mergeSort(E[] list)
|
||||
{
|
||||
mergeSort(list,
|
||||
new Comparator<E>() {
|
||||
@Override
|
||||
public int compare(E e1, E e2)
|
||||
{
|
||||
return ((Comparable<E>) e1).compareTo(e2);
|
||||
}
|
||||
});
|
||||
}
|
||||
static int recursiveCallCount = 0;
|
||||
public static <E> void mergeSort(E[] list,
|
||||
Comparator<? super E> comparator)
|
||||
{
|
||||
|
||||
if (list.length > 1) {
|
||||
recursiveCallCount++;
|
||||
// Merge sort the first half
|
||||
E[] firstHalf = (E[]) new Object[list.length / 2];
|
||||
System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
|
||||
mergeSort(firstHalf, comparator);
|
||||
|
||||
// Merge sort the second half
|
||||
int secondHalfLength = list.length - list.length / 2;
|
||||
E[] secondHalf = (E[]) new Object[secondHalfLength];
|
||||
System.arraycopy(list, list.length / 2,
|
||||
secondHalf, 0, secondHalfLength);
|
||||
mergeSort(secondHalf, comparator);
|
||||
|
||||
// Merge firstHalf with secondHalf
|
||||
E[] temp = merge1(firstHalf, secondHalf, comparator);
|
||||
System.arraycopy(temp, 0, list, 0, temp.length);
|
||||
}
|
||||
}
|
||||
|
||||
private static <E> E[]
|
||||
merge1(E[] list1, E[] list2, Comparator<? super E> comparator)
|
||||
{
|
||||
E[] temp = (E[]) new Object[list1.length + list2.length];
|
||||
|
||||
int current1 = 0; // Index in list1
|
||||
int current2 = 0; // Index in list2
|
||||
int current3 = 0; // Index in temp
|
||||
|
||||
while (current1 < list1.length && current2 < list2.length) {
|
||||
if (comparator.compare(list1[current1], list2[current2]) < 0) {
|
||||
temp[current3++] = list1[current1++];
|
||||
} else {
|
||||
temp[current3++] = list2[current2++];
|
||||
}
|
||||
}
|
||||
|
||||
while (current1 < list1.length) {
|
||||
temp[current3++] = list1[current1++];
|
||||
}
|
||||
|
||||
while (current2 < list2.length) {
|
||||
temp[current3++] = list2[current2++];
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Integer[] list
|
||||
= {
|
||||
2, 3, 2, 5, 6, 1, -2, 3, 14, 12
|
||||
};
|
||||
mergeSort(list);
|
||||
System.out.println("number of recursive calls: " + recursiveCallCount);
|
||||
recursiveCallCount = 0;
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
System.out.print(list[i] + " ");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
String[] list1
|
||||
= {
|
||||
"ABC", "abc", "abm", "Anf", "Good", "Bad", "nice"
|
||||
};
|
||||
mergeSort(list1, (s1, s2) -> s1.compareToIgnoreCase(s2));
|
||||
System.out.println("number of recursive calls: " + recursiveCallCount);
|
||||
recursiveCallCount = 0;
|
||||
for (int i = 0; i < list1.length; i++) {
|
||||
System.out.print(list1[i] + " ");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,56 +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 edu.slcc.asdv.chloe.lab_chloefontenot_maximumorderedstring;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class JudeFindMax {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(consecLetters("abcabcdgabxy"));
|
||||
System.out.println(consecLetters("abcabcdgabmnsxy"));
|
||||
System.out.println(consecLetters("abchjsfhajshfhijklmnopqjfaksfhkajhsf"));
|
||||
System.out.println(consecLetters("hnafffgardghikortmmnmnmn"));
|
||||
}
|
||||
|
||||
public static String consecLetters(String input)
|
||||
{
|
||||
String consec = "";
|
||||
String maxConsec = "";
|
||||
|
||||
for (int i = 1; i < input.length(); i++) //n - 1
|
||||
{
|
||||
//If two in order
|
||||
if (input.charAt(i) > input.charAt(i - 1))
|
||||
{
|
||||
//If length is zero then add previous as well
|
||||
if (consec.length() == 0)
|
||||
consec += input.charAt(i - 1);
|
||||
consec += input.charAt(i);
|
||||
}
|
||||
//If not in order
|
||||
else
|
||||
{
|
||||
//Check current consec length against maximum length
|
||||
if (consec.length() > maxConsec.length())
|
||||
{
|
||||
//set new max
|
||||
maxConsec = consec;
|
||||
}
|
||||
consec = "";
|
||||
}
|
||||
}
|
||||
|
||||
//Check current consec length against maximum length
|
||||
if (consec.length() > maxConsec.length())
|
||||
{
|
||||
//set new max
|
||||
maxConsec = consec;
|
||||
}
|
||||
|
||||
return "Most letters found in order: " + maxConsec;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package edu.slcc.asdv.chloe.lab_chloefontenot_maximumorderedstring;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Lab_ChloeFontenot_MaximumOrderedString {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,2 +0,0 @@
|
||||
com.chloefontenot.mp5.files_chloefontenot.Exercise17_05
|
||||
com.chloefontenot.mp5.files_chloefontenot.DataContainer
|
||||
Binary file not shown.
@@ -1,204 +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.files_chloefontenot;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.HPos;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.ColumnConstraints;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.layout.RowConstraints;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class AddressBookFX extends Application {
|
||||
|
||||
ArrayList<AddressBookEntry> addressArray = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws Exception
|
||||
{
|
||||
GridPane primaryGridPane = new GridPane();
|
||||
GridPane textFieldGridPane = new GridPane();
|
||||
GridPane addressFieldPane = new GridPane();
|
||||
GridPane buttonFieldPane = new GridPane();
|
||||
|
||||
// Text fields / labels
|
||||
textFieldGridPane.add(new Label("Name"), 0, 0);
|
||||
textFieldGridPane.add(new Label("Street"), 0, 1);
|
||||
textFieldGridPane.add(new Label("City"), 0, 2);
|
||||
addressFieldPane.add(new Label("State"), 2, 0);
|
||||
addressFieldPane.add(new Label("ZIP"), 4, 0);
|
||||
TextField nameTextField = new TextField();
|
||||
TextField streetTextField = new TextField();
|
||||
TextField cityTextField = new TextField();
|
||||
TextField stateTextField = new TextField();
|
||||
TextField zipTextField = new TextField();
|
||||
textFieldGridPane.add(nameTextField, 1, 0);
|
||||
textFieldGridPane.add(streetTextField, 1, 1);
|
||||
textFieldGridPane.add(addressFieldPane, 1, 2);
|
||||
addressFieldPane.add(cityTextField, 1, 0);
|
||||
addressFieldPane.add(stateTextField, 3, 0);
|
||||
addressFieldPane.add(zipTextField, 5, 0);
|
||||
textFieldGridPane.getColumnConstraints().add(new ColumnConstraints(50));
|
||||
primaryGridPane.add(textFieldGridPane, 0, 0);
|
||||
|
||||
// Buttons
|
||||
Button addButton = new Button("Add");
|
||||
Button firstButton = new Button("First");
|
||||
Button nextButton = new Button("Next");
|
||||
Button previousButton = new Button("Previous");
|
||||
Button lastButton = new Button("Last");
|
||||
Button updateButton = new Button("Update");
|
||||
buttonFieldPane.add(addButton, 0, 0);
|
||||
buttonFieldPane.add(firstButton, 1, 0);
|
||||
buttonFieldPane.add(nextButton, 2, 0);
|
||||
buttonFieldPane.add(previousButton, 3, 0);
|
||||
buttonFieldPane.add(lastButton, 4, 0);
|
||||
buttonFieldPane.add(updateButton, 5, 0);
|
||||
primaryGridPane.add(buttonFieldPane, 0, 1);
|
||||
primaryGridPane.setAlignment(Pos.CENTER);
|
||||
buttonFieldPane.setHgap(10);
|
||||
|
||||
addButton.setOnAction(e -> {
|
||||
|
||||
});
|
||||
firstButton.setOnAction(e -> {
|
||||
|
||||
});
|
||||
nextButton.setOnAction(e -> {
|
||||
|
||||
});
|
||||
previousButton.setOnAction(e -> {
|
||||
|
||||
});
|
||||
lastButton.setOnAction(e -> {
|
||||
|
||||
});
|
||||
updateButton.setOnAction(e -> {
|
||||
|
||||
});
|
||||
|
||||
Scene scene = new Scene(primaryGridPane);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
File addressBook = new File("AddressBookFX.dat");
|
||||
if (!addressBook.exists()) {
|
||||
try (ObjectOutputStream fileStream = new ObjectOutputStream(new FileOutputStream(addressBook))) {
|
||||
fileStream.writeObject(new DataContainer());
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
} else {
|
||||
|
||||
try (ObjectInputStream fileStream = new ObjectInputStream(new FileInputStream(addressBook))) {
|
||||
addressBook = (ArrayList<AddressBookEntry>) fileStream.readObject();
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
|
||||
launch();
|
||||
}
|
||||
}
|
||||
|
||||
class AddressBookEntry implements Serializable {
|
||||
|
||||
private String name;
|
||||
private String street;
|
||||
private String city;
|
||||
private String state;
|
||||
private String zip;
|
||||
|
||||
public AddressBookEntry()
|
||||
{
|
||||
this.name = "Enter a name here.";
|
||||
this.street = "Enter a street here.";
|
||||
this.city = "Enter a city here.";
|
||||
this.state = "Enter a state here.";
|
||||
this.zip = "Enter a zip here.";
|
||||
}
|
||||
|
||||
public AddressBookEntry(String name, String street, String city, String state, String zip)
|
||||
{
|
||||
this.name = name;
|
||||
this.street = street;
|
||||
this.city = city;
|
||||
this.state = state;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public String getZip()
|
||||
{
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(String zip)
|
||||
{
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public String getState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state)
|
||||
{
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getCity()
|
||||
{
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city)
|
||||
{
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getStreet()
|
||||
{
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street)
|
||||
{
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,52 +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.files_chloefontenot;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Exercise17_01 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
try (RandomAccessFile fileIO = new RandomAccessFile("Exercise17_01.txt", "rw")) {
|
||||
int random = 0;
|
||||
System.out.println("Writing data to file...");
|
||||
for (int i = 1; i < 100 + 1; ++i) {
|
||||
random = (int) (Math.random() * 9) + 1;
|
||||
System.out.print(random + " ");
|
||||
if (i != 0 && i % 10 == 0) {
|
||||
System.out.println();
|
||||
}
|
||||
fileIO.writeInt(random);
|
||||
}
|
||||
System.out.println("Wrote to the file successfully!");
|
||||
System.out.println("File contents:");
|
||||
fileIO.seek(0);
|
||||
int readIterator = 1;
|
||||
while (true) {
|
||||
try {
|
||||
System.out.print(fileIO.readInt() + " ");
|
||||
if (readIterator != 0 && readIterator % 10 == 0) {
|
||||
System.out.println();
|
||||
}
|
||||
++readIterator;
|
||||
} catch (EOFException e) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,70 +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.files_chloefontenot;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Exercise17_03 {
|
||||
|
||||
static int fileSize = Math.abs((int) (Math.random() * 1024));
|
||||
|
||||
public static void writeData()
|
||||
{
|
||||
try (FileOutputStream fileWrite = new FileOutputStream("Exercise17_03.dat")) {
|
||||
// Write a unspecified number of integers into the file.
|
||||
// Must be positive!
|
||||
int randInt = 0;
|
||||
for (int i = 0; i < fileSize; ++i) {
|
||||
randInt = (int) (Math.random() * 10);
|
||||
fileWrite.write(randInt);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
System.out.println("Wrote data to the file!");
|
||||
}
|
||||
|
||||
public static int[] readData()
|
||||
{
|
||||
int[] fileData = new int[fileSize];
|
||||
try (FileInputStream fileRead = new FileInputStream("Exercise17_03.dat")) {
|
||||
// Read the data back
|
||||
int dataIterator = 0;
|
||||
int dataStream = 0;
|
||||
while (fileRead.available() > 0) {
|
||||
dataStream = fileRead.read();
|
||||
fileData[dataIterator++] = dataStream;
|
||||
System.out.print(dataStream + " ");
|
||||
if ((dataIterator + 1) % 10 == 0) {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
return fileData;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println("Ints to write: " + fileSize);
|
||||
writeData();
|
||||
int[] fileData = readData();
|
||||
// Sum the digits
|
||||
int sum = 0;
|
||||
for (int i: fileData) {
|
||||
sum += i;
|
||||
}
|
||||
System.out.println("\nThe sum of the integers in the file is: " + sum);
|
||||
}
|
||||
}
|
||||
@@ -1,79 +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.files_chloefontenot;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.Serializable;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Exercise17_05 {
|
||||
public static void main(String[] args) {
|
||||
File dataFile = new File("Exercise17_05.dat");
|
||||
if(!dataFile.exists()) {
|
||||
try (ObjectOutputStream fileStream = new ObjectOutputStream(new FileOutputStream(dataFile))) {
|
||||
fileStream.writeObject(new DataContainer());
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
DataContainer data = null;
|
||||
try (ObjectInputStream fileStream = new ObjectInputStream(new FileInputStream(dataFile))) {
|
||||
data = (DataContainer) fileStream.readObject();
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
// Now print out the data!
|
||||
System.out.println("We got the data from the file!");
|
||||
System.out.println(data.toString());
|
||||
}
|
||||
}
|
||||
|
||||
class DataContainer implements Serializable {
|
||||
int[] intArray = {1, 2, 3, 4};
|
||||
Date currentDate = new Date();
|
||||
double doubleMoment = 5.5;
|
||||
|
||||
public int[] getIntArray()
|
||||
{
|
||||
return intArray;
|
||||
}
|
||||
|
||||
public Date getCurrentDate()
|
||||
{
|
||||
return currentDate;
|
||||
}
|
||||
|
||||
public double getDoubleMoment()
|
||||
{
|
||||
return doubleMoment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String intString = "[";
|
||||
for (int i = 0; i < intArray.length - 1; ++i) {
|
||||
intString += intArray[i];
|
||||
if (i == (intArray.length - 2)) {
|
||||
intString += "]";
|
||||
} else {
|
||||
intString += ", ";
|
||||
}
|
||||
}
|
||||
return "DataContainer{" + "intArray=" + intString + ", currentDate=" + currentDate + ", doubleMoment=" + doubleMoment + '}';
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
public class SystemInfo {
|
||||
|
||||
public static String javaVersion() {
|
||||
return System.getProperty("java.version");
|
||||
}
|
||||
|
||||
public static String javafxVersion() {
|
||||
return System.getProperty("javafx.version");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,975 +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 edu.slcc.asdv.chloe.mp4_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ASDV2
|
||||
*/
|
||||
public class ArrayListASDV<E>
|
||||
implements Serializable, Cloneable, List<E> {
|
||||
|
||||
private E[] list;
|
||||
private int index;//the index to add at ( length of array)
|
||||
|
||||
//private Class<E> type;
|
||||
/**
|
||||
* Constructs an empty list with an initial capacity of three.
|
||||
*
|
||||
*/
|
||||
public ArrayListASDV() {
|
||||
list = (E[]) new Object[3];
|
||||
index = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an empty list with the specified initial capacity.
|
||||
*
|
||||
* @param initialCapacity - the initial capacity of the list
|
||||
* @throws IllegalArgumentException - if the specified initial capacity is negative
|
||||
*/
|
||||
public ArrayListASDV(int initialCapacity) {
|
||||
if (initialCapacity < 0) {
|
||||
throw new IllegalArgumentException("initialCapacity id negative: " + initialCapacity);
|
||||
}
|
||||
list = (E[]) new Object[initialCapacity];
|
||||
index = 0;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Double the size of the current list and copies to it the old list
|
||||
*
|
||||
* @return the new array.
|
||||
*/
|
||||
private E[] doubleSizeOfList() {
|
||||
list = this.toArray((E[]) new Object[list.length + list.length]);
|
||||
return this.list;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
|
||||
*
|
||||
* @param c - the collection whose elements are to be placed into this
|
||||
* @throws NullPointerException - if the specified collection is null
|
||||
*
|
||||
*
|
||||
*/
|
||||
public ArrayListASDV(Collection<? extends E> c) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this collection changed as a result of the call. false if this collection does not permit duplicates and already contains the specified element.
|
||||
*
|
||||
* @param e - element whose presence in this collection is to be ensured
|
||||
*
|
||||
* @return true if this collection changed as a result of the call
|
||||
* @throws ClassCastException - if the class of the specified element prevents it from being added to this collection
|
||||
* @throws NullPointerException - if the specified element is null and this collection does not permit null elements
|
||||
* @throws IllegalArgumentException - if some property of the element prevents it from being added to this collection
|
||||
*/
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
if (e == null) {
|
||||
throw new NullPointerException("null parameter");
|
||||
}
|
||||
|
||||
list[index++] = e;
|
||||
if (index >= list.length * 0.75) {
|
||||
doubleSizeOfList();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements in this list.
|
||||
*
|
||||
* @return the number of elements in this list.
|
||||
*/
|
||||
@Override
|
||||
public int size() {
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "ArrayListASDV[";
|
||||
|
||||
for (int i = 0; i < index; ++i) {
|
||||
s += list[i] + " ";
|
||||
}
|
||||
s += "]";
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this list contains no elements.
|
||||
*
|
||||
* @return true if this list contains no elements
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return this.index == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
|
||||
*
|
||||
* @param o - element whose presence in this list is to be tested
|
||||
*
|
||||
* @return true if this list contains the specified element
|
||||
*
|
||||
*/
|
||||
public boolean contains(Object o) {
|
||||
|
||||
if (o == null) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < this.index; i++) {
|
||||
if (o.equals(this.list[i])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing all of the elements in this list in proper sequence (from first to last element). The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array. This method acts as bridge between array-based and collection-based APIs. Returns: an array containing all of the elements in this list in proper sequence
|
||||
*
|
||||
* @return an array containing all of the elements in this list in proper sequence
|
||||
*/
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
Object[] returnArray = new Object[index];
|
||||
for (int i = 0; i < index; ++i) {
|
||||
Object objCopy = list[i];
|
||||
returnArray[i] = objCopy;
|
||||
}
|
||||
return returnArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).
|
||||
*
|
||||
* @param o - element to be removed from this list, if present
|
||||
* @return true if this list contained the specified element
|
||||
*/
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
if (o == null) {
|
||||
return false;
|
||||
}
|
||||
Object[] newArray = new Object[index];
|
||||
int i = 0;
|
||||
for (Object arrayElement : list) {
|
||||
try {
|
||||
if (!arrayElement.equals(o)) {
|
||||
newArray[i] = arrayElement;
|
||||
}
|
||||
if ((index - 1) > i) {
|
||||
++i;
|
||||
}
|
||||
} catch (NullPointerException ex) {
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
--index;
|
||||
list = (E[]) newArray;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all of the elements from this list. The list will be empty after this call returns. Note: Traverse the array and set all of its elements to null. Set its index to zero.
|
||||
*/
|
||||
@Override
|
||||
public void clear() {
|
||||
for (int i = 0; i < list.length; ++i) {
|
||||
list[i] = null;
|
||||
}
|
||||
index = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element at the specified position in this list.
|
||||
*
|
||||
* @param index of the element to return
|
||||
* @return the element at the specified position in this list
|
||||
* @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size())
|
||||
*/
|
||||
@Override
|
||||
public E get(int index) {
|
||||
return list[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the element at the specified position in this list with the specified element.
|
||||
*
|
||||
* @param index - index of the element to replace
|
||||
* @param element - element to be stored at the specified position
|
||||
* @return the element previously at the specified position
|
||||
* @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size())
|
||||
*/
|
||||
@Override
|
||||
public E set(int index, E element) {
|
||||
return list[index] = element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
|
||||
*
|
||||
* @param index - index at which the specified element is to be inserted element
|
||||
* @param - element to be inserted
|
||||
* @throws NullPointerException - if the specified element is null and this collection does not permit null elements
|
||||
* @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size())
|
||||
*/
|
||||
public void add(int index, E element) {
|
||||
if (element == null) {
|
||||
throw new NullPointerException("cant add null");
|
||||
}
|
||||
if (index > this.index || index < 0) {
|
||||
throw new IndexOutOfBoundsException("cant add at this index");
|
||||
}
|
||||
|
||||
// Check if the list needs to be resized
|
||||
if (this.index >= list.length) {
|
||||
doubleSizeOfList();
|
||||
}
|
||||
|
||||
// Shift elements to the right to make space for the new element
|
||||
for (int i = this.index; i > index; i--) {
|
||||
list[i] = list[i - 1];
|
||||
}
|
||||
|
||||
// Insert the new element at the specified index
|
||||
list[index] = element;
|
||||
this.index++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
|
||||
*
|
||||
* @param index - the index of the element to be removed
|
||||
* @return the element that was removed from the list
|
||||
* @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size())
|
||||
*/
|
||||
public E remove(int index) {
|
||||
Object removedObject = new Object();
|
||||
if (index < 0 || index >= this.index) {
|
||||
throw new IndexOutOfBoundsException("Index " + index + " out of bounds");
|
||||
}
|
||||
Object[] newArray = new Object[index];
|
||||
int j = 0;
|
||||
for (int i = 0; i < index; ++i) {
|
||||
try {
|
||||
if (i != index) {
|
||||
newArray[j] = list[i];
|
||||
} else {
|
||||
removedObject = list[i];
|
||||
}
|
||||
if ((index - 1) > j) {
|
||||
++j;
|
||||
}
|
||||
} catch (NullPointerException ex) {
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
--this.index;
|
||||
list = (E[]) newArray;
|
||||
return (E) removedObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. Parameters:
|
||||
*
|
||||
* @param o - element to search for
|
||||
* @return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
|
||||
*/
|
||||
@Override
|
||||
public int indexOf(Object o) {
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
if (list[i].equals(o)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. Parameters:
|
||||
*
|
||||
* @param o - element to search for
|
||||
* @return the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element
|
||||
*/
|
||||
@Override
|
||||
public int lastIndexOf(Object o) {
|
||||
for (int i = list.length - 1; i >= 0; i--) {
|
||||
if (list[i] != null && list[i].equals(o)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations. This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list: list.subList(from, to).clear();
|
||||
*
|
||||
* Similar idioms may be constructed for ArrayList.indexOf(Object) and ArrayList.lastIndexOf(Object), and all of the algorithms in the Collections class can be applied to a subList. The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
|
||||
*
|
||||
* @param fromIndex - low endpoint (inclusive) of the subList
|
||||
* @param toIndex - high endpoint (exclusive) of the subList
|
||||
* @return a view of the specified range within this list
|
||||
* @throws IndexOutOfBoundsException - for an illegal endpoint index value (fromIndex LE 0 || toIndex > size || fromIndex > toIndex)
|
||||
* @throws IllegalArgumentException - if the endpoint indices are out of order (fromIndex > toIndex)
|
||||
*/
|
||||
@Override
|
||||
public List<E> subList(int fromIndex, int toIndex) {
|
||||
if (fromIndex < 0 || toIndex > size() || fromIndex > toIndex) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
List<E> sublist = new ArrayListASDV<>();
|
||||
|
||||
for (int i = fromIndex; i < toIndex; i++) {
|
||||
sublist.add(get(i));
|
||||
}
|
||||
return sublist;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list. If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
|
||||
*
|
||||
* @param a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
|
||||
* @return an array containing the elements of the list
|
||||
* @throws ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
|
||||
* @throws NullPointerException - if the specified array is null
|
||||
*/
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
Class<?> clazz = a.getClass();
|
||||
//>length of a is too small
|
||||
if (a.length < index) // Make a new array of a's runtime type
|
||||
{
|
||||
return (T[]) Arrays.copyOf(this.list,
|
||||
index,
|
||||
a.getClass());
|
||||
}
|
||||
|
||||
//>length of a is good
|
||||
System.arraycopy(this.list, 0, a, 0, index);
|
||||
|
||||
//>length of a is greater than this list set nulls
|
||||
if (a.length > index) {
|
||||
for (int i = index; i < a.length; ++i) {
|
||||
a[i] = null;
|
||||
}
|
||||
}
|
||||
return a;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
Iterator<E> it = new Iterator<E>() {
|
||||
int index = 0;
|
||||
//E[] list = (E[]) new Object[3];
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (index == list.length) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
return list[index++];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
if (index < 1) {
|
||||
Object[] newList = new Object[--index];
|
||||
for (int i = 0; i < index; i++) {
|
||||
newList[i] = list[i];
|
||||
}
|
||||
list = (E[]) newList;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given action for each remaining element until all elements have been processed or the action throws an exception- Actions are performed in the order of iteration, if that order is specified- Exceptions thrown by the action are relayed to the caller.
|
||||
*
|
||||
*
|
||||
* @throws NullPointerException - if the specified action is null
|
||||
*/
|
||||
@Override
|
||||
public void forEachRemaining(Consumer<? super E> action) {
|
||||
if (action == null) {
|
||||
throw new NullPointerException("Action is Null");
|
||||
}
|
||||
|
||||
while (hasNext()) {
|
||||
action.accept(next());
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
return (Iterator<E>) it;
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
/**
|
||||
* Returns a list iterator over the elements in this list (in proper sequence). The returned list iterator is fail-fast.
|
||||
*
|
||||
*
|
||||
* @return a list iterator over the elements in this list (in proper sequence
|
||||
*/
|
||||
@Override
|
||||
public ListIterator<E> listIterator() {
|
||||
return listIterator(0);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<E> listIterator(int index) {
|
||||
|
||||
ListIterator<E> it = new ListIterator<E>() {
|
||||
//E[] list = (E[]) new Object[3];
|
||||
int index;
|
||||
|
||||
/**
|
||||
* Returns true if this list iterator has more elements when traversing the list in the forward direction. (In other words, returns true if ListIterator.next would return an element rather than throwing an exception.)
|
||||
*
|
||||
* @return true if the list iterator has more elements when traversing the list in the forward direction
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return next() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next element in the list and advances the cursor position. This method may be called repeatedly to iterate through the list, or intermixed with calls to ListIterator.previous to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)
|
||||
*
|
||||
* @return the next element in the list
|
||||
* @throws NoSuchElementException - if the iteration has no next element
|
||||
*/
|
||||
@Override
|
||||
public E next() throws NoSuchElementException {
|
||||
if (index > list.length) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
System.out.println("List iterator next() "+ list[index]);
|
||||
return list[index++];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return index > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
return list[--index];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the element that would be returned by a subsequent call to next(). (Returns list size if the list iterator is at the end of the list.)
|
||||
*
|
||||
* @return the index of the element that would be returned by a subsequent call to next, or list size if the list iterator is at the end of the list
|
||||
*/
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return index + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the element that would be returned by a subsequent call to previous(). (Returns -1 if the list iterator is at the beginning of the list.)
|
||||
*
|
||||
* @return the index of the element that would be returned by a subsequent call to previous, or -1 if the list iterator is at the beginning of the list
|
||||
*/
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return index - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given action for each remaining element until all elements have been processed or the action throws an exception- Actions are performed in the order of iteration, if that order is specified- Exceptions thrown by the action are relayed to the caller.
|
||||
*
|
||||
*
|
||||
* @throws NullPointerException - if the specified action is null
|
||||
*/
|
||||
@Override
|
||||
public void forEachRemaining(Consumer<? super E> action) {
|
||||
if (action == null) {
|
||||
throw new NullPointerException("Action is Null");
|
||||
}
|
||||
|
||||
while (hasNext()) {
|
||||
action.accept(next());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(E e) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(E e) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
};
|
||||
|
||||
return it;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Returns true if this collection contains all of the elements in the specified collection.
|
||||
*
|
||||
* Parameters: c - collection to be checked for containment in this collection Returns: true if this collection contains all of the elements in the specified collection Throws: ClassCastException - if the types of one or more elements in the specified collection are incompatible with this collection (optional) NullPointerException - if the specified collection contains one or more null elements and this collection does not permit null elements (optional), or if the specified collection is null.
|
||||
*
|
||||
* @param c - collection to be checked for containment in this collection
|
||||
* @return true if this collection contains all of the elements in the specified collection.
|
||||
* @throws ClassCastException - if the types of one or more elements in the specified collection are incompatible with this collection
|
||||
*/
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
for (Object e : c) {
|
||||
if (!contains(e)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
boolean changed = false;
|
||||
for (E e : c) {
|
||||
if (add(e)) {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends E> c) {
|
||||
boolean changed = false;
|
||||
for (E e : c) {
|
||||
add(index++, e);
|
||||
changed = true;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.
|
||||
*
|
||||
* Parameters: c - collection containing elements to be removed from this collection Returns: true if this collection changed as a result of the call Throws: UnsupportedOperationException - if the removeAll method is not supported by this collection ClassCastException - if the types of one or more elements in this collection are incompatible with the specified collection (optional) NullPointerException - if this collection contains one or more null elements and the specified collection does not support null elements (optional), or if the specified collection is null
|
||||
*
|
||||
* @param c - collection containing elements to be removed from this collection
|
||||
* @return true if this collection changed as a result of the call
|
||||
* @throws ClassCastException - if the types of one or more elements in this collection are incompatible with the specified collection
|
||||
*/
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
Objects.requireNonNull(c);
|
||||
|
||||
List<E> elToKeep = new ArrayList<>(this);
|
||||
elToKeep.removeAll(c);
|
||||
elToKeep.removeIf(Objects::isNull);
|
||||
|
||||
clear();
|
||||
addAll(elToKeep);
|
||||
|
||||
return !elToKeep.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.
|
||||
*
|
||||
*
|
||||
* @param c - collection containing elements to be retained in this collection
|
||||
* @return true if this collection changed as a result of the call
|
||||
* @throws ClassCastException - if the types of one or more elements in this collection are incompatible with the specified collection (optional)
|
||||
*/
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
Objects.requireNonNull(c);
|
||||
|
||||
List<E> elToKeep = new ArrayList<>();
|
||||
for (E element : this) {
|
||||
if (c.contains(element)) {
|
||||
elToKeep.add(element);
|
||||
}
|
||||
}
|
||||
elToKeep.removeIf(Objects::isNull);
|
||||
|
||||
clear();
|
||||
addAll(elToKeep);
|
||||
|
||||
return size() != elToKeep.size();
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
throws ClassNotFoundException, InterruptedException {
|
||||
ArrayListASDV<Integer> aaa = new ArrayListASDV();
|
||||
aaa.add(1);
|
||||
aaa.add(2);
|
||||
aaa.add(3);
|
||||
aaa.add(4);
|
||||
|
||||
ArrayListASDV<Integer> list1 = new ArrayListASDV();
|
||||
ArrayListASDV<String> list2 = new ArrayListASDV(4);
|
||||
ArrayListASDV<A1> list3 = new ArrayListASDV(4);
|
||||
|
||||
System.out.println("------------------------------ ");
|
||||
System.out.println("test add");
|
||||
list1.add(10);
|
||||
list1.add(20);
|
||||
list3.add(new A1(-1));
|
||||
list3.add(new A1(-2));
|
||||
Integer[] b
|
||||
= {
|
||||
2, 3
|
||||
};
|
||||
list1.toArray(b);
|
||||
|
||||
list2.add("a");
|
||||
try {
|
||||
list2.add(null);
|
||||
} catch (NullPointerException e) {
|
||||
System.err.println(e);
|
||||
};
|
||||
|
||||
list2.add("b");
|
||||
list2.add("c");
|
||||
list2.add("d");
|
||||
System.out.println("------------------------------ ");
|
||||
System.out.println("test toString");
|
||||
System.out.println(list1);
|
||||
System.out.println(list2);
|
||||
System.out.println(list3);
|
||||
|
||||
System.out.println("------------------------------ ");
|
||||
System.out.println("test contains");
|
||||
System.out.println("contains E");
|
||||
System.out.println("contains c: " + list2.contains("c"));
|
||||
System.out.println("contains null: " + list2.contains(null));
|
||||
System.out.println("contains k: " + list2.contains('k'));
|
||||
System.out.println(list2);
|
||||
System.out.println("contains A(-1): " + list3.contains(new A1(-1)));
|
||||
System.out.println("contains A(-3): " + list3.contains(new A1(-3)));
|
||||
|
||||
System.out.println("------------------------------ ");
|
||||
System.out.print("test toArray(): ");
|
||||
Object[] ar = list2.toArray();
|
||||
System.out.print("[ ");
|
||||
for (int i = 0; i < ar.length; ++i) {
|
||||
System.out.print(ar[i] + " ");
|
||||
}
|
||||
System.out.println(" ] ");
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test clear()");
|
||||
list2.clear();
|
||||
System.out.println(list2);
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test size");
|
||||
System.out.println(list2.size());
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test add(index, element)");
|
||||
for (char a = 'Z'; a >= 'A'; --a) {
|
||||
System.out.println("array size: " + list2.size());
|
||||
list2.add(0, "" + a);
|
||||
}
|
||||
System.out.println(list2);
|
||||
|
||||
list2.add(26, "z");
|
||||
System.out.println(list2);
|
||||
|
||||
list2.add(list2.size() - 2, "y");
|
||||
System.out.println(list2);
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test remove(index)");
|
||||
Object o = list2.remove(27);
|
||||
System.out.println(o);
|
||||
System.out.println(list2);
|
||||
|
||||
try {
|
||||
list2.remove(30);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test remove(Object)");
|
||||
list2.remove("y");
|
||||
System.out.println(list2);
|
||||
System.out.println(list2.remove("not in there"));
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test set(index, Object)");
|
||||
list2.set(0, "0");
|
||||
list2.set(25, "25");
|
||||
System.out.println(list2);
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test indexOf()");
|
||||
System.out.println(list2.indexOf("0"));
|
||||
System.out.println(list2.indexOf("B"));
|
||||
System.out.println(list2.indexOf("25"));
|
||||
System.out.println(list2.indexOf("Y"));
|
||||
System.out.println(list2.indexOf("not there"));
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test lastIndexOf()");
|
||||
list2.add(10, "0");
|
||||
System.out.println(list2.indexOf("0"));
|
||||
System.out.println(list2.lastIndexOf("0"));
|
||||
System.out.println(list2.indexOf("not there"));
|
||||
System.out.println(list2);
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test sublist(from, to)");
|
||||
List<String> l1 = list2.subList(1, 10);
|
||||
ArrayListASDV<String> l2 = (ArrayListASDV<String>) list2.subList(11, 26);
|
||||
|
||||
System.out.println(l1);
|
||||
System.out.println(l2);
|
||||
List<String> l3 = l2.subList(11, 11);
|
||||
System.out.println(l3);
|
||||
try {
|
||||
l2.subList(12, 11);
|
||||
} catch (Exception e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test toArray()");
|
||||
Object[] ar1 = l2.toArray();
|
||||
for (Object obj : ar1) {
|
||||
System.out.print(obj + " ");
|
||||
}
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test toArray(T[] a) small size a");
|
||||
ArrayListASDV<Integer> listX = new ArrayListASDV();
|
||||
listX.add(10);
|
||||
listX.add(20);
|
||||
Integer[] a1
|
||||
= {
|
||||
1
|
||||
};
|
||||
ar = listX.toArray(ar);
|
||||
for (int i = 0; i < ar.length; ++i) {
|
||||
System.out.println(ar[i]);
|
||||
}
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test toArray(T[] a) Big size a");
|
||||
ArrayListASDV<A1> listA1 = new ArrayListASDV();
|
||||
listA1.add(new A1(100));
|
||||
|
||||
A1[] a11
|
||||
= {
|
||||
new A1(-1), new A1(-2), new A1(3)
|
||||
};
|
||||
listA1.toArray(a11);
|
||||
for (int i = 0; i < a11.length; ++i) {
|
||||
System.out.println(a11[i]);
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test Iterator()");
|
||||
|
||||
System.out.println(list2);
|
||||
|
||||
Iterator<String> it = list2.iterator();
|
||||
|
||||
while (it.hasNext()) {
|
||||
System.out.print(it.next() + " ");
|
||||
}
|
||||
System.out.println("");
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test ListIterator1()");
|
||||
ArrayListASDV<Integer> li3 = new ArrayListASDV();
|
||||
li3.add(10);
|
||||
li3.add(20);
|
||||
li3.add(30);
|
||||
li3.add(40);
|
||||
System.out.println(li3);
|
||||
ListIterator<Integer> li = li3.listIterator(2);
|
||||
while (li.hasNext()) {
|
||||
System.out.print("\tnext index: " + li.nextIndex());
|
||||
System.out.print("\tprevious index: " + li.previousIndex());
|
||||
System.out.print("\t" + li.next());
|
||||
}
|
||||
System.out.println("");
|
||||
while (li.hasPrevious()) {
|
||||
System.out.print("\tnext index: " + li.nextIndex());
|
||||
System.out.print("\tprevious index: " + li.previousIndex());
|
||||
System.out.print("\t" + li.previous());
|
||||
}
|
||||
System.out.println("");
|
||||
System.out.println("next index: " + li.nextIndex());
|
||||
System.out.println("previous index: " + li.previousIndex());
|
||||
|
||||
System.out.println("\n--------------------------removeAll-------------");
|
||||
System.out.println("test forEachRemaining()");
|
||||
System.out.println(li.next());
|
||||
li.forEachRemaining(new Consumer<Integer>() {
|
||||
@Override
|
||||
public void accept(Integer t) {
|
||||
System.out.print(t + 1 + " ");
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test containsAll(Collection)");
|
||||
|
||||
List<Integer> ar33 = Arrays.asList(new Integer[]{
|
||||
10, 20
|
||||
});
|
||||
System.out.println(li3.containsAll(ar33));
|
||||
ar33 = Arrays.asList(new Integer[]{
|
||||
10, -1
|
||||
});
|
||||
System.out.println(li3.containsAll(ar33));
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test removeAll(Collection)");
|
||||
|
||||
li3.add(10);
|
||||
li3.add(11);
|
||||
li3.add(10);
|
||||
System.out.println(li3);
|
||||
ar33 = Arrays.asList(new Integer[]{
|
||||
10
|
||||
});
|
||||
System.out.println(li3.removeAll(ar33));
|
||||
System.out.println(li3);
|
||||
List<Object> oar = Arrays.asList(new Object[]{
|
||||
3.3, 40, "abc"
|
||||
});
|
||||
try {
|
||||
li3.removeAll(oar);
|
||||
} catch (ClassCastException e) {
|
||||
Thread.sleep(100);
|
||||
System.err.println(e);
|
||||
}
|
||||
System.out.println(li3);
|
||||
List<A1> sar = Arrays.asList(new A1[]{
|
||||
new A1(999)
|
||||
});
|
||||
try {
|
||||
li3.removeAll(sar);
|
||||
} catch (ClassCastException e) {
|
||||
Thread.sleep(100);
|
||||
System.err.println(e);
|
||||
}
|
||||
System.out.println(li3);
|
||||
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test retainAll(Collection)");
|
||||
ar33 = Arrays.asList(new Integer[]{
|
||||
30
|
||||
});
|
||||
li3.retainAll(ar33);
|
||||
System.out.println(li3);
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test addAll(Collection)");
|
||||
ar33 = Arrays.asList(new Integer[]{
|
||||
1, 2, 3, 4
|
||||
});
|
||||
li3.addAll(ar33);
|
||||
System.out.println(li3);
|
||||
System.out.println("\n---------------------------------------");
|
||||
System.out.println("test addAll(index, Collection)");
|
||||
ar33 = Arrays.asList(new Integer[]{
|
||||
100, 200, 300
|
||||
});
|
||||
li3.addAll(2, ar33);
|
||||
System.out.println(li3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class A1 implements Consumer<A1> {
|
||||
|
||||
int x;
|
||||
|
||||
public A1(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "A1{" + "x=" + x + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final A1 other = (A1) obj;
|
||||
if (this.x != other.x) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(A1 t) {
|
||||
System.out.println(t.x * t.x);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package edu.slcc.asdv.chloe.mp4_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class MP4_ChloeFontenot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
@@ -1,256 +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.files_chloefontenot;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.HPos;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.ColumnConstraints;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.RowConstraints;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class AddressBookFX extends Application {
|
||||
|
||||
static File addressBook = new File("AddressBookFX.dat");
|
||||
static int addressArrayPointer = 0;
|
||||
static ArrayList<AddressBookEntry> addressArray = new ArrayList<>();
|
||||
static TextField nameTextField = new TextField();
|
||||
static TextField streetTextField = new TextField();
|
||||
static TextField cityTextField = new TextField();
|
||||
static TextField stateTextField = new TextField();
|
||||
static TextField zipTextField = new TextField();
|
||||
static Label addressBookCounter = new Label("Items in address book:");
|
||||
static Label indexLabel = new Label();
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws Exception
|
||||
{
|
||||
getData();
|
||||
BorderPane primaryBorderPane = new BorderPane();
|
||||
GridPane textFieldGridPane = new GridPane();
|
||||
GridPane addressFieldPane = new GridPane();
|
||||
GridPane buttonFieldPane = new GridPane();
|
||||
|
||||
// Text fields / labels
|
||||
textFieldGridPane.add(new Label("Name"), 0, 0);
|
||||
textFieldGridPane.add(new Label("Street"), 0, 1);
|
||||
textFieldGridPane.add(new Label("City"), 0, 2);
|
||||
addressFieldPane.add(new Label("State"), 2, 0);
|
||||
addressFieldPane.add(new Label("ZIP"), 4, 0);
|
||||
textFieldGridPane.add(nameTextField, 1, 0);
|
||||
textFieldGridPane.add(streetTextField, 1, 1);
|
||||
textFieldGridPane.add(addressFieldPane, 1, 2);
|
||||
addressFieldPane.add(cityTextField, 1, 0);
|
||||
addressFieldPane.add(stateTextField, 3, 0);
|
||||
addressFieldPane.add(zipTextField, 5, 0);
|
||||
textFieldGridPane.getColumnConstraints().add(new ColumnConstraints(50));
|
||||
primaryBorderPane.setTop(textFieldGridPane);
|
||||
|
||||
// Buttons
|
||||
Button addButton = new Button("Add");
|
||||
Button firstButton = new Button("First");
|
||||
Button nextButton = new Button("Next");
|
||||
Button previousButton = new Button("Previous");
|
||||
Button lastButton = new Button("Last");
|
||||
Button updateButton = new Button("Update");
|
||||
buttonFieldPane.add(indexLabel, 0, 0);
|
||||
buttonFieldPane.add(addButton, 1, 0);
|
||||
buttonFieldPane.add(firstButton, 2, 0);
|
||||
buttonFieldPane.add(nextButton, 3, 0);
|
||||
buttonFieldPane.add(previousButton, 4, 0);
|
||||
buttonFieldPane.add(lastButton, 5, 0);
|
||||
buttonFieldPane.add(updateButton, 6, 0);
|
||||
buttonFieldPane.add(addressBookCounter, 7, 0);
|
||||
|
||||
primaryBorderPane.setBottom(buttonFieldPane);
|
||||
buttonFieldPane.setAlignment(Pos.CENTER);
|
||||
buttonFieldPane.setHgap(10);
|
||||
|
||||
addButton.setOnAction(e -> {
|
||||
addressArray.add(new AddressBookEntry());
|
||||
updateData();
|
||||
});
|
||||
firstButton.setOnAction(e -> {
|
||||
addressArrayPointer = 0;
|
||||
getEntry(addressArrayPointer);
|
||||
});
|
||||
nextButton.setOnAction(e -> {
|
||||
if (addressArrayPointer >= addressArray.size() - 1) {
|
||||
addressArrayPointer = 0;
|
||||
} else {
|
||||
addressArrayPointer++;
|
||||
}
|
||||
getEntry(addressArrayPointer);
|
||||
});
|
||||
previousButton.setOnAction(e -> {
|
||||
if (addressArrayPointer > 0) {
|
||||
addressArrayPointer--;
|
||||
} else {
|
||||
addressArrayPointer = addressArray.size() - 1;
|
||||
}
|
||||
getEntry(addressArrayPointer);
|
||||
});
|
||||
lastButton.setOnAction(e -> {
|
||||
addressArrayPointer = addressArray.size() - 1;
|
||||
getEntry(addressArrayPointer);
|
||||
});
|
||||
updateButton.setOnAction(e -> {
|
||||
AddressBookEntry entry = addressArray.get(addressArrayPointer);
|
||||
entry.setName(nameTextField.getText());
|
||||
entry.setCity(nameTextField.getText());
|
||||
entry.setStreet(streetTextField.getText());
|
||||
entry.setCity(cityTextField.getText());
|
||||
entry.setState(stateTextField.getText());
|
||||
entry.setZip(zipTextField.getText());
|
||||
updateData();
|
||||
});
|
||||
|
||||
// init fields with data from first entry
|
||||
getEntry(0);
|
||||
|
||||
Scene scene = new Scene(primaryBorderPane);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public static AddressBookEntry getEntry(int index)
|
||||
{
|
||||
AddressBookEntry entry = addressArray.get(index);
|
||||
indexLabel.setText("Current index: " + index);
|
||||
addressBookCounter.setText("Items in address book: " + addressArray.size());
|
||||
nameTextField.setText(entry.getName());
|
||||
streetTextField.setText(entry.getStreet());
|
||||
cityTextField.setText(entry.getCity());
|
||||
stateTextField.setText(entry.getState());
|
||||
zipTextField.setText(entry.getZip());
|
||||
return entry;
|
||||
}
|
||||
|
||||
public static void updateData()
|
||||
{
|
||||
try (ObjectOutputStream fileStream = new ObjectOutputStream(new FileOutputStream(addressBook))) {
|
||||
fileStream.writeObject(addressArray);
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void getData()
|
||||
{
|
||||
if (!addressBook.exists()) {
|
||||
addressArray.add(new AddressBookEntry());
|
||||
updateData();
|
||||
} else {
|
||||
try (ObjectInputStream fileStream = new ObjectInputStream(new FileInputStream(addressBook))) {
|
||||
addressArray = (ArrayList<AddressBookEntry>) fileStream.readObject();
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
launch();
|
||||
}
|
||||
}
|
||||
|
||||
class AddressBookEntry implements Serializable {
|
||||
|
||||
private String name;
|
||||
private String street;
|
||||
private String city;
|
||||
private String state;
|
||||
private String zip;
|
||||
|
||||
public AddressBookEntry()
|
||||
{
|
||||
this.name = "Enter a name here.";
|
||||
this.street = "Enter a street here.";
|
||||
this.city = "Enter a city here.";
|
||||
this.state = "Enter a state here.";
|
||||
this.zip = "Enter a zip here.";
|
||||
}
|
||||
|
||||
public AddressBookEntry(String name, String street, String city, String state, String zip)
|
||||
{
|
||||
this.name = name;
|
||||
this.street = street;
|
||||
this.city = city;
|
||||
this.state = state;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public String getZip()
|
||||
{
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(String zip)
|
||||
{
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public String getState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state)
|
||||
{
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getCity()
|
||||
{
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city)
|
||||
{
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getStreet()
|
||||
{
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street)
|
||||
{
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,130 +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.files_chloefontenot;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Alert.AlertType;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class CombineFilesFX extends Application {
|
||||
|
||||
static ArrayList<File> files = new ArrayList<File>();
|
||||
final FileChooser fileChooser = new FileChooser();
|
||||
|
||||
@Override
|
||||
public void start(final Stage stage) throws Exception {
|
||||
fileChooser.setTitle("Open File to Split...");
|
||||
BorderPane primaryBorderPane = new BorderPane();
|
||||
GridPane textFieldGridPane = new GridPane();
|
||||
|
||||
Label infoLabel = new Label("Point me at the files that were output by SplitFilesFX.java.");
|
||||
Label chooseFile = new Label("Choose files to combine: ");
|
||||
Button openFilePicker = new Button("Choose...");
|
||||
Button run = new Button("Start");
|
||||
textFieldGridPane.add(chooseFile, 0, 0);
|
||||
textFieldGridPane.add(openFilePicker, 1, 0);
|
||||
primaryBorderPane.setAlignment(run, Pos.CENTER);
|
||||
|
||||
primaryBorderPane.setTop(infoLabel);
|
||||
primaryBorderPane.setCenter(textFieldGridPane);
|
||||
primaryBorderPane.setBottom(run);
|
||||
|
||||
openFilePicker.setOnAction(e -> {
|
||||
// When we initially get the files from the file chooser, it returns an unsortable list. Because of this, we need to create a new List with its data.
|
||||
files.addAll(fileChooser.showOpenMultipleDialog(openFilePicker.getScene().getWindow()));
|
||||
// The file picker appears to return files in a random order, so we need to sort them by file name.
|
||||
Collections.sort(files, new Comparator<File>() {
|
||||
public int compare(File o1, File o2) {
|
||||
return extractInt(o1.getName()) - extractInt(o2.getName());
|
||||
}
|
||||
|
||||
String findMatch(String s) {
|
||||
Pattern findNum = Pattern.compile("\\d+$");
|
||||
Matcher match = findNum.matcher(s);
|
||||
while (match.find()) {
|
||||
return match.group();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
int extractInt(String s) {
|
||||
String num = findMatch(s);
|
||||
System.out.println(num);
|
||||
// return 0 if no digits found
|
||||
return num.isEmpty() ? 0 : Integer.parseInt(num);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
run.setOnAction(e -> {
|
||||
if (files == null) {
|
||||
e.consume();
|
||||
}
|
||||
combineFiles(files);
|
||||
});
|
||||
|
||||
Scene scene = new Scene(primaryBorderPane);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public static void combineFiles(List<File> filesToCombine) {
|
||||
String outputPath = filesToCombine.get(0).getParent();
|
||||
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + "reconstructed_" + filesToCombine.get(0).getName().substring(0, (filesToCombine.get(0).getName().length() - 2)))) {
|
||||
System.out.println("Writing to " + outputPath + "/" + "reconstructed_" + filesToCombine.get(0).getName().substring(0, (filesToCombine.get(0).getName().length() - 2)));
|
||||
for (File file : filesToCombine) {
|
||||
try (FileInputStream dataIn = new FileInputStream(file)) {
|
||||
System.out.println("Opening the source file " + file.getName() + "!");
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead = -1;
|
||||
|
||||
while ((bytesRead = dataIn.read(buffer)) != -1) {
|
||||
dataOut.write(buffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
Alert alert = new Alert(AlertType.INFORMATION);
|
||||
alert.setTitle("Success!");
|
||||
alert.setContentText("Successfully combined the files!");
|
||||
|
||||
alert.showAndWait();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch();
|
||||
}
|
||||
}
|
||||
@@ -1,52 +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.files_chloefontenot;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Exercise17_01 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
try (RandomAccessFile fileIO = new RandomAccessFile("Exercise17_01.txt", "rw")) {
|
||||
int random = 0;
|
||||
System.out.println("Writing data to file...");
|
||||
for (int i = 1; i < 100 + 1; ++i) {
|
||||
random = (int) (Math.random() * 9) + 1;
|
||||
System.out.print(random + " ");
|
||||
if (i != 0 && i % 10 == 0) {
|
||||
System.out.println();
|
||||
}
|
||||
fileIO.writeInt(random);
|
||||
}
|
||||
System.out.println("Wrote to the file successfully!");
|
||||
System.out.println("File contents:");
|
||||
fileIO.seek(0);
|
||||
int readIterator = 1;
|
||||
while (true) {
|
||||
try {
|
||||
System.out.print(fileIO.readInt() + " ");
|
||||
if (readIterator != 0 && readIterator % 10 == 0) {
|
||||
System.out.println();
|
||||
}
|
||||
++readIterator;
|
||||
} catch (EOFException e) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,70 +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.files_chloefontenot;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Exercise17_03 {
|
||||
|
||||
static int fileSize = Math.abs((int) (Math.random() * 1024));
|
||||
|
||||
public static void writeData()
|
||||
{
|
||||
try (FileOutputStream fileWrite = new FileOutputStream("Exercise17_03.dat")) {
|
||||
// Write a unspecified number of integers into the file.
|
||||
// Must be positive!
|
||||
int randInt = 0;
|
||||
for (int i = 0; i < fileSize; ++i) {
|
||||
randInt = (int) (Math.random() * 10);
|
||||
fileWrite.write(randInt);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
System.out.println("Wrote data to the file!");
|
||||
}
|
||||
|
||||
public static int[] readData()
|
||||
{
|
||||
int[] fileData = new int[fileSize];
|
||||
try (FileInputStream fileRead = new FileInputStream("Exercise17_03.dat")) {
|
||||
// Read the data back
|
||||
int dataIterator = 0;
|
||||
int dataStream = 0;
|
||||
while (fileRead.available() > 0) {
|
||||
dataStream = fileRead.read();
|
||||
fileData[dataIterator++] = dataStream;
|
||||
System.out.print(dataStream + " ");
|
||||
if ((dataIterator + 1) % 10 == 0) {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
return fileData;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println("Ints to write: " + fileSize);
|
||||
writeData();
|
||||
int[] fileData = readData();
|
||||
// Sum the digits
|
||||
int sum = 0;
|
||||
for (int i: fileData) {
|
||||
sum += i;
|
||||
}
|
||||
System.out.println("\nThe sum of the integers in the file is: " + sum);
|
||||
}
|
||||
}
|
||||
@@ -1,79 +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.files_chloefontenot;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.Serializable;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Exercise17_05 {
|
||||
public static void main(String[] args) {
|
||||
File dataFile = new File("Exercise17_05.dat");
|
||||
if(!dataFile.exists()) {
|
||||
try (ObjectOutputStream fileStream = new ObjectOutputStream(new FileOutputStream(dataFile))) {
|
||||
fileStream.writeObject(new DataContainer());
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
DataContainer data = null;
|
||||
try (ObjectInputStream fileStream = new ObjectInputStream(new FileInputStream(dataFile))) {
|
||||
data = (DataContainer) fileStream.readObject();
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
// Now print out the data!
|
||||
System.out.println("We got the data from the file!");
|
||||
System.out.println(data.toString());
|
||||
}
|
||||
}
|
||||
|
||||
class DataContainer implements Serializable {
|
||||
int[] intArray = {1, 2, 3, 4};
|
||||
Date currentDate = new Date();
|
||||
double doubleMoment = 5.5;
|
||||
|
||||
public int[] getIntArray()
|
||||
{
|
||||
return intArray;
|
||||
}
|
||||
|
||||
public Date getCurrentDate()
|
||||
{
|
||||
return currentDate;
|
||||
}
|
||||
|
||||
public double getDoubleMoment()
|
||||
{
|
||||
return doubleMoment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String intString = "[";
|
||||
for (int i = 0; i < intArray.length - 1; ++i) {
|
||||
intString += intArray[i];
|
||||
if (i == (intArray.length - 2)) {
|
||||
intString += "]";
|
||||
} else {
|
||||
intString += ", ";
|
||||
}
|
||||
}
|
||||
return "DataContainer{" + "intArray=" + intString + ", currentDate=" + currentDate + ", doubleMoment=" + doubleMoment + '}';
|
||||
}
|
||||
}
|
||||
@@ -1,147 +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.files_chloefontenot;
|
||||
|
||||
import static com.chloefontenot.mp5.files_chloefontenot.SplitFilesFX.alert;
|
||||
import static com.chloefontenot.mp5.files_chloefontenot.SplitFilesFX.file;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class HexEditorFX extends Application {
|
||||
|
||||
final FileChooser fileChooser = new FileChooser();
|
||||
static File file = null;
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws Exception {
|
||||
Label label = new Label("Open a file...");
|
||||
Button openFileButton = new Button("Choose...");
|
||||
TextArea editorWindow = new TextArea();
|
||||
Button saveFileButton = new Button("Save");
|
||||
|
||||
editorWindow.setWrapText(true);
|
||||
|
||||
BorderPane bp = new BorderPane();
|
||||
GridPane openFileGridPane = new GridPane();
|
||||
|
||||
openFileGridPane.add(label, 0, 0);
|
||||
openFileGridPane.add(openFileButton, 1, 0);
|
||||
|
||||
bp.setTop(openFileGridPane);
|
||||
bp.setCenter(editorWindow);
|
||||
bp.setBottom(saveFileButton);
|
||||
bp.setAlignment(saveFileButton, Pos.CENTER);
|
||||
|
||||
openFileButton.setOnAction(e -> {
|
||||
file = fileChooser.showOpenDialog(openFileButton.getScene().getWindow());
|
||||
byte[] data = null;
|
||||
System.out.println("Getting data from fileand encoding it as hex...");
|
||||
try (FileInputStream dataIn = new FileInputStream(file)) {
|
||||
data = dataIn.readAllBytes();
|
||||
} catch (FileNotFoundException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
String dataString = encodeHexString(data);
|
||||
editorWindow.setText(dataString);
|
||||
});
|
||||
|
||||
saveFileButton.setOnAction(e -> {
|
||||
if (file == null) {
|
||||
alert.setTitle("No file selected!");
|
||||
alert.setHeaderText("No file selected!");
|
||||
alert.setContentText("You have to open a file first, silly!");
|
||||
alert.showAndWait();
|
||||
}
|
||||
System.out.println("Re-encoding hex back into a byte array...");
|
||||
|
||||
try (FileOutputStream dataOut = new FileOutputStream(file)) {
|
||||
byte[] bytesToSave = decodeHexString(editorWindow.getText());
|
||||
dataOut.write(bytesToSave);
|
||||
} catch (FileNotFoundException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
alert.setTitle("Invalid Hex!");
|
||||
alert.setContentText("Invalid hex entered into the text box, unable to save.");
|
||||
alert.showAndWait();
|
||||
}
|
||||
alert.setTitle("Save successful!");
|
||||
alert.setContentText("File saved successfully.");
|
||||
alert.showAndWait();
|
||||
});
|
||||
|
||||
Scene scene = new Scene(bp);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
|
||||
}
|
||||
|
||||
private int toDigit(char hexChar) {
|
||||
int digit = Character.digit(hexChar, 16);
|
||||
if (digit == -1) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid Hexadecimal Character: " + hexChar);
|
||||
}
|
||||
return digit;
|
||||
}
|
||||
|
||||
public byte hexToByte(String hexString) {
|
||||
int firstDigit = toDigit(hexString.charAt(0));
|
||||
int secondDigit = toDigit(hexString.charAt(1));
|
||||
return (byte) ((firstDigit << 4) + secondDigit);
|
||||
}
|
||||
|
||||
public byte[] decodeHexString(String hexString) {
|
||||
if (hexString.length() % 2 == 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid hexadecimal String supplied.");
|
||||
}
|
||||
byte[] bytes = new byte[hexString.length() / 2];
|
||||
for (int i = 0; i < hexString.length(); i += 2) {
|
||||
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public String byteToHex(byte num) {
|
||||
char[] hexDigits = new char[2];
|
||||
hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
|
||||
hexDigits[1] = Character.forDigit((num & 0xF), 16);
|
||||
return new String(hexDigits);
|
||||
}
|
||||
|
||||
public String encodeHexString(byte[] byteArray) {
|
||||
StringBuffer hexStringBuffer = new StringBuffer();
|
||||
for (int i = 0; i < byteArray.length; i++) {
|
||||
hexStringBuffer.append(byteToHex(byteArray[i]));
|
||||
}
|
||||
return hexStringBuffer.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
@@ -1,122 +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.files_chloefontenot;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ProgressBar;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class SplitFilesFX extends Application {
|
||||
static File file = null;
|
||||
final FileChooser fileChooser = new FileChooser();
|
||||
static ProgressBar pb = new ProgressBar();
|
||||
static Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
||||
@Override
|
||||
public void start(final Stage stage) throws Exception
|
||||
{
|
||||
fileChooser.setTitle("Open File to Split...");
|
||||
BorderPane primaryBorderPane = new BorderPane();
|
||||
BorderPane secondaryBoarderPane = new BorderPane();
|
||||
GridPane textFieldGridPane = new GridPane();
|
||||
|
||||
pb.prefWidthProperty().bind(stage.widthProperty());
|
||||
|
||||
Label infoLabel = new Label("If you split a file named tmp into 3 smaller files,\n the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3. ");
|
||||
Label chooseFile = new Label("Choose a file to split: ");
|
||||
Button openFilePicker = new Button("Choose...");
|
||||
Label splitCountLabel = new Label("Enter the amount of files to split into: ");
|
||||
TextField splitCount = new TextField();
|
||||
Button run = new Button("Start");
|
||||
textFieldGridPane.add(chooseFile, 0, 0);
|
||||
textFieldGridPane.add(openFilePicker, 1, 0);
|
||||
textFieldGridPane.add(splitCountLabel, 0, 1);
|
||||
textFieldGridPane.add(splitCount, 1, 1);
|
||||
secondaryBoarderPane.setAlignment(run, Pos.CENTER);
|
||||
|
||||
primaryBorderPane.setTop(infoLabel);
|
||||
primaryBorderPane.setCenter(textFieldGridPane);
|
||||
|
||||
secondaryBoarderPane.setBottom(run);
|
||||
secondaryBoarderPane.setTop(pb);
|
||||
|
||||
primaryBorderPane.setBottom(secondaryBoarderPane);
|
||||
|
||||
openFilePicker.setOnAction(e -> {
|
||||
file = fileChooser.showOpenDialog(openFilePicker.getScene().getWindow());
|
||||
});
|
||||
|
||||
run.setOnAction(e -> {
|
||||
if (file == null) {
|
||||
e.consume();
|
||||
}
|
||||
splitFile(file, Integer.parseInt(splitCount.getText()));
|
||||
});
|
||||
|
||||
Scene scene = new Scene(primaryBorderPane);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public static void splitFile(File fileToSplit, int splitCount) {
|
||||
int outputFileSize = (int) fileToSplit.length() / splitCount;
|
||||
System.out.println("output file size will be: " + outputFileSize);
|
||||
if (outputFileSize < 0) {
|
||||
alert.setTitle("Output files too large");
|
||||
alert.setHeaderText("Output files too large");
|
||||
alert.setContentText("Please increase the amount of files to split!");
|
||||
|
||||
alert.showAndWait();
|
||||
return;
|
||||
}
|
||||
|
||||
double progress = 0;
|
||||
String outputPath = fileToSplit.getParent();
|
||||
// Open the original file
|
||||
try (FileInputStream dataIn = new FileInputStream(fileToSplit)) {
|
||||
System.out.println("Opening the source file!");
|
||||
for (int i = 0; i < splitCount; ++i) {
|
||||
progress = (i * 100) / splitCount;
|
||||
System.out.println(progress);
|
||||
pb.setProgress(progress);
|
||||
dataIn.mark(outputFileSize * (i + 1));
|
||||
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + fileToSplit.getName() + "." + (i + 1))) {
|
||||
dataOut.write(dataIn.readNBytes(outputFileSize), 0, outputFileSize);
|
||||
System.out.println("Writing to " + outputPath + "/" + fileToSplit.getName() + "." + (i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
launch();
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
||||
*/
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
public class SystemInfo {
|
||||
|
||||
public static String javaVersion() {
|
||||
return System.getProperty("java.version");
|
||||
}
|
||||
|
||||
public static String javafxVersion() {
|
||||
return System.getProperty("javafx.version");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,208 +0,0 @@
|
||||
package edu.slcc.asdv.chloe.mp6_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Markou
|
||||
*/
|
||||
public class CircularList<T extends Comparable<T>> {
|
||||
|
||||
Node<T> head;
|
||||
|
||||
public class Node< T extends Comparable<T>>
|
||||
implements Comparable<Node<T>> {
|
||||
|
||||
private T t;
|
||||
|
||||
public void set(T t)
|
||||
{
|
||||
this.t = t;
|
||||
}
|
||||
|
||||
public T get()
|
||||
{
|
||||
return t;
|
||||
}
|
||||
Node<T> next;
|
||||
|
||||
@Override
|
||||
public int compareTo(Node<T> o)
|
||||
{
|
||||
return this.t.compareTo(o.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param t
|
||||
*/
|
||||
public void add(Object t)
|
||||
{
|
||||
Node<T> temp = new Node();
|
||||
temp.set((T) t);
|
||||
|
||||
if (head == null) {
|
||||
head = temp;
|
||||
head.next = head;
|
||||
} else {
|
||||
Node<T> temp2 = head;
|
||||
do {
|
||||
temp2 = temp2.next;
|
||||
} while (temp2.next != head);
|
||||
temp.next = head;
|
||||
temp2.next = temp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private int getSize()
|
||||
{
|
||||
if (head == null) {
|
||||
return 0;
|
||||
}
|
||||
Node<T> temp = head;
|
||||
int count = 0;
|
||||
do {
|
||||
temp = temp.next;
|
||||
count++;
|
||||
} while (temp != head);
|
||||
|
||||
return count;
|
||||
}
|
||||
////////////////////////////////////////////////////////
|
||||
|
||||
public void addAt(Object t, int pos)
|
||||
{
|
||||
if (pos < 0 || pos > getSize()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Node<T> temp = new Node();
|
||||
temp.set((T) t);
|
||||
|
||||
if (head == null) {
|
||||
add(t);
|
||||
} else if (pos == 0) {
|
||||
int oldSize = getSize();
|
||||
Node<T> currentHead = head;
|
||||
temp.next = head;
|
||||
head = temp;
|
||||
// Set the node at the end of the list to point back to the new head.
|
||||
Node<T> pointer = head;
|
||||
for (int i = 0; i < oldSize; ++i) {
|
||||
pointer = pointer.next;
|
||||
}
|
||||
pointer.next = temp;
|
||||
} else {
|
||||
// Iterate to the desired position
|
||||
Node<T> pointer = head;
|
||||
for (int i = 0; i < pos -1; ++i) {
|
||||
pointer = pointer.next;
|
||||
}
|
||||
temp.next = pointer.next;
|
||||
pointer.next = temp;
|
||||
}
|
||||
}
|
||||
//////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* removes the last node
|
||||
*/
|
||||
public void remove()
|
||||
{
|
||||
Node<T> pointer = head;
|
||||
for (int i = 0; i < this.getSize() - 2; ++i) {
|
||||
pointer = pointer.next;
|
||||
}
|
||||
pointer.next = head;
|
||||
}
|
||||
//////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* removes a specific object from the list
|
||||
*/
|
||||
public boolean remove(Object t)
|
||||
{
|
||||
Node<T> pointer = head;
|
||||
boolean isObjectRemoved = false;
|
||||
do {
|
||||
boolean eval = pointer.next.t.equals(t);
|
||||
if (eval) {
|
||||
isObjectRemoved = true;
|
||||
pointer.next = pointer.next.next;
|
||||
break;
|
||||
} else {
|
||||
pointer = pointer.next;
|
||||
}
|
||||
} while (pointer != head);
|
||||
return isObjectRemoved;
|
||||
}
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
public void removeAt(int pos)
|
||||
{
|
||||
Node<T> pointer = head;
|
||||
for (int i = 0; i < pos; ++i) {
|
||||
boolean eval = i == pos - 1;
|
||||
if (eval) {
|
||||
pointer.next = pointer.next.next;
|
||||
break;
|
||||
} else {
|
||||
pointer = pointer.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void print()
|
||||
{
|
||||
|
||||
if (head == null) {
|
||||
return;
|
||||
}
|
||||
Node<T> temp = head;
|
||||
int elementNum = 0;
|
||||
do {
|
||||
System.out.print(++elementNum + ": ");
|
||||
System.out.println(temp.get().toString());
|
||||
temp = temp.next;
|
||||
} while (temp != head);
|
||||
|
||||
}
|
||||
|
||||
public static void main(String... ar)
|
||||
{
|
||||
CircularList<String> list = new CircularList();
|
||||
list.add("Hello");
|
||||
list.add("World");
|
||||
list.add("one");
|
||||
String removeMe = "two";
|
||||
list.add(removeMe);
|
||||
list.add("three");
|
||||
list.add("four");
|
||||
list.add("five");
|
||||
list.add("six");
|
||||
list.add("seven");
|
||||
list.print();
|
||||
System.out.println("-------------");
|
||||
System.out.println("Remove the last object:");
|
||||
list.remove();
|
||||
list.print();
|
||||
System.out.println("-------------");
|
||||
System.out.println("Remove \"two\"");
|
||||
list.remove(removeMe);
|
||||
list.print();
|
||||
System.out.println("-------------");
|
||||
System.out.println("Remove the 3rd element:");
|
||||
list.removeAt(2);
|
||||
list.print();
|
||||
System.out.println("-------------");
|
||||
System.out.println("Add an element to the beginning");
|
||||
list.addAt("Earth", 0);
|
||||
list.print();
|
||||
System.out.println("-------------");
|
||||
System.out.println("Add an element named \"potato\" in the 4th position:");
|
||||
list.addAt("potato", 3);
|
||||
list.print();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,129 +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 edu.slcc.asdv.chloe.mp6_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class DoubleLinkedList<T> {
|
||||
Node<T> head;
|
||||
Node<T> tail;
|
||||
|
||||
class Node<T> {
|
||||
T t;
|
||||
Node<T> left;
|
||||
Node<T> right;
|
||||
}
|
||||
|
||||
public T removeAt(int pos)
|
||||
{
|
||||
if (pos < 0 || pos > size()-1)
|
||||
{
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
//list is empty
|
||||
if ( size() == 0 )
|
||||
throw new EmptyListException( "The list empty.");
|
||||
//remove at 0
|
||||
if ( pos == 0)
|
||||
{
|
||||
|
||||
if ( size() == 1)
|
||||
head = tail = null;
|
||||
else
|
||||
head = head.right;
|
||||
}
|
||||
else if (pos== size() - 1) { //remove at end
|
||||
tail = tail.left;
|
||||
tail.right = null;
|
||||
}
|
||||
else { //remove in the middle
|
||||
Node<T> p = head;
|
||||
for (int i = 0; i < pos; ++i) {
|
||||
p = p.right;
|
||||
p.left.right = p.right;
|
||||
p.right.left = p.right;
|
||||
p.right.left = p.left;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addAt(T t, int pos) {
|
||||
if (pos < 0 || pos > size()) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
Node<T> newNode = new Node<T>();
|
||||
newNode.t = t;
|
||||
if (head == null) { // list is empty
|
||||
head = tail = newNode;
|
||||
} else if (pos == 0) { // add at the front
|
||||
newNode.right = head;
|
||||
head.left = newNode;
|
||||
head = newNode;
|
||||
} else if (pos == size()) {// add at the end
|
||||
newNode.left = tail;
|
||||
tail.right = newNode;
|
||||
tail = newNode;
|
||||
} else { //We're in the middle
|
||||
Node<T> p = head;
|
||||
for (int i = 0; i < pos - 1; ++i) {
|
||||
p = p.right;
|
||||
}
|
||||
newNode.left = p;
|
||||
newNode.right = p.right;
|
||||
p.right.left = newNode;
|
||||
}
|
||||
}
|
||||
public int size() {
|
||||
Node<T> p = head;
|
||||
int count = 0;
|
||||
while (p != null) {
|
||||
count++;
|
||||
p = p.right;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String outputString = "DoubleLinkedList{" + "head=" + head + ", tail=" + tail +", size=" + this.size() + "}\n Contents= [";
|
||||
Node<T> p = head;
|
||||
int count = 0;
|
||||
int size = size();
|
||||
System.out.println(size);
|
||||
while (p != null) {
|
||||
outputString += p.t.toString();
|
||||
count++;
|
||||
if (count < size) {
|
||||
outputString += ", ";
|
||||
} else {
|
||||
outputString += "]";
|
||||
}
|
||||
p = p.right;
|
||||
}
|
||||
return outputString;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
DoubleLinkedList<Integer> list = new DoubleLinkedList<>();
|
||||
list.addAt(10, 0);
|
||||
list.addAt(20, 0);
|
||||
list.addAt(30, 0);
|
||||
list.addAt(40, 0);
|
||||
list.addAt(40, 3);
|
||||
list.addAt(25, 3);
|
||||
list.removeAt(3);
|
||||
list.removeAt(1);
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +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 edu.slcc.asdv.chloe.mp6_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class EmptyListException extends RuntimeException{
|
||||
public EmptyListException() {}
|
||||
public EmptyListException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package edu.slcc.asdv.chloe.mp6_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class MP6_ChloeFontenot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
@@ -1,912 +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 edu.slcc.asdv.chloe.mp6_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class PriorityQueueASDV<E extends Comparable<E>>
|
||||
implements Queue<E>, Cloneable {
|
||||
|
||||
private Node<E> head;//head
|
||||
private Node<E> tail;//tail
|
||||
|
||||
class Node<E> {
|
||||
|
||||
E e;
|
||||
Node<E> l;
|
||||
Node<E> r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
|
||||
*
|
||||
* Specified by: add in interface Collection<E>
|
||||
* Parameters: e - the element to add Returns: true (as specified by Collection.add(E)) Throws: IllegalStateException - if the element cannot be added at this time due to capacity restrictions ClassCastException - if the class of the specified element prevents it from being added to this queue NullPointerException - if the specified element is null and this queue does not permit null elements IllegalArgumentException - if some property of this element prevents it from being added to this queue
|
||||
*
|
||||
* @param e - the element to add
|
||||
* @return true if this collection changed as a result of the call
|
||||
* @throws IllegalStateException - if the element cannot be added at this time due to capacity restrictions
|
||||
* @throws ClassCastException - if the class of the specified element
|
||||
* @throws NullPointerException - if the specified element is null and this queue does not permit null elements
|
||||
* @throws IllegalArgumentException - if some property of this element prevents it from being added to this queue
|
||||
*/
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
if (e == null) {
|
||||
throw new NullPointerException("NULL elements not allowed!");
|
||||
}
|
||||
|
||||
Node<E> newNode = new Node<E>();
|
||||
newNode.e = e;
|
||||
|
||||
//1. empty queue
|
||||
if (this.head == null && this.tail == null) {
|
||||
this.head = this.tail = newNode;
|
||||
return true;
|
||||
}
|
||||
|
||||
int index = findCorrectPositionToInsertElement(e);
|
||||
//int index = findCorrectPositionToInsertElementHashCode(e);
|
||||
|
||||
//2. we add at size ( last node)
|
||||
if (index == size()) {
|
||||
tail.r = newNode;//1
|
||||
newNode.l = tail;//2
|
||||
tail = newNode;//3
|
||||
} //3. we add at 0 in the front
|
||||
else if (index == 0) {
|
||||
newNode.r = head;
|
||||
this.head.l = newNode;
|
||||
this.head = newNode;
|
||||
if (size() == 1) {
|
||||
tail = head;
|
||||
}
|
||||
} //4. we add in the middle
|
||||
else {
|
||||
Node<E> p = head;
|
||||
|
||||
for (int i = 0; i < index - 1; ++i) {
|
||||
p = p.r;
|
||||
}
|
||||
|
||||
//after for loop p point one position before insertion
|
||||
newNode.l = p;//we connect the left of the new node
|
||||
//to the node that is BEFORE
|
||||
//the node to be inserted
|
||||
|
||||
newNode.r = p.r;//we connect the right of the new node
|
||||
// to the node thta is AFTER
|
||||
//the node to be inserted
|
||||
|
||||
p.r = newNode; //we connect the right the node BEFORE the node
|
||||
//to be inserted to the new node
|
||||
|
||||
p.r.r.l = newNode;//we connect the left of the node AFTER the node
|
||||
//to be iserted to the new node
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
Node<E> p = this.head;
|
||||
int count = 0;
|
||||
while (p != null) {
|
||||
p = p.r;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private int findCorrectPositionToInsertElement(E e) {
|
||||
Node<E> p = this.head;
|
||||
int pos = 0;
|
||||
while (p != null) {
|
||||
if (e.compareTo(p.e) > 0) {
|
||||
p = p.r;
|
||||
++pos;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
private int findCorrectPositionToInsertElementHashCode(E e) {
|
||||
Node<E> p = this.head;
|
||||
int pos = 0;
|
||||
while (p != null) {
|
||||
if (e.hashCode() > p.e.hashCode()) {
|
||||
p = p.r;
|
||||
++pos;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add(E), which can fail to insert an element only by throwing an exception.
|
||||
*
|
||||
* @param e - the element to add
|
||||
* @throws IllegalStateException - if the element cannot be added at this time due to capacity restrictions
|
||||
* @throws ClassCastException - if the class of the specified element
|
||||
* @throws NullPointerException - if the specified element is null and this queue does not permit null elements
|
||||
* @throws IllegalArgumentException - if some property of this element prevents it from being added to this queue
|
||||
* @return true if the element was added
|
||||
*/
|
||||
@Override
|
||||
public boolean offer(E e) {
|
||||
return add(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves and removes the head of this queue. This method differs from {@link #poll poll} only in that it throws an exception if this queue is empty.
|
||||
*
|
||||
* <p>
|
||||
* This implementation returns the result of {@code poll} unless the queue is empty.
|
||||
*
|
||||
* @return the head of this queue
|
||||
* @throws NoSuchElementException if this queue is empty
|
||||
*/
|
||||
@Override
|
||||
public E remove() {
|
||||
Node<E> pointer = head.r;
|
||||
E removedElement = head.e;
|
||||
head = head.r;
|
||||
head.l = null;
|
||||
|
||||
return removedElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves and removes the head of this queue, or returns null if this queue is empty.
|
||||
*
|
||||
* Returns: the head of this queue, or null if this queue is empty
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public E poll() {
|
||||
if (size() == 0) {
|
||||
return null;
|
||||
}
|
||||
if (size() > 1) {
|
||||
head = head.r;
|
||||
|
||||
E e = head.l.e;
|
||||
head.l = null;
|
||||
return e;
|
||||
} else //size 1
|
||||
{
|
||||
E e = head.e;
|
||||
head = tail = null;
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves, but does not remove, the head of this queue. This method differs from {@link #peek peek} only in that it throws an exception if this queue is empty.
|
||||
*
|
||||
* @return the head of this queue
|
||||
* @throws NoSuchElementException if this queue is empty
|
||||
*/
|
||||
@Override
|
||||
public E element() {
|
||||
if (head != null) {
|
||||
return (E) head;
|
||||
} else {
|
||||
throw new NoSuchElementException("Element does not exist.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves, but does not remove, the head of this queue, or returns {@code null} if this queue is empty.
|
||||
*
|
||||
* @return the head of this queue, or {@code null} if this queue is empty
|
||||
*/
|
||||
@Override
|
||||
public E peek() {
|
||||
return (E) head;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return head == null && tail == null ? true : false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if this collection contains the specified element. More formally, returns {@code true} if and only if this collection contains at least one element {@code e} such that {@code Objects.equals(o, e)}.
|
||||
*
|
||||
* @param o element whose presence in this collection is to be tested
|
||||
* @return {@code true} if this collection contains the specified element
|
||||
* @throws ClassCastException if the type of the specified element is incompatible with this collection ({@linkplain Collection##optional-restrictions optional})
|
||||
* @throws NullPointerException if the specified element is null and this collection does not permit null elements ({@linkplain Collection##optional-restrictions optional})
|
||||
*/
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
Node<E> pointer = head;
|
||||
do {
|
||||
if (pointer.equals(o)) {
|
||||
return true;
|
||||
} else {
|
||||
pointer = pointer.r;
|
||||
}
|
||||
} while (pointer != null);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
Iterator<E> it = new Iterator<E>() {
|
||||
Node<E> p = head;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return p == null ? false : true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (hasNext() == false) {
|
||||
throw new NoSuchElementException("the is no next element");
|
||||
}
|
||||
E e = p.e;
|
||||
p = p.r;
|
||||
return e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEachRemaining(Consumer<? super E> action) {
|
||||
while (hasNext()) {
|
||||
action.accept(p.e);
|
||||
p = p.r;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return it;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
Node<E> pointer = head;
|
||||
Object[] returnArray = new Object[this.size()];
|
||||
int i = 0;
|
||||
while (pointer.r != null) {
|
||||
returnArray[i++] = pointer.e;
|
||||
pointer = pointer.r;
|
||||
}
|
||||
returnArray[i++] = pointer.e;
|
||||
return returnArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.
|
||||
*
|
||||
* <p>
|
||||
* If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to {@code null}. (This is useful in determining the length of this collection <i>only</i> if the caller knows that this collection does not contain any {@code null} elements.)
|
||||
*
|
||||
* <p>
|
||||
* If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
|
||||
*
|
||||
* @apiNote This method acts as a bridge between array-based and collection-based APIs. It allows an existing array to be reused under certain circumstances. Use {@link #toArray()} to create an array whose runtime type is {@code Object[]}, or use {@link #toArray(IntFunction)} to control the runtime type of the array.
|
||||
*
|
||||
* <p>
|
||||
* Suppose {@code x} is a collection known to contain only strings. The following code can be used to dump the collection into a previously allocated {@code String} array:
|
||||
*
|
||||
* <pre>
|
||||
* String[] y = new String[SIZE];
|
||||
* ...
|
||||
* y = x.toArray(y);</pre>
|
||||
*
|
||||
* <p>
|
||||
* The return value is reassigned to the variable {@code y}, because a new array will be allocated and returned if the collection {@code x} has too many elements to fit into the existing array {@code y}.
|
||||
*
|
||||
* <p>
|
||||
* Note that {@code toArray(new Object[0])} is identical in function to {@code toArray()}.
|
||||
*
|
||||
* @param <T> the component type of the array to contain the collection
|
||||
* @param a the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
|
||||
* @return an array containing all of the elements in this collection
|
||||
* @throws ArrayStoreException if the runtime type of any element in this collection is not assignable to the {@linkplain Class#getComponentType
|
||||
* runtime component type} of the specified array
|
||||
* @throws NullPointerException if the specified array is null
|
||||
*/
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
a = Arrays.copyOf(a, this.size());
|
||||
Node<E> pointer = head;
|
||||
System.out.println(a.getClass());
|
||||
System.out.println(pointer.getClass());
|
||||
System.out.println(pointer.e.getClass());
|
||||
|
||||
for (int i = 0; i < this.size(); ++i) {
|
||||
a[i] = (T) pointer.e;
|
||||
pointer = pointer.r;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).
|
||||
*
|
||||
* @param o - element to be removed from this collection, if present
|
||||
* @throws ClassCastException - if the type of the specified element is incompatible with this collection
|
||||
* @throws NullPointerException - if the specified element is null and this collection does not permit null elements
|
||||
* @return true if an element was removed as a result of this call
|
||||
*/
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
if (o == null) {
|
||||
throw new NullPointerException("null vales not allowed");
|
||||
}
|
||||
if (size() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Node<E> p = this.head;
|
||||
int pos = 0;
|
||||
while (p != this.tail) {
|
||||
if (p.e.equals(o)) {
|
||||
if (size() == 1) {
|
||||
this.head = this.tail = null;
|
||||
return true;
|
||||
}
|
||||
this.removeAt(pos, (E) o);
|
||||
break;
|
||||
}
|
||||
++pos;
|
||||
p = p.r;
|
||||
}
|
||||
if (p == tail && p.e.equals(o)) {
|
||||
this.removeAt(size() - 1, (E) o);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param pos
|
||||
* @param e
|
||||
* @throws IndexOutOfBoundsException - if pos less 0 OR pos greater-equal size()
|
||||
* @return
|
||||
*/
|
||||
private boolean removeAt(int pos, E e) {
|
||||
if (pos < 0 || pos >= size()) {
|
||||
throw new IndexOutOfBoundsException(pos + " is out of bounds");
|
||||
}
|
||||
//1.list is empty
|
||||
if (isEmpty()) {
|
||||
return false;
|
||||
} //2. one node exists
|
||||
else if (size() == 1) {
|
||||
this.head = this.tail = null;
|
||||
} //3. remove in the front( head)
|
||||
else if (pos == 0) {
|
||||
this.head = this.head.r;
|
||||
head.l = null;
|
||||
} //4. remove in the end ( tail)
|
||||
else if (pos == size() - 1) {
|
||||
this.tail = this.tail.l;
|
||||
this.tail.r = null;
|
||||
} //5. remove in the middle ( at least 3 nodes are in the queue)
|
||||
else {
|
||||
Node<E> p = head;
|
||||
for (int i = 0; i < pos - 1; ++i) {
|
||||
p = p.r;
|
||||
}
|
||||
p.r = p.r.r;
|
||||
p.r.l = p;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if this collection contains all of the elements in the specified collection.
|
||||
*
|
||||
* @param c collection to be checked for containment in this collection
|
||||
* @return {@code true} if this collection contains all of the elements in the specified collection
|
||||
* @throws ClassCastException if the types of one or more elements in the specified collection are incompatible with this collection ({@linkplain Collection##optional-restrictions optional})
|
||||
* @throws NullPointerException if the specified collection contains one or more null elements and this collection does not permit null elements ({@linkplain Collection##optional-restrictions optional}) or if the specified collection is null.
|
||||
* @see #contains(Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
// Java already throws a CastCastException if you give it the wrong type, so we don't have to throw that ourselves
|
||||
if (c.contains(null) || c == null) {
|
||||
throw new NullPointerException("The collection you passed to containsAll() contains a null element. Cannot continue.");
|
||||
}
|
||||
// Unpack the collection so we can compare them
|
||||
Object[] compareArray = c.toArray();
|
||||
Node<E> pointer = null;
|
||||
int matchCount = 0;
|
||||
for (Object compare : compareArray) {
|
||||
pointer = head;
|
||||
for (int i = 0; i < size() - 1; ++i) {
|
||||
if (pointer.e.equals(compare)) {
|
||||
matchCount++;
|
||||
}
|
||||
pointer = pointer.r;
|
||||
}
|
||||
}
|
||||
if (matchCount == compareArray.length - 1) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)
|
||||
*
|
||||
* @param c - collection containing elements to be added to this collection
|
||||
* @throws ClassCastException - if the class of an element of the specified collection prevents it from being added to this collection.
|
||||
* @throws NullPointerException - if the specified collection contains a null element and this collection does not permit null elements, or if the specified collection is null
|
||||
* @throws IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this collection
|
||||
* @throws IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this collection
|
||||
* @return true if this collection changed as a result of the call
|
||||
*/
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
int sizeBefore = size();
|
||||
for (E e : c) {
|
||||
add(e);
|
||||
}
|
||||
int sizeAfter = size();
|
||||
return sizeAfter > sizeBefore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.
|
||||
*
|
||||
* @implSpec This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. If it's so contained, it's removed from this collection with the iterator's {@code remove} method.
|
||||
*
|
||||
* <p>
|
||||
* Note that this implementation will throw an {@code UnsupportedOperationException} if the iterator returned by the {@code iterator} method does not implement the {@code remove} method and this collection contains one or more elements in common with the specified collection.
|
||||
*
|
||||
* @throws UnsupportedOperationException {@inheritDoc}
|
||||
* @throws ClassCastException {@inheritDoc}
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
*
|
||||
* @see #remove(Object)
|
||||
* @see #contains(Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
if (c.contains(null) || c == null) {
|
||||
throw new NullPointerException("The collection you passed to removeAll() contains a null element. Cannot continue.");
|
||||
}
|
||||
// Unpack the collection so we can remove them
|
||||
Object[] compareArray = c.toArray();
|
||||
Node<E> pointer = null;
|
||||
boolean removeSuccessful = false;
|
||||
for (Object compare : compareArray) {
|
||||
pointer = head;
|
||||
for (int i = 0; i < size() - 1; ++i) {
|
||||
if (pointer.e.equals(compare)) {
|
||||
remove(pointer.e);
|
||||
removeSuccessful = true;
|
||||
}
|
||||
pointer = pointer.r;
|
||||
}
|
||||
}
|
||||
return removeSuccessful;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
if (c.contains(null) || c == null) {
|
||||
throw new NullPointerException("The collection you passed to retainAll() contains a null element. Cannot continue.");
|
||||
}
|
||||
Node<E> pointer = null;
|
||||
boolean removeSuccessful = false;
|
||||
for (int j = 0; j < c.size() - 1; ++j) {
|
||||
pointer = head;
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
if (!c.contains(pointer.e)) {
|
||||
remove(pointer.e);
|
||||
removeSuccessful = true;
|
||||
}
|
||||
pointer = pointer.r;
|
||||
}
|
||||
}
|
||||
return removeSuccessful;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
// head = tail = null;
|
||||
|
||||
//extra, no necessary to set the link of every node
|
||||
Node<E> p = head;
|
||||
while (p != tail) {
|
||||
p = p.r;
|
||||
p.l = null;
|
||||
}
|
||||
head = tail = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(Consumer<? super E> action) {
|
||||
//1. use a pointer that points to the head
|
||||
//2. while the pointer has not reached the end of the queue
|
||||
//consume it
|
||||
Node<E> p = head;
|
||||
while (p != null) {
|
||||
action.accept(p.e);
|
||||
p = p.r;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "PriorityQueueASDV {";
|
||||
Node<E> p = head;
|
||||
while (p != null) {
|
||||
s += p.e.toString();
|
||||
if (p != tail) {
|
||||
s += ", ";
|
||||
}
|
||||
p = p.r;
|
||||
}
|
||||
|
||||
s += "}";
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object clone()
|
||||
throws CloneNotSupportedException {
|
||||
PriorityQueueASDV<E> c = (PriorityQueueASDV<E>) super.clone();
|
||||
return c;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("\n--> PriorityQueueASDV testing add");
|
||||
PriorityQueueASDV<String> pq1 = new PriorityQueueASDV();
|
||||
pq1.add("Paris");
|
||||
pq1.add("Athens");
|
||||
pq1.add("London");
|
||||
pq1.add("Lafayette");
|
||||
pq1.add("Berlin");
|
||||
|
||||
System.out.println(pq1);
|
||||
|
||||
System.out.println("\n--> Colllections PriorityQueue testing add");
|
||||
|
||||
PriorityQueue<String> pq2 = new PriorityQueue();
|
||||
pq2.add("Paris");
|
||||
pq2.add("Athens");
|
||||
pq2.add("London");
|
||||
pq2.add("Lafayette");
|
||||
pq2.add("Berlin");
|
||||
|
||||
System.out.println(pq2);
|
||||
|
||||
//TEST IT FULLY HERE. FOR ALL METHODS AND ALL CASES.
|
||||
//Have the Jzva PriorityQueue below
|
||||
System.out.println("\n--> PriorityQueueASDV testing remove(object o)");
|
||||
System.out.println("\n\tremove from front Athens");
|
||||
pq1.remove("Athens");
|
||||
pq2.remove("Athens");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\tremove from end Paris");
|
||||
pq1.remove("Paris");
|
||||
pq2.remove("Paris");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq1);
|
||||
|
||||
System.out.println("\n\tremove from the middle Lafayette");
|
||||
pq1.remove("Lafayette");
|
||||
pq2.remove("Lafayette");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\tadd at the end Stocholm");
|
||||
pq1.add("Stocholm");
|
||||
pq2.add("Stocholm");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\tremove from the middle London");
|
||||
pq1.remove("London");
|
||||
pq2.remove("London");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\tremove from the front Berlin");
|
||||
pq1.remove("Berlin");
|
||||
pq2.remove("Berlin");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\tremove from the front/end Stocholm");
|
||||
pq1.remove("Stocholm");
|
||||
pq2.remove("Stocholm");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\tremove from empty queue");
|
||||
pq1.remove("Stocholm");
|
||||
pq2.remove("Stocholm");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n--> PriorityQueueASDV recreate priority queues from empty");
|
||||
pq1.add("Paris");
|
||||
pq1.add("Athens");
|
||||
pq1.add("London");
|
||||
pq1.add("Lafayette");
|
||||
pq1.add("Berlin");
|
||||
pq1.add("Zurich");
|
||||
|
||||
pq2.add("Paris");
|
||||
pq2.add("Athens");
|
||||
pq2.add("London");
|
||||
pq2.add("Lafayette");
|
||||
pq2.add("Berlin");
|
||||
pq2.add("Zurich");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\n+++HERE YOU TEST ALL YOUR METHODS FULLY, and the methods of Colleciion PriorityQueue");
|
||||
|
||||
System.out.println("\n\t offer New York");
|
||||
pq1.offer("New York");
|
||||
pq2.offer("New York");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\t offer Miami");
|
||||
pq1.offer("Miami");
|
||||
pq2.offer("Miami");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\t offer null");
|
||||
try {
|
||||
pq1.offer(null);
|
||||
} catch (Exception e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
try {
|
||||
pq2.offer(null);
|
||||
} catch (Exception e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\t offer ClassCastException with Object");
|
||||
try {
|
||||
pq1.offer((String) new Object());
|
||||
} catch (Exception e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
try {
|
||||
pq2.offer((String) new Object());
|
||||
} catch (Exception e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\t poll suposed to be Athens");
|
||||
System.out.println(pq1.poll());
|
||||
System.out.println(pq2.poll());
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\t Iterator");
|
||||
Iterator<String> it1 = pq1.iterator();
|
||||
Iterator<String> it2 = pq2.iterator();
|
||||
|
||||
while (it1.hasNext()) {
|
||||
System.out.print(it1.next() + " ");
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
|
||||
while (it2.hasNext()) {
|
||||
System.out.print(it2.next() + " ");
|
||||
}
|
||||
System.out.println("");
|
||||
|
||||
System.out.println("\n\t Iterator NoSuchElementException ");
|
||||
|
||||
try {
|
||||
System.out.println(it1.next());
|
||||
} catch (NoSuchElementException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
try {
|
||||
System.out.println(it2.next());
|
||||
} catch (NoSuchElementException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
System.out.println("\n\t Iterator foreach ");
|
||||
it1 = pq1.iterator();
|
||||
it2 = pq2.iterator();
|
||||
it1.forEachRemaining(new Consumer() {
|
||||
@Override
|
||||
public void accept(Object t) {
|
||||
System.out.print(t + "*** ");
|
||||
}
|
||||
});
|
||||
System.out.println("");
|
||||
it2.forEachRemaining(new Consumer() {
|
||||
@Override
|
||||
public void accept(Object t) {
|
||||
System.out.print(t + "+++ ");
|
||||
}
|
||||
});
|
||||
System.out.println("");
|
||||
|
||||
System.out.println("\n\t addAll Houston Chicago");
|
||||
List<String> ar1 = Arrays.asList("Houston", "Chicago");
|
||||
pq1.addAll(ar1);
|
||||
pq2.addAll(ar1);
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\t clear");
|
||||
pq1.clear();
|
||||
pq2.clear();
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
System.out.println("");
|
||||
|
||||
System.out.println("\n--> PriorityQueueASDV recreate priority queues from empty");
|
||||
pq1.add("Paris");
|
||||
pq1.add("Athens");
|
||||
pq1.add("London");
|
||||
pq1.add("Lafayette");
|
||||
pq1.add("Berlin");
|
||||
pq1.add("Zurich");
|
||||
|
||||
pq2.add("Paris");
|
||||
pq2.add("Athens");
|
||||
pq2.add("London");
|
||||
pq2.add("Lafayette");
|
||||
pq2.add("Berlin");
|
||||
pq2.add("Zurich");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("\n\t forEach");
|
||||
pq1.forEach(new Consumer() {
|
||||
@Override
|
||||
public void accept(Object t) {
|
||||
System.out.print(t + "*** ");
|
||||
}
|
||||
});
|
||||
System.out.println("");
|
||||
pq2.forEach(new Consumer() {
|
||||
@Override
|
||||
public void accept(Object t) {
|
||||
System.out.print(t + "+++ ");
|
||||
}
|
||||
});
|
||||
System.out.println("");
|
||||
|
||||
System.out.println("\n\t clone");
|
||||
try {
|
||||
PriorityQueueASDV<String> pq1Cloned
|
||||
= (PriorityQueueASDV<String>) pq1.clone();
|
||||
System.out.println(pq1Cloned);
|
||||
pq1Cloned.add("Las Vegas");
|
||||
System.out.println(pq1Cloned);
|
||||
System.out.println(pq1);
|
||||
|
||||
} catch (CloneNotSupportedException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
pq1.clear();
|
||||
pq2.clear();
|
||||
pq1.add("Paris");
|
||||
pq1.add("Athens");
|
||||
pq1.add("London");
|
||||
pq1.add("Lafayette");
|
||||
pq1.add("Berlin");
|
||||
pq1.add("Zurich");
|
||||
|
||||
pq2.add("Paris");
|
||||
pq2.add("Athens");
|
||||
pq2.add("London");
|
||||
pq2.add("Lafayette");
|
||||
pq2.add("Berlin");
|
||||
pq2.add("Zurich");
|
||||
System.out.println("----------------");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
|
||||
System.out.println("Attempt to remove an element.");
|
||||
pq1.remove();
|
||||
pq2.remove();
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
System.out.println("Get array of the priority queues.");
|
||||
Object pqArray1[] = pq1.toArray();
|
||||
Object pqArray2[] = pq2.toArray();
|
||||
|
||||
printArrays(pqArray1);
|
||||
printArrays(pqArray2);
|
||||
System.out.println();
|
||||
System.out.println("----------------");
|
||||
System.out.println("Test .toArray(T[])");
|
||||
String[] pqArray3 = pq1.toArray(new String[0]);
|
||||
printArrays(pqArray3);
|
||||
System.out.println("----------------");
|
||||
System.out.println("Test containsAll()");
|
||||
ArrayList<String> testArray = new ArrayList<>();
|
||||
testArray.add("Lafayette");
|
||||
testArray.add("Berlin");
|
||||
testArray.add("Zurich");
|
||||
System.out.println("Does pq1 contain Lafayette, Berlin, and Zurich? " + (pq1.containsAll(testArray) ? "yes" : "no"));
|
||||
System.out.println("Does pq1 contain the contents of pq2? " + (pq1.containsAll(pq2) ? "yes" : "no"));
|
||||
System.out.println("Does pq2 contain the contents of pq1? " + (pq2.containsAll(pq1) ? "yes" : "no"));
|
||||
System.out.println("Adding funkytown to testArray...");
|
||||
testArray.add("Funkytown");
|
||||
System.out.println("Does pq1 contain Lafayette, Berlin, Zurich, and Funkytown? " + (pq1.containsAll(testArray) ? "yes" : "no"));
|
||||
System.out.println("Test if containsAll() correctly throws a NullPointerException...");
|
||||
try {
|
||||
testArray.add(null);
|
||||
System.out.println("Does pq1 contain Lafayette, Berlin, Zurich, Funkytown, and null? " + (pq1.containsAll(testArray) ? "yes" : "no"));
|
||||
} catch (NullPointerException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
testArray.remove(null);
|
||||
System.out.println("That worked! Continuing with tests...");
|
||||
System.out.println("----------------");
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
System.out.println("Testing removeAll(Collection<?>)...");
|
||||
System.out.println("Removing the elements in the test array...");
|
||||
pq1.removeAll(testArray);
|
||||
pq2.removeAll(testArray);
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
System.out.println("----------------");
|
||||
System.out.println("Testing retainAll()...");
|
||||
ArrayList<String> testArray2 = new ArrayList<>();
|
||||
testArray2.add("London");
|
||||
testArray2.add("Paris");
|
||||
pq1.retainAll(testArray2);
|
||||
pq2.retainAll(testArray2);
|
||||
System.out.println(pq1);
|
||||
System.out.println(pq2);
|
||||
}
|
||||
|
||||
static void printArrays(Object[] arr) {
|
||||
for (Object element : arr) {
|
||||
System.out.print(element + ", ");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package edu.slcc.asdv.chloe.lab2_chloefontenot;
|
||||
|
||||
import jakarta.ws.rs.ApplicationPath;
|
||||
import jakarta.ws.rs.core.Application;
|
||||
|
||||
/**
|
||||
* Configures Jakarta RESTful Web Services for the application.
|
||||
* @author Juneau
|
||||
*/
|
||||
@ApplicationPath("resources")
|
||||
public class JakartaRestConfiguration extends Application {
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package edu.slcc.asdv.chloe.lab2_chloefontenot.resources;
|
||||
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author
|
||||
*/
|
||||
@Path("jakartaee10")
|
||||
public class JakartaEE10Resource {
|
||||
|
||||
@GET
|
||||
public Response ping(){
|
||||
return Response
|
||||
.ok("ping Jakarta EE")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package edu.slcc.asdv.chloe.lab5.recursion2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Lab5Recursion2_ChloeFontenot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
@@ -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 edu.slcc.asdv.chloe.lab5.recursion2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class NestedLoopsIndexes {
|
||||
static final int ROWS = 3;
|
||||
static final int COLUMNS = 5;
|
||||
|
||||
public static void nestedLoopsIndexesR(int i, int j) {
|
||||
if (j == COLUMNS) {
|
||||
System.out.println();
|
||||
return;
|
||||
} else if (i == ROWS) {
|
||||
return;
|
||||
}
|
||||
System.out.print(i + ", " + j + " ");
|
||||
nestedLoopsIndexesR(i, ++j);
|
||||
if (i + 1 == j) {
|
||||
nestedLoopsIndexesR(++i, 0);
|
||||
}
|
||||
}
|
||||
public static void main(String[] args)
|
||||
{
|
||||
nestedLoopsIndexesR(0, 0);
|
||||
}
|
||||
}
|
||||
@@ -1,33 +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 edu.slcc.asdv.chloe.lab5.recursion2_chloefontenot;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class OccurancesOfChar {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter a string: ");
|
||||
String s = input.nextLine();
|
||||
System.out.print("Enter a character: ");
|
||||
char ch =input.nextLine().charAt(0);
|
||||
int times = count(s, ch);
|
||||
System.out.println(ch + " appears " + times
|
||||
+ (times > 1 ? " times " : " time ") + "in " + s);
|
||||
}
|
||||
public static int count(String str, char a) {
|
||||
int result = 0;
|
||||
if (str.length() > 0) {
|
||||
result = count(str.substring(1), a) +
|
||||
(( str.charAt(0) == a) ? 1 : 0);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +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 edu.slcc.asdv.chloe.lab5.recursion2_chloefontenot;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class OccurencesOfSpecifiedCharacterInArray {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.print("Enter a string: ");
|
||||
Scanner input = new Scanner(System.in);
|
||||
String s = input.nextLine();
|
||||
char[] items = s.toCharArray();
|
||||
|
||||
System.out.print("Enter a character: ");
|
||||
char ch = input.nextLine().trim().charAt(0);
|
||||
|
||||
System.out.println(ch + " appears " + count(items, ch) + " times.");
|
||||
}
|
||||
public static int count(char[] chars, char ch) {
|
||||
return count(chars, chars.length - 1, ch);
|
||||
}
|
||||
public static int count(char[] chars, int high, char ch) {
|
||||
if (high >= 0) {
|
||||
return count(chars, high - 1, ch) + (ch == chars[high] ? 1 : 0);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +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 edu.slcc.asdv.chloe.lab5.recursion2_chloefontenot;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class ReverseInt {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter an integer: ");
|
||||
int i = input.nextInt();
|
||||
System.out.print("The reversal of " + i + " is ");
|
||||
reverseDisplay(i);
|
||||
System.out.println();
|
||||
}
|
||||
public static void reverseDisplay(int value) {
|
||||
if (value != 0) {
|
||||
System.out.print(value % 10);
|
||||
value = value / 10;
|
||||
reverseDisplay(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +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 edu.slcc.asdv.chloe.lab5.recursion2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class SelectionSortR {
|
||||
|
||||
public static void selectionSortR(int[] arr)
|
||||
{
|
||||
selectionSortR(arr, 0, arr.length);
|
||||
}
|
||||
|
||||
public static void swap(int[] arr, int i, int j)
|
||||
{
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
|
||||
public static void selectionSortR(int[] arr, int i, int n)
|
||||
{
|
||||
// Selection sort
|
||||
int min = i;
|
||||
for (int j = i + 1; j < n; ++j) {
|
||||
if (arr[j] < arr[min]) {
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
swap(arr, min, i);
|
||||
if ( i + 1 < n) {
|
||||
selectionSortR(arr, i +1, n);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int[] arr = {
|
||||
8, 2, 1, 1, 7, 4, -1, 50, 49
|
||||
};
|
||||
selectionSortR(arr);
|
||||
|
||||
for (int i = 0; i < arr.length; ++i) {
|
||||
System.out.print(arr[i] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
@@ -1,37 +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 edu.slcc.asdv.chloe.lab5.recursion2_chloefontenot;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class SumOfDigits {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter an integer: ");
|
||||
int i = input.nextInt();
|
||||
System.out.print("The sum of digits in " + i
|
||||
+ " is " + sumDigits(i));
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static long sumDigits(long n)
|
||||
{
|
||||
// Base case: if the number is a single digit, return it
|
||||
if (n < 10) {
|
||||
return n;
|
||||
} else {
|
||||
// Recursive case: sum the last digit and call the function on the remaining digits
|
||||
long lastDigit = n % 10;
|
||||
long remainingDigits = n / 10;
|
||||
return lastDigit + sumDigits(remainingDigits);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +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 edu.slcc.asdv.chloe.lab5.recursion2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class SumSeries1 {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.printf("%-10s%15s\n", "1", "m(i)");
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
System.out.printf("%-10d%-15.6f\n",i, m(i));
|
||||
}
|
||||
}
|
||||
public static double m(int i) {
|
||||
if (i == 1) {
|
||||
return 1;
|
||||
} else {
|
||||
return m(i - 1) + 1.0 / i;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +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 edu.slcc.asdv.chloe.lab5.recursion2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class SumSeries2 {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.printf("%-10s%15s\n", "i", "m(i)");
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
System.out.printf("%-10d%-15.6f\n",i, m(i));
|
||||
}
|
||||
}
|
||||
public static double m(int i) {
|
||||
if (i == 1) {
|
||||
return 1.0 / 3;
|
||||
} else {
|
||||
return m(i - 1) + (double) i / (2 * i + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,34 +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 edu.slcc.asdv.chloe.lab5.recursion2_chloefontenot;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class UpperCaseInArray {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.print("Enter a string: ");
|
||||
Scanner input = new Scanner(System.in);
|
||||
String s = input.nextLine();
|
||||
char[] items = s.toCharArray();
|
||||
System.out.println("The number of uppercase letters is "
|
||||
+ count(items));
|
||||
}
|
||||
|
||||
public static int count(char[] chars) {
|
||||
return count(chars, chars.length - 1);
|
||||
}
|
||||
public static int count(char[] chars, int high) {
|
||||
if (high >= 0) {
|
||||
return count(chars, high - 1) + (Character.isUpperCase(chars[high]) ? 1 : 0);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
package edu.slcc.asdv.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EmptyStackException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class GenericStack<T> {
|
||||
|
||||
private T[] elements;
|
||||
ArrayList<T> elementsList = new ArrayList<T>();
|
||||
private int top;
|
||||
static int size = 4;
|
||||
|
||||
public GenericStack(int size) {
|
||||
elements = (T[]) new Object[size];
|
||||
}
|
||||
public GenericStack()
|
||||
{
|
||||
elements = (T[]) new Object[size];
|
||||
}
|
||||
public boolean push (T element) {
|
||||
if (top == size) {
|
||||
throw new StackOverflowError();
|
||||
}
|
||||
elements[top++] = element;
|
||||
elementsList.add(element);
|
||||
return true;
|
||||
}
|
||||
|
||||
public T pop() {
|
||||
if (top == 0) {
|
||||
throw new EmptyStackException();
|
||||
}
|
||||
--top;
|
||||
elementsList.remove(top);
|
||||
return elements[top];
|
||||
}
|
||||
|
||||
public T peek() {
|
||||
if (top == 0) {
|
||||
throw new EmptyStackException();
|
||||
}
|
||||
return elements[top - 1];
|
||||
}
|
||||
public boolean isEmpty() {
|
||||
boolean returnBoolean = false;
|
||||
if (top == 0) {
|
||||
return true;
|
||||
}
|
||||
return returnBoolean;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String returnString = "";
|
||||
for (int i = top -1; i >= 0; --i) {
|
||||
returnString += elements[i].toString() + ", ";
|
||||
}
|
||||
return returnString;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package edu.slcc.asdv.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Lab6_generics_ChloeFontenot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
@@ -1,48 +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 edu.slcc.asdv.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Max {
|
||||
|
||||
public static Comparable max(Comparable o1, Comparable o2)
|
||||
{
|
||||
if (o1.compareTo(o2) > 0) {
|
||||
return o1;
|
||||
} else {
|
||||
return o2;
|
||||
}
|
||||
}
|
||||
public static <E extends Comparable<E>> E maxSafe(E e1, E e2) {
|
||||
if(e1.compareTo(e2) > 0) {
|
||||
return e1;
|
||||
} else {
|
||||
return e2;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println(max(1, 2));
|
||||
try {
|
||||
System.out.println(maxSafe(1, 2));
|
||||
System.out.println(maxSafe("abc", "ABC"));
|
||||
System.out.println();
|
||||
//System.out.println(maxSafe(1, "two"));
|
||||
|
||||
GenericStack stackUnsafe = new GenericStack();
|
||||
GenericStack<Integer> stackSafe = new GenericStack();
|
||||
stackSafe.push(1); stackSafe.push(2);
|
||||
System.out.println(stackSafe);
|
||||
stackUnsafe.push(1); stackUnsafe.push("two");
|
||||
System.out.println("This line compiles but crashes the program " + max(1, "two"));
|
||||
} catch (ClassCastException e) {
|
||||
System.err.println("RAW TYPES ARE UNSAFE " + e.getMessage()) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 edu.slcc.asdv.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class NoWildCard {
|
||||
|
||||
public static double max(GenericStack<Integer> stack) {
|
||||
double max = Double.MIN_VALUE;
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
double value = stack.pop().doubleValue();
|
||||
if (value > max) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GenericStack<Integer> intStack = new GenericStack<>();
|
||||
intStack.push(1);
|
||||
intStack.push(2);
|
||||
intStack.push(-2);
|
||||
System.out.print("The max number is " + max(intStack));
|
||||
}
|
||||
}
|
||||
@@ -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 edu.slcc.asdv.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class WildCard {
|
||||
|
||||
public static double max(GenericStack<? extends Number> stack) {
|
||||
double max = Double.MIN_VALUE;
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
double value = stack.pop().doubleValue();
|
||||
if (value > max) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GenericStack<Integer> intStack = new GenericStack<>();
|
||||
intStack.push(1);
|
||||
intStack.push(2);
|
||||
intStack.push(-2);
|
||||
System.out.print("The max number is " + max(intStack));
|
||||
}
|
||||
}
|
||||
@@ -1,25 +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 edu.slcc.asdv.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class WildCard2 {
|
||||
public static void print(GenericStack<?> stack) {
|
||||
while (!stack.isEmpty()) {
|
||||
System.out.print(stack.pop() + " ");
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
GenericStack<Integer> intStack = new GenericStack<>();
|
||||
intStack.push(1);
|
||||
intStack.push(2);
|
||||
intStack.push(-2);
|
||||
|
||||
print(intStack);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +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 edu.slcc.asdv.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class WildCardWithSuper {
|
||||
public static <T> void add(GenericStack<T> stack1, GenericStack<? super T> stack2) {
|
||||
while(!stack1.isEmpty()) {
|
||||
stack2.push(stack1.pop());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GenericStack<String> stack1 = new GenericStack<>();
|
||||
GenericStack<Object> stack2 = new GenericStack<>();
|
||||
stack2.push("one");
|
||||
stack2.push(2);
|
||||
stack1.push("one");
|
||||
|
||||
add(stack1, stack2);
|
||||
WildCard2.print(stack2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user