# Beginner; Help me improve my solution (Arithmetic)

Page 1 of 1

## 5 Replies - 436 Views - Last Post: 21 March 2012 - 05:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=271836&amp;s=2b084082f4f931ba435a74eb945c24ba&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 EscSequenceAlpha

• New D.I.C Head

Reputation: 1
• Posts: 17
• Joined: 20-March 12

# Beginner; Help me improve my solution (Arithmetic)

Posted 20 March 2012 - 07:35 PM

So I just started programming and I bought a book called DEITEL how to program 8ed which I'm progressing through, it's a very good book. I've gotten up to chapter 2 2.15 which is basically my first excercise at writing a simple program. I am to extract a sum/product/difference/quotient from a calculation, simple enough.

The program I got to work fine, but I've experienced some errors and did some work arounds, I want to know what the errors were and what is the simple solution.

```package chapter_ii;

import java.util.Scanner;

public class Arithmetic
{
public static void main( String[] args )
{
int number1;
int number2;

Scanner input1 = new Scanner( System.in );
Scanner input2 = new Scanner( System.in );

System.out.println("Please input two numbers");
number1 = input1.nextInt();
number2 = input2.nextInt();

calculate( number1, number2 );
} // end method main

public void calculate( int number1, int number2 )
{
int sum;
int product;
int absoluteNumber1;
int absoluteNumber2;
int difference = 0;
int quotient;

sum = number1 + number2;
product = number1 * number2;
absoluteNumber1 = java.lang.Math.abs(number1);
absoluteNumber2 = java.lang.Math.abs(number2);
if (absoluteNumber1 > absoluteNumber2)
difference = absoluteNumber1 - absoluteNumber2;
if (absoluteNumber2 > absoluteNumber1)
difference = absoluteNumber2 - absoluteNumber1;
if (absoluteNumber1 == absoluteNumber2)
difference = 0;
quotient = number1 / number2;

System.out.printf("The sum is %d \n the product is %d \n the difference is %d \n the quotient is %d", sum, product, difference, quotient);
} // end method calculate
} // end class Arithmetic

```

This code doesnt work because I'm in the middle of trying to remove the static references I was forced to put in. I get an error at
```calculate( number1, number2 );
```

"can not make a static reference to a non static method"

what does this mean? why is it a static reference I don't know this stuff shed some light for me... you can see what I'm trying to do by passing the variables around. How would you program this (keep in mind I created another method because I dont think calculations belong in the main method but maybe thats just me)

---> question one: how would you resolve the static issue?

The next problem is concerning the "difference" variable, for some reason I got an error saying how it must be initialised which is why I put difference = 0 in the declaration, but why is this? because the other variables dont have to be initialised this way, only when the user inputs some numbers... so does that mean those variables are "0" before the user inputs something and if thats the case then why isnt "difference" 0 before it gets assigned aswell?

---> question 2: why must I initialise the "difference" variable with 0 but not the others?

My final question is about my method for finding the difference, its a bit long... is there a simplier solution? I thought about this and realised I could not find the difference between two numbers if I subtracted from a negative number. i.e. 3 - (-4) = 3 + 4 = 7 so thats why I used the absolute method first. Is there a better way or was I expected to make use of if statements and find the absolute method in the way that I did?

---> question 3: was that a good way of finding the difference?

Is This A Good Question/Topic? 0

## Replies To: Beginner; Help me improve my solution (Arithmetic)

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8026
• Posts: 31,151
• Joined: 06-March 08

## Re: Beginner; Help me improve my solution (Arithmetic)

Posted 20 March 2012 - 07:46 PM

It is because you are doing too much things in your main() method. It should be used to instatiate an Arithmetic object where the calculations should be done

```package chapter_ii;

import java.util.Scanner;

public class Arithmetic
{
int number1;
int number2;

Arithmetic(int number1, int number2) {
this.number1 = number1;
this.number2 = number2;
} // end constructor

public void calculate()
{
int sum;
int product;
int absoluteNumber1;
int absoluteNumber2;
int difference = 0;
int quotient;

sum = number1 + number2;
product = number1 * number2;
absoluteNumber1 = java.lang.Math.abs(number1);
absoluteNumber2 = java.lang.Math.abs(number2);
if (absoluteNumber1 > absoluteNumber2)
difference = absoluteNumber1 - absoluteNumber2;
if (absoluteNumber2 > absoluteNumber1)
difference = absoluteNumber2 - absoluteNumber1;
if (absoluteNumber1 == absoluteNumber2)
difference = 0;
quotient = number1 / number2;

System.out.printf("The sum is %d \n the product is %d \n the difference is %d \n the quotient is %d", sum, product, difference, quotient);
} // end method calculate

public static void main( String[] args )
{
// you onl,y need one
Scanner input = new Scanner( System.in );

System.out.println("Please input two numbers");
int num1 = input.nextInt();
int num2 = input.nextInt();
Aritmetic ar = new Arithmetic(num1, num2);
ar.calculate();
} // end method main

} // end class Arithmetic

```

Was This Post Helpful? 3

### #3 ILoveJava

• D.I.C Regular

Reputation: 28
• Posts: 369
• Joined: 12-March 12

## Re: Beginner; Help me improve my solution (Arithmetic)

Posted 20 March 2012 - 07:47 PM

^ what he said.

This post has been edited by ILoveJava: 20 March 2012 - 07:48 PM

Was This Post Helpful? 0

### #4 jon.kiparsky

• Pancakes!

Reputation: 5428
• Posts: 8,738
• Joined: 19-March 11

## Re: Beginner; Help me improve my solution (Arithmetic)

Posted 20 March 2012 - 08:28 PM

More specifically, it's because you need to learn some things about static and instance objects and methods.

First of all, you have classes and you have objects. Classes are sorts of objects, and objects are instances of classes. When you declare an Integer and call it i, you create an instance of the Integer class. "Static" means "pertaining to a class", and "instance" means "pertaining to an object" - that is, pertaining to a particular instance of a class.

A static method can be called without reference to any particular instance, and it has no access to any particular instance's data. An instance method must be called with reference to a particular object.

If you take that Integer object, look at the parseInt and intValue methods. parseInt() is a static method: it takes a String and returns an int, and it just happens that the code is located in the Integer class. No particular Integer object is involved in this transaction. intValue() on the other hand is called on a particular Integer and returns an int. This method clearly needs to know about a particular Integer object's internal data, therefore it is implemented as an instance method.

Now since a static method cannot see the data belonging to any particular object, it cannot call any instance method without having an object to relate it to. Therefore, it's illegal to call this instance method

```public void calculate( int number1, int number2 )
```

from this static method:
```public static void main( String[] args )
```

You need, as pbl says, to make an object which can hold that data.
Was This Post Helpful? 1

### #5 jon.kiparsky

• Pancakes!

Reputation: 5428
• Posts: 8,738
• Joined: 19-March 11

## Re: Beginner; Help me improve my solution (Arithmetic)

Posted 20 March 2012 - 08:37 PM

EscSequenceAlpha, on 20 March 2012 - 09:35 PM, said:

The next problem is concerning the "difference" variable, for some reason I got an error saying how it must be initialised which is why I put difference = 0 in the declaration, but why is this? because the other variables dont have to be initialised this way, only when the user inputs some numbers... so does that mean those variables are "0" before the user inputs something and if thats the case then why isnt "difference" 0 before it gets assigned aswell?

All of the other variables are unambiguously assigned in the course of your calculate method, but the assignments to "difference" are all in if statements. Therefore, when you get to the point where you read the variable, the compiler can't be quite sure that there will be a value there. (even though logically there is no case in which it can be unassigned, the implementers want to be a little cautious here)
Was This Post Helpful? 1

### #6 EscSequenceAlpha

• New D.I.C Head

Reputation: 1
• Posts: 17
• Joined: 20-March 12

## Re: Beginner; Help me improve my solution (Arithmetic)

Posted 21 March 2012 - 05:00 PM

I want to personally thank all of you, I looked over everything that has been said and I think I've learned a lot from you guys.

I've written the most simple UML diagram ever written, looked at it - took what you guys said into account and found I could write the program again (your version) in psudocode which means I must understand the logic now.

I'll keep goung through all the excercises and ask you fine people for help if I have more logical problems or what not.
Was This Post Helpful? 0

Page 1 of 1

 Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).