soompi forums: Java Help Please (loops) - soompi forums

Jump to content

Page 1 of 1

Java Help Please (loops)

#1 User is offline   vanessa_x17 

  • Member
  • Pip
  • Group: Members
  • Posts: 778
  • Joined: 25-April 06

Posted 21 December 2006 - 04:37 PM

im in an intro to java class, and i dont understand loops at much tears.gif
can anyone help explain how to do those programs below you don't have to write it out for me but iono, help me understand how blink.gif

sorry i can't show you my code unsure.gif its in the school's computer and i honestly don't remember what it was

for the first one i can get the squares and the cubes individually, they would list them all but how do i add them all up and have it like the example?

if you could help i will forever love youu blush.gif


first program::
Write a function that adds up the squares and adds up the cubes of integers from 1 to n, where upper limit n is passed as an argument. Your function should only contain one loop only.
A sample run is shown.

Upper Limit: 5
The sum of Squares is: 55
The sum of Cubes is: 225




2nd ::
Write a function that takes a number n as an argument and writes out n rows of asterisks to the screen as shown below.
This sample output of the function occurs when n has the value 4.

****
***
**
*

0

#2 User is offline   spymanut 

  • spymanuts <-- fixed
  • Pip
  • Group: Members
  • Posts: 27
  • Joined: 10-December 05

Posted 21 December 2006 - 05:31 PM

For the first program, how input should be retrieved from the user is unclear blush.gif. Should the user be prompted to enter a value during program execution, or should the user pass in a value as an argument from the command line? Based on the sample runs, I used the former approach for the first program and the latter approach for the second program.

Here, I did not comment the code or perform much input validity checking, both of which you will probably want to do to receive full credit on the assignment phew.gif.

First program:
CODE
import java.util.*;

public class FirstProgram
{
    public static void main(String[] args)
    {
        int upperLimit;
        int sumOfSquares = 0;
        int sumOfCubes = 0;

        try
        {
            Scanner sc = new Scanner(System.in);
            System.out.print("Upper Limit: ");
            upperLimit = sc.nextInt();

            for(int i = 1; i <= upperLimit; i++)
            {
                sumOfSquares += Math.pow(i, 2);
                sumOfCubes += Math.pow(i, 3);
            }

            System.out.println("The sum of Squares is: " + sumOfSquares);
            System.out.println("The sum of Cubes is: " + sumOfCubes);
        }
        catch(InputMismatchException e)
        {
            System.out.println("Input mismatch exception >:(");
        }
    }
}

Second program:
CODE
public class SecondProgram
{
    public static void main(String[] args)
    {
        int n;

        try
        {
            n = Integer.parseInt(args[0]);

            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < n-i; j++)
                    System.out.print("*");
    
                System.out.println();
            }
        }
        catch(java.lang.ArrayIndexOutOfBoundsException e)
        {
            System.out.println("No argument exception >:(");
        }
        catch(java.lang.NumberFormatException e)
        {
            System.out.println("Bad number exception >:(");
        }
    }
}


awdark's #1 fan! <(^_^<)
0

#3 User is offline   blossomambition 

  • Member
  • Pip
  • Group: Members
  • Posts: 184
  • Joined: 23-February 06

Posted 21 December 2006 - 05:42 PM

ouu, i wrote one tooo! haha.

I wasn't sure how you wanted the input for "n" either.. so i made class constants for both programs to see if they'd match your examples. If your prof wanted user input as "n", you can always just use Scanner, shift the current code in main into a static method, and use the scanner as a parameter.

Anyhoos!

Program 1:


Program 2:

0

#4 User is offline   vanessa_x17 

  • Member
  • Pip
  • Group: Members
  • Posts: 778
  • Joined: 25-April 06

Posted 22 December 2006 - 02:13 PM

thank you thank you i get it now you guys are awesome and a great help smile.gif
0

Share this topic:


Page 1 of 1

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users