need basic default constructor , and shoed an example too
s0. write the class Battery and it has
s1. a data for the battery capacity (that is similar to myBalance
of the BankAccount class.)
s2. a parameterized constructor: public Battery(double
amount)
s3. a drain mutator
s4. a change mutator
s5. a getRemainingCapacity accessor
Need to run this tester -
public class BatteryTester{
public static void main(String[] args) {
Battery autoBattery = new Battery(2000); //call default
constructor
System.out.println("1. battery capacity is " +
autoBattery.getRemainingCapacity() + " mAh.");
autoBattery.drain(100);
System.out.println("2. battery capacity is " +
autoBattery.getRemainingCapacity() + " mAh.");
autoBattery.charge();
System.out.println("3. battery capacity is " +
autoBattery.getRemainingCapacity() + " mAh.");
}
}
Similar Example-
/**
BankAccount class
*/
public class BankAccount { //s0
private double myBalance; //s1
public BankAccount() {myBalance = 0.00;}//s2
/**
My parameterizedconstructor
@param initial amount
*/
public BankAccount(double pAmount){myBalance =
pAmount;} //s3
/**
My mtator to deposit $$
@param deposit amount
*/
public void deposit(double pAmount){myBalance +=
pAmount;} //s4
/**
My accessor getBalnace
@return balance
*/
public double getBalance(){return myBalance;} //s5
If you have any problem with the code feel free to comment.
Program
class Battery {
// instance variables
private double capacity;
private double fullCapacity;// for holding full
capacity of battery
public Battery(double capacity) {//
constructor
this.capacity = capacity;
fullCapacity = capacity;
}
public void drain(double i) {
if (i < capacity)// checking if
the drainage value is less than the capacity
capacity -=
i;
else// turn capacity to zero
capacity =
0;
}
public void charge() {
capacity = fullCapacity;// charging
the capacity to fullCapacity
}
public double getRemainingCapacity() {
return capacity;// returning
capacity value
}
}
public class Test {// driver class
public static void main(String[] args) {
Battery autoBattery = new
Battery(2000); // call default constructor
System.out.println("1. battery
capacity is " + autoBattery.getRemainingCapacity() + "
mAh.");
autoBattery.drain(100);
System.out.println("2. battery
capacity is " + autoBattery.getRemainingCapacity() + "
mAh.");
autoBattery.charge();
System.out.println("3. battery
capacity is " + autoBattery.getRemainingCapacity() + "
mAh.");
}
}
Output

need basic default constructor , and shoed an example too s0. write the class Battery and...
Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount. 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) Here 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...
(JAVA NetBeans)
Write programs in java
Modify the textbook example
textbook example:
//BankAccount.java
import java.text.DecimalFormat;
public class BankAccount
{
public final DecimalFormat MONEY
= new DecimalFormat( "$#,##0.00" );
private double balance;
/** Default constructor
* sets balance to 0.0
*/
public BankAccount( )
{
balance = 0.0;
System.out.println( "In BankAccount default constructor" );
}
/** Overloaded constructor
* @param startBalance beginning balance
*/
public BankAccount( double startBalance )
{
if ( balance >= 0.0 )
balance = startBalance;
else
balance...
How would I alter this code to have the output to show the exceptions for not just the negative starting balance and negative interest rate but a negative deposit as well? Here is the class code for BankAccount: /** * This class simulates a bank account. */ public class BankAccount { private double balance; // Account balance private double interestRate; // Interest rate private double interest; // Interest earned /** * The constructor initializes the balance * and interestRate fields...
TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...
use C++
Based on the class declaration below, write out the declaration of its default constructor. class BankAccount { public: BankAccount(); // Sets balance to o BankAccount(double initial_balance); // Sets balance to initial_balance // Member functions omitted private: double balance;
Download BankAccount.java and BankAccountTester.java starting files and drag and drop them into your eclipse project. The BankAccount class declaration in file BankAccount.java is the blueprint of a BankAccount object. Check out the design of a BankAccount class. 1. In BankAccount.java class, all of the method stubs ( methods with a header but empty bodies ) are present to help you get started. Your task is to complete the method bodies. All comments in red in the diagram above indicates what...
C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (name & ID), keeps track of a user's available balance. // It also keeps track of how many transactions (deposits and/or withdrawals) are made. public class BankAccount { Private String name private String id; private double balance; private int numTransactions; // Please define method definitions for Accessors and Mutator functions below Private member variables string getName(); // No set...
Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task Create the following fields and methods for a Triangle class that extends the provided GeometricShapes class public class GeometricShapes { private String color = "red"; private boolean filled; private java.util.Date dateCreated; public GeometricShapes() { dateCreated = new java.util.Date(); } public GeometricShapes(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public void setColor (String color)...
java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...