Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need a different way to add Cubed numbers 1-10

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need a different way to add Cubed numbers 1-10

    /* 
     * Put your documentation header here.
     */
    import java.util.Scanner;
     
    public class Lab5{
     
        /**
         * Returns an integer whose value is a^3 (a cubed).
         * @param any integer to be cubed
         * @return a^3 (a cubed)
         */
        public static int cubed(int a){
            int cube = a * a * a;
            return cube;
        }
     
        /* Answer the following questions about the method cube: 
         *1) What is the method's return type?  
              Integer
     
         *2) What is the method's formal parameter name?
              A
         *3) What is the cube method's signature?
             int a
         *4) What line of code invokes the cube method?
              Return Cube
         *5) What is the name of the argument passed to cube?
         */ //Cubed
     
        public static void main(String[] args) {
           Scanner in = new Scanner(System.in);
           System.out.println("Enter an integer");
           int x = in.nextInt();
     
           // invoking the cubed method
           System.out.println("The cubed value of "+ x + " is "
                   + Lab5.cubed(x)+".");
     
           /* Below this comment, write a piece of code that will sum the cubes
            * 0f all the numbers from 1 to 10 inclusive. 0utput this sum (labelled).
            */
        int a = 1;
        int b = 2*2*2;
        int c = 3*3*3;
        int d = 4*4*4;
        int e = 5*5*5;
        int f = 6*6*6;
        int g = 7*7*7;
        int h = 8*8*8;
        int i = 9*9*9;
        int j = 10*10*10;
     
        System.out.println("Sum of Cubed Numbers of 1-10:  "+a + b + c + d + e + f + g + h + i + j);
        }
     
    }
    /*
     * Copy and paste your program output here
     */

    The last part with int a-j......
    Is there a better way of doing this, like possibly putting into a loop and showing output from maybe a single piece of information? I hope im asking this question right.....

    --- Update ---

    Or really is there a better way of adding them up.... to reduce coding


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,524
    My Mood
    Mellow
    Thanks
    156
    Thanked 615 Times in 602 Posts

    Default Re: Need a different way to add Cubed numbers 1-10

    Yes, there is a better way. Try your loop idea and see what happens.

    Why do most of your answers to questions 1 - 5 have incorrect capitalization?

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need a different way to add Cubed numbers 1-10

    capitalization doesn't really matter... just answering the question.

    ummm.... as far as the loop goes,

    I'm not very keen on the loop idea but I'll do a little more reading and find out.

Similar Threads

  1. how to add strings as numbers?
    By Dixxon in forum Java Theory & Questions
    Replies: 7
    Last Post: December 8th, 2013, 03:55 PM
  2. How to add the numbers in the following series?
    By namenamename in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 16th, 2013, 07:23 PM
  3. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  4. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Code Snippets and Tutorials
    Replies: 10
    Last Post: March 31st, 2011, 05:18 AM
  5. How to add line numbers in Eclipse
    By Brt93yoda in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: August 12th, 2010, 11:03 AM