My remove(item E) method is working fine if I remove an item that is in the list. However, it has an error when I try to remove an item which is not on the list!
Linked List Class
import java.util.NoSuchElementException;
public class LinkedList<E> { private Node<E> head; // head node of the list private int size = 0; // number of elements that have been added to the list // Returns the element at a specific list index. // Big-O: O(n) (due to the nodeAt call, which must traverse the list) public E get(int index)
I'm writing the remove method, where you insert the key and it searches the key, if the key is found you remove the value inside the table and return that value, if they key is not found you return null. I did this method but is not returning anything in the main so I try to print inside an if to see if it was entering the condition and it appears to be looping, I'm using arrays because its an assignment
public V remove(K k) { int key = funcionHash(k); V key2 = (V) tabla[key].value; int intento=1; if(this.estatus[key]==1){ while(intento<this.tabla.length/2){ if(this.tabla[key].key.equals(k)){
So i have used the "remove()" method in conjunction with an iterator to remove an object from a HashSet in my program, and part of my exercise requires me to return said object. What I can do to return an object that has been removed using "remove()" method?
I've been trying to learn more about Big O Notation and I've gotten stuck on a few pieces of code. What is the computational complexity for the following pieces of code?
1:
for(int i = n; i > 0; i /= 2) { for(int j = 1; j < n; j *= 2) { for(int k = 0; k < n; k += 2) { // constant number of operations
[Code] .....
5 : Determine the average processing time of the recursive algorithm. (int n) spends one time unit to return a random integer value uniformly distributed in the range [0,n] whereas all other instructions spend a negligibly small time(e.g., T(0) = 0)
int myTest(int n) { if(n <= 0) return 0; else { int i = random(n - 1); return myTest(i) + myTest(n - 1 - i);
6 : Assume array a contains n values, the method randomValue takes a constant number c of computational steps to produce each output value, and that the method goodSort takes n log n computational steps to sort the array.
So recently I began Data Structures as core subject and the tutorials in this forum are great.
Right now, I seem to have trouble with Big Oh Notation algorithm and what is the mathematical side to it. "f(n) <= c.g(n), for n>=0.
The question I am working on is: Suppose we are maintaining a collection C of elements such that, each time we add a new element to the collection, we copy the contents of C into a new array list of just the right size. What is the running time of adding n elements to an initially empty collection C in this case?
I am trying to calculate a fine in a PoliceOfficer object with method calls to a ParkedCar and ParkingMeter object. The word problem is:
The fine is $25 for the first hour or part of it and $20 for every additional hour of part of it.
My code is:
public class PoliceOfficer public static final int PARKING_FINE1 = 25; public static final int PARKING_FINE2 = 20; public static final int NUMBER_OF_MINUTES_OVER_PARKED = 60; public double calculateFine(){ double calculateFine = 0;
[Code] ....
obviously the fine is not calculated correctly but I'm not sure how to proceed from here...
I've been looking at the calculating PI using Monte Carlos method. All of the codes request an input from the user to be able to determine PI. However, I want the program to print out only 10, 100, 1000, 10000, 100,000 and 1,000,000,000 for PI.
import java.awt.*; import java.applet.*; import java.awt.event.*; public class Assignment extends Applet implements ActionListener { TextArea textInput = new TextArea(); // user input Button analyzebutton = new Button("Analyze"); Button resetbutton = new Button("Reset"); Label lbloutput = new Label ("Please enter text into the textbox!");
Write method distance, which calculates the distance between two points (x1, y1) and (x2, y2). All numbers and returned values should be of type double. Incorporate this method into an program that enable the user to enter the coordinates of the points, then calculate and display the distance by calling the method –distance.
I've tried numerous times to make it work and I'm on the right path, however I'm missing some things in the code to make my results look like this later on, which I've attached onto this post.
I have to write a program to calculate my weight on different planets and to do that, I have to read in the surface gravity of all the planets from a file using a separate method from main
public static double[] readGravity() throws IOException { double[] surfaceGravity = new double[8]; Scanner readFile = new Scanner("gravity1.txt"); int i = 0; while (readFile.hasNext()) { surfaceGravity[i] = readFile.nextDouble(); i++; } return surfaceGravity; }
I get this message in the while loop when I try to run the program:
java.util.InputMismatchException; null (in java.util.Scanner)
with arrays its binary search which finds a value in O(Logn) time but what about linked lists ? the most effiecient algorithm will be O(n) ? and i know that binary search cannot be implement on a linked list , therefore , the only way to search a linked list is a linear search ?
I have understood my programming class up to this point and now I have been given a lab that I can't figure out for the life of me. Here what I have to do: Write a program that will call a method (called f) to calculate the following function" f(x)=(x^2)-16...this is what the output should be:
I need to modify the drawShape method to calculate the distance from the starting point (the diameter) of any shape regardless of how many sides I give it, but I have absolutely no clue where to begin with this. The ultimate goal of the program is to calculate the value of pi using the shape that is drawn.
Java Code:
public class PiTurtle extends Turtle { private double mySize; private int mySides; private double diameter = 0;
I am trying to write a loop that calculates the distance traveled (distance = speed * time). It should use the loop to calc how far the vehicle traveled for each hour of time. My program asks for hours and then mph but its not calculating time * speed. Here is my code.
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter Hours Traveled "); int hoursTraveled = input.nextInt(); System.out.println("Enter MPH "); int mph = input.nextInt();
I've just been having a go at an exercise where I have to create and use a class called Point, with two fields of type double. I have to create some methods for it, one of which is a distanceTo(Point) method, that calculates the distance to another point. I've tried to keep the distanceTo(Point) method short so have created some other methods to use within the method. My question is about the getDistance() method that I've made. As you can see below, I've given it two parameters, which are references to values within two Point objects (this.x and otherPoint.x).
I'm trying to make a calendar that, when you click on the date, the result are stored in a map and visualized in a table that refers to a container.
I successfully created the map mechanism, but I have a problem on the list....
I add the rows in this way:
Object newItemId = container.addItem(); Item riga = container.getItem(newItemId); riga.getItemProperty("Timestamp").setValue(fara); riga.getItemProperty("Date").setValue(asa); . How can I delete a row in the list, when all I have in input is the "Timestamp" information, in a Long value?
I want my jSpinner value "0" to be removed on button click. and I want my jSpinner and jFormattedTextfield and jTextfield value "0" to be Highlighted(e.g: like highlighted by mouse double click) on button click.By removed I mean: nothing to be in the jSpinner. empty as any textfield. the jSpinner default value is 0 and on Click I want the value of the jSpinner to be removed and Highlighted, I need both examples code.
import javax.swing.Timer; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Ball extends JPanel { private int delay = 10; private Timer timer = new Timer(delay, new TimerListener()); private int x = 0; private int y = 0;
How can I remove the ActionListener from the buttons of my application after i have got something happen.
Please consider the following application:
Java Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Exer1218659 extends JFrame { private String[] textButtons; private Container contents;
[Code] .....
After the line 37 executes I expect that the ActionHandler (ah) will be removed from all of the buttons but this do not happen, then all the remaining button still responding to the clicks. Where is the problem in my code.
My program gives me an error when I try to "digUp" the purse. The error appears in the purse class.
import java.io.*; public class Purse { //Fields public double gp; public double gold; public double silver; public double copper; public double platinum; public static final double GOLD_VALUE = 1;