Java Programming- Don\'t use break or any advanced way, make it simple Goals In
ID: 3806405 • Letter: J
Question
Java Programming- Don't use break or any advanced way, make it simple
Goals
In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to
Write classes that use arrays and ArrayLists of objects as instance variables
Write methods that process arrays and ArrayLists of objects
Write getter and setter methods for instance variables
Write methods using object parameters and primitive types
Question-
This program uses the StockHolding class that we wrote in the lab for Writing Classes.
Write a class PortfolioList. A PortfolioList object maintains a portfolio of StockHolding objects. A PortfolioList
keeps an ArrayList<StockHolding>
has a no-argument constructor
Explanation / Answer
PortfolioDriver.java
public class PortfolioDriver {
public static void main(String[] args) {
PortfolioList p1 = new PortfolioList();
PortfolioList p2 = new PortfolioList();
p1.add(new StockHolding("AAPL",3, 100 ));
p1.add(new StockHolding("AAPP",2, 200 ));
p2.add(new StockHolding("ABCD",3, 500 ));
p2.add(new StockHolding("EFGH",6, 600 ));
p2.add(new StockHolding("IJKL",7, 700 ));
p2.add(new StockHolding("MNOP",8, 800 ));
System.out.println(p1.toString());
System.out.println("--------------------------------");
System.out.println(p2.toString());
StockHolding s = p1.find("AAPP");
System.out.println(s.toString());
StockHolding s1 = p2.find("IJKL");
System.out.println(s1.toString());
System.out.println("-------Removed second portfolio all objects---------------");
p2.remove("ABCD");
p2.remove("EFGH");
p2.remove("IJKL");
p2.remove("MNOP");
System.out.println(p2.toString());
}
}
PortfolioList.java
import java.util.ArrayList;
public class PortfolioList {
ArrayList<StockHolding> list = new ArrayList<StockHolding>();
public PortfolioList(){
}
public void add(StockHolding stock){
list.add(stock);
}
public void remove(String ticker){
for(StockHolding s: list){
if(s.getTicker().equals(ticker)){
list.remove(s);
}
break;
}
}
public StockHolding find(String ticker) {
for(StockHolding s: list){
if(s.getTicker().equals(ticker)){
return s;
}
}
return null;
}
public String toString() {
String str = "";
for(StockHolding s: list){
str = str + s.toString()+" ";
}
return str;
}
}
StockHolding.java
public class StockHolding {
private String ticker ;
private int shares ;
private double initialSharePrice;
private double currentSharePrice;
public StockHolding(String ticker, int numberShares, double initialPrice) {
this.ticker = ticker;
this.shares = numberShares;
this.initialSharePrice = initialPrice;
}
public String getTicker() {
return ticker;
}
public void setTicker(String ticker) {
this.ticker = ticker;
}
public int getShares() {
return shares;
}
public void setShares(int shares) {
this.shares = shares;
}
public double getInitialSharePrice() {
return initialSharePrice;
}
public void setInitialSharePrice(double initialSharePrice) {
this.initialSharePrice = initialSharePrice;
}
public double getCurrentSharePrice() {
return currentSharePrice;
}
public void setCurrentSharePrice(double currentSharePrice) {
this.currentSharePrice = currentSharePrice;
}
public double getInitialCost() {
return shares * initialSharePrice;
}
public double getCurrentValue() {
return shares * currentSharePrice;
}
public double getCurrentProfit() {
return shares * (currentSharePrice-initialSharePrice);
}
public String toString() {
return "Stock " + getTicker() + ", " + getShares() + ", shares bought at " + getInitialSharePrice()
+ ", current price " + getCurrentSharePrice();
}
}
Output:
Stock AAPL, 3, shares bought at 100.0, current price 0.0
Stock AAPP, 2, shares bought at 200.0, current price 0.0
--------------------------------
Stock ABCD, 3, shares bought at 500.0, current price 0.0
Stock EFGH, 6, shares bought at 600.0, current price 0.0
Stock IJKL, 7, shares bought at 700.0, current price 0.0
Stock MNOP, 8, shares bought at 800.0, current price 0.0
Stock AAPP, 2, shares bought at 200.0, current price 0.0
Stock IJKL, 7, shares bought at 700.0, current price 0.0
-------Removed second portfolio all objects---------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.