Question
solve two parts: Part 1: The Portfolio Object Class This is based on Programming Project P8.13 in Big Java: Late Objects As part of a
solve two parts:
Part 1: The Portfolio Object Class
This is based on Programming Project P8.13 in Big Java: Late Objects
As part of a NetBeans Java application project, implement a class Portfolio. This class has two objects (data fields), checking and savings, of the type BankAccount that was developed in How To 8.1 (BankAccount.java).
Declare private data fields for the checking and savings BankAccounts.
Implement a default (no-arg) constructor that creates the BankAccount object for each data field.
Implement four methods:
public void deposit(double amount, String account)
public void withdraw(double amount, String account)
public void transfer(double amount, String account)
public double getBalance(String account): returns the current account balance
public String toString(): returns the Portfolio as a String (see test program and output, below)
In each of these methods, the account string is "S" or "C":
For the deposit or withdrawal, it indicates which account is affected.
For a transfer, it indicates the account from which the money is taken; the money is automatically transferred to the other account.
You should, of course, also create a test program as the main method of your application. One suggestion:
public static void main(String[] args) { Portfolio folio = new Portfolio(); // start with a new Portfolio folio.deposit(10000, "S"); // deposit $10,000.00 in savings folio.transfer(1500, "S"); // transfer $1,500.00 to checking // get the savings balance (should be $8,500.00) System.out.println("\tsavings: " + folio.getBalance("S")); // get the checking balance (should be $1,500.00) System.out.println("\tchecking: " + folio.getBalance("C")); System.out.println("---------------------------------------------------------------------"); System.out.println(folio); // display the Portfolio (calls toString()) System.out.println("---------------------------------------------------------------------"); folio.withdraw(500, "C"); // withdraw $500.00 from checking folio.transfer(1000, "S"); // transfer $1,000.00 to checking folio.deposit(3000, "S"); // deposit $3,000.00 in savings // get the savings balance (should be $10,500.00) System.out.println("\tsavings: " + folio.getBalance("S")); // get the checking balance (should be $2,000.00) System.out.println("\tchecking: " + folio.getBalance("C")); System.out.println("---------------------------------------------------------------------"); System.out.println(folio); // display the Portfolio (calls toString()) } // end main
run: savings: 8500.0 checking: 1500.0 --------------------------------------------------------------------- Savings: 8500.0 Checking: 1500.0 --------------------------------------------------------------------- savings: 10500.0 checking: 2000.0 --------------------------------------------------------------------- Savings: 10500.0 Checking: 2000.0 BUILD SUCCESSFUL
Design Note: For those who wish to make their output look more "currency-like" (e.g., $8,500.00 instead of 8500.0), I suggest you look at the Java object class java.text.DecimalFormat.
Part 2: The Portfolio Object Class
This is based on Programming Projects P8.20 and P8.21 in Big Java: Late Objects
1. The colored bands on the top-most resistor shown in the photo below indicate a resistance of 6.2 k 5 percent. The resistor tolerance of 5 percent indicates the acceptable variation in the resistance. A 6.2 k 5 percent resistor could have a resistance as small as 5.89 k or as large as 6.51 k. We say that 6.2 k is the nominal value of the resistance and that the actual value of the resistance can be any value between 5.89 k and 6.51 k.
As part of a NetBeans Java application project, implement a class Resistor. Make sure to include:
data fields for the nominal resistance, tolerance, and actual resistance
a single constructor that accepts values for the nominal resistance and tolerance and then determines the actual value randomly
public getter methods for the data fields (nominal resistance, tolerance, and the actual resistance)
Write a main method for the program that demonstrates that the class works properly by displaying actual resistances for ten 330 10 percent resistors.
Then, once your Resistor class is fully tested and working ...
2. Create a new NetBeans Java application project, copy your Resistor class into it, and then add a method to the Resistor class that returns a description of the color bands for the resistance and tolerance. A resistor has four color bands:
The first band is the first significant digit of the actual resistance value.
The second band is the second significant digit of the actual resistance value.
The third band is the decimal multiplier.
The fourth band indicates the tolerance.
For example (using the values from the table as a key), a resistor with red, violet, green, and gold bands (left to right) will have 2 as the first digit, 7 as the second digit, a multiplier of 105, and a tolerance of 5 percent, for a resistance of 2,700 k, plus or minus 5 percent.
Use the following main method to demonstrate this modified Resistor class:
public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Resistance: "); double resistance = in.nextDouble(); System.out.print("Tolerance in percent: "); double tolerance= in.nextDouble(); Resistor r = new Resistor(resistance, tolerance); System.out.print(r.colorBands(5)); // multiplier of 10^5 } // end main
Note that in the sample run shown below, user input is shown in color.
run: Resistance: 6.2 Tolerance in percent: 5 gray yellow yellow gold BUILD SUCCESSFUL
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started