Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TERM PROJECT PART 1 AND 2 #19

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
302 changes: 302 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions SDEV333-Term-Project.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
10 changes: 0 additions & 10 deletions src/Main.java

This file was deleted.

9 changes: 9 additions & 0 deletions src/bag/Bag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bag;

public interface Bag <E> extends Iterable<E>
{
void add(E item );
boolean isEmpty();
int size();

}
138 changes: 138 additions & 0 deletions src/bag/LinkedBag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package bag;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedBag <E> implements Bag<E> {
// Create Private Class Node
private class Node
{
E data;
Node next;
}
// fields
private Node head;
private int size;
// constructor
public LinkedBag() {
head = null;
size = 0;
}

/**
* Add an item to the stack.
*
* Runtime: o(n)
* This method will add the node to the back of the list.
* Since this is a singly linkedlist , it will going through all of the entire nodes until it reaches the back nodes.
*
* @param item the item to be added
*/
@Override
public void add(E item) {
// create new node
Node newItem = new Node();
// make the node data equals to item
newItem.data = item;
// if the first item is the head, which mean the list is empty
if (head == null){
head = new Node();
head.data = item;
size++;
}else { // if the list is not empty
Node current = head;
while(current.next != null )
{
current = current.next;
}
current.next = newItem;
size++;
}
}
/**
* Checks to see if the stack is empty.
*
* Runtime: o(1)
* This method will only check the size field to see if the size is either zero or not zero .
*
* @return true if the stack is empty, false otherwise
*/

@Override
public boolean isEmpty() {
if(size == 0 )
{
return true;
}
else{
return false;
}
}

/**
* Returns a count of the number of items in the stack.
*
* Runtime: o(1)
* * This method will only check the size field to see what it is stored.
*
* @return the number of items in the stack
*/
@Override
public int size() {
return size;
}

/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
@Override
public Iterator<E> iterator() {
LinkedIterator theIterator = new LinkedIterator();
return theIterator;
}
private class LinkedIterator implements Iterator<E> {
private Node current;

public LinkedIterator( ) {
current = head;
}


/**
* Returns {@code true} if the iteration has more elements.
* (In other words, returns {@code true} if {@link #next} would
* return an element rather than throwing an exception.)
*
* @return {@code true} if the iteration has more elements
*/
@Override
public boolean hasNext() {
if(current == null)
{
return false;
}
else{
return true;
}
}

/**
* Returns the next element in the iteration.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iteration has no more elements
*/
@Override
public E next() {
if(current == null)
{
throw new NoSuchElementException("There is no next one to go to! ");
}
E item = current.data;
current = current.next;
return item;
}
}
}
26 changes: 26 additions & 0 deletions src/bag/Stats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package bag;

import java.util.Scanner;

public class Stats {
public static void main(String[] args) {
Bag<Double> numbers = new LinkedBag<>();
Scanner scan = new Scanner("100 99 101 120 98 107 109 81 101 90");
while(scan.hasNext())
{
Double number = Double.parseDouble(scan.next());
numbers.add(number);
}
int size = numbers.size();
Double sum = 0.0;
for (Double num : numbers) {
sum += num;
}
Double mean = sum / size;

for (Double number : numbers ) {
System.out.println(number);
}
System.out.println("mean = " + mean);
}
}
Loading