1. Create a class called Stock with instance variables symbol, totalStock, curre
ID: 3836956 • Letter: 1
Question
1. Create a class called Stock with instance variables symbol, totalStock, currentPrice and purchasedPrice. Create a constructor for the class. Include all the accessor and mutator methods. Include the toString and equals method. Also include two other methods called lossOrGain to calculate the amount of the money that the person lost or made. Also include a method call sell that accepts the number of the shares to sell and returns the amount of the money that the person is going to receive. Make sure to create a driver class as well.
2. Create a class called BMI with the following instance variables: name, age, weight(in pounds), and height. Create a constructor for your class so that you can create objects of type BMI. Include all the methods such as accessors, mutators, toString, equals. Also include a method called getBMI which calculates the BMI for the person and returns the BMI. use the following formula. You need to conver the height to meter and the weigh to kilogram. 1 kg = .45 pounds
1 meter = .0254 inches
Bmi = (weight in kg * heigh(in meter)) / (height in meter)* (height in meter)
Also include a method called getStatus to return a String representing the person’s status
Bmi < 16 returns “seriously underweight
Bmi < 18 returns “underweight”
Bmi < 24 “returns normal weight”
Bmi < 29 returns “overweight”
Bmi < 35 returns “seriously overweight”
Anything else returns “gravely overweight”
Also include a driver class to create couple of the objects and output the bmi and the status for each person.
3. Create a class called Account with the instance variables name, id and balance. Include a constructor, accessors, mutators, toString and equals method. Also include a method called transfer so the money can be transferred from one account to another account. Write a method called close accounts and closes the account and return all the money in the account. Create a method called interest that get the interest rate as a parameter and returns the amount of the interest on the balance. Make sure to create a driver class to test your account class.
Explanation / Answer
/* IMPORTANT: class must not be public. */
/*
* uncomment this if you want to read input.
import java.io.BufferedReader;
import java.io.InputStreamReader;
*/
class Stock {
private String symbol ;
private int totalStock ;
private double currentPrice ;
private double purchasedPrice ;
public Stock(String currency , int totalStock , double purchasedPrice , double currentPrice ) {
symbol = currency ;
this.totalStock = totalStock ;
this.currentPrice = currentPrice ;
this.purchasedPrice = purchasedPrice ;
}
public String getSymbol()
{
return symbol;
}
public int getTotalStock()
{
return totalStock;
}
public double getCurrentPrice()
{
return currentPrice;
}
public double getpurchasedPrice()
{
return purchasedPrice;
}
public void setSymbol(String currency)
{
symbol = currency;
}
public void setTotalStock(int totalStock)
{
this.totalStock = totalStock ;
}
public void setCurrentPrice(double currentPrice)
{
this.currentPrice = currentPrice ;
}
public void setpurchasedPrice(double purchasedPrice)
{
this.purchasedPrice = purchasedPrice;
}
public String toString()
{
return "Total Stock is "+ totalStock + ", purchase price was " + symbol + purchasedPrice + ", the current price is " + symbol +currentPrice ;
}
public Boolean equals(Stock s)
{
if(this.symbol.equals(s.symbol)&&this.totalStock==s.totalStock&&this.purchasedPrice==s.purchasedPrice&&this.currentPrice==s.currentPrice)
return true ;
else
return false;
}
public void lossOrGain()
{
double diff = totalStock*currentPrice - totalStock*purchasedPrice ;
if( diff < 0 )
{
diff *= -1 ;
System.out.println("The holder lost "+symbol+diff);
}
else
{
System.out.println("The holder gained "+symbol+diff);
}
}
public double sell(int s)
{
totalStock -= s ;
return s*currentPrice ;
}
}
class MainClass {
public static void main(String args[] ) throws Exception {
Stock s1 = new Stock("$",100,5,6);
Stock s2 = new Stock("$",100,5,6);
Stock s3 = new Stock("$",100,6,5);
System.out.println(s1.toString());
if(s1.equals(s2))
System.out.println("Are Equal");
if(!s1.equals(s3))
System.out.println("Are Not Equal");
s1.lossOrGain();
s3.lossOrGain();
System.out.println("The sell gives an amount "+s3.sell(30));
System.out.println("After sell the stock left for s3 is "+s3.getTotalStock());
s3.setCurrentPrice(10);
System.out.println("The current price for s3 is "+s3.getCurrentPrice());
s3.lossOrGain();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.