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

# Thread: Re: Help writing the IF statement for Y/N

1. ## Re: Help writing the IF statement for Y/N

I have a similar assignment that I am having problems with:

Many public schools in California are experiencing a shortage of funds for special student activities.
Some schools started Jog-A-Thons as a fundraiser. Each student gathers sponsors that pledge a particular
amount per lap jogged. Only 6 students are participating in the Jog-a-thon, but each student can have many
pledges. Many people also support the Jog-A-Thon by donating just \$5.00 as a fixed amount, regardless of the
laps. Write a program that tracks this activity.

Create a class called Student with 6 data members (2 should be static).

1. Student-number is an integer (1-6).
2. Laps is an integer that stores the number of laps run.
3. Pledge is a double that stores the amount of money pledged per lap.
4. Amount is a double holding the money earned (laps times pledge) for one pledge.
5. Totals is an array of 7 doubles that store the grand totals earned by donations and each of the 6 students
for all of their pledges.
6. Names is an array of 7 strings that are initialized to “Donation” and the 6 student names: Tom, Ryan,
Mandy, Jacob, Devon, and Jessi.

Create six (6) methods:

1. opening() displays the school name: Grass Valley Elementary, and “Jog-A-Thon”.
2. a no-argument constructor that initializes the number=0, laps=1, and pledge=5.00.
3. getdata() prompts for the student number, laps, and pledge.
4. gettotals() calculates the amount earned and updates the grand totals by student.
5. toString() displays all data, except the grand totals, including the student name (or “Donation”).
6. showTotals() to display the grand totals by student number, showing the student name (or “Donation”),
and grand total for that student. This function must be useable with or without an instantiated object.

Create a “test2.java” class with just the main() function. Create an array of 50 Students. The main() function
displays the opening and then repeats until the user answers ‘N’ to a “Enter another pledge(y/n): “ prompt
allowing upper or lower case ‘y’ or ‘n’. Each student may have many pledges, but do not allow more than 50
total pledges (your array size). Within the loop instantiate a Student using the array of Students, and prompt
again if the pledge is a “Donation” allowing upper or lower case ‘y’ or ‘n’. If the pledge is a donation, do not
call the getdata() method. (a “Donation” is student 0, always only 1 lap, for a pledge of 5.00, your constructor
takes care of this.) If the student is not a “Donation” call the getdata() method. Call the other methods for all
students, including “Donation”. When the user finishes entering all pledges, but before ending the program,
display a report showing all pledges and donations, and also call the showTotals() method showing grand totals
by student (including donations).

--- Update ---

/* this is what I have done so far */

```<

import java.util.Scanner;
import java.text.*;

public class Student
{
private static double[] Total = new double[7];
private static String[] Names = {"Donation","Tom","Ryan","Mandy","Jacob","Devon","Jessi"};
private static	Scanner keyboard = new Scanner (System.in);
private static int Student_number, Laps;
double Amount, Pledge, Grand_Total, Totals;
String name, s;
char choice;

NumberFormat dollarForm = NumberFormat. getCurrencyInstance();

public Student()
{
s = "";
name = "";
Student_number = 0;
Pledge = 5.00;
Laps = 1;
}

public static void opening()
{
System.out.println("\n==================================================================\n");
System.out.println("\n               Grass Valley Elementary Jog-A-Thon! \n");
System.out.println("\n==================================================================\n");
}

public void getdata()
{
for (int i=0; i<7; i++) System.out.println(i+1 + ".  " + Names[i]);
System.out.print("\nEnter a donation? (y/n): ");

s = keyboard.nextLine();
choice = s.charAt(0);

switch (choice)
{
case 'y':
case 'Y':
Amount = Pledge * Laps;
break;
case 'n':
case 'N':
System.out.print("\nPlease enter a student's number: ");
Student_number = keyboard.nextInt();

System.out.print("\nPlease enter number of Laps: ");
Laps = keyboard.nextInt();

Pledge = keyboard.nextDouble();
break;
default:
System.out.println("No student selected\n");
}
}

public void gettotals()
{
Amount = Pledge * Laps;
Totals += Amount;
}

public String toString()
{
name = Names[Student_number-1];
String display = ("\nName = " + name + "\nLaps = " + Laps + "\nPledge = "
+ dollarForm.format(Pledge) + "\nAmount = " + dollarForm.format(Amount));
return display;
}

public void showTotals()
{
Grand_Total = Totals;
System.out.println("\nName = " + name + "\nGrand Total = " + dollarForm.format(Grand_Total) + "\n");
}

}

>```

--- Update ---

// and here is the rest:

```<

import java.util.Scanner;
import java.text.*;

public class test2
{
public static void main(String[] args)
{
final int Max = 50;
String another = "Y";
int t = 0;
Scanner Keyboard = new Scanner (System.in);

Student.opening();

do
{
t++;

System.out.print("\nEnter another pledge? (y/n): ");
another = Keyboard.nextLine();
}
while (Character.toUpperCase(another.charAt(0)) == 'Y' && t < Max);

for (int i=0; i < t; i++)

}
}

>```

--- Update ---

Any advice or suggestions is much appreciated. Thanks in advance for providing newbs like myself with a forum to ask for help without getting flamed or trolled! If I'm able to, I will definitely try to "pay it forward".

2. ## Re: Help writing the IF statement for Y/N

NOTE: Post moved to own thread.

Do you have any specific questions or problems with the code?

One minor problem I see is the formatting is not consistent. Indentations within {}s is poor making it hard to understand the logic.

3. ## Re: Help writing the IF statement for Y/N

My problem is with the following step:

"5. Totals is an array of 7 doubles that store the grand totals earned by donations and each of the 6 students for all of their pledges."

My previous assignment was a foreign exchange menu where the conversion rates were in an array. I understood how to store those values because the conversion rate for each country was fixed.

But I'm not sure how to store an array of separate grand totals for donations & each student when the totals are continuously accumulating.

--- Update ---

Should I change the following line of code:
```<
private static double[] Total = new double[7];
>```
to something resembling this:
```<
private static double[] Total = new double[Totals[Student_number-1]];
>```

4. ## Re: Help writing the IF statement for Y/N

how to store an array of separate grand totals for donations & each student
Are you asking how to update an element in an array?
anArray[idx] = <new value for the element>;

Or are you asking how to get the correct size to use when defining an array?
Your posted sample code gets the size from an element in the Totals array.