Question

need basic default constructor , and shoed an example too s0. write the class Battery and...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
need basic default constructor , and shoed an example too s0. write the class Battery and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount....

    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.tex...

    (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...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    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...

  • How would I alter this code to have the output to show the exceptions for not...

    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...

  • use C++ Based on the class declaration below, write out the declaration of its default constructor....

    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;

  • C++ Please create the class named BankAccount with the following properties below: // The BankAccount class...

    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...

  • java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    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...

    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...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    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...

  • For this lab you must write a complete class in C++. This will be a class...

    For this lab you must write a complete class in C++. This will be a class named BankAccount. This class must have the following private variables: 1. accountHolderName : string 2. balance : double 3. interestRate: double This class must have the following public constructor: 1. BancAccount(name : string, balance : double, rate : double) This class must have the following public member functions: 1. getAccountHolderName() : string a. returns the account holders name 2. getBalance() : double a. returns...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT