Using squared() method inside of cubed() method

Page 1 of 1

4 Replies - 6753 Views - Last Post: 19 March 2010 - 07:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=162588&amp;s=7719e7db6ef24267964be0e33eca6cda&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 240z240z

Reputation: 1
• Posts: 17
• Joined: 15-March 10

Using squared() method inside of cubed() method

Posted 17 March 2010 - 02:19 PM

Hello Java Mates! The first program Squares then Cubes a number with two methods.

Now I am change how I get the cubed number by using the results in the first method to multiply the original number.

So instead of stating, Cubed = 8 * 8 * 8, I am to say: Cubed = result * 8 (or maybe the method name * 8 not sure).

Both programs work below. I tried to modify the first program to make it work but after 5 hours my brain is wack.

But in the second program when I call the results of the first method I get two extra lines of code?

```// brings scanner to the class
import java.util.Scanner;

public class ExponentA
{
public static void main (String [] args)
{
Scanner Keyboard= new Scanner (System.in); // enables scanner for HMI

// variable declared
int square;

System.out.print("Enter number to be squared and then cubed: "); // prompts user to input number
square = Keyboard.nextInt();  // places value of human input inside variable
squareprod(square); // calls squareprod method
cubres(square); // calls cubres method

}
// squares number
public static void squareprod(int square)
{
int sqprd = 0; //declares variable of integer data type and sets contents to zero
sqprd = square * square;
System.out.println("\n " + square + " Squared equals: " + sqprd );
}
// cubes number
public static void cubres(int square)
{
int cubnum = 0; //declares variable of integer data type and sets contents to zero
cubnum =  square * square * square;
System.out.println("\n "  + square + " Cubed equals: " + cubnum );
}
}

```

SECOND PROGRAM - The one that needs the reults of the 1st method, it prints two extra lines due to calling the method/results?

```// brings scanner to the class
import java.util.Scanner;

public class Exponent2
{
public static void main (String [] args)
{
Scanner Keyboard= new Scanner (System.in); // enables scanner for HMI

// variable declared
int square;

System.out.print("Enter number to be squared and then cubed: "); // prompts user to input number
square = Keyboard.nextInt();  // places value of human input inside variable
squareprod(square); // calls squareprod method
cubres(square); // calls cubres method

}
// squares number
public static int squareprod(int square)
{
int result;
int sqprd = 0; //declares variable of integer data type and sets contents to zero
sqprd = square * square;
result = sqprd;
System.out.println("\n " + square + " Squared equals: " + sqprd );
return result;
}
// cubes number
public static void cubres(int square)
{
int cubnum = 0; //declares variable of integer data type and sets contents to zero
squareprod(square);
cubnum =  squareprod(square) * square;
System.out.println("\n "  + square + " Cubed equals: " + cubnum );
}
}

```

Is This A Good Question/Topic? 0

Replies To: Using squared() method inside of cubed() method

#2 k0b13r

Reputation: 15
• Posts: 243
• Joined: 18-July 06

Re: Using squared() method inside of cubed() method

Posted 17 March 2010 - 02:44 PM

The problem is your squareprod method, because it's returning the value and printing a line. So, in second piece of code you get two extra line because of
```squareprod(square);
cubnum =  squareprod(square) * square;

```

Every squareprod call prints new line.

I would suggest something like this:
```public static int squareprod(int square)
{
return square*square; // Just return squared value
}

public static void cubres(int square)
{
return squareprod(square)*square; // Get squared value and multiply it again
}

```

And your printing should look like:
```System.out.println("Squared equals: " + squareprod(square));
System.out.println("Cubed equals: " + cubres(square));

```

I hope it helps

This post has been edited by k0b13r: 17 March 2010 - 02:47 PM

#3 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

Re: Using squared() method inside of cubed() method

Posted 17 March 2010 - 02:54 PM

Nice work k0b13r!

Since "Java" is a terrible name for a question in the Java help forum, I'm going to rename it to something more descriptive.

#4 240z240z

Reputation: 1
• Posts: 17
• Joined: 15-March 10

Re: Using squared() method inside of cubed() method

Posted 19 March 2010 - 07:25 AM

Thank you k0b13r ! Forgive the delay in response Im over here in Iraq and just trying to push through on Java...it must be the book...and me...did great in Visual Basic course but not getting Java like I did VB...

New code below....

YOU ARE THE BEST my friend thank you again!

VR
K

```// brings scanner to the class
import java.util.Scanner;

public class Exponent2
{
public static void main (String [] args)
{
Scanner Keyboard= new Scanner (System.in); // enables scanner for HMI

// variable declared
int square;

System.out.print("Enter number to be squared and then cubed: "); // prompts user to input number
square = Keyboard.nextInt();  // places value of human input inside variable
squareprod(square); // calls squareprod method
cubres(square); // calls cubres method
System.out.println(square + " Squared equals: " + squareprod(square)); //displays squared results
System.out.println(square + " Cubed equals: " + cubres(square)); //displays cubed  results
}
// squares number
public static int squareprod(int square)
{
return square*square; // Just return squared value
}
// cubes number
public static int cubres(int square)
{
return squareprod(square)*square; // Get squared value and multiply it again
}
}

```

#5 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

Re: Using squared() method inside of cubed() method

Posted 19 March 2010 - 07:36 AM

You got your other methods great. I just want to keep you aware of something.
```        square = Keyboard.nextInt();  // places value of human input inside variable

// THESE TWO CALLS DO NOTHING
squareprod(square); // calls squareprod method
cubres(square); // calls cubres method

// THESE TWO CALLS ARE GREAT
System.out.println(square + " Squared equals: " + squareprod(square)); //displays squared results
System.out.println(square + " Cubed equals: " + cubres(square)); //displays cubed  results

```

Since squareprod() and cubres() return ints, you need to sore those ints somewhere, you can't just call them like void methods and expect them to do anything. Since those method calls do nothing, you could just remove them OR do something like this:
```       square = Keyboard.nextInt();  // places value of human input inside variable

// Put into int variables
int squaredProduct = squareprod(square); // calls squareprod method
int cubedProduct = cubres(square); // calls cubres method

// Now just print those variables.
System.out.println(square + " Squared equals: " + squaredProduct); //displays squared results
System.out.println(square + " Cubed equals: " + cubedProduct); //displays cubed  results

```