Question

In: Computer Science

PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...

PUT IN JAVA PROGRAMMING

The StockB class: Design a class named StockB that contains the following properties:
• Name of company
• Number of shares owned
• Value of each share
• Total value of all shares
and the following operations:
• Acquire stock in a company
• Buy more shares of the same stock
• Sell stock
• Update the per-share value of a stock
• Display information about the holdings
• The StockB class should have the proper constructor(s).
• Create/write a client class (program) to test and use your StockB class. In the client program, you need to create objects of the StockB type and use those objects to perform some meaningful and valid stock transactions.

Solutions

Expert Solution

// StockB.java

public class StockB {
private String name;
private int sharesOwned;
private double perShareCost;
private double totalSharesCost;

/**
* @param name
* @param sharesOwned
* @param perShareCost
*/
public StockB(String name, int sharesOwned, double perShareCost) {
this.name = name;
this.sharesOwned = sharesOwned;
this.perShareCost = perShareCost;
this.totalSharesCost = sharesOwned*perShareCost;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the sharesOwned
*/
public int getSharesOwned() {
return sharesOwned;
}

/**
* @param sharesOwned
* the sharesOwned to set
*/
public void setSharesOwned(int sharesOwned) {
this.sharesOwned = sharesOwned;
}

/**
* @return the perShareCost
*/
public double getPerShareCost() {
return perShareCost;
}

/**
* @param perShareCost
* the perShareCost to set
*/
public void setPerShareCost(double perShareCost) {
this.perShareCost = perShareCost;
}

/**
* @return the totalSharesCost
*/
public double getTotalSharesCost() {
return totalSharesCost;
}

public void buyMoreStock(int shares)
{
this.sharesOwned+=shares;
}
public double sellStock(double sellPrice)
{
double totSellAmt=0.0;
totSellAmt=sellPrice*sharesOwned;
return totSellAmt;
}
public void updatePreShareCost(double newPrice)
{
this.perShareCost=newPrice;
}
public void displayInfo()
{
System.out.println("Stock Name :"+name);
System.out.println("Shares Owned :"+sharesOwned);
System.out.println("Per Share Price :$"+perShareCost);
System.out.println("Total Stock Cost :$"+totalSharesCost);
}
}

// Test.java

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
/*
* Declaring variables
*/
String name;
int noOfShares;
double perShareCost;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

// Getting the input entered by the user
System.out.print("Enter the Stock name :");
name = sc.next();
System.out.print("Enter no of shares owned :");
noOfShares = sc.nextInt();
System.out.print("Enter per share cost :$");
perShareCost = sc.nextDouble();
  
// Creating an instance of StockB class
StockB sb=new StockB(name, noOfShares, perShareCost);
sb.displayInfo();
  
// Getting the input entered by the user
System.out.print("Enter how many no of shares you want to buy again :");
int moreNoOfShares = sc.nextInt();
  
System.out.print("Enter new per share cost :$");
double newPerShareCost = sc.nextDouble();
  
// calling the methods
sb.buyMoreStock(moreNoOfShares);
sb.updatePreShareCost(newPerShareCost);
sb.displayInfo();
  
System.out.print("Enter selling price of per share:$");
double sellPrice = sc.nextDouble();
  
  
  
double totBuyingAmount=sb.getTotalSharesCost();
double totSellingAmount=sb.sellStock(sellPrice);
if(totSellingAmount>totBuyingAmount)
{
System.out.println("You got a profit of $"+(totSellingAmount-totBuyingAmount));
}
else
{
System.out.println("You got a loss of $"+(-(totSellingAmount-totBuyingAmount)));
}
  
  
}

}

output:

note: plzzz don't give dislike.....plzzz comment if you have any problem i will try to solve your problem.....plzzz give thumbs up i am in need....


Related Solutions

PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
Design a class named Account (put it in a package named accountspackages) with the following UML...
Design a class named Account (put it in a package named accountspackages) with the following UML diagram: Account -customerID: int -customerName: String -balance: double +setCustomerID(int): void +setCustomerName(String): void +setBalance(double):void +getCustomerID(): int +getCustomerName(): String +getBalance(): double +deposit(double): void +withdraw(double): void +printInformation():void The method withdraw(double) withdraws a specified amount from the account if the amount is less than or equal the balance, otherwise the method prints the message: Sorry! The account does not have sufficient funds. The method printInformation() prints:     the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT