Hello, I need help with my java study guide! Hoping someone could provide exampl
ID: 3833849 • Letter: H
Question
Hello, I need help with my java study guide! Hoping someone could provide examples for each question.
ArrayLists –
Be able to use built-in ArrayList methods to identify, locate and access a given element.
Inheritance –
You’ll be asked to write code to develop an inheritance hierarchy (super class / sub class.)
For example:
Given the Savings sub-class that follows, create the super-class named ‘Account’:
Make the Account class abstract.
Data variables in the Account class are ‘name’ (type String which can be accessed
directly by the sub-class but not by the outside world,) and ‘balance’ (type double which
cannot be accessed by any sub-class.)
Add the methods that are referenced in the Savings sub-class.
Make it so that the ‘deductFees’ method is known to the Account class, but the
implementation for deductFees is required to be in the Savings class.
public class Savings extends Account {
public Savings(String name, double balance) {
super(name, balance); }
public void deductFees() {
setBalance(getBalance() * .50); }
public String toString() {
// Show the account type, depositor's name and balance...
return "Savings account for " + super.toString(); }
}
Explanation / Answer
I am trying to explain ArrayLists using a simple program:
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class BankLocationDAOImpl implements BankLocationDAO{
@SuppressWarnings("unchecked")
public List<BankLocation> getListOfBanks() throws Exception {
SessionFactory sessionFactory;
Session session = null;
List<BankLocation> list = null;
List<BankLocationEntity> entityList = null;
BankLocation locationTO = null;
try {
sessionFactory = HibernateUtility.createSessionFactory();
session = sessionFactory.openSession();
session.beginTransaction();
Query query=session.createQuery("Select bl from BankLocationEntity bl");
entityList =query.list();
list = new ArrayList<BankLocation>();
for (BankLocationEntity bankLocationEntity : entityList)
{
locationTO = new BankLocation();
locationTO.setLocationId(bankLocationEntity.getLocationId());
locationTO.setStateName(bankLocationEntity.getStateName());
locationTO.setBranchAddress(bankLocationEntity
.getBranchAddress());
list.add(locationTO);
}
session.getTransaction().commit();
return list;
}
catch (Exception e)
{
Logger.logError(this.getClass().getName(),"getListOfBanks", e.getMessage());
throw new Exception("DAO.TECHNICAL_ERROR");
}
finally
{
session.close();
}
}
}
Now let us discuss the concept of Inheritamce
abstract class Account {
private double balance;
public String name;
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public abstract void deductFees();
public Account(String name, double balance) {
this.balance = balance;
this.name = name;
}
@Override
public String toString() {
return "name:" + name + ", balance:" + balance;
}
}
public class Savings extends Account {
public Savings(String name, double balance) {
super(name, balance);
}
public void deductFees() {
setBalance(getBalance() * .50);
}
public String toString() {
// Show the account type, depositor's name and balance...
return "Savings account for " + super.toString();
}
public static void main(String[] args) {
Savings s=new Savings("Surya",15000.0);
s.deductFees();
System.out.println(s.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.