Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone please check my program against the assignment (see below) to see if I meet the requirements for this assignment? Exercise #2: Design and

Can someone please check my program against the assignment (see below) to see if I meet the requirements for this assignment?

Exercise #2: Design and implement class Stock to represent a companys stock. The class defines the following attributes (variables) and methods:

A class variable of type String named Symbol for the stocks symbol. A class variable of type String named Name for the stocks name. A class variable of type double named previousClosingPrice to store the last closing price. A class variable of type double named currentPrice to store the current price. A constructor method to create a stock with user-specified name and symbol. Method getName() that returns the stocks name. Method getSymbol() that returns the stocks symbol. Method setClosingPrice() that sets the previous closing price. Method setCurrentPrice() that sets the current price. Method getChangePercent() that returns the percentage changed from

previousClosingPrice to currentPrice. Using the formula: (previousClosingPrice currentPrice)/currentPrice * 100

Method named toString()printout a meaningful description of a stock object when passing the object name to the print statement.

The statement PRINT yahooStock would print the string: Yahoo stocks closing price is $234.54

public class Stock { String name; String symbol; double previousClosingPrice; double currentPrice;

public Stock(String name, String symbol, double pCp, double cP) { this.name = name; this.symbol = symbol; previousClosingPrice = pCp; currentPrice = cP; } public String getName() { return name; } // End getName Method public String getSymbol() { return symbol; } // End getSymbol Method public double setClosingPrice() { return previousClosingPrice; } // End setClosingPrice Method public double setCurrentPrice() { return currentPrice; } // End setCurrentPrice Method public double getChangePercent() { return (previousClosingPrice - currentPrice) / currentPrice * 100; } // End getChangePercent Method public String toString() { return name + "'s closing price is $" + currentPrice; } // End named toString Method } // End Class Stock

Now design and implement a test program to create two stock objects: one for Google with symbol GOG and the second is for Microsoft with symbol MSF. Set their closing and current prices according to the information below. Next, test the class methods on each object to print the information in a similar manner.

public class TestProgramStock {

public static void main(String[] args) { Stock google = new Stock("Google Stock", "GOG", 134.67, 131.98); System.out.println(google.getName() + ":"); System.out.println("\tSymbol: " + google.getSymbol()); System.out.println("\tClosing Price: " + google.setClosingPrice()); System.out.println("\tCurrent Price: " + google.setCurrentPrice()); System.out.println("\tChange Percent: " + String.format("%.0f%%",google.getChangePercent())); System.out.println("\t" + google.toString()); System.out.println(); Stock microsoft = new Stock("Microsoft Stock", "MSF", 156.52, 161.22); System.out.println(microsoft.getName() + ":"); System.out.println("\tSymbol: " + microsoft.getSymbol()); System.out.println("\tClosing Price: " + microsoft.setClosingPrice()); System.out.println("\tCurrent Price: " + microsoft.setCurrentPrice()); System.out.println("\tChange Percent: "+ String.format("%.0f%%",microsoft.getChangePercent())); System.out.println("\t" + microsoft.toString()); } /// End Main Method

} // End TestProgramStock Program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

More Books

Students also viewed these Databases questions