Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab Exercise 5.4 Instructions: Code a program using the methods you created in LE 5.3. Use the sample output (below) to determine the logic of

Lab Exercise 5.4 Instructions: Code a program using the methods you created in LE 5.3. Use the sample output (below) to determine the logic of your method calls in the main(). Consider the following:

1. Re-read LE 5.3 instructions to determine the class-level variables (fields).

2. Use the data in the sample output to test your LE 5.4.

3. Be aware of invoking an empty call to nextLine() after a call to nextInt().

4. In the main(),

a. call the method in LE 5.3s 1a. Store the return value in a variable called year.

1) Your name for LE 5.3 1a method? ____________________________________

b. code a for loop that

1) calls the method that ascertains fundraising, which is LE 5.3's 1b, as an argument for the method that sets the earnings, which is LE 5.3's 1c.

a) Your name for LE 5.3 1b method? ____________________________________

b) Your name for LE 5.3 1c method? ____________________________________

c. use the method that sets the cost of the senior trip, which is LE 5.3's 1d, as an argument for the method that displays the senior trip information, which is LE 5.3's 1e.

1) Your name for LE 5.3 1d method? ____________________________________

2) Your name for LE 5.3 1e method? ____________________________________

5. The format for method calls is: E.g.: method1(method2(arg1), method3(arg1));

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

A. A method that prompts for the grade level and returns it as an integer. Print the message Invalid grade level! Try again. if the grade level is out of range. Re-prompt until a valid entry is made. Declare a local year variable to hold 1 for Freshmen, 2 for Sophomore, etc.

import java.util.Scanner;

public class LE_5_3 {

private static Scanner sc = new Scanner(System.in);

private static int gradeLevel()

{

int gradeValue = 0;

while(true)

{

System.out.printf(" 1. Freshmem 2. Sophomore 3. Junior 4. " + " Senior Enter the number for your grade level: ");

gradeValue = sc.nextInt();

sc.nextLine();

if(gradeValue < 0 || gradeValue > 4)

{

System.out.printf(" Invalid grade level. Try Again.");

}

else break;

}

return gradeValue;

}

B. A value-receiving method that accepts a counter value, and asks if there was a fundraiser for the grade level and returns that answer.

Prompt: Did you hold a fundraiser in your Xxxxxxxxx year?

private static boolean fundRaiser(int level)

{

String year = "";

switch(level)

{

case 1: year = "Freshmen"; break;

case 2: year = "Sophomore"; break;

case 3: year = "Junior"; break;

case 4: year = "Senior"; break;

}

System.out.printf(" Did you hold your found raiser in your %s year? [Y/N]: ", year);

String response = sc.nextLine();

return (response.equalsIgnoreCase("y"));

C.A value-receiving method that accepts the answer from 1b, prompts for the grade level earnings and returns either what was earned or zero.

Prompt: Enter your Xxxxxxxxx fundraiser earnings:

private static double gradeLevelEarning(boolean isFundRaiser)

{

double earning = 0;

if(isFundRaiser)

{

System.out.printf(" Enter your fund raiser earnings: "); // prompt for input.

earning = sc.nextDouble(); // read the earnings.

sc.nextLine(); // to consume the new line character.

}

return earning;

D. A method that prompts and returns the cost of a senior trip:

private static double seniorTripCost()

{

System.out.printf(" Enter the cost of senior trip: "); // prompt

double seniorTripCost = sc.nextDouble(); // read

sc.nextLine(); // to consume the new line character.

return seniorTripCost;

}

E. A method that accepts the cost of a senior trip and the total earnings to date and calculates the difference and prints it:

EARNINGS REPORT FOR SENIOR TRIP

Cost of Your Senior Trip:$ZZZ,ZZ9.99

Earnings Year-To-Date:ZZZ,ZZ9.99

Difference:$ZZZ,ZZ9.99

private static void results(double seniorTripCost, double totalEarningsToDate)

{

System.out.printf(" EARNINGS REPORT FOR SENIOR TRIP");

System.out.printf(" Cost of Your Senior Trip: $%f", seniorTripCost);

System.out.printf(" Earnings Year-To-Date: $%f", totalEarningsToDate);

double difference = totalEarningsToDate-seniorTripCost;

System.out.printf(" Difference: $%f", difference);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Derive expressions for the rates of forward and reverse reactions?

Answered: 1 week ago

Question

Write an expression for half-life and explain it with a diagram.

Answered: 1 week ago

Question

What do you mean by underwriting of shares ?

Answered: 1 week ago

Question

Define "Rights Issue".

Answered: 1 week ago