Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Stock. java public class Stock { private String name; //name of company private

ID: 3722793 • Letter: S

Question

Stock. java

public class Stock {
   private String name; //name of company
   private String symbol; //Symbol of company on Stock Exchange
   private double price; //price per share
   private int shares; //number of shares
  
   /**
Constructor with name and symbol
   */
   public Stock(String newName, String newSymbol) {
name = newName;
symbol = newSymbol;
price = 0.0;
shares = 0;
   }
  
   /**
Get name of stock
@return name of stock
   */
   public String getName( ) {
return name;
   }
  
   /**
Get symbol of stock
@return symbol of stock
   */
   public String getSymbol( ) {
return symbol;
   }
  
   /**
Get price of stock
@return price of stock
   */
   public double getPrice( ) {
return price;
   }
  
   /**
Get number of shares
@return number of shares
   */
   public int getShares( ) {
return shares;
   }
  
   /**
Set price of shares
@param newPrice price of shares
   */
   public void setPrice(double newPrice) {
price = newPrice;
   }
  
   /**
Set number of shares
@param newShares number of shares
   */
   public void setShares(int newShares) {
shares = newShares;
   }
}

TradeStock.java


public class TradeStock {
   public static void main(String[] args) {
  
//declarations
Scanner in = new Scanner(System.in);
String name; //Name of stock
String symbol; //Symbol of stock
double price; //price per share of stock
int shares; //number of shares of stock

//Get name and symbol

System.out.print("Enter name of stock: ");
name = in.nextLine( );
System.out.print("Enter symbol of stock: ");
symbol = in.nextLine( );

}
//Instantiat Stock object with price and symbol
Stock myStock = new Stock(name, symbol);

//prompt user for price and set it
System.out.print("Enter price of stock: ");
price = in.nextDouble( );
while(in.nextDouble()) {
if (price < 0.0) {
System.out.println("Values must be positive, try again");
}
}
myStock.setPrice(price);

//prompt user for number of shares and set it
System.out.print("Enter number of shares: ");
shares = in.nextInt( );
myStock.setShares(shares);

//print stock info using spaces with each output
System.out.printf("%nName: %s%n" ,myStock.getName());
System.out.printf("Symbol: %s%n" , myStock.getSymbol());
System.out.printf("Price: %.2f%n", myStock.getPrice());
System.out.printf("Shares: %d%n" , myStock.getShares());
   }
}

Exercise 2 - Validating Stock Information Using Mutators (3 points) Must be completed during the lab period. 10. Make a copy of files for Exercise 1 into a new folder for Exercise2 11. Another strategy to validate user inputs is to test the attribute value in its corresponding mutator method. Use the rules for valid data from Exercise 1. If the value of the attribute is valid, save the new value in the appropriate attribute and return a value of true. If the value is invalid, do NOT change the attribute and return a value anexay: Ex2 jim java Tradestock of false. Do not print an error message in the mutator Janeway:Ex2 jims java TradeStock Enter name of stock: Apple Get symbol of stock: AP Invalid symbol of stock - Must be between 3 and 6 characters, inclusively Enter name of stock: Apple Get symbol of stock: APPL Enter price of stock: e Invalid Price Must be> e) method! Modify the muors nerayiexa ins dava Tradestock Enter name of stock: Apple Get symbol of stock: APPL Enter price of stock: 538.40 Enter number of shares: 1 Invalid Number of Shares Must be 1e and 1808 inclusively and a multiple of 1e for price and number of shares as described above. Janeway: Ex2 jn$ java TradeStock Enter name of stock: Apple Get symbol of stock: APPL 12. However, there are no mutators for name and symbo Enter price of stock: 53e.48 as thev are set in the constructor. While a constructor can contain if statements, there is no return value Enter number of shares: 28 Name: Apple Symbol: APPL Price: 530.48 Shares: 20 Janeway: Ex2 jims 13. Add a default constructor to the Stock class. Set the value of the name and symbol to the empty String 14. Add mutators for the name and symbol using the rules for valid data from Exercise 1. 15. Modify the code in the main method in the TradeStock class to use the default constructor to create an empty object. 16. Then use the mutators to validate the user-entered data. Test the return value to determine if the user input is valid. If the return value is false, print the same error messages as in Exercise 1 and terminate the program 17. The output of the program should be exactly the same as Exercise 1. Remember to test the program thoroughly with valid and invalid data

Explanation / Answer

TradeStock .java

import java.util.Scanner;

public class TradeStock {
   public static void main(String[] args) {

       // declarations
       Scanner in = new Scanner(System.in);
       String name; // Name of stock
       String symbol; // Symbol of stock
       double price; // price per share of stock
       int shares; // number of shares of stock

       // Get name and symbol

       System.out.print("Enter name of stock: ");
       name = in.nextLine();
       System.out.print("Enter symbol of stock: ");
       symbol = in.nextLine();

       if(symbol.length()<3 || symbol.length()>=6)
       {
           System.out.println("Invalid Symbol of Stock ! Must be between 3 to 6 letters (both inclusive)");
           System.exit(0);
       }
       // Calling Default Construtor
       Stock myStock = new Stock();

       //set Name and Symbol
       myStock.setName(name);
       myStock.setSymbol(symbol);
      
       // prompt user for price and set it
       System.out.print("Enter price of stock: ");
       price = in.nextDouble();
       while (true) {
           if (price < 0.0) {
               System.out.println("Values must be positive, try again");
               // prompt user for price and set it
               System.out.print("Enter price of stock: ");
               price = in.nextDouble();
               continue;
           } else {
               break;
           }
       }
       if(price<=0)
       {
           System.out.println("Invalid price -Must be >0");
           System.exit(0);
       }
       myStock.setPrice(price);

       // prompt user for number of shares and set it
       System.out.print("Enter number of shares: ");
       shares = in.nextInt();
       if(shares<10||shares>1000||shares%10!=0)
       {
           System.out.println("Invalid Number of Shares -Must be 10 and 1000 inclusively and a multiple of 10");
           System.exit(0);
       }
       myStock.setShares(shares);

       // print stock info using spaces with each output
       System.out.printf("%nName: %s%n", myStock.getName());
       System.out.printf("Symbol: %s%n", myStock.getSymbol());
       System.out.printf("Price: %.2f%n", myStock.getPrice());
       System.out.printf("Shares: %d%n", myStock.getShares());
   }
}

Stock.java

public class Stock {
   private String name; // name of company
   private String symbol; // Symbol of company on Stock Exchange
   private double price; // price per share
   private int shares; // number of shares

  
   //Default Constructor
   public Stock() {
   setName("");
   setSymbol("");
  
   }
   /**
   * Constructor with name and symbol
   */
   public Stock(String newName, String newSymbol) {
       name = newName;
       symbol = newSymbol;
       price = 0.0;
       shares = 0;
   }

   /**
   * Get name of stock
   *
   * @return name of stock
   */
   public String getName() {
       return name;
   }

   /**
   * Get symbol of stock
   *
   * @return symbol of stock
   */
   public String getSymbol() {
       return symbol;
   }
  

   public void setName(String name) {
       this.name = name;
   }
   public void setSymbol(String symbol) {
       this.symbol = symbol;
   }
   /**
   * Get price of stock
   *
   * @return price of stock
   */
   public double getPrice() {
       return price;
   }

   /**
   * Get number of shares
   *
   * @return number of shares
   */
   public int getShares() {
       return shares;
   }

   /**
   * Set price of shares
   *
   * @param newPrice
   *            price of shares
   */
   public void setPrice(double newPrice) {
       price = newPrice;
   }

   /**
   * Set number of shares
   *
   * @param newShares
   *            number of shares
   */
   public void setShares(int newShares) {
       shares = newShares;
   }
}

output:

Enter name of stock: Apple
Enter symbol of stock: a
Invalid Symbol of Stock ! Must be between 3 to 6 letters (both inclusive)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote