Java Language
Add a recursive method to the program shown in the previous section that states how many nodes does the stack have.
Code:
class Stack {
protected Node top;
Stack() {
top = null; }
boolean isEmpty() {
return( top == null); }
void push(int v) {
Node tempPointer;
tempPointer = new Node(v);
tempPointer.nextNode = top;
top = tempPointer; }
int pop() {
int tempValue;
tempValue = top.value;
top = top.nextNode;
return tempValue; }
void printStack() {
Node aPointer = top;
String tempString = "";
while (aPointer != null) {
tempString = tempString + aPointer.value + "\n";
aPointer = aPointer.nextNode; }
System.out.println(tempString); }
boolean hasValue(int v) {
if (top.value == v) {
return true; }
else {
return hasValueSubList(top,v);
}
}
boolean hasValueSubList(Node ptr, int v) {
if (ptr.nextNode == null) {
return false; }
else if (ptr.nextNode.value == v) {
return true; }
else {
return hasValueSubList(ptr.nextNode,v);
}
}
}
class Node {
int value;
Node nextNode;
Node(int v, Node n) {
value = v;
nextNode = n;
}
Node (int v) {
this(v,null);
}
}
public class StackWithLinkedList2{
public static void main(String[] args){
int popValue;
Stack myStack = new Stack();
myStack.push(5);
myStack.push(7);
myStack.push(9);
System.out.println(myStack.hasValue(11));
}
}
System.out.println(myStack.hasValue(11));
}
}
In: Computer Science
Java Language
Add a recursive method to the program shown in the previous section that allows remove the last node from the stack.
Code:
class Stack {
protected Node top;
Stack() {
top = null; }
boolean isEmpty() {
return( top == null); }
void push(int v) {
Node tempPointer;
tempPointer = new Node(v);
tempPointer.nextNode = top;
top = tempPointer; }
int pop() {
int tempValue;
tempValue = top.value;
top = top.nextNode;
return tempValue; }
void printStack() {
Node aPointer = top;
String tempString = "";
while (aPointer != null) {
tempString = tempString + aPointer.value + "\n";
aPointer = aPointer.nextNode; }
System.out.println(tempString); }
boolean hasValue(int v) {
if (top.value == v) {
return true; }
else {
return hasValueSubList(top,v);
}
}
boolean hasValueSubList(Node ptr, int v) {
if (ptr.nextNode == null) {
return false; }
else if (ptr.nextNode.value == v) {
return true; }
else {
return hasValueSubList(ptr.nextNode,v);
}
}
}
class Node {
int value;
Node nextNode;
Node(int v, Node n) {
value = v;
nextNode = n;
}
Node (int v) {
this(v,null);
}
}
public class StackWithLinkedList2{
public static void main(String[] args){
int popValue;
Stack myStack = new Stack();
myStack.push(5);
myStack.push(7);
myStack.push(9);
System.out.println(myStack.hasValue(11));
}
}
System.out.println(myStack.hasValue(11)); } }
System.out.println(myStack.hasValue(11)); } } System.out.println(myStack.hasValue(11)); } }
In: Computer Science
In: Biology
Java: Determining whether a tree exists in a directed graph
I'm trying to figure out how to determine if a tree exists in a directed graph, I need help with the isTree() function. the code for the Bag class used is below the main code if it's needed.
import algs13.Bag;
import java.util.HashSet;
// See instructions below
public class MyDigraph {
static class Node {
private String key;
private Bag<Node> adj;
public Node (String key) {
this.key = key;
this.adj = new Bag<> ();
}
public String toString () { return key; }
public void addEdgeTo (Node n) { adj.add (n); }
public Bag<Node> adj () { return adj; }
}
Node[] node;
int V;
int E;
public static boolean DEBUG = false;
public MyDigraph (int V) {
if (V < 0) throw new IllegalArgumentException("Number of vertices in a Digraph must be nonnegative");
this.V = V;
this.E = 0;
this.node = new Node[V];
for (int i=0; i<V; i++) {
node[i] = new Node ("n" + (DEBUG ? i : StdRandom.uniform (100)));
}
}
public MyDigraph(Digraph G) {
this (G.V ()); // run the first constructor
for (int v=0; v<V; v++) {
for (int w : G.adj (v))
addEdge(v, w);
}
}
public MyDigraph(In in) {
this (in.readInt()); // run the first constructor
int E = in.readInt();
if (E < 0) throw new IllegalArgumentException("Number of edges in a Digraph must be nonnegative");
for (int i = 0; i < E; i++) {
int v = in.readInt();
int w = in.readInt();
addEdge(v, w);
}
}
public void addEdge(int v, int w) {
if (v < 0 || v >= V) throw new IndexOutOfBoundsException("vertex " + v + " is not between 0 and " + (V-1));
if (w < 0 || w >= V) throw new IndexOutOfBoundsException("vertex " + w + " is not between 0 and " + (V-1));
node[v].addEdgeTo (node[w]);
E++;
}
public String toString() {
StringBuilder s = new StringBuilder();
String NEWLINE = System.getProperty("line.separator");
s.append(V + " vertices, " + E + " edges " + NEWLINE);
for (int v = 0; v < V; v++) {
s.append(String.format("%s: ", node[v]));
for (Node w : node[v].adj ()) {
s.append(String.format("%s ", w));
}
s.append(NEWLINE);
}
return s.toString();
}
public void toGraphviz(String filename) {
GraphvizBuilder gb = new GraphvizBuilder ();
for (int v = 0; v < V; v++) {
gb.addNode (node[v]);
for (Node n : node[v].adj ())
gb.addEdge (node[v], n);
}
gb.toFile (filename);
}
// isTree returns true if the object graph rooted at node[s] is a (rooted out) tree
// (e.g. No duplicate paths or cycles)
public boolean isTree (int s) {
// TODO
return false;
}
====bag class code====
package algs13;
import stdlib.*;
public class Bag<T> implements Iterable<T> {
private int N; // number of elements in bag
private Node<T> first; // beginning of bag
// helper linked list class
private static class Node<T> {
public Node() { }
public T item;
public Node<T> next;
}
/**
* Create an empty stack.
*/
public Bag() {
first = null;
N = 0;
}
/**
* Is the BAG empty?
*/
public boolean isEmpty() {
return first == null;
}
/**
* Return the number of items in the bag.
*/
public int size() {
return N;
}
/**
* Add the item to the bag.
*/
public void add(T item) {
Node<T> oldfirst =
first;
first = new Node<>();
first.item = item;
first.next = oldfirst;
N++;
}
// check internal invariants
protected static <T> boolean check(Bag<T>
that) {
int N = that.N;
Bag.Node<T> first =
that.first;
if (N == 0) {
if (first !=
null) return false;
}
else if (N == 1) {
if (first ==
null) return false;
if (first.next
!= null) return false;
}
else {
if (first.next
== null) return false;
}
// check internal consistency of
instance variable N
int numberOfNodes = 0;
for (Bag.Node<T> x = first; x
!= null; x = x.next) {
numberOfNodes++;
}
if (numberOfNodes != N) return
false;
return true;
}
/**
* Return an iterator that iterates over the items in
the bag.
*/
public Iterator<T> iterator() {
return new ListIterator();
}
// an iterator, doesn't implement remove() since
it's optional
private class ListIterator implements
Iterator<T> {
private Node<T> current =
first;
public boolean hasNext() {
return current != null; }
public void remove() { throw new
UnsupportedOperationException(); }
public T next() {
if (!hasNext())
throw new NoSuchElementException();
T item =
current.item;
current =
current.next;
return
item;
}
}
/**
* A test client.
*/
public static void main(String[] args) {
StdIn.fromString ("to be or not to
- be - - that - - - is");
Bag<String> bag = new
Bag<>();
while (!StdIn.isEmpty()) {
String item =
StdIn.readString();
bag.add(item);
}
StdOut.println("size of bag = "
+ bag.size());
for (String s : bag) {
StdOut.println(s);
}
}
}
In: Computer Science
read the following descriptions of hypothetical studies. Explain whether each is an experiment, non-experiment, or quasi-experiment and state why for identifying, and for explanation). 5. To test whether phone use decreases academic success, a professor compares the grades of students who have used their phones in class to those of students who have never used their phones in class. 6.Belk Gym gives out surveys to students who use the facilities during the month of January to find out how satisfied they feel with the gym and the work out classes offered.
In: Psychology
| Rx: | |
| Castor oil | 10% (v/v) |
| Tween 80 and Span 20 | 2% (w/v) |
| Simple syrup | qs 50 mL |
How many gram(s) of Span 20 are needed to prepare for the prescription, given that the required HLB for the emulsion is 10, Span 20 has an HLB of 8.6, and Tween 80 has an HLB of 15?
In: Nursing
In java.
Write a method static <K, V> void addToMultiMap(Map<K, Set<V>> map, K key, V value). addToMultiMap must add the value, if present, to the set associated with the given key, creating the set if necessary. You may assume all keys already in the map are associated with non-null values.
For full credit, your method must include generic types, and must not contain unnecessary method calls or loops, even if they do not otherwise impact correctness. You may assume Map, Set, HashMap, and HashSet are correctly imported from java.util. You may not import other classes.
In: Computer Science
Prove the following formulas, where u, v, z are complex numbers and z = x +iy.
a. sin(u+v) = sin u cos v + cos u sin v.
b. cos(u+v) + cos u cos v - sin u sin v.
c. sin^2 z + cos^2 z = 1.
d. cos(iy) = cosh y, sin (iy) = i sinh y.
e. cos z = cos x cosh y - i sin x sinh y.
f. sin z = sin x cosh y + i cos x sinh y.
In: Advanced Math
Solve the following dissolution exercises:
A. 3 aqueous solutions are mixed: 50 mL of 10% w/v HCl, 35 mL of
3.2% w/v NaCl and 65 mL of 13% w/v KCl, which is the final
concentration of the solution in %p/v with respect to H +, Na +, K
+, Cl-
B. What volumes of 8M, 5M and 3M HCl should be mixed to prepare 1 L of 6M HCl
C.
It is necessary to prepare 35 mL of 15% w/v H2O2 solution from a 5% w/v H2O2 solution and another 10% w/v solution. What volume should be used for each solution?
In: Chemistry
If V (dimension k-1) is a subspace of W (dimension K), and V has an orthonormal basis {v1,v2.....vk-1}. Work out a orthonormal basis of W in terms of that of V and the orthogonal complement of V in W.
Provide detailed reasoning.
In: Advanced Math