JAVA Calculator - Equals Method To Return Appropriate Value Using Standard Arithmetic

Apr 18, 2014

I am having trouble using the equals method to return the appropriate value using standard arithmetic.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

[Code] ....

I know that "inText.setText(Integer.toString(secondOperand));" in the code is wrong, I just don't know the right code...

View Replies


ADVERTISEMENT

Equals Method - Take Object Reference And Return True If Given Object Equals This Object

Mar 28, 2014

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?

View Replies View Related

Simulate Simple Calculator That Performs Basic Arithmetic Operations Of JAVA

Sep 9, 2014

How can i write a java program that simulate a simple calculator that performs the basic arithmetic operations of JAVA. (JOption Pane and Repeat control structure)

Example : Addition, Subtraction, Multiplication, Division.

The program will prompt to input two floating point numbers and a character that represents the operations presented above. In the event that the operator input is valid, prompt the user to input the operator again.

Sample Output :

First number 7
Second number 4
Menu

<+> Addition
<-> Subtraction
<*> Multiplication
</> Division

Enter Choice: * <enter>

The answer is 28.

View Replies View Related

Overriding Equals Method - Cannot Invoke Equals On Primitive Type (double)

Jan 7, 2014

I am trying to override the equals method for my class TeamMember I have two fields:

private Details details;
private double salary;

Code for override I get an error on salary saying cannot invoke equals (double) on the primitive type (double)

Is their something I am missing/coding wrong?

Java Code:

public boolean equals(Object obj) {
//test exceptional cases
//avoid potential NullPointerException/ClassCastException
if ((obj == null) || (this.getClass() != obj.getClass()))
return false;
TeamMember other = (TeamMember) obj; //cast to a TeamMember object
// compare fields details and salary
return this.details.equals(other.details)
&& this.salary.equals(other.salary);
} mh_sh_highlight_all('java');

View Replies View Related

Method Must Return Int Type - If Given Integer Is Strong Return A / If Not Return B

Sep 7, 2014

I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?

I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.

mlong should return an int depending on the X.moth. at the moment my code looks like this:

// File1:
public class date {
public int day;
public int month;
public int year;
}

// File 2:
public class monthlength {
public int mlong(date X) {
int t;
t = X.month;
if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12)
{ return 31; }
if(t == 4 || t == 6 || t == 9 || t == 11)
{return 30;}
}
}

View Replies View Related

Calculator Only Return 0.0?

Sep 2, 2014

I am making a calculator in Java. However, when I press the "=" button on my calculator, it always returns 0.0, no matter what. I don't understand why? The code is below:

import javax.swing.*;//import the packages needed for gui
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.List;
import static java.lang.Math.*;
public class CalculatorCopy {

[Code] ....

View Replies View Related

Java Project - UPC Calculator / Calling ToString Method?

Feb 20, 2014

Our goal is to write a pretty simple program, one that takes the 12 digit UPC code entered by a user and to not only spit it back out in a format with dashes using toString, and also returns the first digit, a 2 more groups of digits numbering 2-6 and 7-11, and finally display the 12th digit. It then performs an equation to check the last digit and make sure the UPC code is correct.

However, being so new to java (I only learned visual basic before), with this I was introduced to two new concepts that for some reason I simply cannot grasp for the life of me: Using and calling the toString method, and calling on methods that are created in a completely different class file.

The first section of code is my UPC class, which is meant to contain all my methods as well as the toString to be called on:

public class UPC
{
// Instance variables
private int itemType; // digit 1
private int manufacturer; // digits 2,3,4,5,6
private int product; // digits 7,8,9,10,11
private int checkDigit; // digit 12

[Code] .....

View Replies View Related

Getting Error On Equals Method For Objects

Mar 26, 2014

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

if(length.equals(object.length) && width.equals(object.width))

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..

View Replies View Related

Clarity On Overriding Equals Method

Oct 2, 2014

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?
}

View Replies View Related

Unable To Create Method That Calculates Standard Deviation Of Array

Jun 15, 2014

I am trying to create a method that calculates the standard deviation of array. What I want to be able to do is something like this

package standardDevaitionAndMean;
public class StandardDeviationTest {
public static void main (String[] args)
{
int [] array = {12,12,12,12,12,12,12};
standardDev = array.StandardDevation();
System.out.print(standardDev);
}
}

With something like this

package standardDevaitionAndMean;
public class StandardDeviation
{double mean1;
double standDev;
public double Mean(double ... array)
{
for(int i=0; i<array.length;i++)

[Code]...

So basically I want to be able to make an array in a class and be able to calculate its standard deviation with my method in my other class. I know the code I wrote is terrible but I just wanted to show what I am trying to do. I am kind of shaky on how arrays operate

View Replies View Related

How To Return Values In Object Return Type Function (method)

Apr 2, 2014

How do i take input values for TwoDPoint (which are objects) and return it back in numerical values also print them.

When i create an object in main method and pass values to my function of return type TwoDPoint,it gives error:- found int,int need TwoDPoiint,TwoDPoint.

// Here is what i tried to do:

Created class TwoDPoint that contains two fields x, y which are of type int. Defined another class TestTwoDPoint, where a main method is defined.In the main method created two TwoDPoint objects.

Then I modified the class TestTwoDPoint and add another function to it. This function takes two TwoDPoints as input and returns the TwoDPoint that is farthest from the point (0,0).

Then I added another function to TestTwoDPoint. This function takes two TwoDPoints as input and returns a new TwoDPoint whose x value is the sum of x values of the input TwoDPoint's and whose y value is the sum of the y values of the input TwoDPoint's.

class TwoDPoint {
int x = 2;
int y = 4;
}
class TestTwoDPoint {
public static void main(String args[]) {
TwoDPoint obj1 = new TwoDPoint();
System.out.println(obj1.x);
System.out.println(obj1.y);

[Code] ....

View Replies View Related

Overriding Equals Method From Object Class

May 5, 2014

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.

package javaapplication5;
import java.text.NumberFormat;
import java.util.Scanner;
import java.lang.Object;
public class Product {
private String product;
private String description;
private double price;

[Code] ....

And here is the stack trace

Exception in thread "main" java.lang.NullPointerException
at javaapplication5.Product.equals(Product.java:42)
at javaapplication5.Product.main(Product.java:24)
Java Result: 1

View Replies View Related

Netbeans - Unable To Debug Equals Method

Mar 11, 2015

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).

View Replies View Related

Why Java Does Not Support Return Type In Method Overloading

Feb 7, 2015

why Java does not support return type in method overloading. I coded following and it compiles and runs without any errors.

class Ideone
{
public static void main (String[] args) {
sum(3,5);

[code]....

If Java did not support overloading based on return type, this program should not work, right?

View Replies View Related

Creating Equals Method To Compare Strings In A Class?

Mar 26, 2015

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?

View Replies View Related

How To Test And Finish ToString And Equals Method In Code

Jan 19, 2014

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) {

[code]....

View Replies View Related

Arithmetic Methods Whose Main Method Holds Two Integer Variables

Jan 15, 2015

a. Create an application named ArithmeticMethods whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayNumberPlus10(), displayNumberPlus100(), and displayNumberPlus1000(). Create each method to perform the task its name implies.

b. Modify the ArithmeticMethods class to accept the values of the two integers from a user at the keyboard.

View Replies View Related

Program That Implement Final Method And Perform Arithmetic Operation

Mar 4, 2014

class A
{
public final int Add(int a,int b) {
return a+b;
}
}

class B
{
public static void main (String args[])
{
System.out.println(Model.Add(5,10));
}
}

This is what I have. I'm not sure if this even makes use of a final method. But when I run it I get an error saying cannot find symbol for line 13.

View Replies View Related

Java Method - Return True If String Can Be Found As Element Of Array And False Otherwise

Mar 17, 2014

I am new to Java and would like to ask how to do this question specifically:

Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.

View Replies View Related

How Does Java Know Where To Import Classes From The Standard Libraries

Aug 19, 2014

I was wondering how Java knows where to Import classes from when we don't specify the whole directory path.

For example: import java.util.* Does It automatically search In both your current directory and the directory created during Installation that contains the standard library (wherever It may have been Installed) ?

View Replies View Related

NoClassDefFoundError With Java Standard Platform Class?

Feb 18, 2015

I am facing NoClassDefFoundError with one of the java standard platform class (java.text.DigitList$1)

java.text.DigitList class is part of rt.jar so it should not create any issue as such.

I am using Java 1.6.0_06

StackTrace:
Unhandled exception java.lang.NoClassDefFoundError: java/text/DigitList$1
at java.text.DigitList.shouldRoundUp(DigitList.java:4 11)
at java.text.DigitList.round(DigitList.java:363)
at java.text.DigitList.set(DigitList.java:351)
at java.text.DigitList.set(DigitList.java:272)
at java.text.DecimalFormat.format(DecimalFormat.java: 584)
at java.text.DecimalFormat.format(DecimalFormat.java: 507)
at java.text.NumberFormat.format(NumberFormat.java:26 9)

View Replies View Related

Handling Button Events In Eclipse Standard 4.4 / Java?

Jul 20, 2014

Code:
package button;
import javax.swing.*;
import java.awt.event.*;
public class Actions extends JFrame implements ActionListener
{
JPanel pnl = new JPanel();

[Code] ....

Errors:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The type Actions must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
event cannot be resolved
event cannot be resolved
at button.Actions.<init>(Actions.java:4)
at button.Actions.main(Actions.java:9)

View Replies View Related

How To Return Array From A Method / Back Into Main Method That Prints Out Stuff

May 27, 2014

I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?

View Replies View Related

Test Arithmetic Java Programming Error

Oct 13, 2014

I have written this program but I am seeing error when I compile this program ( javac TestArithmetic.java).

1)TestArithmetic.java:26 cannot find symbol

2)symbol: class Arithmetic

3)location: class TestArithmetic

4) Arithmetic ar = new Arithmetic(x1,x2,x3);

1)TestArithmetic.java:26 cannot find symbol

2)symbol: class Arithmetic

3)location: class TestArithmetic

4) Arithmetic ar = new Arithmetic(x1,x2,x3);

2 errors.

Code :

import java.util.*;
class TestArithmetic {
public static void main (String[] args){
float number;
Scanner console= new Scanner (System.in);
System.out.println("Enter first number");

[Code] .....

View Replies View Related

How To Link Compress Method To Return Statement Method GetPText

Oct 30, 2014

Alright, I don't understand how to link my compress method to my return statement method "getPText". Also in my compression I only want it to compress for 3 or more consecutive letters.

import java.util.Scanner;
class RunLengthCode {
String pText;
String cText; 
void setPText(String PText) {
pText = "";
}

[Code]...

View Replies View Related

Set Method Return Type Determined Inside Method

Dec 25, 2014

I need to write a method that will consume string representation of Object type and will return one object of this type. How to set return type for the method in this case?

Here is exmaple :

public <?> identifyType(String typeString){
if (typesString.matches("String")){
return new String("");
}else if (typeString.matches("Integer")){
return new Integer(0);
}
//....etc..}

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved