Beginner Java Programming; Finish the StockExchangeArray class which manages Sto
ID: 3915222 • Letter: B
Question
Beginner Java Programming;
Finish the StockExchangeArray class which manages Stock objects. Its methods manipulate an array (not an ArrayList).
StockExchangeArray has an instance variable Stock[]. Call the instance variable stocks. It has a constructor that takes an array of Stocks as a parameter and initializes the instance variable with it.
It has methods:
public double totalValue() - finds the total value of all the Stocks in this StockExchangeArray.
public void swap(int index1, int index2) - swaps the element at index1 with the element at index2. If either index is out of bounds, do not change anything.
public String cheapest() - gets the symbol for the Stock with the lowest price per share. If more than one Stock has the same low price, return the symbol for the first.
public String toString() - gets a string representation of the array - provided. (This is why the instance variable must be called stocks.)
(You can assume the array is not empty.)
You can check your solution here: http://www.codecheck.it/files/1803200219bj5pl3fezo00t45evvmo9chqz
Explanation / Answer
import java.util.*;
class Stock
{
private String symbol;
private double price;
public Stock(String theSymbol, double thePrice)
{
symbol = theSymbol;
price = thePrice;
}
public double getPrice()
{
return price;
}
public String getSymbol()
{
return symbol;
}
public String toString()
{
String s = getClass().getName() + "[symbol=" + symbol
+ ",price=" + price + "]";
return s;
}
}
class StockExchangeArray{
Stock Stock[];
public StockExchangeArray(Stock arr[]) {
int size=arr.length;
Stock=new Stock[size];
for(int i=0;i<size;i++)
Stock[i]=arr[i];
}
public double totalValue() {
double sum=0;
for(int i=0;i<Stock.length;i++) {
sum+=Stock[i].getPrice();
}
return sum;
}
public void swap(int index1,int index2) {
int length=Stock.length;
if(index1>=length||index1<0||index2>=length||index2<0) {
System.out.println("index out of bound");
return;
}
Stock temp=Stock[index1];
Stock[index1]=Stock[index2];
Stock[index2]=temp;
}
public String cheapest() {
int length=Stock.length,min_index;
double min=Stock[0].getPrice();
min_index=0;
for(int i=1;i<length;i++) {
if(min>Stock[i].getPrice()) {
min=Stock[i].getPrice();
min_index=i;}
}
return Stock[min_index].getSymbol();
}
public String toString() {
int length=Stock.length;
String str="";
for(int i=0;i<length;i++)
str += "[symbol=" + Stock[i].getSymbol()+" "
+ ",price=" + Stock[i].getPrice() + "] ";
return str;
}
public static class StockExchange {
public static void main(String[] args)
{
Stock[] stocks = new Stock[4];
stocks[0] = new Stock("AAPL", 176.82);
stocks[1] = new Stock("FB", 180.40);
stocks[2] = new Stock("AMZN", 1523.61);
stocks[3] = new Stock("GOOGL", 1094.76);
StockExchangeArray market = new StockExchangeArray(stocks);
System.out.println("Cheapest: " + market.cheapest());
System.out.println("Expected: AAPL");
System.out.println("Total value: " + market.totalValue());
System.out.println("Expected: 2975.59");
stocks = new Stock[5];
stocks[0] = new Stock("AAPL", 176.82);
stocks[1] = new Stock("FB", 180.40);
stocks[2] = new Stock("AMZN", 1523.61);
stocks[3] = new Stock("GOOGL", 1094.76);
stocks[4] = new Stock("MSFT", 93.64);
market = new StockExchangeArray(stocks);
System.out.println("Cheapest: " + market.cheapest());
System.out.println("Expected: MSFT");
System.out.println("Total value: " + market.totalValue());
System.out.println("Expected: 3069.23");
//test swap
market.swap(0, 3);
System.out.println(market);
System.out.println("Expected: [Stock[symbol=GOOGL,price=1094.76], Stock[symbol=FB,price=180.4], Stock[symbol=AMZN,price=1523.61], Stock[symbol=AAPL,price=176.82], Stock[symbol=MSFT,price=93.64]]");
//swap bad index. Should have no effect
market.swap(-1, 1);
market.swap(2, -1);
market.swap(1, 5);
market.swap(5, 2);
System.out.println(market);
System.out.println("Expected: [Stock[symbol=GOOGL,price=1094.76], Stock[symbol=FB,price=180.4], Stock[symbol=AMZN,price=1523.61], Stock[symbol=AAPL,price=176.82], Stock[symbol=MSFT,price=93.64]]");
}
}
for any query please comment.
please upvote if find it helpful.
Thank you!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.