Java Recursion (Harmonic Sum) help

Status
Not open for further replies.

clank

Beta member
Messages
2
Location
Minnesota
EDIT - This was originally two threads that have been merged.

Hello, I need help with using recursion in a Java program! My assignment is to:
Write a program to compute the harmonic sum using a recursive harmonicSum() function. HarmonicSum = 1 + 1/2 + 1/3 + …. 1/n
Here is what I have so far:
class Harmonic
{
public harmonicSum(int n)
{
if (n == 1) return 1;
return n * fractor(n-1);
}
}


public static void main(String[] args)
{
Harmonic factor = new Harmonic();

factor.harmonicSum(int n)

}

-I do not completely understand all of this yet, so any input is appreciated. Also am I going to need some kind of loop?
 
Help with error message- displayData(float[],float[],int) in DollarToYen cannot be a

:exclamation:I need help figuring out a couple errors.

Basically my assignment is to write a program to display the Japanese Yen value of the balance of up to 100 American Dollar bank deposits. Your program should be able to convert Dollar to Yen. Write functions that read in data, convert the currency from Dollars to Yen for each bank deposit and display the value of each deposit in both Dollars and Yen.

I keep getting a few error messages saying: DollarToYen.java:62: displayData(float[],float[],int) in DollarToYen cannot be a
pplied to (float[],float[])
deposits.displayData(dollars, yen);


Here's my code:
import java.util.*;

class DollarToYen
{
public static final int MAX_DEPOSITS = 100;
public static final float DOLLAR_TO_YEN = 94.02684f;

// read number of Dollars in each account from the keyboard
void readDollars(float[] dollars, int count )
{
Scanner kb = new Scanner(System.in);
for(int index = 0; index < count; index++)
{
System.out.print("Enter the deposit amount(dollars) : $");
dollars[index] = kb.nextInt();
}
}

// Convert Dollars to Yen
void dollarsToYen(float dollars[],float yen[],int count)
{
for (int index = 0; index < count; index++ )
yen[index] = dollars[index] * DOLLAR_TO_YEN;
}

// Display the amount in each account in both Dollars and Yen
void displayData(float dollars[],float yen[],int count )
{
for(int index = 0; index < count; index++ )
{
System.out.println("\nAccount ["+(index+1)+"] : ");
System.out.println("\t"+dollars[index]+" dollars");
System.out.println("\t"+yen[index]+" yen");
}
System.out.println();
}

public static void main(String[] args)
{
int num; // Actual number of deposits
float[] dollars = new float[MAX_DEPOSITS]; // Dollars
float[] yen = new float[MAX_DEPOSITS]; // Yen

Scanner kb = new Scanner(System.in);
DollarToYen deposits = new DollarToYen();


// Prompt the user for the number of deposits
System.out.print("Enter the number of deposits: ");
num = kb.nextInt();

{
// Read the amount in each deposit
deposits.readDollars(dollars);

// Convert Dollars to Yen.
deposits.dollarsToYen(dollars, yen);

// Display the amount in each deposit
deposits.displayData(dollars, yen);


}
}
}
 
Re: Help with error message- displayData(float[],float[],int) in DollarToYen cannot b

As I said in your other thread - we are not going to do your school work for you. And as I did there, I will give you a tip to get you in the right direction - look at your usage of the displayData method compared to its specification.
 
Re: Help with error message- displayData(float[],float[],int) in DollarToYen cannot b

As I said in your other thread - we are not going to do your school work for you. And as I did there, I will give you a tip to get you in the right direction - look at your usage of the displayData method compared to its specification.

And as such this thread is now locked.
 
Status
Not open for further replies.
Back
Top Bottom