Question

In: Computer Science

in java Write a class named “Stock” to model a stock. The properties and methods of...

in java

Write a class named “Stock” to model a stock. The properties and methods of the class are shown in figure below. The method “ percentage ” computes the percentage of the change of the current price vs the previous closing price. Write a program to test the “Stock” class. In the program, create a Stock object with the stock symbol SUND, name Sun Microsystem V, previous closing price of 100. Set a new current price randomly and display the price percentage.

Stock

private String symbol
private String name
private double previousClosingPrice

private double currentPrice
public Stock()
public Stock(String symbol, String name)

public String getSymbol()
public String getName()
public double getPreviousClosingPrice()
public double getCurrentPrice()
public void setSymbol(String symbol)
public void setName(String name)
public void setPreviousClosingPrice(double price)
public void setCurrentPrice(double price)
public double percentage()

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Stock.java

public class Stock {
   // Declaring variables
   private String symbol;
   private String name;
   private double previousClosingPrice;
   private double currentPrice;

   // Parameterized constructor
   public Stock(String symbol, String name) {
       this.symbol = symbol;
       this.name = name;
   }

   // Setters and getters
   public String getSymbol() {
       return symbol;
   }

   public void setSymbol(String symbol) {
       this.symbol = symbol;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public double getPreviousClosingPrice() {
       return previousClosingPrice;
   }

   public void setPreviousClosingPrice(double previousClosingPrice) {
       this.previousClosingPrice = previousClosingPrice;
   }

   public double getCurrentPrice() {
       return currentPrice;
   }

   // This method will find the change in percentage
   double percentage() {
       return ((currentPrice - previousClosingPrice) / previousClosingPrice) * 100;
   }

   public void setCurrentPrice(double currentPrice) {
       this.currentPrice = currentPrice;
   }

}

=======================================

==========================================

// Test.java

public class Test {

   public static void main(String[] args) {
double currentPrice=0.0;
  
       Stock s=new Stock("SUND","Sun Microsystem V");
       s.setPreviousClosingPrice(100);
       currentPrice=(Math.random() * (101)) + 100;
       s.setCurrentPrice(currentPrice);
      
       System.out.printf("Price Percentage : %.2f%%\n",s.percentage());

   }

}

==============================================

==========================================

output:

=====================Could you plz rate me well.Thank You


Related Solutions

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...
Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include...
Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include member variables for name (string), price (double), shares (double). Write a default constructor. Write a constructor that takes values for all member variables as parameters. Write a copy constructor. Implement Get/Set methods for all member variables. Implement the CalculateValue function. This function should multiply the prices by the shares and return that value. Use the following function header: double CalculateValue(). Add a member overload...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
In Java create a simple class named student with the following properties: id age gpa credit...
In Java create a simple class named student with the following properties: id age gpa credit hours accomplished Also, create the following methods: Constructors Getters and setters
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
Write a complete java program that Define a class named sample containing: A method named division...
Write a complete java program that Define a class named sample containing: A method named division that receives two integers x and y and returns the division of the two numbers. It must handle any possible exceptions. A method named printArray that receives an array of double arr and an integer index and prints the element in the positon index. It must handle any possible exceptions. main method that recievs input from user and repeat if wrong input. Then it...
In Java... Create a class named _MyArrays which has a main( ) and other static methods....
In Java... Create a class named _MyArrays which has a main( ) and other static methods. Part I (30%) [Main method] In the main() - Request the number of reviewers (r) and movies (m) - Declare a r x m two-dimensional array of integers - Allow the user to enter distinct values , row by row to fill the two dimensional array - Display the two-dimensional array using a _displayArray method. - Using the _printHighestLowestMovieRating method print the highest and...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, NOT LOOPS. You may use JDK String Methods like substring() and length(), but do not use the JDK methods to avoid coding algorithms assigned. For example, don’t use String.revers(). public boolean recursiveContains(char c, String s) { if (s.length() == 0) return false; if (s.charAt(s.length() - 1) == c) return true; else return recursiveContains(c, s.substring(0, s.length() - 1)); } public boolean recursiveAllCharactersSame(String s) return...
Using C#: Write a class named Employee that has the following properties: Name - The Name...
Using C#: Write a class named Employee that has the following properties: Name - The Name property holds the employee's name IdNumber - The IdNumber property holds the employee's ID number Department - The Department property holds the name of the department in which the employee works Position - The Position property holds the employee's job title The class should have the following overloaded constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT