While Loop - How Result Being Replace If It Only Equals To 1
Jul 6, 2014
I was reading a book and came across this while loop.
public class Powers {
public static void main (String [] args){
int e;
int result;
for(int i = 0; i < 10; i++){
[Code] .....
This loop presents the following (I'm sure it's not necessary):
2 to the 0 power is 1
2 to the 1 power is 2
2 to the 2 power is 4
2 to the 3 power is 8
2 to the 4 power is 16
2 to the 5 power is 32
2 to the 6 power is 64
2 to the 7 power is 128
2 to the 8 power is 256
2 to the 9 power is 512
I am just having a difficult time understand and grasping this concept. My main issue is result *=2; this is making it very difficult to understand. How is result being replace if it only equals to 1.
having a hard time with my do while loop. for some reason in my else if structure it will break when i using .equals in the do while loop. But if i try doing tmp == "D" || tmp == "d" it will keep looping how can i go about using alternative for .equals?
Here is my code:
do { Object object = new Object(); System.out.println("For a deposit Enter: D Withdrawal Enter: W " + "or 'Quit' to exit."); String tmp = input.next(); if (tmp.equals("D"))
Below is a method that is suppose to insert a value inside a stack based on the index. I thought it would be easier to copy the value, if any, that was in the index into the variable holder and replace it with the input value. After in which it would copy the holder value and place it at the top of the stack. (topOfStack variable is what the holder is copying too.)
public void pushExact (int index, String input) { String holder = ""; if (maxSize == 0) { theStack[topOfStack] = input; topOfStack++; } else if (topOfStack + 1 < maxSize) { for (int n= maxSize - 1;n >= 0;n--) {
Create an equals method that takes an object reference and returns true if the given object equals this object.
Hint: You'll need 'instanceof' and cast to a (Geocache)
So far I have:
public boolean equals(Object O){ if(O instanceof Geocache){ Geocache j=(Geocache) O; if (this.equals(j)) //I know this is wrong... but I can't figure it out return true; }
else return false; }
I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?
In the above program even if i comment out the Hashcode method , i believe it is still taking the memory address values from the native hashcode method of Object class. but the equals override implentation says that i have two insertions which are same . So as per my logic it should not allow the duplicate element to enter.but its not so ...the duplicate element is well inserted without hashcode .
All I am trying to do is to make a section of code execute if two strings are equal. The two strings are userId and "A001062". When I use the debugger in Eclipse, I can see the value of userId as "A001062" but whatever string comparison I try never evaluates to true. I have tried
userId=="A001602" userId.equals("A001602") "A001602.equals(userId) Assigning A001062 to a string called AAA and comparing userId to AAA
My code is as follows. I have also attached a screen shot from the Eclipse Debugger which makes me think the string comparison should succeed. I never see the debugger execute the print line nor do I see the print line on the JBOSS console.
String userId = StringUtils.trim(nextLine[HR_USER_ID]); String AAA="A001062"; if (userId.intern().equals(AAA.intern())) {System.out.print("MKP1: " + userId+"-"+managerId);} if (userId.compareTo("DTS0428")==0) {System.out.print("MKP2: " + userId+"-"+managerId);}
I have to create a class that has two fields. One called length and the other width. I have to make a method that returns the tract area. Similarly, I also have to make a method that indicates whether two objects have the same fields. Here is the code that I have assembled...so far
// create private fields to hold width and length private double width; private double length;
[Code].....
My problem is encountered when writing that equals method
I get an error saying HTML Code: cannot invoke equals(double) on the primitive type double. Meanwhile, I do see, to realize that when I change my fields to capital "Double." The problem disappears; however, in my class I have never dealt with a situation where I have to use capital d in double. In fact, I don't even know what's the difference between Double and double. I do know what double is but not the other one..
(Count positive and negative numbers and compute the average of numbers). Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number.
I moved the different boolean statements around, but I'm not getting the sentinel value to end the run. It continues to let me add integers endlessly. The code I wrote is below:
package exerciseFourOne; import java.util.Scanner; public class AverageOfIntergers { public static void main(String[] args) { // TODO Auto-generated method stub int positive = 0; // number of positive integers int negative = 0; // number of negative integers int sum = 0; // value of sum of integers
In Java code, write class called Student with the following features:
• a private instance variable int studentNumber that is initialized to zero. • a private instance variable String firstName; • a private instance variable String lastName; • a constructor which takes an integer argument and two String arguments to initializes the three respective data items. • a public method with signature equals(Student s) . . .
So far this is my code :
public class student { private int studentnumber = 0; public student () { firstname= "forename": lastname="surname": public student (integer studentnumber, string firstname, string lastname) { this.firstname= firstname this.lastname= lastname:
My question is how do i add the integer in the argument do i have to use int =? and how would i go about doing the public signature equals...
Im working on a roman numeral to arabic converter and all I had to do was fill out the conversion method romanToDecimal. But for some reason no matter what number I enter It always says my number is equal to one.
//Quiz 1 EC import java.util.*; class Roman { private String romanNum; private int decimalNum; public Roman(){ romanNum = "I"; decimalNum = 1;
For a few days I've been reading about the importance of overriding the equals method. How overriding it actually determines or checks the values stored in the variable. I realize that you can check the values stored in the primitive datatypes with "==", and when you don't override the equals method it acts the same way, right? When used with a reference datatype, "==" or the default equals() method only compares, or sees, if the variable is pointing to the same instance of a class. For some reason, in the examples, what is taking place to actually check the values stored inside the variables.
Here is part of an example (I've added comments for things that are confusing me):
@Override public boolean equals(Object obj) { //So we use Object here instead of the class type // we're overriding this equals method for? Is this so that we can use it to check different types? (overloading?) if (obj == this) { return true;
//Isn't this checking to see if the calling object is the same as the object we're passing to it? Why doesn't this return false? } if (obj == null || obj.getClass() != this.getClass()) { return false; }
//How exactly do we check the values stored in each object though? }
All I am trying to do is to make a section of code execute if two strings are equal. The two strings are userId and "A001062". When I use the debugger in Eclipse, I can see the value of userId as "A001062" but whatever string comparison I try never evaluates to true. I have tried
userId=="A001602" userId.equals("A001602") "A001602.equals(userId) Assigning A001062 to a string called AAA and comparing userId to AAA
My code is as follows. I have also attached a screen shot from the Eclipse Debugger which makes me think the string comparison should succeed. I never see the debugger execute the print line nor do I see the print line on the JBOSS console.
String userId = StringUtils.trim(nextLine[HR_USER_ID]); String AAA="A001062"; if (userId.intern().equals(AAA.intern())) {System.out.print("MKP1: " + userId+"-"+managerId);} if (userId.compareTo("DTS0428")==0) {System.out.print("MKP2: " + userId+"-"+managerId);}
I am attempting to override the equals method from the Object class which checks if two variables point towards the same object. I want the method to check if if the argument being passed in(an object) has the same data(instance variables) as the object that's calling this method. A NullPointerException is being thrown; Here is the code.
Exception in thread "main" java.lang.NullPointerException at javaapplication5.Product.equals(Product.java:42) at javaapplication5.Product.main(Product.java:24) Java Result: 1
I need to debug the equals method implementation of a class I've made, but I cannot for the life of me get Netbeans' debugger to step into it. I can step into other methods from the class (most of which implement the methods in an interface) that are called in the main method (just like the equals method). I've tried...
-Disabling all the step filters -Clearing the Netbeans cache -Moving the call to the equals method out of the if statement it's in and just calling it as its own statement -placing breakpoints within the equals method as well as on the call to the method -placing a method breakpoint on the overridden equals method in addition to the other locations -Using the shift-F7 version of the step into command
I'm using Netbeans 8.0.1 (I don't know if this is the latest version, but the last time I tried to update everything died and I had to completely remove NB and reinstall it) and JDK 8u05 (I think).
Here is my code the whole program is working correctly but the Boolean equals and the has code and it is a requirement for the assignment. Why it is not working.
I know there are issues with the code I am new with java and was struggling so I have to clean my code up before I submit the assignment but for right now I have the out put the way I want it except the Boolean and hash code methods always output that the rectangles aren't equal even when I know they are and it outputs the not equal statement twice??
I can't figure out why my code doesn't work. My task is to replace for example ä=>ae, using this method String.charAt(int index). So here is my code:
public class pich { public static void main(String[] args) { String text = "Die süße Hündin Leica läuft in die Höhle des fülligen Bären "+ "Iliyan (ein Übergrößenträger), der sie zum Teekränzchen eingeladen hat."+ " An ihrem Öhrchen trägt sie modisch eine Ähre."; String textOhneUmlaute = "";
[Code] ....
when I launch my code I get the same String and nothing has changed
How would I create a equals method to compare strings in a class that I'm creating. I need to create the method in my class, and then call it in a driver. How would I do this?
Write a class encapsulating the concept of a course grade, assuming a course grade has the following attributes: a course name and a letter grade. Include a constructor, the accessor and mutator, and methods toString and equals.Write a client class to test all the methods in your class.
how to test and finish the toString and equals method in this code ?
package labmodule7num57; import java.util.*; public class LabModule7Num57 { // Constructors// private String name; private String letterGrade; public LabModule7Num57 (String name,String letterGrade) {
Basically, I am trying to write a method that will take a string, a target, and a replacement (a replacement function like string.replace). I want it to replace all instances of target with replacement EXCEPT for instances that occur within single or double quotes.
Example input / output:
this "Wont" be " replaced, be"
call: replace(theString, "replaced", "narf") and replace(theString, "be", "rafn")
I have a 2D arraylist, named as adjLists, which contains arraylists, containing values like these. Each two values are a "pair" and each third is a "flag" for the pair.
I use code above to search for specified value pairs in these lists. vertexnum is the number of "sub arraylists" in adjLists.
for (int l = 0; l < vertexnum; l++) { if (adjLists.get(l).get(0) == p.x && adjLists.get(l).get(1) == p.y) { for (int h = 0; h < adjLists.get(l).size(); h += 3) { for (int g = 0; g < vertexnum; g++) { if ((vertexSprite[g].getX() + vertexSprite[g].getWidth() / 2) == adjLists.get(l).get(h)
[Code] ....
This code is to search exact values and replace their flag in every occurences. It can not find all the occurences of the values/pair in the lists, it replaces flag value only a few time. Value of score should be incremented with plus 1 after each found occurence, but this code increments it more than the number of matches.