This lab is designed to give you practice working with programmer defined method
ID: 3633628 • Letter: T
Question
This lab is designed to give you practice working with programmer defined methods. You will write a method named validate to help a local banker. Your method will need to utilize two input parameters and the method's return value (for its output).Getting Started
To start this exercise, you should
Open eclipse and start a new Java project named Lab09
Add a Class (named Lab09) to this project.
Cut the program from this document and paste (replacing everything) in the Eclipse editor window.
Lab09.Java
/*
Comment block here ...
*/
import java.util.Scanner;
public class Lab09
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
double curBal = 45.32;
double amount;
System.out.print("Please enter a amount to update account by $");
amount = stdIn.nextDouble();
System.out.print(" ");
System.out.print("Customer's balance (before transaction) = $");
System.out.println(curBal + " ");
System.out.print("Requested update amount = $");
System.out.println(amount + " ");
double actAmount;
actAmount = validate(curBal, amount);
curBal += actAmount;
System.out.print("Actual update amount = $");
System.out.println(actAmount + " ");
System.out.print("Customer's balance (after transaction) = $");
System.out.println(curBal + " ");
System.out.println(" Thank you and good-bye! ");
}
public static double validate(double bal, double amount)
{
// Write your code here ...
return 0.0; // actual return value here (NOT 0.0)
}
}
Problem Description
- Read through the existing code to understand what it does and what is left for you to do.
- Compile and run the program as-is, to verify that you understand what it does and what is left for you to do.
- Write the missing code in the Java program program provided so that it satisfies the following specification:
For this part of the assignment you will be using the Lab09.java file that you copied using the command above.
- A local banker has asked you to write a method named validate which takes :
-a customer's current balance
-the amount of money to update the account by :
- a positive dollar amount is a deposit
- a negative dollar amount is a withdraw
as input parameters, and then returns
for deposits: the deposit amount is simply returned by the function.
for withdraws: the balance must not become negative. If a withdraw is requested for more than the current balance then the amount (which would zero the balance if withdrawn) is returned by the method. Otherwise the withdraw amount is simply returned by the method.
Explanation / Answer
import java.util.Scanner;
public class Lab09 {
/**
* @param args
*/
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
double curBal = 45.32;
double amount;
System.out.print("Please enter a amount to update account by $");
amount = stdIn.nextDouble();
System.out.print(" ");
System.out.print("Customer's balance (before transaction) = $");
System.out.println(curBal + " ");
System.out.print("Requested update amount = $");
System.out.println(amount + " ");
double actAmount;
actAmount = validate(curBal, amount);
curBal += actAmount;
System.out.print("Actual update amount = $");
System.out.println(actAmount + " ");
System.out.print("Customer's balance (after transaction) = $");
System.out.println(curBal + " ");
System.out.println(" Thank you and good-bye! ");
}
public static double validate(double bal, double amount)
{
//deposit
if(amount > 0)
{
return bal + amount;
}
//withdraw
if(bal < amount)
{
return bal;
}
return bal-amount;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.